zhangbaoxing
commited on
Commit
·
428a750
1
Parent(s):
334140f
添加 requirements.txt
Browse files- app.py +6 -32
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,87 +1,61 @@
|
|
| 1 |
import openai
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
-
# 从环境变量中获取 OpenAI 的 API 密钥并设置
|
| 5 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 6 |
|
| 7 |
|
| 8 |
-
# 定义一个代表对话的类
|
| 9 |
class Conversation:
|
| 10 |
def __init__(self, prompt, num_of_round):
|
| 11 |
-
# 初始化方法,设置对话的提示和对话回合数
|
| 12 |
self.prompt = prompt
|
| 13 |
self.num_of_round = num_of_round
|
| 14 |
self.messages = []
|
| 15 |
-
# 将系统的提示消息添加到消息列表中
|
| 16 |
self.messages.append({"role": "system", "content": self.prompt})
|
| 17 |
|
| 18 |
def ask(self, question):
|
| 19 |
-
# 用户提问的方法
|
| 20 |
try:
|
| 21 |
-
# 将用户的问题添加到消息列表中
|
| 22 |
self.messages.append({"role": "user", "content": question})
|
| 23 |
-
# 使用 OpenAI 的 API 发起请求,获取模型的响应
|
| 24 |
response = openai.ChatCompletion.create(
|
| 25 |
-
model="gpt-3.5-turbo",
|
| 26 |
-
messages=self.messages,
|
| 27 |
-
temperature=0.5,
|
| 28 |
-
max_tokens=2048,
|
| 29 |
-
top_p=1,
|
| 30 |
)
|
| 31 |
except Exception as e:
|
| 32 |
-
# 打印并返回异常
|
| 33 |
print(e)
|
| 34 |
return e
|
| 35 |
|
| 36 |
-
# 从响应中提取助手的消息
|
| 37 |
message = response["choices"][0]["message"]["content"]
|
| 38 |
-
# 将助手的回复添加到消息列表中
|
| 39 |
self.messages.append({"role": "assistant", "content": message})
|
| 40 |
|
| 41 |
-
# 如果消息列表超过了规定的回合数,删除最早的用户和助手的消息
|
| 42 |
if len(self.messages) > self.num_of_round * 2 + 1:
|
| 43 |
del self.messages[1:3]
|
| 44 |
return message
|
| 45 |
|
| 46 |
|
| 47 |
-
# 导入 gradio 库,这是一个用于创建交互式 UI 的库
|
| 48 |
-
import gradio as gr
|
| 49 |
-
|
| 50 |
-
# 设置一个提示,定义了与模型的对话上下文和要求
|
| 51 |
prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
|
| 52 |
1. 你的回答必须是中文
|
| 53 |
2. 回答限制在100个字以内"""
|
| 54 |
|
| 55 |
-
# 使用先前定义的 Conversation 类创建一个对话实例
|
| 56 |
conv = Conversation(prompt, 5)
|
| 57 |
|
| 58 |
|
| 59 |
-
# 定义预测函数,它将获取输入,更新历史记录并返回模型的回答
|
| 60 |
def predict(input, history=[]):
|
| 61 |
-
# 将用户输入添加到历史记录中
|
| 62 |
history.append(input)
|
| 63 |
-
# 获取模型的回答
|
| 64 |
response = conv.ask(input)
|
| 65 |
-
# 将模型的回答添加到历史记录中
|
| 66 |
history.append(response)
|
| 67 |
-
# 创建一个包含用户和模型交互的列表
|
| 68 |
responses = [(u, b) for u, b in zip(history[::2], history[1::2])]
|
| 69 |
-
# 返回交互列表和完整历史记录
|
| 70 |
return responses, history
|
| 71 |
|
| 72 |
|
| 73 |
-
# 定义一个 UI 块,自定义其样式
|
| 74 |
with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:500px}") as demo:
|
| 75 |
-
# 创建一个聊天机器人界面
|
| 76 |
chatbot = gr.Chatbot(elem_id="chatbot")
|
| 77 |
-
# 创建一个状态对象,用于存储和传递历史记录
|
| 78 |
state = gr.State([])
|
| 79 |
|
| 80 |
-
# 在 UI 的行中定义一个文本框,用于用户输入
|
| 81 |
with gr.Row():
|
| 82 |
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
| 83 |
|
| 84 |
-
# 当文本框中有提交时,调用预测函数并更新聊天机器人界面和状态
|
| 85 |
txt.submit(predict, [txt, state], [chatbot, state])
|
| 86 |
|
| 87 |
demo.launch()
|
|
|
|
| 1 |
import openai
|
| 2 |
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
|
|
|
|
| 5 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 6 |
|
| 7 |
|
|
|
|
| 8 |
class Conversation:
|
| 9 |
def __init__(self, prompt, num_of_round):
|
|
|
|
| 10 |
self.prompt = prompt
|
| 11 |
self.num_of_round = num_of_round
|
| 12 |
self.messages = []
|
|
|
|
| 13 |
self.messages.append({"role": "system", "content": self.prompt})
|
| 14 |
|
| 15 |
def ask(self, question):
|
|
|
|
| 16 |
try:
|
|
|
|
| 17 |
self.messages.append({"role": "user", "content": question})
|
|
|
|
| 18 |
response = openai.ChatCompletion.create(
|
| 19 |
+
model="gpt-3.5-turbo",
|
| 20 |
+
messages=self.messages,
|
| 21 |
+
temperature=0.5,
|
| 22 |
+
max_tokens=2048,
|
| 23 |
+
top_p=1,
|
| 24 |
)
|
| 25 |
except Exception as e:
|
|
|
|
| 26 |
print(e)
|
| 27 |
return e
|
| 28 |
|
|
|
|
| 29 |
message = response["choices"][0]["message"]["content"]
|
|
|
|
| 30 |
self.messages.append({"role": "assistant", "content": message})
|
| 31 |
|
|
|
|
| 32 |
if len(self.messages) > self.num_of_round * 2 + 1:
|
| 33 |
del self.messages[1:3]
|
| 34 |
return message
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
|
| 38 |
1. 你的回答必须是中文
|
| 39 |
2. 回答限制在100个字以内"""
|
| 40 |
|
|
|
|
| 41 |
conv = Conversation(prompt, 5)
|
| 42 |
|
| 43 |
|
|
|
|
| 44 |
def predict(input, history=[]):
|
|
|
|
| 45 |
history.append(input)
|
|
|
|
| 46 |
response = conv.ask(input)
|
|
|
|
| 47 |
history.append(response)
|
|
|
|
| 48 |
responses = [(u, b) for u, b in zip(history[::2], history[1::2])]
|
|
|
|
| 49 |
return responses, history
|
| 50 |
|
| 51 |
|
|
|
|
| 52 |
with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:500px}") as demo:
|
|
|
|
| 53 |
chatbot = gr.Chatbot(elem_id="chatbot")
|
|
|
|
| 54 |
state = gr.State([])
|
| 55 |
|
|
|
|
| 56 |
with gr.Row():
|
| 57 |
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
| 58 |
|
|
|
|
| 59 |
txt.submit(predict, [txt, state], [chatbot, state])
|
| 60 |
|
| 61 |
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openai
|