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

DeepSeek智能对话助手项目

目录:

    • 1、效果图
    • 2、实现代码
    • 3、温度和TopK的作用对比

1、效果图

在这里插入图片描述

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 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
api_base = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"client = openai.OpenAI(api_key=api_key, base_url=api_base)def chat_stream(message: str, history: List[List[str]], temperature: float = 0.7,top_k: int = 40,system_prompt: str = "你是一个有帮助的助手。") -> Iterator[Any]:"""流式输出DeepSeek响应"""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.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="控制输出词汇的多样性")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] = responseyield historymsg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, [chatbot, temperature, top_k, system_prompt], chatbot)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、温度和TopK的作用对比

在这里插入图片描述

在这里插入图片描述
温度和TopK的调节可以使用大模型的回答更加精准和多样化。

http://www.xdnf.cn/news/8580.html

相关文章:

  • 对神经正切核的理解和推导(1)
  • MRI大型数据集FastMRI介绍
  • Spring Boot中使用AMQP协议与RabbitMQ
  • 考研408《计算机组成原理》复习笔记,第二章(3)数值数据的运算和存储(定点数计算)
  • 【C】中断处理函数模板
  • JavaScript- 2.2 内置对象MATH
  • 精益数据分析(84/126):打造商业造钱机器——从融资思维到盈利模型的落地实践
  • Go核心特性与并发编程
  • 基于Springboot + vue3实现的养老系统
  • Java多线程编程最佳实践
  • 展示了一个三轴(X, Y, Z)坐标系!
  • RAID技术全解析:从基础到实战应用指南
  • 学习STC51单片机14(芯片为STC89C52RC)
  • OpenLayers 加载鹰眼控件
  • Kotlin中let、run、with、apply及also的差别
  • SQL 语言
  • 策略建模:AI系统背后的“心灵感应”技术
  • 一文快速了解Vue3服务端渲染(SSR)
  • Windows逆向工程提升之IMAGE_RESOURCE_DIRECTORY
  • linux taskset 查询或设置进程绑定CPU
  • Vue3的模块化设计: 使用Script Setup API
  • 人脸美颜磨皮祛痘3:深度学习SUNet神经网络实现图片修复(含训练代码、数据集和GUI交互界面)
  • 【MPC控制 - 从ACC到自动驾驶】ACC系统原理与MPC初步认知
  • P3392 涂条纹
  • 零基础学习计算机网络编程----网络基本知识
  • python安装
  • css五边形
  • Spring boot使用
  • Git使用
  • 串扰与反射对信号完整性的影响