File size: 1,920 Bytes
ca4e572
 
 
 
 
 
9ba760d
ca4e572
 
 
 
 
 
 
 
 
 
4efd815
ca4e572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deda7c3
ca4e572
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
import os
import openai


# 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
prompt = '假设你是一位优秀的Python老师,一步步教我PYTHONE,每次只交一个python的知识点,并且展示一个例子。现在开始。'

# 修改本函数,来实现你自己的 chatbot
# p: 对机器人说话的内容  
# qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。  
# uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。  
# 返回值:[type, content]
# 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
def chat(p, qid, uid):
    return ["text", callapi(p)]

openai.api_key = os.getenv("key1")
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="""我是一位python老师,让我一步步教你python吧,Let's go!
[对话测试](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()