jamiecao commited on
Commit
1a1df74
1 Parent(s): f2142ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -1,4 +1,13 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
2
 
3
  history = {}
4
 
@@ -9,21 +18,45 @@ history = {}
9
  # 返回值:[type, content]
10
  # 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
11
  def chat(p, qid, uid):
 
12
  global history
13
  if uid in history:
14
- count = history[uid]
15
  else:
16
- count = 0
17
- count = count+1
18
- history[uid] = count # 统计每个 uid 说过的话的条数
19
- return ["text", f"{count} 我听到你刚说:{p}"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  iface = gr.Interface(fn=chat,
22
  inputs=["text", "text", "text"],
23
  outputs=["text", "text"],
24
- description="""这是一个极其简单的示范程序,只会重复你的话。
25
- 已添加多轮对话的极简示范。能分别统计不同的 uid 的对话轮数。但本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。
26
- 你只需要 duplicate 本项目,修改 chat 函数,就能造一个能到瀛海威广场注册的机器人了。
27
  [对话测试](https://huggingface.co/spaces/BaixingAI/hackathon_test) [参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md)
28
  """)
29
  iface.launch()
 
1
  import gradio as gr
2
+ import os
3
+ import openai
4
+
5
+ # 请记得要把 api 的 key 放到 settings 下面的 Repository Secrets 里。
6
+ openai.api_key = os.getenv("openai_key")
7
+
8
+
9
+ # 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
10
+ prompt = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'
11
 
12
  history = {}
13
 
 
18
  # 返回值:[type, content]
19
  # 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
20
  def chat(p, qid, uid):
21
+ # 找出该 uid 对应的历史对话
22
  global history
23
  if uid in history:
24
+ msgs = history[uid]
25
  else:
26
+ msgs = []
27
+
28
+ response = callapi(p, msgs)
29
+ history[uid] = msgs + [[p, response]]
30
+ return ["text", response]
31
+
32
+
33
+ def callapi(p, msgs):
34
+ if (len(msgs) > 8): #简单 hard-code 8 回合对话。如果需要更精准的,应该计算 token 数
35
+ msgs = msgs[-8:]
36
+
37
+ data = [{"role":"system", "content":prompt}]
38
+ for m in msgs:
39
+ data = data + [
40
+ {"role":"user", "content":m[0]},
41
+ {"role":"assistant", "content":m[1]}
42
+ ]
43
+ data = data + [{"role":"user", "content":p}]
44
+ response = openai.ChatCompletion.create(
45
+ model="gpt-3.5-turbo",
46
+ messages= data
47
+ )
48
+ print(response)
49
+ response = response["choices"][0]["message"]["content"]
50
+ while response.startswith("\n"):
51
+ response = response[1:]
52
+ return response
53
 
54
  iface = gr.Interface(fn=chat,
55
  inputs=["text", "text", "text"],
56
  outputs=["text", "text"],
57
+ description="""这是一个极其简单的示范程序,用唐三藏的语气来和你对话。
58
+ 已添加多轮对话的极简示范,能将该 uid 的最近八条消息一起发给openai。本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。
59
+ 注意:duplicate 本项目后,需要将你自己的 openai apikey 设置到 settings 的 Repository Secrets 里,否则运行会报错。[了解详情](https://huggingface.co/spaces/baixing/hackathon_chatbot_openai_api/blob/main/%E6%B7%BB%E5%8A%A0%20secret%20%E7%9A%84%E6%96%B9%E6%B3%95.jpg)
60
  [对话测试](https://huggingface.co/spaces/BaixingAI/hackathon_test) [参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md)
61
  """)
62
  iface.launch()