🤣最近在某音上不是很火一个各个身份回复消息的小键盘,收费感人,在本教程中,我们将在五分钟内使用 Python 编写一个 GUI 聊天软件,调用 DeepSeek API 进行智能对话,并支持不同的聊天风格,如 东北话、二进制、文言文、莎士比亚风格、海盗腔 等。纯属娱乐搞笑哈。
1. 项目介绍
功能特点
✅ 调用 DeepSeek API 进行智能对话 ✅ 多种聊天身份(东北话、二进制、文言文、莎士比亚风格、海盗腔) ✅ 轻量级 GUI 界面,无需 Web 服务器 ✅ 解析 JSON,提取 API 返回的内容
2. 环境准备
-
安装 Python(推荐 3.8+)
-
安装 requests 库(用于 API 调用)
pip install requests
-
Tkinter 是 Python 内置的 GUI 库,不需要额外安装。
3. 代码实现
3.1.解释
关于提示词:叫AI做程序的功能和方法要阐述:
关于Deepseek API 返回数据类型
DeepSeek API 返回的数据格式为 JSON。一般情况下,返回的数据结构如下:
{
"choices": [
{
"message": {
"content": "这里是 AI 的回答内容"
}
}
]
}
其中:
-
choices
是一个列表,包含 AI 生成的回答。 -
message
内的content
字段是 AI 生成的文本内容。 -
如果 API 请求错误或参数不正确,DeepSeek 可能会返回
error
字段,例如:
{
"error": "Invalid API key"
}
在解析 API 返回的数据时,我们通常需要检查 choices
是否存在,并确保 message
内的 content
字段可用。
关于API key,一共有10元的免费额度,够用很久。
3.2 完整代码
import requests
import tkinter as tk
from tkinter import ttk, scrolledtext
# DeepSeek API 配置
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions" # 替换为正确 API 地址
DEEPSEEK_API_KEY = "your_deepseek_api_key" # 替换为你的 API Key
# 预设不同的聊天身份
CHAT_STYLES = {
"默认": "You are a helpful assistant.",
"东北话": "You are a Chinese assistant speaking in Dongbei dialect. Use relaxed, humorous, and casual language.",
"二进制": "You are an AI that only replies in binary code.",
"文言文": "You are an ancient Chinese scholar. Reply in Classical Chinese (文言文).",
"莎士比亚风格": "You are William Shakespeare. Reply in Old English poetic style.",
"海盗腔": "You are a pirate. Reply with a pirate accent, using phrases like 'Arrr' and 'matey'."
}
class DeepSeekChatApp:
def __init__(self, root):
self.root = root
self.root.title("DeepSeek Chat")
self.root.geometry("500x400")
# 输入框
self.label_input = tk.Label(root, text="请输入你的问题:")
self.label_input.pack(pady=5)
self.text_input = tk.Entry(root, width=50)
self.text_input.pack(pady=5)
# 选择聊天风格
self.label_style = tk.Label(root, text="选择聊天身份:")
self.label_style.pack(pady=5)
self.style_combo = ttk.Combobox(root, values=list(CHAT_STYLES.keys()))
self.style_combo.set("默认")
self.style_combo.pack(pady=5)
# 发送按钮
self.send_button = tk.Button(root, text="发送", command=self.get_reply)
self.send_button.pack(pady=10)
# 回复框
self.label_response = tk.Label(root, text="回复:")
self.label_response.pack(pady=5)
self.response_box = scrolledtext.ScrolledText(root, width=60, height=10, wrap=tk.WORD)
self.response_box.pack(pady=5)
self.response_box.config(state=tk.DISABLED)
def get_reply(self):
user_message = self.text_input.get().strip()
selected_style = self.style_combo.get()
if not user_message:
self.display_response("请输入问题!")
return
system_prompt = CHAT_STYLES.get(selected_style, CHAT_STYLES["默认"])
try:
response = requests.post(
DEEPSEEK_API_URL,
headers={"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"},
json={
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_message}
],
"max_tokens": 100,
"temperature": 0.7
}
)
response_data = response.json()
print("DeepSeek API response:", response_data)
if response.status_code != 200:
reply = f"错误: {response_data.get('error', '未知错误')}"
else:
choices = response_data.get("choices", [])
if not choices or "message" not in choices[0] or "content" not in choices[0]["message"]:
reply = "API 返回格式错误"
else:
reply = choices[0]["message"]["content"].strip()
except Exception as e:
reply = f"请求失败: {str(e)}"
self.display_response(reply)
def display_response(self, text):
self.response_box.config(state=tk.NORMAL)
self.response_box.delete("1.0", tk.END)
self.response_box.insert(tk.END, text)
self.response_box.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = DeepSeekChatApp(root)
root.mainloop()
4. 运行方法
4.1 安装依赖
pip install requests
4.2 运行程序
python deepseek_chat_tk.py
5. 示例效果(一些DS的回复,这家伙回复对话无敌,回复特定的答案简直bullshit)
东北话
二进制(抽象)
文言文(抽象)
6. 总结
🚀 本地 Tkinter GUI,无需 Web 服务器
🚀 调用 DeepSeek API,并解析 JSON,返回可读文本
🚀 支持不同聊天风格,让 AI 说东北话、二进制、文言文等
✨ 现在就试试,看看 DeepSeek AI 会怎么回复你的问题吧! 🚀