ecarr-bend commited on
Commit
e5ffd65
·
1 Parent(s): 1868e0a

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -122
app.py DELETED
@@ -1,122 +0,0 @@
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-markstrat"
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
- print("CHECK - setting up gradio chatbot UI")
75
-
76
- # build Gradio selectable options in Chat UI
77
- model_type=gr.Dropdown(choices=["gpt-4 + rag",
78
- "gpt-3.5-turbo + rag"],
79
- value="gpt-4 + rag",
80
- type="index",
81
- label="LLM Models"
82
- )
83
-
84
- # gradio chatbot UI
85
- def predict(message, history, model_type):
86
- # clearing RAG memory
87
- memory.clear()
88
-
89
- # specifying LLM to use
90
- if (model_type==0):
91
- llm = llm4
92
- elif (model_type==1):
93
- llm = llm35
94
- else:
95
- llm = llm4
96
-
97
- llm_response = agent_executor({"input":message})
98
-
99
- return llm_response["output"]
100
-
101
- # set up and run chat interface
102
- kellogg_agent = gr.ChatInterface(
103
- fn=predict,
104
- chatbot=gr.Chatbot(height=500),
105
- textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7),
106
- title="Kellogg Course AI Assistant",
107
- description="Please provide your questions about courses offered by Kellogg.",
108
- additional_inputs=[model_type],
109
- additional_inputs_accordion_name="AI Assistant Options:",
110
- examples=[["What is MarkStrat?"]],
111
- # cache_examples=True,
112
- # retry_btn=None,
113
- undo_btn="Delete Previous",
114
- clear_btn="Clear",
115
- )
116
-
117
- user_cred = os.environ.get("USER_CRED")
118
- pass_cred = os.environ.get("PASS_CRED")
119
-
120
- # start UI
121
- if __name__ == "__main__":
122
- kellogg_agent.queue().launch(auth=(user_cred, pass_cred))