peichao.dong commited on
Commit
e5580aa
1 Parent(s): 48c34ad

update robot with langchain

Browse files
Files changed (2) hide show
  1. app.py +14 -37
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,50 +1,24 @@
1
  import gradio as gr
2
  import openai
3
  import os
 
 
 
 
4
 
5
  openai.api_key = os.environ.get("OPENAI_API_KEY")
6
 
 
 
 
7
 
8
- class Conversation:
9
- def __init__(self, prompt, num_of_round):
10
- self.prompt = prompt
11
- self.num_of_round = num_of_round
12
- self.messages = []
13
- self.messages.append({"role": "system", "content": self.prompt})
14
-
15
- def ask(self, question):
16
- try:
17
- self.messages.append({"role": "user", "content": question})
18
- response = openai.ChatCompletion.create(
19
- model="gpt-3.5-turbo",
20
- messages=self.messages,
21
- temperature=0.5,
22
- max_tokens=2048,
23
- top_p=1,
24
- )
25
- except Exception as e:
26
- print(e)
27
- return e
28
-
29
- message = response["choices"][0]["message"]["content"]
30
- self.messages.append({"role": "assistant", "content": message})
31
-
32
- if len(self.messages) > self.num_of_round*2 + 1:
33
- del self.messages[1:3]
34
- # Remove the first round conversation left.
35
- return message
36
-
37
-
38
- prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
39
- 1. 你的回答必须是中文
40
- 2. 回答限制在100个字以内"""
41
-
42
- conv = Conversation(prompt, 10)
43
 
44
 
45
  def answer(question, history=[]):
46
  history.append(question)
47
- response = conv.ask(question)
48
  history.append(response)
49
  responses = [(u, b) for u, b in zip(history[::2], history[1::2])]
50
  return responses, history
@@ -58,6 +32,9 @@ with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as d
58
  txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
59
  container=False)
60
 
61
- txt.submit(answer, [txt, state], [chatbot, state])
 
 
 
62
 
63
  demo.launch()
 
1
  import gradio as gr
2
  import openai
3
  import os
4
+ from langchain.chains import ConversationChain
5
+ from langchain.llms import OpenAI
6
+ from langchain.memory import ConversationEntityMemory
7
+ from langchain.memory.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
8
 
9
  openai.api_key = os.environ.get("OPENAI_API_KEY")
10
 
11
+ llm = OpenAI(model_name="text-davinci-003", stop="\n\n",
12
+ max_tokens=2048, temperature=0.5)
13
+ entityMemory = ConversationEntityMemory(llm=llm)
14
 
15
+ conversation = ConversationChain(
16
+ llm=llm, prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE, verbose=True, memory=entityMemory)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
 
19
  def answer(question, history=[]):
20
  history.append(question)
21
+ response = conversation.predict(input=question)
22
  history.append(response)
23
  responses = [(u, b) for u, b in zip(history[::2], history[1::2])]
24
  return responses, history
 
32
  txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
33
  container=False)
34
 
35
+ with gr.Row():
36
+ button = gr.Button("Submit")
37
+
38
+ button.click(answer, [txt, state], [chatbot, state])
39
 
40
  demo.launch()
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- openai==0.27.4
 
 
 
1
+ openai==0.27.4
2
+ gradio
3
+ langchain