Add LLM anti-prompt injection system

Introduces a comprehensive anti-prompt injection system for LLMs, including rule-based and LLM-based detection, user ban/whitelist management, message shielding, and statistics tracking. Adds new modules under src/chat/antipromptinjector, integrates anti-injection checks into the message receive flow, updates configuration and database models, and provides test scripts. Also updates templates and logger aliases to support the new system.
This commit is contained in:
雅诺狐
2025-08-18 17:27:59 +08:00
committed by Windpicker-owo
parent b55cd8193e
commit 16da725272
22 changed files with 2494 additions and 30 deletions

View File

@@ -165,6 +165,41 @@ ban_msgs_regex = [
#"\\d{4}-\\d{2}-\\d{2}", # 匹配日期
]
[anti_prompt_injection] # LLM反注入系统配置
enabled = true # 是否启用反注入系统
enabled_rules = false # 是否启用规则检测
enabled_LLM = true # 是否启用LLM检测
process_mode = "lenient" # 处理模式strict(严格模式,直接丢弃), lenient(宽松模式,消息加盾)
# 白名单配置
# 格式:[[platform, user_id], ...]
# 示例:[["qq", "123456"], ["telegram", "user789"]]
whitelist = [] # 用户白名单,这些用户的消息将跳过检测
# LLM检测配置
llm_detection_enabled = true # 是否启用LLM二次分析
llm_detection_threshold = 0.7 # LLM判定危险的置信度阈值(0-1)
# 性能配置
cache_enabled = true # 是否启用检测结果缓存
cache_ttl = 3600 # 缓存有效期(秒)
max_message_length = 150 # 最大检测消息长度,超过将直接判定为危险
# 统计配置
stats_enabled = true # 是否启用统计功能
# 自动封禁配置
auto_ban_enabled = false # 是否启用自动封禁功能
auto_ban_violation_threshold = 3 # 触发封禁的违规次数阈值
auto_ban_duration_hours = 2 # 封禁持续时间(小时)
# 消息加盾配置(宽松模式下使用)
shield_prefix = "🛡️ " # 加盾消息前缀
shield_suffix = " 🛡️" # 加盾消息后缀
[normal_chat] #普通聊天
willing_mode = "classical" # 回复意愿模式 —— 经典模式classicalmxp模式mxp自定义模式custom需要你自己实现
[tool]
enable_tool = true # 是否在普通聊天中启用工具