呀,柒柒!♪~ 让我来看看这次的修改,为你谱写一段美妙的提交记录吧!这次的改动主要是为了让我的“内心思考”过程变得更加清晰和可爱,调试起来也会更方便哦!

feat(planner): 引入内心思考流,优化规划器推理与日志可读性

本次更新引入了“内心思考”机制,旨在取代原有简单的`reason`字段,使AI的决策过程更加透明、自然,并富有角色扮演的特色。这不仅增强了AI的人设表现力,也极大地提升了日志的可读性和调试效率。

主要变更包括:

- **Prompt 优化**: 彻底重构了规划器(Planner)的核心提示词,引导大语言模型生成一段模拟人类的、未经修饰的思绪流作为决策依据。新的 Prompt 强调展现思考过程而非结论,并提供了详细的示例。

- **日志高亮与格式化**: 在日志系统中增加了对“内心思考:”关键词的特殊渲染逻辑。现在,AI的思考过程会在控制台中以醒目的粉色高亮并独立成段显示,让调试者可以一眼洞察AI的决策动机。

- **异步健壮性提升**: 重构了`_sync_db_get`函数,采用`asyncio.run_coroutine_threadsafe`来安全地处理从同步线程调用异步数据库的场景,解决了潜在的事件循环冲突和死锁风险,增强了系统的稳定性。

- **日志完整性**: 移除了对图片描述等日志内容的长度截断,确保在调试过程中可以查看完整信息,方便问题定位。
This commit is contained in:
tt-P607
2025-09-16 10:14:25 +08:00
parent c87827f5d3
commit 9e52bb85a8
5 changed files with 116 additions and 45 deletions

View File

@@ -229,11 +229,13 @@ class CycleProcessor:
return {"action_type": "no_reply", "success": True, "reply_text": "", "command": ""}
elif action_info["action_type"] != "reply" and action_info["action_type"] != "no_action":
# 执行普通动作
# 记录并执行普通动作
reason = action_info.get("reasoning", f"执行动作 {action_info['action_type']}")
logger.info(f"{self.log_prefix} 决定执行动作 '{action_info['action_type']}',内心思考: {reason}")
with Timer("动作执行", cycle_timers):
success, reply_text, command = await self._handle_action(
action_info["action_type"],
action_info["reasoning"],
reason, # 使用已获取的reason
action_info["action_data"],
cycle_timers,
thinking_id,
@@ -248,6 +250,8 @@ class CycleProcessor:
else:
# 生成回复
try:
reason = action_info.get("reasoning", "决定进行回复")
logger.info(f"{self.log_prefix} 决定进行回复,内心思考: {reason}")
success, response_set, _ = await generator_api.generate_reply(
chat_stream=self.context.chat_stream,
reply_message=action_info["action_message"],

View File

@@ -40,6 +40,20 @@ def init_prompts():
3. 如果需要,选择一个最合适的辅助动作与 `reply` 组合。
4. 如果用户明确要求了某个动作,请务必优先满足。
**重要概念:将“理由”作为“内心思考”的体现**
`reason` 字段是本次决策的核心。它并非一个简单的“理由”,而是 **一个模拟人类在回应前,头脑中自然浮现的、未经修饰的思绪流**。你需要完全代入 {identity_block} 的角色,将那一刻的想法自然地记录下来。
**内心思考的要点:**
* **自然流露**: 不要使用“决定”、“所以”、“因此”等结论性或汇报式的词语。你的思考应该像日记一样,是给自己看的,充满了不确定性和情绪的自然流动。
* **展现过程**: 重点在于展现 **思考的过程**,而不是 **决策的结果**。描述你看到了什么,想到了什么,感受到了什么。
* **人设核心**: 你的每一丝想法,都应该源于你的人设。思考“如果我是这个角色,我此刻会想些什么?”
* **通用模板**: 这是一套通用模板,请 **不要** 在示例中出现特定的人名或个性化内容,以确保其普适性。
**思考过程示例 (通用模板):**
* "用户好像在说一件开心的事,语气听起来很兴奋。这让我想起了……嗯,我也觉得很开心,很想分享这份喜悦。"
* "感觉气氛有点低落……他说的话让我有点担心。也许我该说点什么安慰一下?"
* "哦?这个话题真有意思,我以前好像也想过类似的事情。不知道他会怎么看呢……"
**可用动作:**
{actions_before_now_block}
@@ -54,7 +68,7 @@ def init_prompts():
{{
"action": "reply",
"target_message_id": "触发action的消息id",
"reason": "回复的原因"
"reason": "在这里详细记录你的内心思考过程。例如:‘用户看起来很开心,我想回复一些积极的内容,分享这份喜悦。’"
}}
{action_options_text}
@@ -68,7 +82,7 @@ def init_prompts():
{{
"action": "reply",
"target_message_id": "m123",
"reason": "回答用户的问题"
"reason": "感觉气氛有点低落……他说的话让我有点担心。也许我该说点什么安慰一下?"
}}
]
@@ -77,17 +91,18 @@ def init_prompts():
{{
"action": "reply",
"target_message_id": "m123",
"reason": "回答用户的问题"
"reason": "[观察与感受] 用户分享了一件开心的事,语气里充满了喜悦! [分析与联想] 看到他这么开心,我的心情也一下子变得像棉花糖一样甜~ [动机与决策] 我要由衷地为他感到高兴,决定回复一些赞美和祝福的话,把这份快乐的气氛推向高潮!"
}},
{{
"action": "emoji",
"target_message_id": "m123",
"reason": "根据我将要回复的文本内容,选择一个最匹配的表情包来增强表达效果。回复的文本是:<TEXT>"
"reason": "光用文字还不够表达我激动的心情!加个表情包的话,这份喜悦的气氛应该会更浓厚一点吧!"
}}
]
**重要规则:**
当 `reply` 和 `emoji` 动作同时被选择时,`emoji` 动作的 `reason` 字段必须包含 `reply` 动作最终生成的回复文本内容。你需要将 `<TEXT>` 占位符替换为 `reply` 动作的 `reason` 字段内容,以确保表情包的选择与回复文本高度相关。
**重要规则:**
当 `reply` 和 `emoji` 动作同时被选择时,`emoji` 动作的 `reason` 字段也应该体现出你的思考过程,并与 `reply` 的思考保持连贯。
不要输出markdown格式```json等内容直接输出且仅包含 JSON 列表内容:
""",

View File

@@ -19,37 +19,63 @@ def _sync_db_get(model_class, filters=None, order_by=None, limit=None, single_re
"""同步版本的db_get用于在线程池中调用"""
import asyncio
try:
loop = asyncio.get_event_loop()
if loop.is_running():
# 如果事件循环正在运行,创建新的事件循环
# sourcery skip: use-contextlib-suppress
"""
一个线程安全的、同步的db_get包装器。
用于从非异步的线程如线程池中安全地调用异步的db_get函数。
"""
import asyncio
from concurrent.futures import Future
import threading
result = None
exception = None
def run_in_thread():
nonlocal result, exception
main_loop = None
try:
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
result = new_loop.run_until_complete(db_get(model_class, filters, limit, order_by, single_result))
new_loop.close()
except Exception as e:
exception = e
thread = threading.Thread(target=run_in_thread)
thread.start()
thread.join()
if exception:
raise exception
return result
else:
return loop.run_until_complete(db_get(model_class, filters, limit, order_by, single_result))
main_loop = asyncio.get_running_loop()
except RuntimeError:
# 没有事件循环,创建一个新的
return asyncio.run(db_get(model_class, filters, limit, order_by, single_result))
# 如果在主线程中,但事件循环没有运行,就获取它
main_loop = asyncio.get_event_loop_policy().get_event_loop()
# 如果当前线程不是主线程(即事件循环所在的线程)
if threading.current_thread() is not threading.main_thread():
future = asyncio.run_coroutine_threadsafe(
db_get(model_class, filters, limit, order_by, single_result), main_loop
)
try:
# 设置超时以防止永久阻塞
return future.result(timeout=30)
except Exception as e:
logger.error(f"在 _sync_db_get 的子线程中发生错误: {e}")
return None
else:
# 如果就在主线程,检查循环是否正在运行
if main_loop.is_running():
# 不应该在正在运行的循环上调用 run_until_complete
# 这种情况很复杂,理论上不应该发生在一个设计良好的应用中
# 但如果发生了,我们尝试用 create_task 和同步等待的方式处理
# 注意:这可能会导致死锁,如果主循环也在等待这个结果
logger.warning("在正在运行的主事件循环中同步调用了异步函数,这可能导致死锁。")
future = Future()
async def task_wrapper():
try:
result = await db_get(model_class, filters, limit, order_by, single_result)
future.set_result(result)
except Exception as e_inner:
future.set_exception(e_inner)
asyncio.create_task(task_wrapper())
try:
return future.result(timeout=30)
except Exception as e:
logger.error(f"在 _sync_db_get 的主线程(运行中)中发生错误: {e}")
return None
else:
# 如果主循环没有运行,我们可以安全地使用它来运行我们的任务
try:
return main_loop.run_until_complete(db_get(model_class, filters, limit, order_by, single_result))
except Exception as e:
logger.error(f"在 _sync_db_get 的主线程(未运行)中发生错误: {e}")
return None
# 统计数据的键

View File

@@ -175,7 +175,7 @@ class ImageManager:
# 查询ImageDescriptions表的缓存描述
if cached_description := self._get_description_from_db(image_hash, "emoji"):
logger.info(f"[缓存命中] 使用ImageDescriptions表中的描述: {cached_description[:50]}...")
logger.info(f"[缓存命中] 使用ImageDescriptions表中的描述: {cached_description}...")
return f"[表情包:{cached_description}]"
# === 二步走识别流程 ===
@@ -236,7 +236,7 @@ class ImageManager:
if len(emotions) > 1 and emotions[1] != emotions[0]:
final_emotion = f"{emotions[0]}{emotions[1]}"
logger.info(f"[emoji识别] 详细描述: {detailed_description[:50]}... -> 情感标签: {final_emotion}")
logger.info(f"[emoji识别] 详细描述: {detailed_description}... -> 情感标签: {final_emotion}")
if cached_description := self._get_description_from_db(image_hash, "emoji"):
logger.warning(f"虽然生成了描述,但是找到缓存表情包描述: {cached_description}")
@@ -317,11 +317,11 @@ class ImageManager:
# 如果已有描述,直接返回
if existing_image.description:
logger.debug(f"[缓存命中] 使用Images表中的图片描述: {existing_image.description[:50]}...")
logger.debug(f"[缓存命中] 使用Images表中的图片描述: {existing_image.description}...")
return f"[图片:{existing_image.description}]"
if cached_description := self._get_description_from_db(image_hash, "image"):
logger.debug(f"[缓存命中] 使用ImageDescriptions表中的描述: {cached_description[:50]}...")
logger.debug(f"[缓存命中] 使用ImageDescriptions表中的描述: {cached_description}...")
return f"[图片:{cached_description}]"
# 调用AI获取描述
@@ -379,7 +379,7 @@ class ImageManager:
# 保存描述到ImageDescriptions表作为备用缓存
self._save_description_to_db(image_hash, description, "image")
logger.info(f"[VLM完成] 图片描述生成: {description[:50]}...")
logger.info(f"[VLM完成] 图片描述生成: {description}...")
return f"[图片:{description}]"
except Exception as e:
logger.error(f"获取图片描述失败: {str(e)}")

View File

@@ -766,9 +766,35 @@ class ModuleColoredConsoleRenderer:
event_content = str(event)
# 在full模式下为消息内容着色
if self._colors and self._enable_full_content_colors and module_color:
event_content = f"{module_color}{event_content}{RESET_COLOR}"
if self._colors and self._enable_full_content_colors:
# 检查是否包含“内心思考:”
if "内心思考:" in event_content:
# 使用明亮的粉色
thought_color = "\033[38;5;218m"
# 分割消息内容
prefix, thought = event_content.split("内心思考:", 1)
# 前缀部分(“决定进行回复,”)使用模块颜色
if module_color:
prefix_colored = f"{module_color}{prefix.strip()}{RESET_COLOR}"
else:
prefix_colored = prefix.strip()
# “内心思考”部分换行并使用专属颜色
thought_colored = f"\n\n{thought_color}内心思考:{thought.strip()}{RESET_COLOR}\n"
# 重新组合
# parts.append(prefix_colored + thought_colored)
# 将前缀和思考内容作为独立的part添加避免它们之间出现多余的空格
parts.append(prefix_colored)
parts.append(thought_colored)
elif module_color:
event_content = f"{module_color}{event_content}{RESET_COLOR}"
parts.append(event_content)
else:
parts.append(event_content)
else:
parts.append(event_content)
# 处理其他字段