|
import gradio as gr |
|
import os |
|
import openai |
|
|
|
|
|
openai.api_key = os.getenv("openai_key") |
|
|
|
|
|
|
|
prompt = 'I want you to act as a career counselor. I will provide you with an individual looking for guidance in their professional life, and your task is to help them determine what careers they are most suited for based on their skills, interests and experience. You should also conduct research into the various options available, explain the job market trends in different industries and advice on which qualifications would be beneficial for pursuing particular fields. output in chinese.' |
|
|
|
history = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat(p, qid, uid): |
|
|
|
global history |
|
if uid in history: |
|
msgs = history[uid] |
|
else: |
|
msgs = [] |
|
|
|
response = callapi(p, msgs) |
|
history[uid] = msgs + [[p, response]] |
|
return ["text", response] |
|
|
|
|
|
def callapi(p, msgs): |
|
if (len(msgs) > 8): |
|
msgs = msgs[-8:] |
|
|
|
data = [{"role":"system", "content":prompt}] |
|
for m in msgs: |
|
data = data + [ |
|
{"role":"user", "content":m[0]}, |
|
{"role":"assistant", "content":m[1]} |
|
] |
|
data = data + [{"role":"user", "content":p}] |
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages= data |
|
) |
|
print(response) |
|
response = response["choices"][0]["message"]["content"] |
|
while response.startswith("\n"): |
|
response = response[1:] |
|
return response |
|
|
|
iface = gr.Interface(fn=chat, |
|
inputs=["text", "text", "text"], |
|
outputs=["text", "text"], |
|
description="""这是一个极其简单的示范程序,用唐三藏的语气来和你对话。 |
|
已添加多轮对话的极简示范,能将该 uid 的最近八条消息一起发给openai。本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。 |
|
注意: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) |
|
[对话测试](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) |
|
""") |
|
iface.launch() |