AUST001 commited on
Commit
47ec66f
·
1 Parent(s): c7b78db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -29
app.py CHANGED
@@ -2,10 +2,8 @@ import openai
2
  import gradio as gr
3
  import random
4
 
5
-
6
  openai.api_key = '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'
7
 
8
- # 上下文列表,包含交互历史
9
  context = [
10
  {"role": "system", "content": "你是一个聪明的AI助手。"},
11
  {"role": "user", "content": "你会说中文吗?"},
@@ -13,41 +11,41 @@ context = [
13
  ]
14
 
15
  def get_assistant_response(user_question, context):
16
- # 将问题添加到上下文列表
17
  context.append({"role": "user", "content": user_question})
18
- # 调用API
19
  response = openai.ChatCompletion.create(
20
- model = 'gpt-3.5-turbo',
21
- messages = context,
22
- temperature = 0
23
- )
24
-
25
- # 获取回答
26
  assistant_response = response.choices[0].message['content']
27
-
28
- # 将回答添加到上下文列表
29
  context.append({"role": "assistant", "content": assistant_response})
30
-
31
  return assistant_response
32
 
33
- def greet(question):
34
  global context
35
- if question=='clear':
36
  context = [
37
- {"role": "system", "content": "你是一个聪明的AI助手。"},
38
- {"role": "user", "content": "你会说中文吗?"},
39
- {"role": "assistant", "content": "是的,我可以说中文。"}
40
- ]
41
- return 'clear success'
42
  else:
43
- return get_assistant_response(question, context)
44
- # return type(question)
45
-
46
- demo = gr.Interface(fn=greet, inputs=gr.Textbox(lines=20, placeholder='Please enter your question'),
47
- outputs=gr.Textbox(lines=20, placeholder='Waiting for AI to answer'),
48
- title="AI助手",
49
- description="请输入您的问题,AI助手会给出回答。支持连续对话,可以记录对话历史。重新开始对话输入clear,输出clear success表示重新开启对话。")
 
 
 
 
 
 
 
 
50
 
51
  if __name__ == "__main__":
52
- # demo.launch(share='True')
53
- demo.launch()
 
2
  import gradio as gr
3
  import random
4
 
 
5
  openai.api_key = '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'
6
 
 
7
  context = [
8
  {"role": "system", "content": "你是一个聪明的AI助手。"},
9
  {"role": "user", "content": "你会说中文吗?"},
 
11
  ]
12
 
13
  def get_assistant_response(user_question, context):
 
14
  context.append({"role": "user", "content": user_question})
 
15
  response = openai.ChatCompletion.create(
16
+ model='gpt-3.5-turbo',
17
+ messages=context,
18
+ temperature=0
19
+ )
 
 
20
  assistant_response = response.choices[0].message['content']
 
 
21
  context.append({"role": "assistant", "content": assistant_response})
 
22
  return assistant_response
23
 
24
+ def greet(user_question, clear_history):
25
  global context
26
+ if clear_history:
27
  context = [
28
+ {"role": "system", "content": "你是一个聪明的AI助手。"},
29
+ {"role": "user", "content": "你会说中文吗?"},
30
+ {"role": "assistant", "content": "是的,我可以说中文。"}
31
+ ]
32
+ return '清空成功', ''
33
  else:
34
+ return '', get_assistant_response(user_question, context)
35
+
36
+ demo = gr.Interface(
37
+ fn=greet,
38
+ inputs=[
39
+ gr.Textbox(lines=21, label='请输入问题', placeholder='请输入您的问题'),
40
+ gr.Checkbox(label='清空聊天记录', default=False)
41
+ ],
42
+ outputs=[
43
+ gr.Textbox(lines=1, label='聊天记录已清空', placeholder='等待清空聊天记录'),
44
+ gr.Textbox(lines=20, label='AI回答', placeholder='等待AI回答')
45
+ ],
46
+ title="AI助手",
47
+ description="请输入您的问题,AI助手会给出回答。支持连续对话,可以记录对话历史。重新开始对话勾选清空聊天记录,输出清空成功表示重新开启对话。"
48
+ )
49
 
50
  if __name__ == "__main__":
51
+ demo.launch()