-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feature/deepseek thinking mode #4190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
kawayiYokami
wants to merge
2
commits into
AstrBotDevs:master
from
kawayiYokami:feature/deepseek-thinking-mode
+126
−1
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| from astrbot.core.agent.tool import ToolSet | ||
| from astrbot.core.provider.entities import LLMResponse | ||
|
|
||
| from ..entities import ProviderType | ||
| from ..register import register_provider_adapter | ||
| from .openai_source import ProviderOpenAIOfficial | ||
|
|
||
|
|
||
| @register_provider_adapter( | ||
| "deepseek_chat_completion", | ||
| "DeepSeek API Chat Completion 提供商适配器", | ||
| ProviderType.CHAT_COMPLETION, | ||
| default_config_tmpl={ | ||
| "id": "deepseek", | ||
| "provider": "deepseek", | ||
| "type": "deepseek_chat_completion", | ||
| "provider_type": "chat_completion", | ||
| "enable": True, | ||
| "key": [], | ||
| "api_base": "https://api.deepseek.com/v1", | ||
| "timeout": 120, | ||
| "custom_headers": {}, | ||
| "hint": "DeepSeek 官方 API,支持思考模式。", | ||
| }, | ||
| provider_display_name="DeepSeek", | ||
| ) | ||
| class ProviderDeepSeek(ProviderOpenAIOfficial): | ||
| def _should_enable_thinking(self, payloads: dict) -> bool: | ||
| """判断是否应该启用思考模式 | ||
| 规则: | ||
| 1. deepseek-chat 模型无条件关闭思维链 | ||
| 2. deepseek-reasoner 模型无条件开启思维链 | ||
| 3. 其他模型根据 ds_thinking_tool_call 配置决定 | ||
| """ | ||
| model = payloads.get("model", "").lower() | ||
|
|
||
| # deepseek-chat 强制关闭 | ||
| if "deepseek-chat" in model: | ||
| return False | ||
|
|
||
| # deepseek-reasoner 强制开启 | ||
| if "deepseek-reasoner" in model or "reasoner" in model: | ||
| return True | ||
|
|
||
| # 其他模型根据配置决定 | ||
| return self.provider_config.get("ds_thinking_tool_call", False) | ||
|
|
||
| async def _query_stream( | ||
| self, | ||
| payloads: dict, | ||
| tools: ToolSet | None, | ||
| ): | ||
| # 判断是否启用思考模式 | ||
| ds_thinking_enabled = self._should_enable_thinking(payloads) | ||
|
|
||
| if ds_thinking_enabled: | ||
| messages = payloads.get("messages", []) | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # DeepSeek API 要求:思考模式下每条助手消息必须有 reasoning_content | ||
| # 先确保每条助手消息都有这个字段 | ||
| for msg in messages: | ||
| if msg.get("role") == "assistant": | ||
| if "reasoning_content" not in msg: | ||
| # 工具调用消息等特殊消息初始化为空字符串 | ||
| msg["reasoning_content"] = "" | ||
|
|
||
| # 清理历史消息中的 reasoning_content | ||
| # 只有最后一条是 user 时才清空(说明是新问题) | ||
| if messages and messages[-1].get("role") == "user": | ||
| for msg in messages: | ||
| if msg.get("role") == "assistant" and "reasoning_content" in msg: | ||
| msg["reasoning_content"] = "" | ||
|
|
||
| # 添加 thinking 参数(父类会自动把它放到 extra_body) | ||
| payloads["thinking"] = {"type": "enabled"} | ||
|
|
||
| # 调用父类方法 | ||
| async for response in super()._query_stream(payloads, tools): | ||
| yield response | ||
|
|
||
| async def _query( | ||
| self, | ||
| payloads: dict, | ||
| tools: ToolSet | None, | ||
| ) -> LLMResponse: | ||
| # 判断是否启用思考模式 | ||
| ds_thinking_enabled = self._should_enable_thinking(payloads) | ||
|
|
||
| if ds_thinking_enabled: | ||
| messages = payloads.get("messages", []) | ||
|
|
||
| # DeepSeek API 要求:思考模式下每条助手消息必须有 reasoning_content | ||
| # 先确保每条助手消息都有这个字段 | ||
| for msg in messages: | ||
| if msg.get("role") == "assistant": | ||
| if "reasoning_content" not in msg: | ||
| # 工具调用消息等特殊消息初始化为空字符串 | ||
| msg["reasoning_content"] = "" | ||
|
|
||
| # 清理历史消息中的 reasoning_content | ||
| # 只有最后一条是 user 时才清空(说明是新问题) | ||
| if messages and messages[-1].get("role") == "user": | ||
| for msg in messages: | ||
| if msg.get("role") == "assistant" and "reasoning_content" in msg: | ||
| msg["reasoning_content"] = "" | ||
|
|
||
| # 添加 thinking 参数 | ||
| payloads["thinking"] = {"type": "enabled"} | ||
|
|
||
| # 调用父类方法 | ||
| return await super()._query(payloads, tools) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.