villageideate commited on
Commit
e9cc6f1
1 Parent(s): ef72b32

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.embeddings.openai import OpenAIEmbeddings
2
+ from langchain.vectorstores import Chroma
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.prompts.chat import (
5
+ ChatPromptTemplate,
6
+ SystemMessagePromptTemplate,
7
+ HumanMessagePromptTemplate,
8
+ )
9
+ from langchain.chains import ChatVectorDBChain
10
+ import gradio as gr
11
+
12
+ # initialize variables
13
+ query = ""
14
+ chat_history = []
15
+ system_template = "You are a helpful assistant. Use the following pieces of context to \
16
+ answer the user's question. {context}"
17
+ temperature = 0
18
+ qa = None
19
+
20
+ # function called when user submits a question
21
+ def chatbot(input, history=[]):
22
+ result = qa({"question": input, "chat_history": []})
23
+ history.append((input, result["answer"]))
24
+ return history, history
25
+
26
+
27
+ # function to set OpenAI key and initalize LangChain chain
28
+ def set_open_ai_key(input):
29
+ print("Setting OpenAI key to ", input)
30
+ # os.environ["OPENAI_API_KEY"] = input
31
+
32
+ # read in embedddings from vector db store
33
+ embeddings = OpenAIEmbeddings(openai_api_key=input)
34
+ vectordb = Chroma(persist_directory="vector_store", embedding_function=embeddings)
35
+
36
+ # initialize chatbot message templates
37
+ messages = [
38
+ SystemMessagePromptTemplate.from_template(system_template),
39
+ HumanMessagePromptTemplate.from_template("{question}\Standalone question"),
40
+ ]
41
+ prompt = ChatPromptTemplate.from_messages(messages)
42
+
43
+ # initialize chatbot chain
44
+ global qa
45
+ qa = ChatVectorDBChain.from_llm(
46
+ ChatOpenAI(temperature=temperature, openai_api_key=input),
47
+ vectordb,
48
+ qa_prompt=prompt,
49
+ )
50
+
51
+
52
+ # example questions user can choose from in Gradio UI to test the chatbot
53
+ examples = [
54
+ "What does Biggie Smalls say about learning from other people's mistakes?",
55
+ "What can I learn from Megan Quinn about reading?",
56
+ "What does Scott Belsky say about optimizing what works?",
57
+ "What is product market fit?",
58
+ "What is the best way to lower CAC?",
59
+ "What is the most important competitive advantage a business can have today?",
60
+ ]
61
+
62
+ # initialize Gradio UI using chatbot UI
63
+ gr_interface = gr.Interface(
64
+ fn=chatbot,
65
+ inputs=["text", "state"],
66
+ outputs=["chatbot", "state"],
67
+ title="TrenBot",
68
+ description="Q&A Chatbot for Tren Griffin's views on the \
69
+ market, tech, and everything else",
70
+ examples=examples,
71
+ thumbnail="https://i0.wp.com/25iq.com/wp-content/uploads/2012/10/tren_griffin_crop.jpg",
72
+ )
73
+
74
+ # add OpenAI key textbox to Gradio UI
75
+ with gr_interface:
76
+ openai_key_textbox = gr.Textbox(
77
+ lines=1,
78
+ placeholder="Paste your OpenAI key here and hit Enter.",
79
+ type="password",
80
+ label="Your OpenAI Key",
81
+ )
82
+ openai_key_textbox.submit(set_open_ai_key, inputs=openai_key_textbox)
83
+
84
+ # launch Gradio UI
85
+ gr_interface.launch(share=True)