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:
@@ -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(
|
async def generate_response_for_image(
|
||||||
@@ -300,11 +301,12 @@ class LLMRequest:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# 检查是否启用反截断
|
# 检查是否启用反截断
|
||||||
use_anti_truncation = getattr(api_provider, "anti_truncation", False)
|
# 检查是否为该模型启用反截断
|
||||||
|
use_anti_truncation = getattr(model_info, "use_anti_truncation", False)
|
||||||
processed_prompt = prompt
|
processed_prompt = prompt
|
||||||
if use_anti_truncation:
|
if use_anti_truncation:
|
||||||
processed_prompt += self.anti_truncation_instruction
|
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)
|
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_empty_reply = not tool_calls and (not content or content.strip() == "")
|
||||||
is_truncated = False
|
is_truncated = False
|
||||||
if use_anti_truncation:
|
if use_anti_truncation:
|
||||||
if content.endswith("[done]"):
|
if content.endswith(self.end_marker):
|
||||||
content = content[:-6].strip()
|
content = content[: -len(self.end_marker)].strip()
|
||||||
else:
|
else:
|
||||||
is_truncated = True
|
is_truncated = True
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[inner]
|
[inner]
|
||||||
version = "1.2.8"
|
version = "1.2.9"
|
||||||
|
|
||||||
# 配置文件版本号迭代规则同bot_config.toml
|
# 配置文件版本号迭代规则同bot_config.toml
|
||||||
|
|
||||||
@@ -41,7 +41,6 @@ timeout = 30
|
|||||||
retry_interval = 10
|
retry_interval = 10
|
||||||
enable_content_obfuscation = true # 启用内容混淆功能
|
enable_content_obfuscation = true # 启用内容混淆功能
|
||||||
obfuscation_intensity = 2 # 混淆强度(1-3级,1=低强度,2=中强度,3=高强度)
|
obfuscation_intensity = 2 # 混淆强度(1-3级,1=低强度,2=中强度,3=高强度)
|
||||||
anti_truncation_instruction = true # 防阶段移到了模型配置里
|
|
||||||
|
|
||||||
|
|
||||||
[[models]] # 模型(可以配置多个)
|
[[models]] # 模型(可以配置多个)
|
||||||
@@ -51,6 +50,7 @@ api_provider = "DeepSeek" # API服务商名称(对应在api_providers
|
|||||||
price_in = 2.0 # 输入价格(用于API调用统计,单位:元/ M token)(可选,若无该字段,默认值为0)
|
price_in = 2.0 # 输入价格(用于API调用统计,单位:元/ M token)(可选,若无该字段,默认值为0)
|
||||||
price_out = 8.0 # 输出价格(用于API调用统计,单位:元/ M token)(可选,若无该字段,默认值为0)
|
price_out = 8.0 # 输出价格(用于API调用统计,单位:元/ M token)(可选,若无该字段,默认值为0)
|
||||||
#force_stream_mode = true # 强制流式输出模式(若模型不支持非流式输出,请取消该注释,启用强制流式输出,若无该字段,默认值为false)
|
#force_stream_mode = true # 强制流式输出模式(若模型不支持非流式输出,请取消该注释,启用强制流式输出,若无该字段,默认值为false)
|
||||||
|
#use_anti_truncation = true # [可选] 启用反截断功能。当模型输出不完整时,系统会自动重试。建议只为有需要的模型(如Gemini)开启。
|
||||||
|
|
||||||
[[models]]
|
[[models]]
|
||||||
model_identifier = "Pro/deepseek-ai/DeepSeek-V3"
|
model_identifier = "Pro/deepseek-ai/DeepSeek-V3"
|
||||||
|
|||||||
Reference in New Issue
Block a user