xuhuizhi commited on
Commit
17d04c8
·
1 Parent(s): be7890d

[feat]增加LLM,修改requirements

Browse files
Files changed (2) hide show
  1. app.py +44 -15
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,19 +1,45 @@
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
- '1. 你的描述不能含有输入的任何文字。不能重复不能重复不能重复' \
12
  '2. 能用最短的句子描述物品。' \
13
- '3. 要求你的语言风格是软萌的风格,尽量口语化。'
 
 
 
14
 
15
  history = {}
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # 修改本函数,来实现你自己的 chatbot
18
  # p: 对机器人说话的内容
19
  # qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。
@@ -27,26 +53,28 @@ def chat(p, qid, uid):
27
  msgs = history[uid]
28
  else:
29
  msgs = []
30
-
31
  response = callapi(p, msgs)
32
  history[uid] = msgs + [[p, response]]
33
  return ["text", response]
34
 
35
 
36
  def callapi(p, msgs):
37
- if (len(msgs) > 8): #简单 hard-code 8 回合对话。如果需要更精准的,应该计算 token 数
38
  msgs = msgs[-8:]
39
-
40
- data = [{"role":"system", "content":prompt}]
41
  for m in msgs:
42
  data = data + [
43
- {"role":"user", "content":m[0]},
44
- {"role":"assistant", "content":m[1]}
45
  ]
46
- data = data + [{"role":"user", "content":p}]
 
 
47
  response = openai.ChatCompletion.create(
48
  model="gpt-3.5-turbo",
49
- messages= data
50
  )
51
  print(response)
52
  response = response["choices"][0]["message"]["content"]
@@ -54,12 +82,13 @@ def callapi(p, msgs):
54
  response = response[1:]
55
  return response
56
 
57
- iface = gr.Interface(fn=chat,
58
- inputs=["text", "text", "text"],
 
59
  outputs=["text", "text"],
60
  description="""这个机器人可以帮你在玩《你比划我猜》大显身手,用最短最精确的词描述一个物品。
61
  已添加多轮对话的极简示范,能将该 uid 的最近八条消息一起发给openai。本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。
62
  注意: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)
63
  [对话测试](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)
64
  """)
65
- iface.launch()
 
1
  import gradio as gr
2
  import os
3
  import openai
4
+ import langchain
5
 
6
  # 请记得要把 api 的 key 放到 settings 下面的 Repository Secrets 里。
 
7
 
8
+ from langchain import OpenAI, LLMChain, PromptTemplate
9
+
10
+ openai.api_key = os.getenv("openai_key")
11
 
12
  # 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
13
+ template = """我们现在正在玩《你比划我猜》的游戏,请你描述输入的物品,使别人能够猜出来你说的是什么。有三点要求:' \
14
+ '1. 你的描述不能含有输入的任何文字。不能包含输入的字不能包含输入的字不能包含输入的字哪怕是单字也不行。' \
15
  '2. 能用最短的句子描述物品。' \
16
+ '3. 要求你的语言风格是硬汉的风格,尽量口语化。' \
17
+ '4. 你要说出输入是几个字。
18
+ Object:{text}
19
+ Description:"""
20
 
21
  history = {}
22
 
23
+ prompt = PromptTemplate(
24
+ template=template,
25
+ input_variables=['text']
26
+ )
27
+
28
+ # llm_chain = LLMChain(
29
+ # prompt=prompt,
30
+ # llm="gpt-3.5-turbo"
31
+ # )
32
+ # print(llm_chain)
33
+
34
+ llm_chain = LLMChain(
35
+ llm=OpenAI(),
36
+ prompt=prompt,
37
+ # llm="gpt-3.5-turbo"
38
+ # verbose=True,
39
+ )
40
+
41
+ # print(llm_chain.run(question))
42
+
43
  # 修改本函数,来实现你自己的 chatbot
44
  # p: 对机器人说话的内容
45
  # qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。
 
53
  msgs = history[uid]
54
  else:
55
  msgs = []
56
+
57
  response = callapi(p, msgs)
58
  history[uid] = msgs + [[p, response]]
59
  return ["text", response]
60
 
61
 
62
  def callapi(p, msgs):
63
+ if (len(msgs) > 8): # 简单 hard-code 8 回合对话。如果需要更精准的,应该计算 token 数
64
  msgs = msgs[-8:]
65
+
66
+ data = [{"role": "system", "content": llm_chain.prompt}]
67
  for m in msgs:
68
  data = data + [
69
+ {"role": "user", "content": m[0]},
70
+ {"role": "assistant", "content": m[1]}
71
  ]
72
+ data = data + [{"role": "user", "content": p}]
73
+
74
+
75
  response = openai.ChatCompletion.create(
76
  model="gpt-3.5-turbo",
77
+ messages=data
78
  )
79
  print(response)
80
  response = response["choices"][0]["message"]["content"]
 
82
  response = response[1:]
83
  return response
84
 
85
+
86
+ iface = gr.Interface(fn=chat,
87
+ inputs=["text", "text", "text"],
88
  outputs=["text", "text"],
89
  description="""这个机器人可以帮你在玩《你比划我猜》大显身手,用最短最精确的词描述一个物品。
90
  已添加多轮对话的极简示范,能将该 uid 的最近八条消息一起发给openai。本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。
91
  注意: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)
92
  [对话测试](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)
93
  """)
94
+ iface.launch()
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- openai==0.27.0
 
 
 
1
+ openai==0.27.0
2
+ langchain==0.0.27
3
+ huggingface_hub==0.13.2