diff --git a/packages/thinking_filter/main.py b/packages/thinking_filter/main.py index eb36a107f..26c2db9b9 100644 --- a/packages/thinking_filter/main.py +++ b/packages/thinking_filter/main.py @@ -24,47 +24,48 @@ class R1Filter(Star): @filter.on_llm_response() async def resp(self, event: AstrMessageEvent, response: LLMResponse): if self.display_reasoning_text: + # 显示推理内容的处理逻辑 if ( response and response.raw_completion and isinstance(response.raw_completion, ChatCompletion) + and len(response.raw_completion.choices) > 0 + and response.raw_completion.choices[0].message ): - if ( - len(response.raw_completion.choices) - and response.raw_completion.choices[0].message + message = response.raw_completion.choices[0].message + reasoning_content = "" # 初始化 reasoning_content + + # 检查 Groq deepseek-r1-distill-llama-70b 模型的 'reasoning' 属性 + if hasattr(message, "reasoning") and message.reasoning: + reasoning_content = message.reasoning + # 检查 DeepSeek deepseek-reasoner 模型的 'reasoning_content' + elif ( + hasattr(message, "reasoning_content") and message.reasoning_content ): - message = response.raw_completion.choices[0].message - reasoning_content = "" # 初始化 reasoning_content - - # 检查 Groq deepseek-r1-distill-llama-70b模型的 'reasoning' 属性 - if hasattr(message, "reasoning") and message.reasoning: - reasoning_content = message.reasoning - # 检查 DeepSeek deepseek-reasoner模型的 'reasoning_content' - elif ( - hasattr(message, "reasoning_content") - and message.reasoning_content - ): - reasoning_content = message.reasoning_content - - if reasoning_content: - response.completion_text = ( - f"🤔思考:{reasoning_content}\n\n{message.content}" - ) - else: - response.completion_text = message.content + reasoning_content = message.reasoning_content + if reasoning_content: + response.completion_text = ( + f"🤔思考:{reasoning_content}\n\n{message.content}" + ) + else: + response.completion_text = message.content else: - # DeepSeek 官方的模型的思考存在了 reason_content 字段因此不需要过滤 + # 过滤推理标签的处理逻辑 completion_text = response.completion_text - # 适配 ollama deepseek-r1 模型 + + # 检查并移除 标签 if r"" in completion_text or r"" in completion_text: + # 移除配对的标签及其内容 completion_text = re.sub( r".*?", "", completion_text, flags=re.DOTALL ).strip() - # 可能有单标签情况 + + # 移除可能残留的单个标签 completion_text = ( completion_text.replace(r"", "") .replace(r"", "") .strip() ) + response.completion_text = completion_text