mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
feat(deepseek): add independent DeepSeek bot module with dedicated config
Separate DeepSeek from ChatGPTBot into its own module (models/deepseek/) with dedicated deepseek_api_key and deepseek_api_base config fields, avoiding config conflicts when switching between providers. Backward compatible with old users who configured DeepSeek via open_ai_api_key/open_ai_api_base through automatic fallback. Made-with: Cursor
This commit is contained in:
@@ -74,7 +74,7 @@ class AgentLLMModel(LLMModel):
|
|||||||
("qwen", const.QWEN_DASHSCOPE), ("qwq", const.QWEN_DASHSCOPE), ("qvq", const.QWEN_DASHSCOPE),
|
("qwen", const.QWEN_DASHSCOPE), ("qwq", const.QWEN_DASHSCOPE), ("qvq", const.QWEN_DASHSCOPE),
|
||||||
("gemini", const.GEMINI), ("glm", const.ZHIPU_AI), ("claude", const.CLAUDEAPI),
|
("gemini", const.GEMINI), ("glm", const.ZHIPU_AI), ("claude", const.CLAUDEAPI),
|
||||||
("moonshot", const.MOONSHOT), ("kimi", const.MOONSHOT),
|
("moonshot", const.MOONSHOT), ("kimi", const.MOONSHOT),
|
||||||
("doubao", const.DOUBAO),
|
("doubao", const.DOUBAO), ("deepseek", const.DEEPSEEK),
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, bridge: Bridge, bot_type: str = "chat"):
|
def __init__(self, bridge: Bridge, bot_type: str = "chat"):
|
||||||
@@ -115,8 +115,6 @@ class AgentLLMModel(LLMModel):
|
|||||||
return const.QWEN_DASHSCOPE
|
return const.QWEN_DASHSCOPE
|
||||||
if model_name in [const.MOONSHOT, "moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"]:
|
if model_name in [const.MOONSHOT, "moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"]:
|
||||||
return const.MOONSHOT
|
return const.MOONSHOT
|
||||||
if model_name in [const.DEEPSEEK_CHAT, const.DEEPSEEK_REASONER]:
|
|
||||||
return const.OPENAI
|
|
||||||
for prefix, btype in self._MODEL_PREFIX_MAP:
|
for prefix, btype in self._MODEL_PREFIX_MAP:
|
||||||
if model_name.startswith(prefix):
|
if model_name.startswith(prefix):
|
||||||
return btype
|
return btype
|
||||||
|
|||||||
@@ -61,6 +61,9 @@ class Bridge(object):
|
|||||||
if model_type and model_type.startswith("doubao"):
|
if model_type and model_type.startswith("doubao"):
|
||||||
self.btype["chat"] = const.DOUBAO
|
self.btype["chat"] = const.DOUBAO
|
||||||
|
|
||||||
|
if model_type and model_type.startswith("deepseek"):
|
||||||
|
self.btype["chat"] = const.DEEPSEEK
|
||||||
|
|
||||||
if model_type in [const.MODELSCOPE]:
|
if model_type in [const.MODELSCOPE]:
|
||||||
self.btype["chat"] = const.MODELSCOPE
|
self.btype["chat"] = const.MODELSCOPE
|
||||||
|
|
||||||
|
|||||||
@@ -563,9 +563,9 @@ class ConfigHandler:
|
|||||||
}),
|
}),
|
||||||
("deepseek", {
|
("deepseek", {
|
||||||
"label": "DeepSeek",
|
"label": "DeepSeek",
|
||||||
"api_key_field": "open_ai_api_key",
|
"api_key_field": "deepseek_api_key",
|
||||||
"api_base_key": None,
|
"api_base_key": "deepseek_api_base",
|
||||||
"api_base_default": None,
|
"api_base_default": "https://api.deepseek.com/v1",
|
||||||
"models": [const.DEEPSEEK_CHAT, const.DEEPSEEK_REASONER],
|
"models": [const.DEEPSEEK_CHAT, const.DEEPSEEK_REASONER],
|
||||||
}),
|
}),
|
||||||
("linkai", {
|
("linkai", {
|
||||||
@@ -579,9 +579,9 @@ class ConfigHandler:
|
|||||||
|
|
||||||
EDITABLE_KEYS = {
|
EDITABLE_KEYS = {
|
||||||
"model", "bot_type", "use_linkai",
|
"model", "bot_type", "use_linkai",
|
||||||
"open_ai_api_base", "claude_api_base", "gemini_api_base",
|
"open_ai_api_base", "deepseek_api_base", "claude_api_base", "gemini_api_base",
|
||||||
"zhipu_ai_api_base", "moonshot_base_url", "ark_base_url",
|
"zhipu_ai_api_base", "moonshot_base_url", "ark_base_url",
|
||||||
"open_ai_api_key", "claude_api_key", "gemini_api_key",
|
"open_ai_api_key", "deepseek_api_key", "claude_api_key", "gemini_api_key",
|
||||||
"zhipu_ai_api_key", "dashscope_api_key", "moonshot_api_key",
|
"zhipu_ai_api_key", "dashscope_api_key", "moonshot_api_key",
|
||||||
"ark_api_key", "minimax_api_key", "linkai_api_key",
|
"ark_api_key", "minimax_api_key", "linkai_api_key",
|
||||||
"agent_max_context_tokens", "agent_max_context_turns", "agent_max_steps",
|
"agent_max_context_tokens", "agent_max_context_turns", "agent_max_steps",
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ def create_bot(bot_type):
|
|||||||
from models.baidu.baidu_wenxin import BaiduWenxinBot
|
from models.baidu.baidu_wenxin import BaiduWenxinBot
|
||||||
return BaiduWenxinBot()
|
return BaiduWenxinBot()
|
||||||
|
|
||||||
elif bot_type in (const.OPENAI, const.CHATGPT, const.DEEPSEEK): # OpenAI-compatible API
|
elif bot_type == const.DEEPSEEK:
|
||||||
|
from models.deepseek.deepseek_bot import DeepSeekBot
|
||||||
|
return DeepSeekBot()
|
||||||
|
|
||||||
|
elif bot_type in (const.OPENAI, const.CHATGPT): # OpenAI-compatible API
|
||||||
from models.chatgpt.chat_gpt_bot import ChatGPTBot
|
from models.chatgpt.chat_gpt_bot import ChatGPTBot
|
||||||
return ChatGPTBot()
|
return ChatGPTBot()
|
||||||
|
|
||||||
|
|||||||
0
models/deepseek/__init__.py
Normal file
0
models/deepseek/__init__.py
Normal file
160
models/deepseek/deepseek_bot.py
Normal file
160
models/deepseek/deepseek_bot.py
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
# encoding:utf-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
DeepSeek Bot — fully OpenAI-compatible, uses its own API key / base config.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from models.bot import Bot
|
||||||
|
from models.openai_compatible_bot import OpenAICompatibleBot
|
||||||
|
from models.session_manager import SessionManager
|
||||||
|
from bridge.context import ContextType
|
||||||
|
from bridge.reply import Reply, ReplyType
|
||||||
|
from common import const
|
||||||
|
from common.log import logger
|
||||||
|
from config import conf, load_config
|
||||||
|
from .deepseek_session import DeepSeekSession
|
||||||
|
|
||||||
|
DEFAULT_API_BASE = "https://api.deepseek.com/v1"
|
||||||
|
|
||||||
|
|
||||||
|
class DeepSeekBot(Bot, OpenAICompatibleBot):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.sessions = SessionManager(
|
||||||
|
DeepSeekSession,
|
||||||
|
model=conf().get("model") or const.DEEPSEEK_CHAT,
|
||||||
|
)
|
||||||
|
conf_model = conf().get("model") or const.DEEPSEEK_CHAT
|
||||||
|
self.args = {
|
||||||
|
"model": conf_model,
|
||||||
|
"temperature": conf().get("temperature", 0.7),
|
||||||
|
"top_p": conf().get("top_p", 1.0),
|
||||||
|
"frequency_penalty": conf().get("frequency_penalty", 0.0),
|
||||||
|
"presence_penalty": conf().get("presence_penalty", 0.0),
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------- config helpers ----------
|
||||||
|
|
||||||
|
@property
|
||||||
|
def api_key(self):
|
||||||
|
return conf().get("deepseek_api_key") or conf().get("open_ai_api_key")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def api_base(self):
|
||||||
|
url = (
|
||||||
|
conf().get("deepseek_api_base")
|
||||||
|
or conf().get("open_ai_api_base")
|
||||||
|
or DEFAULT_API_BASE
|
||||||
|
)
|
||||||
|
return url.rstrip("/")
|
||||||
|
|
||||||
|
def get_api_config(self):
|
||||||
|
"""OpenAICompatibleBot interface — used by call_with_tools()."""
|
||||||
|
return {
|
||||||
|
"api_key": self.api_key,
|
||||||
|
"api_base": self.api_base,
|
||||||
|
"model": conf().get("model", const.DEEPSEEK_CHAT),
|
||||||
|
"default_temperature": conf().get("temperature", 0.7),
|
||||||
|
"default_top_p": conf().get("top_p", 1.0),
|
||||||
|
"default_frequency_penalty": conf().get("frequency_penalty", 0.0),
|
||||||
|
"default_presence_penalty": conf().get("presence_penalty", 0.0),
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------- simple chat (non-agent mode) ----------
|
||||||
|
|
||||||
|
def reply(self, query, context=None):
|
||||||
|
if context.type == ContextType.TEXT:
|
||||||
|
logger.info("[DEEPSEEK] query={}".format(query))
|
||||||
|
|
||||||
|
session_id = context["session_id"]
|
||||||
|
reply = None
|
||||||
|
clear_memory_commands = conf().get("clear_memory_commands", ["#清除记忆"])
|
||||||
|
if query in clear_memory_commands:
|
||||||
|
self.sessions.clear_session(session_id)
|
||||||
|
reply = Reply(ReplyType.INFO, "记忆已清除")
|
||||||
|
elif query == "#清除所有":
|
||||||
|
self.sessions.clear_all_session()
|
||||||
|
reply = Reply(ReplyType.INFO, "所有人记忆已清除")
|
||||||
|
elif query == "#更新配置":
|
||||||
|
load_config()
|
||||||
|
reply = Reply(ReplyType.INFO, "配置已更新")
|
||||||
|
if reply:
|
||||||
|
return reply
|
||||||
|
|
||||||
|
session = self.sessions.session_query(query, session_id)
|
||||||
|
logger.debug("[DEEPSEEK] session query={}".format(session.messages))
|
||||||
|
|
||||||
|
new_args = self.args.copy()
|
||||||
|
reply_content = self.reply_text(session, args=new_args)
|
||||||
|
logger.debug(
|
||||||
|
"[DEEPSEEK] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format(
|
||||||
|
session.messages, session_id,
|
||||||
|
reply_content["content"], reply_content["completion_tokens"],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if reply_content["completion_tokens"] == 0 and len(reply_content["content"]) > 0:
|
||||||
|
reply = Reply(ReplyType.ERROR, reply_content["content"])
|
||||||
|
elif reply_content["completion_tokens"] > 0:
|
||||||
|
self.sessions.session_reply(
|
||||||
|
reply_content["content"], session_id, reply_content["total_tokens"],
|
||||||
|
)
|
||||||
|
reply = Reply(ReplyType.TEXT, reply_content["content"])
|
||||||
|
else:
|
||||||
|
reply = Reply(ReplyType.ERROR, reply_content["content"])
|
||||||
|
logger.debug("[DEEPSEEK] reply {} used 0 tokens.".format(reply_content))
|
||||||
|
return reply
|
||||||
|
else:
|
||||||
|
reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type))
|
||||||
|
return reply
|
||||||
|
|
||||||
|
def reply_text(self, session, args=None, retry_count: int = 0) -> dict:
|
||||||
|
try:
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Authorization": "Bearer " + self.api_key,
|
||||||
|
}
|
||||||
|
body = args.copy()
|
||||||
|
body["messages"] = session.messages
|
||||||
|
|
||||||
|
res = requests.post(
|
||||||
|
f"{self.api_base}/chat/completions",
|
||||||
|
headers=headers,
|
||||||
|
json=body,
|
||||||
|
timeout=180,
|
||||||
|
)
|
||||||
|
if res.status_code == 200:
|
||||||
|
response = res.json()
|
||||||
|
return {
|
||||||
|
"total_tokens": response["usage"]["total_tokens"],
|
||||||
|
"completion_tokens": response["usage"]["completion_tokens"],
|
||||||
|
"content": response["choices"][0]["message"]["content"],
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
response = res.json()
|
||||||
|
error = response.get("error", {})
|
||||||
|
logger.error(
|
||||||
|
f"[DEEPSEEK] chat failed, status_code={res.status_code}, "
|
||||||
|
f"msg={error.get('message')}, type={error.get('type')}"
|
||||||
|
)
|
||||||
|
result = {"completion_tokens": 0, "content": "提问太快啦,请休息一下再问我吧"}
|
||||||
|
need_retry = False
|
||||||
|
if res.status_code >= 500:
|
||||||
|
need_retry = retry_count < 2
|
||||||
|
elif res.status_code == 401:
|
||||||
|
result["content"] = "授权失败,请检查API Key是否正确"
|
||||||
|
elif res.status_code == 429:
|
||||||
|
result["content"] = "请求过于频繁,请稍后再试"
|
||||||
|
need_retry = retry_count < 2
|
||||||
|
|
||||||
|
if need_retry:
|
||||||
|
time.sleep(3)
|
||||||
|
return self.reply_text(session, args, retry_count + 1)
|
||||||
|
return result
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(e)
|
||||||
|
if retry_count < 2:
|
||||||
|
return self.reply_text(session, args, retry_count + 1)
|
||||||
|
return {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"}
|
||||||
57
models/deepseek/deepseek_session.py
Normal file
57
models/deepseek/deepseek_session.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
from models.session_manager import Session
|
||||||
|
from common.log import logger
|
||||||
|
|
||||||
|
|
||||||
|
class DeepSeekSession(Session):
|
||||||
|
def __init__(self, session_id, system_prompt=None, model="deepseek-chat"):
|
||||||
|
super().__init__(session_id, system_prompt)
|
||||||
|
self.model = model
|
||||||
|
self.reset()
|
||||||
|
|
||||||
|
def discard_exceeding(self, max_tokens, cur_tokens=None):
|
||||||
|
precise = True
|
||||||
|
try:
|
||||||
|
cur_tokens = self.calc_tokens()
|
||||||
|
except Exception as e:
|
||||||
|
precise = False
|
||||||
|
if cur_tokens is None:
|
||||||
|
raise e
|
||||||
|
logger.debug("Exception when counting tokens precisely for query: {}".format(e))
|
||||||
|
while cur_tokens > max_tokens:
|
||||||
|
if len(self.messages) > 2:
|
||||||
|
self.messages.pop(1)
|
||||||
|
elif len(self.messages) == 2 and self.messages[1]["role"] == "assistant":
|
||||||
|
self.messages.pop(1)
|
||||||
|
if precise:
|
||||||
|
cur_tokens = self.calc_tokens()
|
||||||
|
else:
|
||||||
|
cur_tokens = cur_tokens - max_tokens
|
||||||
|
break
|
||||||
|
elif len(self.messages) == 2 and self.messages[1]["role"] == "user":
|
||||||
|
logger.warn("user message exceed max_tokens. total_tokens={}".format(cur_tokens))
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
logger.debug("max_tokens={}, total_tokens={}, len(messages)={}".format(
|
||||||
|
max_tokens, cur_tokens, len(self.messages)))
|
||||||
|
break
|
||||||
|
if precise:
|
||||||
|
cur_tokens = self.calc_tokens()
|
||||||
|
else:
|
||||||
|
cur_tokens = cur_tokens - max_tokens
|
||||||
|
return cur_tokens
|
||||||
|
|
||||||
|
def calc_tokens(self):
|
||||||
|
return num_tokens_from_messages(self.messages, self.model)
|
||||||
|
|
||||||
|
|
||||||
|
def num_tokens_from_messages(messages, model):
|
||||||
|
tokens = 0
|
||||||
|
for msg in messages:
|
||||||
|
content = msg.get("content", "")
|
||||||
|
if isinstance(content, str):
|
||||||
|
tokens += len(content)
|
||||||
|
elif isinstance(content, list):
|
||||||
|
for block in content:
|
||||||
|
if isinstance(block, dict):
|
||||||
|
tokens += len(block.get("text", ""))
|
||||||
|
return tokens
|
||||||
Reference in New Issue
Block a user