minh commited on
Commit
3cd8399
·
1 Parent(s): b4f6e5d

fix memory bug and clean up code

Browse files
Files changed (1) hide show
  1. app.py +19 -26
app.py CHANGED
@@ -1,9 +1,4 @@
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
@@ -11,19 +6,11 @@ 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 = [
@@ -37,11 +24,11 @@ def initialize():
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
@@ -57,11 +44,17 @@ def initialize():
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, ""
@@ -94,15 +87,15 @@ with gr.Blocks() as demo:
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)
 
1
  import os
 
 
 
 
 
2
  import gradio as gr
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain import LLMMathChain
 
6
  from langchain.agents import initialize_agent, Tool, AgentType
7
 
8
 
 
 
 
 
 
 
 
 
9
  def initialize():
10
  # Define models
11
  llm = ChatOpenAI(temperature=0)
12
 
13
+ search = GoogleSerperAPIWrapper()
14
  wiki = WikipediaAPIWrapper(top_k_results = 1)
15
  llm_math_chain = LLMMathChain(llm=llm)
16
  tools = [
 
24
  func=wiki.run,
25
  description="useful for when you need to answer questions about historical entity. the input to this should be a single search term."
26
  ),
27
+ Tool(
28
+ name = "Current Search",
29
+ func=search.run,
30
+ 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."
31
+ )
32
  ]
33
 
34
  from langchain.memory import ConversationBufferMemory
 
44
  return chatbot_engine
45
 
46
 
47
+ def set_key(api_key):
48
+ if not api_key:
49
+ return "Key can't be empty!", None
50
+
51
+ os.environ["OPENAI_API_KEY"] = api_key
52
+
53
+ chatbot_engine = initialize()
54
+ return "Key received", chatbot_engine
55
 
56
+
57
+ def chat(chat_history, chatbot_engine, message=""):
58
  # Empty msg
59
  if not message.strip():
60
  return chat_history, chat_history, ""
 
87
  api_key_set.click(
88
  fn=set_key,
89
  inputs=[openai_api_key_textbox],
90
+ outputs=[api_key_set, chatbot_engine],
91
  )
92
 
93
+ gr.Markdown("""<h1><center>Chat with your online-connected bot!</center></h1>""")
 
94
  chatbot = gr.Chatbot()
95
  message = gr.Textbox()
96
  submit = gr.Button("SEND")
97
  submit.click(chat, inputs=[chat_history, chatbot_engine, message], outputs=[chatbot, chat_history, message])
98
 
99
+
100
  if __name__ == "__main__":
101
  demo.launch(debug = True)