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:
committed by
Windpicker-owo
parent
80b5c3c6f8
commit
45b6d7d06c
@@ -20,8 +20,6 @@ class APIProvider(ValidatedConfigBase):
|
||||
default=10, ge=1, 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
|
||||
def validate_base_url(cls, v):
|
||||
@@ -73,6 +71,8 @@ class ModelInfo(ValidatedConfigBase):
|
||||
force_stream_mode: bool = Field(default=False, description="是否强制使用流式输出模式")
|
||||
extra_params: dict[str, Any] = Field(default_factory=dict, description="额外参数(用于API调用时的额外配置)")
|
||||
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
|
||||
def validate_prices(cls, v):
|
||||
|
||||
@@ -289,7 +289,7 @@ class _PromptProcessor:
|
||||
"""
|
||||
|
||||
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:
|
||||
"""
|
||||
为请求准备最终的提示词。
|
||||
@@ -307,7 +307,7 @@ class _PromptProcessor:
|
||||
str: 处理后的、可以直接发送给模型的完整提示词。
|
||||
"""
|
||||
# 步骤1: 根据API提供商的配置应用内容混淆
|
||||
processed_prompt = await self._apply_content_obfuscation(prompt, api_provider)
|
||||
processed_prompt = await self._apply_content_obfuscation(prompt, model_info)
|
||||
|
||||
# 步骤2: 检查模型是否需要注入反截断指令
|
||||
if getattr(model_info, "use_anti_truncation", False):
|
||||
@@ -332,7 +332,7 @@ class _PromptProcessor:
|
||||
is_truncated = True
|
||||
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提供商的配置对文本进行内容混淆。
|
||||
|
||||
@@ -347,12 +347,12 @@ class _PromptProcessor:
|
||||
str: 经过混淆处理的文本。
|
||||
"""
|
||||
# 检查当前API提供商是否启用了内容混淆功能
|
||||
if not getattr(api_provider, "enable_content_obfuscation", False):
|
||||
if not model_info.enable_content_obfuscation or False:
|
||||
return text
|
||||
|
||||
# 获取混淆强度,默认为1
|
||||
intensity = getattr(api_provider, "obfuscation_intensity", 1)
|
||||
logger.info(f"为API提供商 '{api_provider.name}' 启用内容混淆,强度级别: {intensity}")
|
||||
intensity = model_info.obfuscation_intensity or 1
|
||||
logger.info(f"为模型 '{model_info.name}' 启用内容混淆,强度级别: {intensity}")
|
||||
|
||||
# 将抗审查指令和原始文本拼接
|
||||
processed_text = self.noise_instruction + "\n\n" + text
|
||||
@@ -679,7 +679,7 @@ class _RequestStrategy:
|
||||
if request_type == RequestType.RESPONSE and "prompt" in request_kwargs:
|
||||
prompt = request_kwargs.pop("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()
|
||||
request_kwargs["message_list"] = [message]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[inner]
|
||||
version = "1.3.7"
|
||||
version = "1.3.8"
|
||||
|
||||
# 配置文件版本号迭代规则同bot_config.toml
|
||||
|
||||
@@ -30,18 +30,6 @@ max_retry = 2
|
||||
timeout = 30
|
||||
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]] # 模型(可以配置多个)
|
||||
model_identifier = "deepseek-chat" # 模型标识符(API服务商提供的模型标识符)
|
||||
@@ -51,6 +39,8 @@ price_in = 2.0 # 输入价格(用于API调用统计,单
|
||||
price_out = 8.0 # 输出价格(用于API调用统计,单位:元/ M token)(可选,若无该字段,默认值为0)
|
||||
#force_stream_mode = true # 强制流式输出模式(若模型不支持非流式输出,请取消该注释,启用强制流式输出,若无该字段,默认值为false)
|
||||
#use_anti_truncation = true # [可选] 启用反截断功能。当模型输出不完整时,系统会自动重试。建议只为有需要的模型(如Gemini)开启。
|
||||
#enable_content_obfuscation = true # [可选] 启用内容混淆功能,用于特定场景下的内容处理(例如某些内容审查比较严的模型和稀疏注意模型)
|
||||
#obfuscation_intensity = 2 # 混淆强度(1-3级,1=低强度,2=中强度,3=高强度)
|
||||
|
||||
[[models]]
|
||||
model_identifier = "deepseek-ai/DeepSeek-V3.2-Exp"
|
||||
|
||||
Reference in New Issue
Block a user