feat: 添加开关,控制视觉结果是否合并到LLM记忆

This commit is contained in:
ganbin 2025-07-15 11:31:41 +08:00
parent 2e719e2ccc
commit e39d32f9af
5 changed files with 67 additions and 19 deletions

View file

@ -0,0 +1,4 @@
INSERT INTO sys_params (`id`, `param_code`, `param_value`, `value_type`, `param_type`, `remark`, `creator`,
`create_date`, `updater`, `update_date`)
VALUES (1944947962685796353, 'enable_visual_memory_merge', 'false', 'boolean', 1, '是否合并视觉解析结果到llm',
1929745310624604161, '2025-07-15 10:31:44', 1929745310624604161, '2025-07-15 10:31:44');

View file

@ -261,3 +261,10 @@ databaseChangeLog:
- sqlFile: - sqlFile:
encoding: utf8 encoding: utf8
path: classpath:db/changelog/202507081646.sql path: classpath:db/changelog/202507081646.sql
- changeSet:
id: 202507151050
author: ganbin
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/202507151050.sql

View file

@ -644,7 +644,7 @@ class ConnectionHandler:
# 更新系统prompt至上下文 # 更新系统prompt至上下文
self.dialogue.update_system_message(self.prompt) self.dialogue.update_system_message(self.prompt)
def chat(self, query, tool_call=False, depth=0): def chat(self, query, tool_call=False, depth=0, enable_tts=True):
self.logger.bind(tag=TAG).info(f"大模型收到用户消息: {query}") self.logger.bind(tag=TAG).info(f"大模型收到用户消息: {query}")
self.llm_finish_task = False self.llm_finish_task = False
@ -654,6 +654,7 @@ class ConnectionHandler:
# 为最顶层时新建会话ID和发送FIRST请求 # 为最顶层时新建会话ID和发送FIRST请求
if depth == 0: if depth == 0:
self.sentence_id = str(uuid.uuid4().hex) self.sentence_id = str(uuid.uuid4().hex)
if enable_tts:
self.tts.tts_text_queue.put( self.tts.tts_text_queue.put(
TTSMessageDTO( TTSMessageDTO(
sentence_id=self.sentence_id, sentence_id=self.sentence_id,
@ -732,6 +733,7 @@ class ConnectionHandler:
if content is not None and len(content) > 0: if content is not None and len(content) > 0:
if not tool_call_flag: if not tool_call_flag:
response_message.append(content) response_message.append(content)
if enable_tts:
self.tts.tts_text_queue.put( self.tts.tts_text_queue.put(
TTSMessageDTO( TTSMessageDTO(
sentence_id=self.sentence_id, sentence_id=self.sentence_id,
@ -793,7 +795,7 @@ class ConnectionHandler:
self.dialogue.put( self.dialogue.put(
Message(role="assistant", content="".join(response_message)) Message(role="assistant", content="".join(response_message))
) )
if depth == 0: if depth == 0 and enable_tts:
self.tts.tts_text_queue.put( self.tts.tts_text_queue.put(
TTSMessageDTO( TTSMessageDTO(
sentence_id=self.sentence_id, sentence_id=self.sentence_id,

View file

@ -8,6 +8,7 @@ from core.providers.tts.dto.dto import ContentType
from core.utils.dialogue import Message from core.utils.dialogue import Message
from plugins_func.register import Action, ActionResponse from plugins_func.register import Action, ActionResponse
from core.providers.tts.dto.dto import TTSMessageDTO, SentenceType from core.providers.tts.dto.dto import TTSMessageDTO, SentenceType
from config.settings import load_config
TAG = __name__ TAG = __name__
@ -118,6 +119,29 @@ async def process_intent_result(conn, intent_result, original_text):
if result: if result:
if result.action == Action.RESPONSE: # 直接回复前端 if result.action == Action.RESPONSE: # 直接回复前端
enable_visual_memory_merge = load_config().get('enable_visual_memory_merge')
if enable_visual_memory_merge and function_name == "self_camera_take_photo":
# 1. 直接 TTS 视觉模型的结果
text = result.response
if text is not None:
speak_txt(conn, text)
# 2. 结构化消息
vision_message = (
f"【系统观察】检测到用户提出 '{original_text}' 的拍照意图。视觉模型已完成处理,并返回以下环境分析报告:\n"
f"--------------------\n"
f"视觉分析结果:\n"
f"{result.response}\n"
f"--------------------\n"
f"请将上述视觉信息作为本次对话的重要记忆和后续提问的上下文依据。"
f"你不需要做任何回答,或简短恢复'ok'即可。"
)
# 3. 作为"用户消息"调用主LLM chat,仅用于记忆,不TTS
try:
conn.chat(vision_message, tool_call=False, enable_tts=False)
except Exception as e:
conn.logger.bind(tag=TAG).error(f"主LLM记忆同步失败: {e}")
return True
else:
text = result.response text = result.response
if text is not None: if text is not None:
speak_txt(conn, text) speak_txt(conn, text)

View file

@ -96,6 +96,17 @@ class IntentProvider(IntentProviderBase):
"1. 只返回JSON格式,不要包含任何其他文字\n" "1. 只返回JSON格式,不要包含任何其他文字\n"
'2. 如果没有找到匹配的函数,返回{"function_call": {"name": "continue_chat"}}\n' '2. 如果没有找到匹配的函数,返回{"function_call": {"name": "continue_chat"}}\n'
"3. 确保返回的JSON格式正确,包含所有必要的字段\n" "3. 确保返回的JSON格式正确,包含所有必要的字段\n"
"**补充:**"
"在处理用户意图时,请注意上下文的连续性,避免重复识别已完成的操作。例如:"
"以拍照为例,可能第一轮用户说:"
"user: 打开摄像头。(用户可能想让你看看摄像头拍到了什么。等等)\n"
"你正常识别为拍照意图:并做出来类似如下回复\n"
"llm: 回复照片的内容。\n"
"第二轮对话:"
"user: 照片里有几个人。\n"
"---------------------"
"此时不要再次将意图识别为“拍照”或“打开摄像头”,而是应当继续处理当前对话上下文中的“照片内容”,即:"
'返回: {"function_call": {"name": "continue_chat"}}\n'
"特殊说明:\n" "特殊说明:\n"
"- 当用户单次输入包含多个指令时(如'打开灯并且调高音量')\n" "- 当用户单次输入包含多个指令时(如'打开灯并且调高音量')\n"
"- 请返回多个function_call组成的JSON数组\n" "- 请返回多个function_call组成的JSON数组\n"