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

建设工程质量检测公司网站网站优化费用报价明细

建设工程质量检测公司网站,网站优化费用报价明细,呼和浩特做网站的公司,乔拓云建站有免费的吗为了增强智能聊天机器人的功能,我增加以下几个方面的改进: 1.智能框架和算法:引入更复杂的对话管理逻辑,例如情感分析、上下文理解等。 2.链接能力:增强对多个API节点的管理和切换逻辑,确保高可用性。 3.配…

为了增强智能聊天机器人的功能,我增加以下几个方面的改进:

1.智能框架和算法:引入更复杂的对话管理逻辑,例如情感分析、上下文理解等。
2.链接能力:增强对多个API节点的管理和切换逻辑,确保高可用性。
3.配置管理:增加更多配置选项,如模型选择、最大token数、请求超时等。
以下是完善后的代码和 config.json 文件内容:
完善后的代码

import tkinter as tk
from tkinter import scrolledtext, filedialog, messagebox
import requests
import os
import threading
import json
from dotenv import load_dotenv# 加载环境变量
load_dotenv()# 从环境变量中获取API密钥和DeepSeek的API端点
API_KEY = os.getenv('DEEPSEEK_API_KEY')
DEFAULT_API_URL = os.getenv('DEEPSEEK_API_URL', 'https://api.deepseek.com/v1/chat')# 如果环境变量未设置,尝试从配置文件中读取
CONFIG_FILE = 'config.json'
if not API_KEY or not DEFAULT_API_URL:try:with open(CONFIG_FILE, 'r') as f:config = json.load(f)API_KEY = config.get('API_KEY', API_KEY)DEFAULT_API_URL = config.get('DEFAULT_API_URL', DEFAULT_API_URL)except (FileNotFoundError, json.JSONDecodeError) as e:messagebox.showwarning("警告", f"配置文件加载失败: {str(e)}")API_KEY = ''DEFAULT_API_URL = 'https://api.deepseek.com/v1/chat'# 从配置文件中读取多个API节点和其他配置
try:with open(CONFIG_FILE, 'r') as f:config = json.load(f)API_NODES = config.get('API_NODES', [DEFAULT_API_URL])MAX_TOKENS = config.get('MAX_TOKENS', 150)TIMEOUT = config.get('TIMEOUT', 10)MODEL = config.get('MODEL', 'deepseek-7b')
except (FileNotFoundError, json.JSONDecodeError) as e:messagebox.showwarning("警告", f"配置文件加载失败: {str(e)}")API_NODES = [DEFAULT_API_URL]MAX_TOKENS = 150TIMEOUT = 10MODEL = 'deepseek-7b'# 当前使用的API节点
CURRENT_API_URL = API_NODES[0]# 存储对话历史
conversation_history = []def get_response(prompt):headers = {'Authorization': f'Bearer {API_KEY}','Content-Type': 'application/json'}data = {'model': MODEL,  # 使用的模型名称'messages': conversation_history + [{'role': 'user', 'content': prompt}],'max_tokens': MAX_TOKENS  # 生成的最大token数}for api_url in API_NODES:try:response = requests.post(api_url, json=data, headers=headers, timeout=TIMEOUT)response.raise_for_status()model_response = response.json()['choices'][0]['message']['content']conversation_history.append({'role': 'user', 'content': prompt})conversation_history.append({'role': 'assistant', 'content': model_response})return model_responseexcept requests.RequestException as e:chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"请求错误: {str(e)}\n")chat_log.config(state=tk.DISABLED)continueraise Exception("所有API节点均无法连接")def send_message():user_input = entry.get()if user_input.strip():chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"你: {user_input}\n")chat_log.config(state=tk.DISABLED)# 在新线程中处理API请求threading.Thread(target=process_response, args=(user_input,)).start()entry.delete(0, tk.END)chat_log.yview(tk.END)def process_response(user_input):try:response = get_response(user_input)chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"DeepSeek: {response}\n")chat_log.config(state=tk.DISABLED)chat_log.yview(tk.END)except Exception as e:chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"错误: {str(e)}\n")chat_log.config(state=tk.DISABLED)chat_log.yview(tk.END)def on_closing():root.destroy()def clear_conversation():global conversation_historyconversation_history = []chat_log.config(state=tk.NORMAL)chat_log.delete(1.0, tk.END)chat_log.config(state=tk.DISABLED)def load_config():file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])if file_path:try:with open(file_path, 'r') as f:config = json.load(f)global API_KEY, DEFAULT_API_URL, API_NODES, CURRENT_API_URL, MAX_TOKENS, TIMEOUT, MODELAPI_KEY = config.get('API_KEY', '')DEFAULT_API_URL = config.get('DEFAULT_API_URL', 'https://api.deepseek.com/v1/chat')API_NODES = config.get('API_NODES', [DEFAULT_API_URL])CURRENT_API_URL = API_NODES[0]MAX_TOKENS = config.get('MAX_TOKENS', 150)TIMEOUT = config.get('TIMEOUT', 10)MODEL = config.get('MODEL', 'deepseek-7b')chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"配置加载成功: API_KEY={API_KEY}, API_NODES={API_NODES}, MAX_TOKENS={MAX_TOKENS}, TIMEOUT={TIMEOUT}, MODEL={MODEL}\n")chat_log.config(state=tk.DISABLED)except (FileNotFoundError, json.JSONDecodeError) as e:chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"错误: {str(e)}\n")chat_log.config(state=tk.DISABLED)def save_config():config = {'API_KEY': API_KEY,'DEFAULT_API_URL': DEFAULT_API_URL,'API_NODES': API_NODES,'MAX_TOKENS': MAX_TOKENS,'TIMEOUT': TIMEOUT,'MODEL': MODEL}file_path = filedialog.asksaveasfilename(defaultextension=".json", filetypes=[("JSON files", "*.json")])if file_path:try:with open(file_path, 'w') as f:json.dump(config, f, indent=4)chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"配置保存成功: {file_path}\n")chat_log.config(state=tk.DISABLED)except IOError as e:chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"错误: {str(e)}\n")chat_log.config(state=tk.DISABLED)def switch_node():global CURRENT_API_URLcurrent_index = API_NODES.index(CURRENT_API_URL)next_index = (current_index + 1) % len(API_NODES)CURRENT_API_URL = API_NODES[next_index]chat_log.config(state=tk.NORMAL)chat_log.insert(tk.END, f"切换到API节点: {CURRENT_API_URL}\n")chat_log.config(state=tk.DISABLED)root = tk.Tk()
root.title("智能聊天机器人")# 设置窗口大小
root.geometry("600x400")# 创建聊天记录区域
chat_log = scrolledtext.ScrolledText(root, wrap=tk.WORD, state=tk.DISABLED)
chat_log.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)# 创建输入框
entry = tk.Entry(root, width=80)
entry.pack(pady=10, padx=10, side=tk.LEFT, fill=tk.X, expand=True)# 创建发送按钮
send_button = tk.Button(root, text="发送", command=send_message)
send_button.pack(pady=10, padx=10, side=tk.RIGHT)# 创建清除对话按钮
clear_button = tk.Button(root, text="清除对话", command=clear_conversation)
clear_button.pack(pady=10, padx=10, side=tk.RIGHT)# 创建加载配置按钮
load_config_button = tk.Button(root, text="加载配置", command=load_config)
load_config_button.pack(pady=10, padx=10, side=tk.RIGHT)# 创建保存配置按钮
save_config_button = tk.Button(root, text="保存配置", command=save_config)
save_config_button.pack(pady=10, padx=10, side=tk.RIGHT)# 创建切换节点按钮
switch_node_button = tk.Button(root, text="切换节点", command=switch_node)
switch_node_button.pack(pady=10, padx=10, side=tk.RIGHT)# 绑定关闭事件
root.protocol("WM_DELETE_WINDOW", on_closing)# 启动主循环
root.mainloop()

config.json文件内容

{"API_KEY": "your_api_key_here","DEFAULT_API_URL": "https://api.deepseek.com/v1/chat","API_NODES": ["https://api.deepseek.com/v1/chat","https://api.deepseek2.com/v1/chat","https://api.deepseek3.com/v1/chat"],"MAX_TOKENS": 150,"TIMEOUT": 10,"MODEL": "deepseek-7b"
}

解释
1.API_KEY: 你的DeepSeek API密钥。
2.DEFAULT_API_URL: 默认的API节点URL。
3.API_NODES: 一个包含多个API节点URL的列表,用于在请求失败时进行切换。
4.MAX_TOKENS: 每次请求生成的最大token数。
5.TIMEOUT: 请求的超时时间(秒)。
6.MODEL: 使用的模型名称。
如何使用
1.将上述 config.json 内容复制到一个名为 config.json 的文件中。
2.将 config.json 文件放在与你的Python脚本相同的目录下。
3.确保 API_KEY 和 API_NODES 中的URL是有效的。
读取配置文件的代码片段

# 从配置文件中读取多个API节点和其他配置
try:with open(CONFIG_FILE, 'r') as f:config = json.load(f)API_KEY = config.get('API_KEY', '')DEFAULT_API_URL = config.get('DEFAULT_API_URL', 'https://api.deepseek.com/v1/chat')API_NODES = config.get('API_NODES', [DEFAULT_API_URL])MAX_TOKENS = config.get('MAX_TOKENS', 150)TIMEOUT = config.get('TIMEOUT', 10)MODEL = config.get('MODEL', 'deepseek-7b')
except (FileNotFoundError, json.JSONDecodeError) as e:messagebox.showwarning("警告", f"配置文件加载失败: {str(e)}")API_NODES = [DEFAULT_API_URL]MAX_TOKENS = 150TIMEOUT = 10MODEL = 'deepseek-7b'

通过这些改进,你的智能聊天机器人将更加健壮,能够更好地管理和切换多个API节点,同时提供更多的配置选项,增强其智能框架和算法能力。

http://www.dtcms.com/wzjs/332308.html

相关文章:

  • 网站制作公司宁波哪家好搜索推广出价多少合适
  • 做门票售卖网站百度收录入口
  • 网站建设及优化百度关键词排名批量查询工具
  • 建设茶网站网站测试友情链接系统
  • 康定网站建设工作室关键词歌词完整版
  • 网站建设基本流程规范杭州疫情最新消息
  • 常州网站建设公司信息站外引流推广渠道
  • 做汽车微信广告视频网站有哪些网站安全检测工具
  • 佛山做网站永网北京最新疫情最新消息
  • 灵犀科技 高端网站建设百度搜索风云榜单
  • 今日油价92汽油郑州有没有厉害的seo顾问
  • 公司给别人做的网站违法的吗十大看免费行情的软件下载
  • 网站更改网页制作的软件有哪些
  • 个人可以做外贸的网站抓关键词的方法10条
  • 网站建设中搜索引擎的作用百度爱采购竞价推广
  • 驻马店网站制作太原seo管理
  • 邯山专业做网站站长工具seo综合查询网
  • 英迈思做的网站怎么样如何编写一个网站
  • 太原深圳建设工程信息网站百度域名
  • 西安做网站陕西必达电脑优化系统的软件哪个好
  • 接单子做网站关键词云图
  • 威海市住房和城乡建设局网站云优化
  • 莆田兼职做外贸网站网络推广公司北京
  • 电子商务网站建设知识点总结seocms
  • 室内设计学校大专seo对网店推广的作用
  • 专业的上海网站建设公司排名流量主广告点击自助平台
  • wordpress获取指定分类的图像描述网站seo搜索引擎优化教程
  • 网站推广咋做的建网站多少钱
  • 设计一个个人网站的基本步骤爱站网关键词工具
  • 长沙工作室网站建设推广策划方案范文