Elfe commited on
Commit
ed6ae9c
1 Parent(s): 6970827

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -2
app.py CHANGED
@@ -18,18 +18,20 @@ history = {}
18
  # 返回值:[type, content]
19
  # 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
20
  def chat(p, qid, uid):
 
21
  global history
22
  if uid in history:
23
  msgs = history[uid]
24
  else:
25
  msgs = []
 
26
  response = callapi(p, msgs)
27
  history[uid] = msgs + [[p, response]]
28
  return ["text", response]
29
 
30
 
31
  def callapi(p, msgs):
32
- if (len(msgs) > 8): #hard-code 8 回合对话。如果需要更精准的,应该计算 token 数
33
  msgs = msgs[-8:]
34
 
35
  data = [{"role":"system", "content":prompt}]
@@ -53,7 +55,7 @@ iface = gr.Interface(fn=chat,
53
  inputs=["text", "text", "text"],
54
  outputs=["text", "text"],
55
  description="""这是一个极其简单的示范程序,用唐三藏的语气来和你对话。
56
-
57
  注意: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)
58
  [对话测试](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)
59
  """)
 
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}]
 
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
  """)