From 424c0fa0933cddb0937f01fea28d1d1277a598ba Mon Sep 17 00:00:00 2001 From: tt-P607 <68868379+tt-P607@users.noreply.github.com> Date: Sat, 30 Aug 2025 19:08:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor(llm=5Fmodels):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=8F=8D=E6=88=AA=E6=96=AD=E6=9C=BA=E5=88=B6=E5=B9=B6=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将反截断功能的启用配置从API服务商级别迁移到单个模型级别,提供了更细粒度的控制。 主要变更: - 在`LLMRequest`中,将硬编码的结束标记`[done]`替换为可配置的`self.end_marker`。 - 反截断检查逻辑从`api_provider`配置改为读取`model_info`中的`use_anti_truncation`布尔值。 - 更新了`model_config_template.toml`,移除了全局的反截断开关,并为每个模型增加了可选的`use_anti_truncation`配置项。 --- src/llm_models/utils_model.py | 18 ++++++++++-------- template/model_config_template.toml | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/llm_models/utils_model.py b/src/llm_models/utils_model.py index d40c42ec0..c3b75d6ef 100644 --- a/src/llm_models/utils_model.py +++ b/src/llm_models/utils_model.py @@ -139,14 +139,15 @@ class LLMRequest: """ # 反截断指令 - self.anti_truncation_instruction = """ + self.end_marker = "###MAI_RESPONSE_END###" + self.anti_truncation_instruction = f""" **【输出完成信令】** -这是一个非常重要的指令,请务必遵守。在你的回复内容完全结束后,请务必在最后另起一行,只写 `[done]` 作为结束标志。 +这是一个非常重要的指令,请务必遵守。在你的回复内容完全结束后,请务必在最后另起一行,只写 `{self.end_marker}` 作为结束标志。 例如: <你的回复内容> -[done] +{self.end_marker} -这有助于我判断你的输出是否被截断。请不要在 `[done]` 前后添加任何其他文字或标点。 +这有助于我判断你的输出是否被截断。请不要在 `{self.end_marker}` 前后添加任何其他文字或标点。 """ async def generate_response_for_image( @@ -300,11 +301,12 @@ class LLMRequest: try: # 检查是否启用反截断 - use_anti_truncation = getattr(api_provider, "anti_truncation", False) + # 检查是否为该模型启用反截断 + use_anti_truncation = getattr(model_info, "use_anti_truncation", False) processed_prompt = prompt if use_anti_truncation: processed_prompt += self.anti_truncation_instruction - logger.info(f"'{model_name}' for task '{self.task_name}' 已启用反截断功能") + logger.info(f"模型 '{model_name}' (任务: '{self.task_name}') 已启用反截断功能。") processed_prompt = self._apply_content_obfuscation(processed_prompt, api_provider) @@ -341,8 +343,8 @@ class LLMRequest: is_empty_reply = not tool_calls and (not content or content.strip() == "") is_truncated = False if use_anti_truncation: - if content.endswith("[done]"): - content = content[:-6].strip() + if content.endswith(self.end_marker): + content = content[: -len(self.end_marker)].strip() else: is_truncated = True diff --git a/template/model_config_template.toml b/template/model_config_template.toml index 5a4e5b677..eadebb00c 100644 --- a/template/model_config_template.toml +++ b/template/model_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "1.2.8" +version = "1.2.9" # 配置文件版本号迭代规则同bot_config.toml @@ -41,7 +41,6 @@ timeout = 30 retry_interval = 10 enable_content_obfuscation = true # 启用内容混淆功能 obfuscation_intensity = 2 # 混淆强度(1-3级,1=低强度,2=中强度,3=高强度) -anti_truncation_instruction = true # 防阶段移到了模型配置里 [[models]] # 模型(可以配置多个) @@ -51,6 +50,7 @@ api_provider = "DeepSeek" # API服务商名称(对应在api_providers price_in = 2.0 # 输入价格(用于API调用统计,单位:元/ M token)(可选,若无该字段,默认值为0) price_out = 8.0 # 输出价格(用于API调用统计,单位:元/ M token)(可选,若无该字段,默认值为0) #force_stream_mode = true # 强制流式输出模式(若模型不支持非流式输出,请取消该注释,启用强制流式输出,若无该字段,默认值为false) +#use_anti_truncation = true # [可选] 启用反截断功能。当模型输出不完整时,系统会自动重试。建议只为有需要的模型(如Gemini)开启。 [[models]] model_identifier = "Pro/deepseek-ai/DeepSeek-V3"