Commit ·
636ae3b
0
Parent(s):
Duplicate from Jiana/hackathon_chatbot_openai_api_ai_explorer
Browse filesCo-authored-by: Jiana Yang <Jiana@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +14 -0
- app.py +215 -0
- requirements.txt +1 -0
- /346/267/273/345/212/240 secret /347/232/204/346/226/271/346/263/225.jpg +0 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: hackathon chatbot openai api
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.20.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: cc-by-4.0
|
| 11 |
+
duplicated_from: Jiana/hackathon_chatbot_openai_api_ai_explorer
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/doc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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import openai
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# 请记得要把 api 的 key 放到 settings 下面的 Repository Secrets 里。
|
| 7 |
+
openai.api_key = os.getenv("openai_key")
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(filename="test.log", format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
|
| 10 |
+
datefmt='%Y-%m-%d %H:%M:%S ',
|
| 11 |
+
level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger()
|
| 13 |
+
KZT = logging.StreamHandler()
|
| 14 |
+
KZT.setLevel(logging.DEBUG)
|
| 15 |
+
logger.addHandler(KZT)
|
| 16 |
+
logger.info('Start logging...')
|
| 17 |
+
|
| 18 |
+
logger.info(f"openai.api_key: {openai.api_key}")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
|
| 22 |
+
# prompt = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'
|
| 23 |
+
prompt = '请假设自己是一名旅行计划师,给我设计一条去{日本}旅行的{5天}行程,包含{2位成人和1位儿童}。我希望整个行程{偏轻松、度假},包含 {去奈良看小鹿} 和 {去春日大社赏樱花} 。请给我推荐一个详细的每日行程,以表格的方式呈现,包含有趣的景点、活动和推荐理由。'
|
| 24 |
+
|
| 25 |
+
history = {}
|
| 26 |
+
|
| 27 |
+
# 修改本函数,来实现你自己的 chatbot
|
| 28 |
+
# p: 对机器人说话的内容
|
| 29 |
+
# qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。
|
| 30 |
+
# uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
|
| 31 |
+
# 返回值:[type, content]
|
| 32 |
+
# 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
prompt_0 = "您好!我是您的智能旅行计划师!请告诉我你想去哪里旅行?您可以直接回复我目的地,例如日本,香港,或者西双版纳。"
|
| 36 |
+
prompt_1 = "好的。请问您计划什么时候出行?您可以回答我具体的日期,例如4月1日到4月5日;或者回答我模糊的时间,例如未来三个月,5天。"
|
| 37 |
+
prompt_2 = "收到。请问您计划多少人出行?请回答我具体的人数和类型,例如2位成人和1位儿童。"
|
| 38 |
+
prompt_3 = "您的出行目的是什么?您可以回答我例如亲子游、情侣/蜜月、休闲度假、文化体验、地标打卡、探险。"
|
| 39 |
+
prompt_4 = "您有没有什么需要打卡的地点或者特殊需求?您可以告诉我具体的景点,例如金阁寺,或者特定的活动,例如赏樱花。"
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
predefined_prompts = [
|
| 43 |
+
prompt_0,
|
| 44 |
+
prompt_1,
|
| 45 |
+
prompt_2,
|
| 46 |
+
prompt_3,
|
| 47 |
+
prompt_4
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
_user_response = ["pred def" for _ in range(6)]
|
| 51 |
+
|
| 52 |
+
# count_i = 1
|
| 53 |
+
count_i_dict = dict()
|
| 54 |
+
|
| 55 |
+
def init_user_info(uid):
|
| 56 |
+
global count_i_dict
|
| 57 |
+
count_i_dict[uid] = dict()
|
| 58 |
+
count_i_dict[uid]["count_i"] = 1
|
| 59 |
+
count_i_dict[uid]["user_response"] = _user_response
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def get_count_i(uid):
|
| 63 |
+
global count_i_dict
|
| 64 |
+
if uid not in count_i_dict:
|
| 65 |
+
return 1
|
| 66 |
+
return count_i_dict[uid]["count_i"]
|
| 67 |
+
|
| 68 |
+
def set_count_i(uid, delta=1):
|
| 69 |
+
global count_i_dict
|
| 70 |
+
if uid not in count_i_dict:
|
| 71 |
+
# TODO: call user prof init
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
count_i_dict[uid]["count_i"] += delta
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def get_user_response(uid, i=0):
|
| 78 |
+
global count_i_dict
|
| 79 |
+
|
| 80 |
+
if uid not in count_i_dict:
|
| 81 |
+
return ""
|
| 82 |
+
|
| 83 |
+
return count_i_dict[uid]["user_response"][i]
|
| 84 |
+
|
| 85 |
+
def set_user_response(uid, i, current_p):
|
| 86 |
+
global count_i_dict
|
| 87 |
+
if uid not in count_i_dict:
|
| 88 |
+
# TODO: call user prof init
|
| 89 |
+
return
|
| 90 |
+
|
| 91 |
+
count_i_dict[uid]["user_response"][i] = current_p
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def get_info(p, qid, uid):
|
| 97 |
+
global predefined_prompts
|
| 98 |
+
# global user_response
|
| 99 |
+
# global count_i
|
| 100 |
+
global count_i_dict
|
| 101 |
+
|
| 102 |
+
if uid not in count_i_dict:
|
| 103 |
+
init_user_info(uid)
|
| 104 |
+
logger.info(f"Init info for user {uid}")
|
| 105 |
+
|
| 106 |
+
# current_count_i = count_i_dict[uid]["count_i"]
|
| 107 |
+
current_count_i = get_count_i(uid)
|
| 108 |
+
# count_i_dict[uid]["user_response"][current_count_i-1] = p
|
| 109 |
+
set_user_response(uid, current_count_i-1, p)
|
| 110 |
+
|
| 111 |
+
# user_response[current_count_i-1] = p
|
| 112 |
+
# user_response[count_i-1] = p
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
next_prompt = predefined_prompts[current_count_i]
|
| 116 |
+
set_count_i(uid, 1)
|
| 117 |
+
|
| 118 |
+
# count_i += 1
|
| 119 |
+
# count_i_dict[uid]["count_i"] += 1
|
| 120 |
+
|
| 121 |
+
logger.info(f"In get info loop: get from user: {p}")
|
| 122 |
+
logger.info(f"In get info loop: Our next prompt: {next_prompt}, uid={uid}, count_i={get_count_i(uid)}")
|
| 123 |
+
return ["text", next_prompt]
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def chat(p, qid, uid):
|
| 128 |
+
# global user_response
|
| 129 |
+
# global count_i
|
| 130 |
+
|
| 131 |
+
global count_i_dict
|
| 132 |
+
|
| 133 |
+
count_i = get_count_i(uid)
|
| 134 |
+
# count_i = count_i_dict[uid]["count_i"]
|
| 135 |
+
|
| 136 |
+
if count_i <= 4:
|
| 137 |
+
ret = get_info(p, qid, uid)
|
| 138 |
+
return ret
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
if count_i == 5: # 5
|
| 143 |
+
# user_response[4] = p
|
| 144 |
+
set_user_response(uid, count_i-1, p)
|
| 145 |
+
# prompt_0_response = user_response[0]
|
| 146 |
+
# prompt_1_response = user_response[1]
|
| 147 |
+
# prompt_2_response = user_response[2]
|
| 148 |
+
# prompt_3_response = user_response[3]
|
| 149 |
+
# prompt_4_response = user_response[4]
|
| 150 |
+
|
| 151 |
+
prompt_0_response = get_user_response(uid, 0)
|
| 152 |
+
prompt_1_response = get_user_response(uid, 1)
|
| 153 |
+
prompt_2_response = get_user_response(uid, 2)
|
| 154 |
+
prompt_3_response = get_user_response(uid, 3)
|
| 155 |
+
prompt_4_response = get_user_response(uid, 4)
|
| 156 |
+
|
| 157 |
+
final_prompt = f"请假设自己是一名旅行计划师,给我设计一条去{prompt_0_response}旅行的{prompt_1_response}行程,包含{prompt_2_response}。我的出行目的是{prompt_3_response},希望包含 {prompt_4_response} 。请给我推荐一个详细的每日行程,以表格的方式呈现,包含有趣的景点、活动和推荐理由。请尽量考虑可执行性。"
|
| 158 |
+
p = final_prompt
|
| 159 |
+
logger.info(f"Our final prompt: {final_prompt}")
|
| 160 |
+
logger.info(f"count_i: {count_i}")
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
# 找出该 uid 对应的历史对话
|
| 164 |
+
global history
|
| 165 |
+
if uid in history:
|
| 166 |
+
msgs = history[uid]
|
| 167 |
+
else:
|
| 168 |
+
msgs = []
|
| 169 |
+
|
| 170 |
+
response = callapi(p, msgs)
|
| 171 |
+
history[uid] = msgs + [[p, response]]
|
| 172 |
+
logger.info(f"history: {msgs}")
|
| 173 |
+
logger.info(f"p: {p}")
|
| 174 |
+
logger.info(f"response: {response}")
|
| 175 |
+
# count_i_dict[uid]["count_i"] += 1
|
| 176 |
+
set_count_i(uid,1)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
return ["text", response]
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def callapi(p, msgs):
|
| 183 |
+
if (len(msgs) > 8): #简单 hard-code 8 回合对话。如果需要更精准的,应该计算 token 数
|
| 184 |
+
msgs = msgs[-8:]
|
| 185 |
+
|
| 186 |
+
data = [{"role":"system", "content":prompt}]
|
| 187 |
+
for m in msgs:
|
| 188 |
+
data = data + [
|
| 189 |
+
{"role":"user", "content":m[0]},
|
| 190 |
+
{"role":"assistant", "content":m[1]}
|
| 191 |
+
]
|
| 192 |
+
data = data + [{"role":"user", "content":p}]
|
| 193 |
+
response = openai.ChatCompletion.create(
|
| 194 |
+
model="gpt-3.5-turbo",
|
| 195 |
+
messages= data
|
| 196 |
+
)
|
| 197 |
+
print(response)
|
| 198 |
+
response = response["choices"][0]["message"]["content"]
|
| 199 |
+
while response.startswith("\n"):
|
| 200 |
+
response = response[1:]
|
| 201 |
+
return response
|
| 202 |
+
|
| 203 |
+
iface = gr.Interface(fn=chat,
|
| 204 |
+
inputs=["text", "text", "text"],
|
| 205 |
+
outputs=["text", "text"],
|
| 206 |
+
description="""您好!我是您的智能旅行计划师。请告诉我你想去哪里旅行?您可以直接回复我目的地,例如日本,香港,或者西双版纳。""")
|
| 207 |
+
|
| 208 |
+
comments = """
|
| 209 |
+
已添加多轮对话的极简示范,能将该 uid 的最近八条消息一起发给openai。本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。
|
| 210 |
+
注意: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)
|
| 211 |
+
[对话测试](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)
|
| 212 |
+
|
| 213 |
+
"""
|
| 214 |
+
|
| 215 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openai==0.27.0
|
/346/267/273/345/212/240 secret /347/232/204/346/226/271/346/263/225.jpg
ADDED
|