ecarr-bend commited on
Commit
03d5670
·
1 Parent(s): 22e8059

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import pinecone
4
+ import openai
5
+
6
+ from langchain.embeddings.openai import OpenAIEmbeddings
7
+ from langchain.chat_models import ChatOpenAI
8
+ from langchain.vectorstores import Pinecone
9
+
10
+ from langchain.agents.openai_functions_agent.agent_token_buffer_memory import AgentTokenBufferMemory
11
+ from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
12
+ from langchain.schema.messages import SystemMessage
13
+ from langchain.prompts import MessagesPlaceholder
14
+ from langchain.agents import AgentExecutor
15
+ from langchain.agents.agent_toolkits import create_retriever_tool
16
+
17
+ print("CHECK - Pinecone vector db setup")
18
+
19
+ # set up OpenAI environment vars and embeddings
20
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
21
+ embeddings = OpenAIEmbeddings()
22
+
23
+ # initialize pinecone db
24
+ index_name = "kellogg-course-assistant"
25
+
26
+ pinecone.init(
27
+ api_key=os.getenv("PINECONE_API_KEY"), # find at app.pinecone.io
28
+ environment=os.getenv("PINECONE_ENV"), # next to api key in console
29
+ )
30
+
31
+ # load existing index
32
+ vectorsearch = Pinecone.from_existing_index(index_name, embeddings)
33
+ retriever = vectorsearch.as_retriever()
34
+
35
+ print("CHECK - setting up conversational retrieval agent")
36
+
37
+ # create LLM
38
+ llm4 = ChatOpenAI(temperature = 0.1, model_name="gpt-4")
39
+ llm35 = ChatOpenAI(temperature = 0.1, model_name="gpt-3.5-turbo-16k")
40
+ llm = llm4
41
+
42
+ # create retrieval tool
43
+ tool = create_retriever_tool(
44
+ retriever,
45
+ "search_kellogg_site",
46
+ "Searches and returns content from within the Kellogg website."
47
+ )
48
+ tools = [tool]
49
+
50
+ # conversational retrieval agent component construction - memory, prompt template, agent, agent executor
51
+ # This is needed for both the memory and the prompt
52
+ memory_key = "history"
53
+ memory = AgentTokenBufferMemory(memory_key=memory_key, llm=llm)
54
+ # memory = AgentTokenBufferMemory(memory_key=memory_key, llm=llm, max_history=0, max_token_limit= 4000)
55
+
56
+ system_message = SystemMessage(
57
+ content=(
58
+ "You are a helpful educational expert providing advice to students of the Northwestern business school Kellogg. "
59
+ "Use both your knowledge and the Kellogg site search tool to generate helpful answers for questions about courses and providing a list of suggested web course articles for more information. "
60
+ "Format your answer with distinct <h3>titles</h3> and <h3>subtitles</h3>, <b>emphasis</b>, <b>bold</b>, <i>italic<i>, <li>lists</li>, and tables *use html code*. For lists, or bullet points, always start them by having a topic in <b>emphasis</b> before going into the description. Ensure to frequently take concepts and break them down into bullet points or lists following the emphasis directions that were just laid out."
61
+ "Do not include details of your intermediate steps in the final response. "
62
+ "At the end of your response, provide links to relevant web course articles returned by the retriever."
63
+ )
64
+ )
65
+
66
+ prompt = OpenAIFunctionsAgent.create_prompt(
67
+ system_message=system_message,
68
+ extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)]
69
+ )
70
+
71
+ agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
72
+ agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True,return_intermediate_steps=True)
73
+
74
+ # query = "I first noticed that data wasn’t flowing from splunk and received the following error message - No errors were recorded for the customer’s Splunk input"
75
+ # result = agent_executor({"input":query})
76
+
77
+ print("CHECK - setting up gradio chatbot UI")
78
+
79
+ # build Gradio selectable options in Chat UI
80
+ model_type=gr.Dropdown(choices=["gpt-4 + rag",
81
+ "gpt-3.5-turbo + rag"],
82
+ value="gpt-4 + rag",
83
+ type="index",
84
+ label="LLM Models"
85
+ )
86
+
87
+ # gradio chatbot UI
88
+ def predict(message, history, model_type):
89
+ # clearing RAG memory
90
+ memory.clear()
91
+
92
+ # specifying LLM to use
93
+ if (model_type==0):
94
+ llm = llm4
95
+ elif (model_type==1):
96
+ llm = llm35
97
+ else:
98
+ llm = llm4
99
+
100
+ llm_response = agent_executor({"input":message})
101
+
102
+ return llm_response["output"]
103
+
104
+ # set up and run chat interface
105
+ kellogg_agent = gr.ChatInterface(
106
+ fn=predict,
107
+ chatbot=gr.Chatbot(height=500),
108
+ textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7),
109
+ title="Kellogg Course AI Assistant",
110
+ description="Please provide your questions about courses offered by Kellogg.",
111
+ additional_inputs=[model_type],
112
+ additional_inputs_accordion_name="AI Assistant Options:",
113
+ examples=[["Can you tell me about a marketing major? What would I want from my career if I went that way instead of say strategy?"],
114
+ ["I'm interested in strategy. Can you give me a recommendation of courses I should consider over the next year?"],
115
+ ["I'm wanting to know more about advertising. Can you recommend some courses on that subject?"],
116
+ ["How many credits do I need to graduate?"],
117
+ ["I loved the Competitive Strategy and industrial structure class. Can you tell me others like that one?"]],
118
+ # cache_examples=True,
119
+ # retry_btn=None,
120
+ undo_btn="Delete Previous",
121
+ clear_btn="Clear",
122
+ )
123
+
124
+ # start UI
125
+ if __name__ == "__main__":
126
+ kellogg_agent.queue().launch()