keywords提示词优化

pull/260/head
Hosigus 2025-03-12 00:08:48 +08:00
parent 7274573736
commit 68b0f7630e
2 changed files with 39 additions and 16 deletions

View File

@ -314,7 +314,15 @@ class BotConfig:
def keywords_reaction(parent: dict): def keywords_reaction(parent: dict):
keywords_reaction_config = parent["keywords_reaction"] keywords_reaction_config = parent["keywords_reaction"]
if keywords_reaction_config.get("enable", False): if keywords_reaction_config.get("enable", False):
config.keywords_reaction_rules = keywords_reaction_config.get("rules", config.keywords_reaction_rules) config.keywords_reaction_rules = [
{
**rule, # 保留原有字段
"keywords": [kw.lower() for kw in rule.get("keywords", [])] # 关键词转小写
}
for rule in keywords_reaction_config.get("rules", config.keywords_reaction_rules)
if rule.get("enable", False)
]
def chinese_typo(parent: dict): def chinese_typo(parent: dict):
chinese_typo_config = parent["chinese_typo"] chinese_typo_config = parent["chinese_typo"]

View File

@ -123,15 +123,30 @@ class PromptBuilder:
activate_prompt = f"以上是你正在和{sender_name}私聊的内容,{memory_prompt} 现在昵称为 '{sender_name}' 的用户说的:{message_txt}。引起了你的注意,你和ta{relation_prompt},{mood_prompt},你想要{relation_prompt_2}" activate_prompt = f"以上是你正在和{sender_name}私聊的内容,{memory_prompt} 现在昵称为 '{sender_name}' 的用户说的:{message_txt}。引起了你的注意,你和ta{relation_prompt},{mood_prompt},你想要{relation_prompt_2}"
# 关键词检测与反应 # 关键词检测与反应
keywords_reaction_prompt = '' # 获取当前规则中(实际命中的关键词-反应)
triggered_keyword_prompt_arr = []
lower_msg_text = message_txt.lower()
for rule in global_config.keywords_reaction_rules: for rule in global_config.keywords_reaction_rules:
if rule.get("enable", False): matched_kws = [kw for kw in rule.get("keywords", []) if kw in lower_msg_text]
if any(keyword in message_txt.lower() for keyword in rule.get("keywords", [])): if not matched_kws:
logger.info(f"检测到以下关键词之一:{rule.get('keywords', [])},触发反应:{rule.get('reaction', '')}") continue
keywords_reaction_prompt += rule.get("reaction", "") + ''
#人格选择 reaction = rule.get("reaction", "")
personality=global_config.PROMPT_PERSONALITY logger.info(f"检测到关键词:{matched_kws},触发反应:{reaction}")
triggered_keyword_prompt_arr.append((matched_kws, reaction))
"""
群聊的发言有命中预设规则你需要按照要求的反应回复:
- 命中关键字: 'test', 'hello'; 回复要求: '回答测试成功'
- 命中关键字: '人机'; 回复要求: '否定这一事实,攻击或反问他。注意,只有认为你是机器人才回复,否则不要否认'
"""
keywords_reaction_prompt = ''
if len(triggered_keyword_prompt_arr) > 0:
keywords_reaction_prompt = '群聊的发言有命中,你需要按照预设的反应回复:\n' \
+ "\n".join(f'- 命中关键字: {", ".join(map(repr, matched_kws))}; 回复要求: {reaction!r}' for matched_kws, reaction in triggered_keyword_prompt_arr)
# 人格选择
personality = global_config.PROMPT_PERSONALITY
probability_1 = global_config.PERSONALITY_1 probability_1 = global_config.PERSONALITY_1
probability_2 = global_config.PERSONALITY_2 probability_2 = global_config.PERSONALITY_2
probability_3 = global_config.PERSONALITY_3 probability_3 = global_config.PERSONALITY_3