minh commited on
Commit
b4f6e5d
1 Parent(s): 49a651b
Files changed (2) hide show
  1. app.py +108 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # os.environ["OPENAI_API_TYPE"]=os.getenv("OPENAI_API_TYPE")
3
+ # os.environ["OPENAI_API_VERSION"]=os.getenv("OPENAI_API_VERSION")
4
+ # os.environ["OPENAI_API_BASE"]=os.getenv("OPENAI_API_BASE")
5
+ # os.environ["OPENAI_API_KEY"]=os.getenv("OPENAI_API_KEY")
6
+
7
+ import gradio as gr
8
+ from langchain.chat_models import ChatOpenAI
9
+ from langchain import LLMMathChain
10
+ from langchain.utilities import GoogleSerperAPIWrapper, WikipediaAPIWrapper
11
+ from langchain.agents import initialize_agent, Tool, AgentType
12
+
13
+
14
+ def set_key(api_key):
15
+ if not api_key:
16
+ return "Key can't be empty!"
17
+
18
+ os.environ["OPENAI_API_KEY"] = api_key
19
+ return "Key received"
20
+
21
+
22
+ def initialize():
23
+ # Define models
24
+ llm = ChatOpenAI(temperature=0)
25
+
26
+ # search = GoogleSerperAPIWrapper(serper_api_key='845bee8ad6861cd07d480cfae78df20352481282')
27
+ wiki = WikipediaAPIWrapper(top_k_results = 1)
28
+ llm_math_chain = LLMMathChain(llm=llm)
29
+ tools = [
30
+ Tool(
31
+ name="Calculator",
32
+ func=llm_math_chain.run,
33
+ description="useful for when you need to answer questions about math"
34
+ ),
35
+ Tool(
36
+ name = "wikipedia",
37
+ func=wiki.run,
38
+ description="useful for when you need to answer questions about historical entity. the input to this should be a single search term."
39
+ ),
40
+ # Tool(
41
+ # name = "Current Search",
42
+ # func=search.run,
43
+ # description="useful for when you need to answer questions about current events or the current state of the world, also useful if there is no wikipedia result. the input to this should be a single search term."
44
+ # ),
45
+ ]
46
+
47
+ from langchain.memory import ConversationBufferMemory
48
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
49
+
50
+ chatbot_engine = initialize_agent(
51
+ tools,
52
+ llm,
53
+ agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
54
+ verbose=True,
55
+ memory=memory)
56
+
57
+ return chatbot_engine
58
+
59
+
60
+ def chat(chat_history, chatbot_engine, message=""):
61
+ if not chatbot_engine:
62
+ print("no engine")
63
+ chatbot_engine = initialize()
64
+
65
+ # Empty msg
66
+ if not message.strip():
67
+ return chat_history, chat_history, ""
68
+
69
+ # Execute message
70
+ try:
71
+ result = chatbot_engine.run(message.strip())
72
+ except ValueError:
73
+ result = "I can't handle this request, please try something else."
74
+
75
+ chat_history.append((message, result))
76
+ return chat_history, chat_history, ""
77
+
78
+
79
+ with gr.Blocks() as demo:
80
+ # Declearing states
81
+ chat_history = gr.State([])
82
+ chatbot_engine = gr.State()
83
+
84
+ with gr.Row():
85
+ openai_api_key_textbox = gr.Textbox(
86
+ placeholder="Paste your OpenAI API key (sk-...)",
87
+ show_label=False,
88
+ lines=1,
89
+ type="password",
90
+ )
91
+
92
+ api_key_set = gr.Button("Set key")
93
+
94
+ api_key_set.click(
95
+ fn=set_key,
96
+ inputs=[openai_api_key_textbox],
97
+ outputs=[api_key_set],
98
+ )
99
+
100
+
101
+ gr.Markdown("""<h1><center>Chat with your personal assistant!</center></h1>""")
102
+ chatbot = gr.Chatbot()
103
+ message = gr.Textbox()
104
+ submit = gr.Button("SEND")
105
+ submit.click(chat, inputs=[chat_history, chatbot_engine, message], outputs=[chatbot, chat_history, message])
106
+
107
+ if __name__ == "__main__":
108
+ demo.launch(debug = True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openai
2
+ langchain==0.0.148
3
+ gradio
4
+ python-dotenv
5
+ wikipedia