refactor(llm_models): 优化反截断机制并迁移配置

将反截断功能的启用配置从API服务商级别迁移到单个模型级别,提供了更细粒度的控制。

主要变更:
- 在`LLMRequest`中,将硬编码的结束标记`[done]`替换为可配置的`self.end_marker`。
- 反截断检查逻辑从`api_provider`配置改为读取`model_info`中的`use_anti_truncation`布尔值。
- 更新了`model_config_template.toml`,移除了全局的反截断开关,并为每个模型增加了可选的`use_anti_truncation`配置项。
This commit is contained in:
tt-P607
2025-08-30 19:08:43 +08:00
parent 6b2e2d6373
commit a7b16cae0b
2 changed files with 12 additions and 10 deletions

View File

@@ -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(
@@ -299,11 +300,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)
@@ -340,8 +342,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

View File

@@ -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"