Commit ·
e985dca
1
Parent(s): 959dd7a
Upload 2 files
Browse files- app.py +125 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"Do not include details of your intermediate steps in the final response. "
|
| 61 |
+
"At the end of your response, provide links to relevant web course articles returned by the retriever."
|
| 62 |
+
)
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
prompt = OpenAIFunctionsAgent.create_prompt(
|
| 66 |
+
system_message=system_message,
|
| 67 |
+
extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
|
| 71 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True,return_intermediate_steps=True)
|
| 72 |
+
|
| 73 |
+
# 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"
|
| 74 |
+
# result = agent_executor({"input":query})
|
| 75 |
+
|
| 76 |
+
print("CHECK - setting up gradio chatbot UI")
|
| 77 |
+
|
| 78 |
+
# build Gradio selectable options in Chat UI
|
| 79 |
+
model_type=gr.Dropdown(choices=["gpt-4 + rag",
|
| 80 |
+
"gpt-3.5-turbo + rag"],
|
| 81 |
+
value="gpt-4 + rag",
|
| 82 |
+
type="index",
|
| 83 |
+
label="LLM Models"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# gradio chatbot UI
|
| 87 |
+
def predict(message, history, model_type):
|
| 88 |
+
# clearing RAG memory
|
| 89 |
+
memory.clear()
|
| 90 |
+
|
| 91 |
+
# specifying LLM to use
|
| 92 |
+
if (model_type==0):
|
| 93 |
+
llm = llm4
|
| 94 |
+
elif (model_type==1):
|
| 95 |
+
llm = llm35
|
| 96 |
+
else:
|
| 97 |
+
llm = llm4
|
| 98 |
+
|
| 99 |
+
llm_response = agent_executor({"input":message})
|
| 100 |
+
|
| 101 |
+
return llm_response["output"]
|
| 102 |
+
|
| 103 |
+
# set up and run chat interface
|
| 104 |
+
cribl_agent = gr.ChatInterface(
|
| 105 |
+
fn=predict,
|
| 106 |
+
chatbot=gr.Chatbot(height=500),
|
| 107 |
+
textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7),
|
| 108 |
+
title="Kellogg Course AI Assistant",
|
| 109 |
+
description="Please provide your questions about courses offered by Kellogg.",
|
| 110 |
+
additional_inputs=[model_type],
|
| 111 |
+
additional_inputs_accordion_name="AI Assistant Options:",
|
| 112 |
+
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?"],
|
| 113 |
+
["I'm interested in strategy, can you give me a recommendation of courses I should consider over the next year?"],
|
| 114 |
+
["I'm wanting to know more about advertising, can you recommend some courses on that subject?"],
|
| 115 |
+
["How many credits do I need to graduate?"],
|
| 116 |
+
["I loved the Competitive Strategy and industrial structure class, can you tell me others like that one?"]]
|
| 117 |
+
# cache_examples=True,
|
| 118 |
+
# retry_btn=None,
|
| 119 |
+
undo_btn="Delete Previous",
|
| 120 |
+
clear_btn="Clear",
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# start UI
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
cribl_agent.queue().launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
openai
|
| 3 |
+
langchain
|
| 4 |
+
pinecone-client
|
| 5 |
+
tiktoken
|