Spaces:
Runtime error
Runtime error
Init App
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import openai
|
| 4 |
+
|
| 5 |
+
# 如果你想只靠 prompt 来创建一个能完成特定功能的 chatbot,只需要修改这句 prompt 就够了。
|
| 6 |
+
prompt = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'
|
| 7 |
+
|
| 8 |
+
def chat(p, qid, uid):
|
| 9 |
+
# demo 简单起见,只支持单轮对话。qid、uid参数都忽略
|
| 10 |
+
return ["text", callapi(p)]
|
| 11 |
+
|
| 12 |
+
openai.api_key = os.getenv("openai_key")
|
| 13 |
+
def callapi(p):
|
| 14 |
+
response = openai.ChatCompletion.create(
|
| 15 |
+
model="gpt-3.5-turbo",
|
| 16 |
+
messages= [{"role":"system", "content":prompt},
|
| 17 |
+
{"role":"user", "content":p}
|
| 18 |
+
]
|
| 19 |
+
)
|
| 20 |
+
response = response["choices"][0]["message"]["content"]
|
| 21 |
+
while response.startswith("\n"):
|
| 22 |
+
response = response[1:]
|
| 23 |
+
|
| 24 |
+
return response
|
| 25 |
+
|
| 26 |
+
iface = gr.Interface(fn=chat,
|
| 27 |
+
inputs=["text", "text", "text"],
|
| 28 |
+
outputs=["text", "text"])
|
| 29 |
+
iface.launch()
|