From 7f284ec4c43b712d360213c7308c975e8385af67 Mon Sep 17 00:00:00 2001 From: StarHeartHunt Date: Sat, 15 Feb 2025 14:46:14 +0800 Subject: [PATCH] :bug: filter out think in normal content --- nonebot_plugin_llmchat/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/nonebot_plugin_llmchat/__init__.py b/nonebot_plugin_llmchat/__init__.py index 0be2745..59e26cb 100644 --- a/nonebot_plugin_llmchat/__init__.py +++ b/nonebot_plugin_llmchat/__init__.py @@ -13,6 +13,7 @@ import asyncio from openai import AsyncOpenAI from .config import Config, PresetConfig import time +import re import json import os import random @@ -37,6 +38,10 @@ __plugin_meta__ = PluginMetadata( pluginConfig = get_plugin_config(Config).llmchat driver = get_driver() +def filter_think(content:str) -> str: + filtered_content = re.sub(r".*?", "", content, flags=re.DOTALL) + return filtered_content.strip() + # 初始化群组状态 class GroupState: def __init__(self): @@ -192,7 +197,10 @@ f''' ) logger.debug(f"收到API响应 使用token数:{response.usage.total_tokens}") - reply = response.choices[0].message.content + if not state.output_reasoning_content: + reply = filter_think(response.choices[0].message.content) + else: + reply = response.choices[0].message.content # 请求成功后再保存历史记录,保证user和assistant穿插,防止R1模型报错 state.history.append({"role": "user", "content": content})