目录:
- 1、使用背景
- 2、实现代码
- 3、Gradio 的 yield 机制
1、使用背景
比如所有易建联是什么时候退役的?使用大模型对这种实事回答不准确,需要通过联网搜索处理。
正确答案应该是2023年8月29日退役。
2、实现代码
# import gradio as gr
# def reverse_text(text):
# return text[::-1]
# demo=gr.Interface(fn=reverse_text,inputs="text",outputs="text")
# demo.launch(share="True")
import gradio as gr
import openai
from typing import List, Any, Iterator
# 配置DeepSeek API
api_key = "xxxxxxxxxxxxxxxxxxxxxx"
api_base = "xxxxxxxxxxxxxxxxxxxxxx"
client = openai.OpenAI(api_key=api_key, base_url=api_base)
search_results = "易建联于2023年8月29日深夜通过个人社交媒体宣布正式退役,结束了他21年的职业篮球生涯。"
#这里我是预设的答案,具体可以调用谷歌搜索引擎api或者通过python去爬取网页获取;我这里图简单对于这种实事不准确的优先返回人工验证的正确答案
preset_answer = "易建联于2023年8月29日深夜通过个人社交媒体宣布正式退役。"
def chat_stream(message: str, #用户输入的问题
history: List[List[str]],
temperature: float = 0.7,
top_k: int = 40,
system_prompt: str = "你是一个有帮助的助手。") -> Iterator[Any]:
"""流式输出DeepSeek响应"""
# 检查是否是特定问题
if message.strip() == "易建联什么时候退役的":
yield preset_answer
return
messages = [{"role": "system", "content": system_prompt}]
# 添加历史记录
for human_msg, ai_msg in history:
messages.append({"role": "user", "content": human_msg})
messages.append({"role": "assistant", "content": ai_msg})
# 添加当前消息
messages.append({"role": "user", "content": message})
# 调用API进行流式输出
response = client.chat.completions.create(
model="Qwen/Qwen2.5-VL-72B-Instruct",
messages=messages,# 包含系统提示+历史对话+当前问题
temperature=temperature,
top_p=1-(1.0/top_k) if top_k > 1 else 1.0,
stream=True# 启用流式输出
)
full_response = ""
for chunk in response:
if chunk.choices and len(chunk.choices) > 0:
content = chunk.choices[0].delta.content
if content:
full_response += content
yield full_response# 每次迭代更新聊天窗口
# 自定义CSS样式
custom_css = """
#chatbot {
height: 600px !important;
border-radius: 10px;
border: 1px solid #e0e0e0;
font-family: "Microsoft YaHei", "微软雅黑", sans-serif;
}
.message {
padding: 10px;
border-radius: 5px;
margin: 5px 0;
font-size: 15px;
line-height: 1.6;
}
.user-message {
background-color: #e3f2fd;
}
.bot-message {
background-color: #f5f5f5;
}
.gradio-container {
font-family: "Microsoft YaHei", "微软雅黑", sans-serif;
}
"""
# 创建Gradio界面
with gr.Blocks(title="DeepSeek 智能助手", css=custom_css, theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# 🤖 DeepSeek 智能助手
欢迎使用 DeepSeek 智能助手!您可以通过右侧的设置来调整 AI 的行为。
""")
with gr.Row():
with gr.Column(scale=4):
chatbot = gr.Chatbot(
height=600,
bubble_full_width=False,
show_copy_button=True,
elem_id="chatbot"
)
with gr.Row():
msg = gr.Textbox(
label="输入消息",
placeholder="在这里输入您的问题...",
scale=8,
container=False
)
submit_btn = gr.Button("发送", variant="primary", scale=1)
with gr.Row():
clear = gr.Button("清除历史", variant="secondary")
with gr.Column(scale=1):
system_prompt = gr.Textbox(
label="系统提示词",
value="你是一个有帮助的助手。",
lines=3
)
temperature = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.7,
step=0.1,
label="温度",
info="较高的值会使输出更加随机,较低的值会使输出更加确定"
)
top_k = gr.Slider(
minimum=1,
maximum=100,
value=40,
step=1,
label="Top K",
info="控制输出词汇的多样性"
)
#user函数清空输入框,更新聊天历史
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history, temp, top_k_val, sys_prompt):
history[-1][1] = ""# 初始化助手回复为空字符串
for response in chat_stream(history[-1][0], history[:-1], temp, top_k_val, sys_prompt):#调用大模型的回答
history[-1][1] = response# 逐步更新回答
yield history# 流式返回
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, [chatbot, temperature, top_k, system_prompt], chatbot
)
#点击发送时触发此方法调用user函数,user处理完成后调用bot函数
submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, [chatbot, temperature, top_k, system_prompt], chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()
3、Gradio 的 yield 机制
- Gradio 的 yield 机制:
每次 yield full_response 会实时更新 chatbot 组件的当前回复部分(history[-1][1])。
- 最终状态:
当流式结束时,chatbot 中会显示完整的对话记录,例如:
[[“易建联什么时候退役的”, “易建联于2023年8月29日深夜通过个人社交媒体宣布正式退役。”]]