当前位置: 首页 > article >正文

DeepSeek联网Google搜索引擎

目录:

    • 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_answerreturnmessages = [{"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.contentif content:full_response += contentyield 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日深夜通过个人社交媒体宣布正式退役。”]]

http://www.lryc.cn/news/2385933.html

相关文章:

  • 理论物理:为什么在极低温(接近绝对零度)时,经典理论失效?
  • 奈雪小程序任务脚本
  • 上海医日健集团物联网专利技术领跑智慧药房赛道
  • 基于Java+MySQL实现(Web)图书借阅管理系统
  • SAR ADC的功耗设计
  • PP-OCRv5
  • nginx的一些配置的意思
  • Agent模型微调
  • Android-OkHttp与Retrofit学习总结
  • 移远三款主流5G模块RM500U,RM520N,RG200U比较
  • C++引用以及和指针的区别
  • firfox 国外版和国内版本账号不互通问题处理
  • Linux基本指令篇 —— whoami指令
  • 用go从零构建写一个RPC(3)--异步调用+多路复用实现
  • 力扣395做题笔记
  • Python-numpy中常用的统计函数及转换函数
  • 【C语言干货】free细节
  • 网络安全-等级保护(等保) 2-0 等级保护制度现行技术标准
  • WebSocket(看这一篇就够了)
  • 旧物回收小程序:让闲置焕发光彩,为生活增添价值
  • 精益数据分析(73/126):黏性阶段的功能优先级法则——七问决策模型与风险控制
  • React声明式编程(手动控制,大型项目,深度定制)与Vue响应式系统(自动优化,中小型项目,快速开发)区别
  • 数学建模MathAI智能体-2025电工杯A题实战
  • 跨平台游戏引擎 Axmol-2.6.0 发布
  • C# Windows Forms应用程序-002
  • 理解计算机系统_线程(八):并行
  • 【MySQL】09.索引
  • 【备忘】 windows 11安装 AdGuardHome,实现开机自启,使用 DoH
  • [Windows] 游戏常用运行库- Game Runtime Libraries Package(6.2.25.0409)
  • MYSQL order 、group 与row_number详解