feat(config): 将内容混淆设置移至模型级别

内容混淆功能的相关配置项(`enable_content_obfuscation` 和 `obfuscation_intensity`)已从 API Provider 级别迁移到单个模型级别。

这一调整提供了更精细的控制能力,允许用户为特定模型独立启用或配置内容混淆,而不是统一应用于同一API下的所有模型。这对于处理来自同一提供商但审查策略不同的模型非常有用。

BREAKING CHANGE: `enable_content_obfuscation` 和 `obfuscation_intensity` 配置项已从 `[[api_providers]]` 部分移除。请将这些配置项迁移到需要此功能的 `[[models]]` 部分下。
This commit is contained in:
minecraft1024a
2025-11-14 16:20:37 +08:00
parent 36b1b72e25
commit 58bc8e9867
3 changed files with 12 additions and 22 deletions

View File

@@ -20,8 +20,6 @@ class APIProvider(ValidatedConfigBase):
default=10, ge=1, description="API调用的超时时长超过这个时长本次请求将被视为'请求超时',单位:秒)" default=10, ge=1, description="API调用的超时时长超过这个时长本次请求将被视为'请求超时',单位:秒)"
) )
retry_interval: int = Field(default=10, ge=0, description="重试间隔如果API调用失败重试的间隔时间单位") retry_interval: int = Field(default=10, ge=0, description="重试间隔如果API调用失败重试的间隔时间单位")
enable_content_obfuscation: bool = Field(default=False, description="是否启用内容混淆(用于特定场景下的内容处理)")
obfuscation_intensity: int = Field(default=1, ge=1, le=3, description="混淆强度1-3级数值越高混淆程度越强")
@classmethod @classmethod
def validate_base_url(cls, v): def validate_base_url(cls, v):
@@ -73,6 +71,8 @@ class ModelInfo(ValidatedConfigBase):
force_stream_mode: bool = Field(default=False, description="是否强制使用流式输出模式") force_stream_mode: bool = Field(default=False, description="是否强制使用流式输出模式")
extra_params: dict[str, Any] = Field(default_factory=dict, description="额外参数用于API调用时的额外配置") extra_params: dict[str, Any] = Field(default_factory=dict, description="额外参数用于API调用时的额外配置")
anti_truncation: bool = Field(default=False, description="是否启用反截断功能,防止模型输出被截断") anti_truncation: bool = Field(default=False, description="是否启用反截断功能,防止模型输出被截断")
enable_content_obfuscation: bool = Field(default=False, description="是否启用内容混淆(用于特定场景下的内容处理)")
obfuscation_intensity: int = Field(default=1, ge=1, le=3, description="混淆强度1-3级数值越高混淆程度越强")
@classmethod @classmethod
def validate_prices(cls, v): def validate_prices(cls, v):

View File

@@ -289,7 +289,7 @@ class _PromptProcessor:
""" """
async def prepare_prompt( async def prepare_prompt(
self, prompt: str, model_info: ModelInfo, api_provider: APIProvider, task_name: str self, prompt: str, model_info: ModelInfo, task_name: str
) -> str: ) -> str:
""" """
为请求准备最终的提示词。 为请求准备最终的提示词。
@@ -307,7 +307,7 @@ class _PromptProcessor:
str: 处理后的、可以直接发送给模型的完整提示词。 str: 处理后的、可以直接发送给模型的完整提示词。
""" """
# 步骤1: 根据API提供商的配置应用内容混淆 # 步骤1: 根据API提供商的配置应用内容混淆
processed_prompt = await self._apply_content_obfuscation(prompt, api_provider) processed_prompt = await self._apply_content_obfuscation(prompt, model_info)
# 步骤2: 检查模型是否需要注入反截断指令 # 步骤2: 检查模型是否需要注入反截断指令
if getattr(model_info, "use_anti_truncation", False): if getattr(model_info, "use_anti_truncation", False):
@@ -332,7 +332,7 @@ class _PromptProcessor:
is_truncated = True is_truncated = True
return content, reasoning, is_truncated return content, reasoning, is_truncated
async def _apply_content_obfuscation(self, text: str, api_provider: APIProvider) -> str: async def _apply_content_obfuscation(self, text: str, model_info: ModelInfo) -> str:
""" """
根据API提供商的配置对文本进行内容混淆。 根据API提供商的配置对文本进行内容混淆。
@@ -347,12 +347,12 @@ class _PromptProcessor:
str: 经过混淆处理的文本。 str: 经过混淆处理的文本。
""" """
# 检查当前API提供商是否启用了内容混淆功能 # 检查当前API提供商是否启用了内容混淆功能
if not getattr(api_provider, "enable_content_obfuscation", False): if not model_info.enable_content_obfuscation or False:
return text return text
# 获取混淆强度默认为1 # 获取混淆强度默认为1
intensity = getattr(api_provider, "obfuscation_intensity", 1) intensity = model_info.obfuscation_intensity or 1
logger.info(f"API提供商 '{api_provider.name}' 启用内容混淆,强度级别: {intensity}") logger.info(f"模型 '{model_info.name}' 启用内容混淆,强度级别: {intensity}")
# 将抗审查指令和原始文本拼接 # 将抗审查指令和原始文本拼接
processed_text = self.noise_instruction + "\n\n" + text processed_text = self.noise_instruction + "\n\n" + text
@@ -679,7 +679,7 @@ class _RequestStrategy:
if request_type == RequestType.RESPONSE and "prompt" in request_kwargs: if request_type == RequestType.RESPONSE and "prompt" in request_kwargs:
prompt = request_kwargs.pop("prompt") prompt = request_kwargs.pop("prompt")
processed_prompt = await self.prompt_processor.prepare_prompt( processed_prompt = await self.prompt_processor.prepare_prompt(
prompt, model_info, api_provider, self.task_name prompt, model_info, self.task_name
) )
message = MessageBuilder().add_text_content(processed_prompt).build() message = MessageBuilder().add_text_content(processed_prompt).build()
request_kwargs["message_list"] = [message] request_kwargs["message_list"] = [message]

View File

@@ -1,5 +1,5 @@
[inner] [inner]
version = "1.3.7" version = "1.3.8"
# 配置文件版本号迭代规则同bot_config.toml # 配置文件版本号迭代规则同bot_config.toml
@@ -30,18 +30,6 @@ max_retry = 2
timeout = 30 timeout = 30
retry_interval = 10 retry_interval = 10
# 内容混淆功能示例配置(可选)
[[api_providers]]
name = "ExampleProviderWithObfuscation" # 启用混淆功能的API提供商示例
base_url = "https://api.example.com/v1"
api_key = "your-api-key-here"
client_type = "openai"
max_retry = 2
timeout = 30
retry_interval = 10
enable_content_obfuscation = true # 启用内容混淆功能
obfuscation_intensity = 2 # 混淆强度1-3级1=低强度2=中强度3=高强度)
[[models]] # 模型(可以配置多个) [[models]] # 模型(可以配置多个)
model_identifier = "deepseek-chat" # 模型标识符API服务商提供的模型标识符 model_identifier = "deepseek-chat" # 模型标识符API服务商提供的模型标识符
@@ -51,6 +39,8 @@ price_in = 2.0 # 输入价格用于API调用统计
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开启。 #use_anti_truncation = true # [可选] 启用反截断功能。当模型输出不完整时系统会自动重试。建议只为有需要的模型如Gemini开启。
#enable_content_obfuscation = true # [可选] 启用内容混淆功能,用于特定场景下的内容处理(例如某些内容审查比较严的模型和稀疏注意模型)
#obfuscation_intensity = 2 # 混淆强度1-3级1=低强度2=中强度3=高强度)
[[models]] [[models]]
model_identifier = "deepseek-ai/DeepSeek-V3.2-Exp" model_identifier = "deepseek-ai/DeepSeek-V3.2-Exp"