MIXSHARE
commited on
Commit
·
c5ed619
1
Parent(s):
7d6f7a6
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,23 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import openai
|
4 |
|
|
|
5 |
openai.api_key = os.getenv("openai_key")
|
6 |
|
|
|
|
|
7 |
prompt = '请你调用你能获取的所有的博物百科资源库,用小助手的口吻来回答问题,开头第一句是你好啊,好奇宝宝!结尾是有没有解答你的疑惑呢?欢迎继续提问!由于是面向青少年儿童,请务必做到内容有理有据语言风格有趣生动,附上来源出处'
|
8 |
|
9 |
history = {}
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
global history
|
13 |
if uid in history:
|
14 |
msgs = history[uid]
|
@@ -16,22 +26,19 @@ def chat(p, qid, uid, rating=None):
|
|
16 |
msgs = []
|
17 |
|
18 |
response = callapi(p, msgs)
|
19 |
-
|
20 |
-
if rating is not None and len(msgs) > 0:
|
21 |
-
msgs[-1]["rating"] = rating
|
22 |
-
|
23 |
-
history[uid] = msgs + [{"question": p, "answer": response}]
|
24 |
return ["text", response]
|
25 |
|
|
|
26 |
def callapi(p, msgs):
|
27 |
-
if (len(msgs) > 8):
|
28 |
msgs = msgs[-8:]
|
29 |
|
30 |
data = [{"role":"system", "content":prompt}]
|
31 |
for m in msgs:
|
32 |
data = data + [
|
33 |
-
{"role":"user", "content":m[
|
34 |
-
{"role":"assistant", "content":m[
|
35 |
]
|
36 |
data = data + [{"role":"user", "content":p}]
|
37 |
response = openai.ChatCompletion.create(
|
@@ -45,9 +52,10 @@ def callapi(p, msgs):
|
|
45 |
return response
|
46 |
|
47 |
iface = gr.Interface(fn=chat,
|
48 |
-
inputs=["text", "text", "text",
|
49 |
outputs=["text", "text"],
|
50 |
description="""这是一个面向青少年儿童的博物百科全书问答助手,希望能够让你天马行空的想象力和好奇心得到满足!
|
51 |
-
""")
|
52 |
|
53 |
-
|
|
|
|
|
|
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 |
|
14 |
+
# 修改本函数,来实现你自己的 chatbot
|
15 |
+
# p: 对机器人说话的内容
|
16 |
+
# qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。
|
17 |
+
# uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
|
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]
|
|
|
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(
|
|
|
52 |
return response
|
53 |
|
54 |
iface = gr.Interface(fn=chat,
|
55 |
+
inputs=["text", "text", "text"],
|
56 |
outputs=["text", "text"],
|
57 |
description="""这是一个面向青少年儿童的博物百科全书问答助手,希望能够让你天马行空的想象力和好奇心得到满足!
|
|
|
58 |
|
59 |
+
|
60 |
+
""")
|
61 |
+
iface.launch()
|