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

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.xdnf.cn/news/8730.html

相关文章:

  • Windows端的C函数setlocale、printf与wprintf打印中文字符谜局小解
  • 深入浅出IIC协议 - 从总线原理到FPGA实战开发 -- 第六篇:AXI4-Lite桥接设计
  • 金众诚业财一体化解决方案如何提升项目盈利能力?
  • 国际荐酒师(香港)协会亮相新西兰葡萄酒巡展深度参与赵凤仪大师班
  • 《数据结构》系列笔记|附扫描手写笔记 1.0开篇-数据结构在学什么?
  • 【八股战神篇】操作系统高频面试题
  • Markdown 到 LaTeX:Overleaf 学习笔记
  • 华为OD机试真题——欢乐周末 (2025B卷:200分)Java/python/JavaScript/C/C++/GO最佳实现
  • 《深入探秘:从底层搭建Python微服务之FastAPI与Docker部署》
  • 在Linux下用GPIO模拟I2C通信(软件)
  • 前端流行框架Vue3教程:26. 异步组件
  • [医学影像 AI] 使用 PyTorch 和 MedicalZooPytorch 实现 3D 医学影像分割
  • xss-labs第15关
  • 历年华中科技大学保研上机真题
  • 【数据结构】图论探秘:广度优先遍历(BFS)与生成树的构建艺术
  • DAY35
  • JVM 的内存模型
  • 【MySQL系列】SQL 分组统计与排序
  • Vue-数组操作方法技术解析大纲
  • 【爬虫学习】Python数据采集进阶:从请求优化到解析技术实战
  • 解决论文中字体未嵌入的问题
  • Q2:如果 Channel 没有关闭,读取会一直阻塞吗?
  • leetcode654.最大二叉树:递归分治下的最大值索引定位与树构建
  • 显示docker桌面,vnc远程连接docker
  • Android应用中设置非系统默认语言(使用Kotlin)
  • 机械师安装ubantu双系统:三、GPT分区安装Ubantu
  • 【医学影像 AI】医学影像 AI 入门:PyTorch 基础与数据加载
  • 并发编程艺术--AQS底层源码解析(一)
  • 计算机视觉---YOLOv2
  • [特殊字符] Function Calling 技术详解与 Qwen 模型实践指南