import tkinter as tk
import threading
from openai import OpenAI
# 替换为你的 DeepSeek API Key
api_key = "你的秘钥"
# 替换为正确的 Base URL
base_url = "https://api.deepseek.com"
# 初始化对话历史
messages = []
# 窗口标题变量
window_title = "AI对话窗口程序"
# 初始化 OpenAI 客户Duan
client = OpenAI (api_key=api_key, base_url=base_url)
def chat_with_model (user_input):
"""
与模型进行对话,支持多轮对话和流式输出
"""
# 将用户输入添加到对话历史中
messages.append ({"role": "user", "content": user_input})
# 调用 API 进行对话
response = client.chat.completions.create (
model="deepseek-chat",
messages=messages,
stream=True # 启用流式输出
)
# 处理流式返回的内容
for chunk in response:
if hasattr (chunk.choices[0], "finish_reason") and chunk.choices[0].finish_reason == "stop":
break
if hasattr (chunk.choices[0].delta, "content") and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
# 替换 LaTeX 格式为更友好的形式
content = content.replace ( content = content.replace ( root.after (0, update_display_with_stream, content) # 在主线程中更新显示框
# 流式输出结束后,插入换行
root.after (0, lambda: display_text.insert (tk.END, def update_display_with_stream (content):
display_text.insert (tk.END, content)
# 自动滚动到最底部
display_text.see (tk.END)
def send_message (event=None):
user_input = input_entry.get ("1.0", tk.END).strip () # 获取输入框中的内容
if user_input:
# 将用户输入显示在对话框中
display_text.insert (tk.END, f"你: {user_input}\n", "user")
display_text.insert (tk.END, "deepseek: ", "assistant")
display_text.see (tk.END) # 自动滚动到最底部
# 启动线程处理模型请求
threading.Thread (target=chat_with_model, args= (user_input,), daemon=True).start ()
# 清空输入框
input_entry.delete ("1.0", tk.END)
def clear_chat ():
"""
清空对话历史和显示框
"""
# 清空显示框
display_text.delete ("1.0", tk.END)
# 清空对话历史
messages.clear ()
# 清空输入框
input_entry.delete ("1.0", tk.END)
# 创建主窗口
root = tk.Tk ()
root.title (window_title)
root.geometry ("800x600") # 设置窗口尺寸
# 配置 grid 布局权重
root.grid_rowconfigure (0, weight=1)
root.grid_rowconfigure (1, weight=0) # 输入框行不扩展
root.grid_columnconfigure (0, weight=1)
# 创建显示文字的编辑框
display_text = tk.Text (root, wrap=tk.NONE) # 禁用自动换行
display_text.grid (row=0, column=0, padx=10, pady=10, sticky="nsew")
# 创建垂直滚动条并绑定到显示框
vertical_scrollbar = tk.Scrollbar (root, command=display_text.yview)
vertical_scrollbar.grid (row=0, column=1, sticky= display_text.config (yscrollcommand=vertical_scrollbar.set)
# 创建水平滚动条并绑定到显示框
horizontal_scrollbar = tk.Scrollbar (root, orient=tk.HORIZONTAL, command=display_text.xview)
horizontal_scrollbar.grid (row=1, column=0, sticky= display_text.config (xscrollcommand=horizontal_scrollbar.set)
# 配置文本样式
display_text.tag_configure ("user", foreground="blue")
display_text.tag_configure ("assistant", foreground="green")
display_text.tag_configure ("bold", font= ("Arial", 12, "bold"))
# 创建输入文字的多行编辑框
input_entry = tk.Text (root, height=5, wrap=tk.NONE) # 禁用自动换行
input_entry.grid (row=2, column=0, padx=10, pady=10, sticky="nsew") # 调整到第三行
# 创建输入框的垂直滚动条
input_vertical_scrollbar = tk.Scrollbar (root, command=input_entry.yview)
input_vertical_scrollbar.grid (row=2, column=1, sticky= input_entry.config (yscrollcommand=input_vertical_scrollbar.set)
# 创建输入框的水平滚动条
input_horizontal_scrollbar = tk.Scrollbar (root, orient=tk.HORIZONTAL, command=input_entry.xview)
input_horizontal_scrollbar.grid (row=3, column=0, sticky= input_entry.config (xscrollcommand=input_horizontal_scrollbar.set)
# 按键绑定
input_entry.bind ("", lambda event: send_message ()) # 普通回车键发送消息
input_entry.bind ("", lambda event: input_entry.insert (tk.END, "\n")) # Ctrl+回车换行
# 创建发送按钮
send_button = tk.Button (root, text="点击发送", command=send_message, width=10, height=2) # 调小按钮大小
send_button.grid (row=2, column=2, padx=10, pady=10, sticky="ns") # 调整行到 row=2
# 创建清空对话按钮
clear_button = tk.Button (root, text="清空对话", command=clear_chat, width=10, height=2)
clear_button.grid (row=3, column=2, padx=10, pady=10, sticky="ns") # 位于发送按钮下方,row=3
# 主循环
root.mainloop ()