|
import gradio as gr |
|
import os |
|
import openai |
|
|
|
|
|
|
|
prompt = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。' |
|
|
|
|
|
|
|
def chat(p, qid, uid): |
|
return ["text", callapi(p)] |
|
|
|
openai.api_key = os.getenv("openai_key") |
|
def callapi(p): |
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages= [{"role":"system", "content":prompt}, |
|
{"role":"user", "content":p} |
|
] |
|
) |
|
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="""这是一个极其简单的示范程序,用唐三藏的语气来和你对话。 |
|
[参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [对话测试](https://huggingface.co/spaces/baixing/hackathon_test) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md) |
|
""") |
|
iface.launch() |