呀,柒柒,这次我们对表情包系统进行了一次超级棒的大升级哦!它现在变得更聪明、更懂我们的心意啦!就像我一样,总能找到最完美的表情来点亮对话!♪~

这是我为你准备的提交信息,你看看喜不喜欢~

feat(chat): 使用 LLM 优化表情包选择与分析

本次提交对表情包系统进行了核心重构,从原有的基于关键词相似度匹配的简单算法,升级为由大型语言模型(LLM)驱动的智能决策流程。这使得表情包的选择和分析更加精准、智能和人性化。

主要变更包括:

1.  **引入 LLM 进行表情包选择**
    -   重写了 `get_emoji_for_text` 方法,废弃了原有的编辑距离算法。
    -   新流程会根据配置随机抽取一部分表情包作为候选,并构建一个精细的 Prompt,引导 LLM 根据输入的“情感描述”选择最匹配的表情包。这让选择不再局限于字面匹配,而是能理解更深层次的语境和情绪。

2.  **优化表情包描述与分析流程**
    -   大幅改进了 `build_emoji_description` 中的 VLM 和 LLM 提示词,使其能生成更懂网络文化、更详细的表情包描述,并提炼出更精准的情感关键词。
    -   为动态图(GIF)和静态图设计了不同的分析策略,以获得更高质量的描述结果。

3.  **增强 Planner 动作连贯性**
    -   更新了 `planner_prompts`,明确要求当 `reply` 和 `emoji` 动作同时触发时,`emoji` 的选择必须基于 `reply` 动作生成的最终文本内容。这确保了文字和表情包的表达高度一致。

4.  **逻辑与配置微调**
    -   在 `utils_image` 中,现在只有当“偷表情包”功能开启时,才会保存接收到的表情包,避免了不必要的文件存储。
    -   将表情包检查间隔 `check_interval` 的类型从 `int` 改为 `float`,允许更灵活的配置。
This commit is contained in:
tt-P607
2025-09-14 16:44:35 +08:00
parent 6793a46f41
commit 5bf798ed0a
4 changed files with 182 additions and 177 deletions

View File

@@ -242,48 +242,50 @@ class ImageManager:
logger.warning(f"虽然生成了描述,但是找到缓存表情包描述: {cached_description}")
return f"[表情包:{cached_description}]"
# 保存表情包文件和元数据(用于可能的后续分析)
logger.debug(f"保存表情包: {image_hash}")
current_timestamp = time.time()
filename = f"{int(current_timestamp)}_{image_hash[:8]}.{image_format}"
emoji_dir = os.path.join(self.IMAGE_DIR, "emoji")
os.makedirs(emoji_dir, exist_ok=True)
file_path = os.path.join(emoji_dir, filename)
# 只有在开启“偷表情包”功能时,才将接收到的表情包保存到待注册目录
if global_config.emoji.steal_emoji:
logger.debug(f"偷取表情包功能已开启,保存表情包: {image_hash}")
current_timestamp = time.time()
filename = f"{int(current_timestamp)}_{image_hash[:8]}.{image_format}"
emoji_dir = os.path.join(self.IMAGE_DIR, "emoji")
os.makedirs(emoji_dir, exist_ok=True)
file_path = os.path.join(emoji_dir, filename)
try:
# 保存文件
with open(file_path, "wb") as f:
f.write(image_bytes)
# 保存到数据库 (Images表) - 包含详细描述用于可能的注册流程
try:
from src.common.database.sqlalchemy_models import get_db_session
# 保存文件
with open(file_path, "wb") as f:
f.write(image_bytes)
with get_db_session() as session:
existing_img = session.execute(
select(Images).where(and_(Images.emoji_hash == image_hash, Images.type == "emoji"))
).scalar()
# 保存到数据库 (Images表) - 包含详细描述用于可能的注册流程
try:
from src.common.database.sqlalchemy_models import get_db_session
with get_db_session() as session:
existing_img = session.execute(
select(Images).where(and_(Images.emoji_hash == image_hash, Images.type == "emoji"))
).scalar()
if existing_img:
existing_img.path = file_path
existing_img.description = detailed_description # 保存详细描述
existing_img.timestamp = current_timestamp
else:
new_img = Images(
emoji_hash=image_hash,
path=file_path,
type="emoji",
description=detailed_description, # 保存详细描述
timestamp=current_timestamp,
)
session.add(new_img)
session.commit()
except Exception as e:
logger.error(f"保存到Images表失败: {str(e)}")
if existing_img:
existing_img.path = file_path
existing_img.description = detailed_description # 保存详细描述
existing_img.timestamp = current_timestamp
else:
new_img = Images(
emoji_hash=image_hash,
path=file_path,
type="emoji",
description=detailed_description, # 保存详细描述
timestamp=current_timestamp,
)
session.add(new_img)
session.commit()
# 会在上下文管理器中自动调用
except Exception as e:
logger.error(f"保存到Images表失败: {str(e)}")
except Exception as e:
logger.error(f"保存表情包文件或元数据失败: {str(e)}")
logger.error(f"保存表情包文件或元数据失败: {str(e)}")
else:
logger.debug("偷取表情包功能已关闭,跳过保存。")
# 保存最终的情感标签到缓存 (ImageDescriptions表)
self._save_description_to_db(image_hash, final_emotion, "emoji")