feat(memory,conversation): 记忆富化/证据包、时间线幂等字段与对话分段全链路
数据库 - 新增迁移 0003:timeline_events.memory_source_id 外键 → memory_sources,便于按 ingest 源做时间线幂等 后端 - 记忆 - 新增 ingest 后 LLM 富化(摘要/事实/时间线),可配置开关与最大字符数 - 新增证据包组装:合并 chunk、摘要、事实、时间线、故事等检索结果;支持空 query 时是否仍带 rolling 等开关 - repo/retriever/service/router/schemas/summarizer/timeline/extractor 等扩展;文档 memory-retrieval.md 更新 后端 - 对话 WS - 增加 PING/PONG;分段 ASR 日志与空音频处理;转写失败与「无助手回复」错误提示更明确 - 助手多段回复持久化使用统一分隔符,与分段逻辑一致 后端 - Agent - reply_limits:按 [SPLIT] 与段落拆段,并保证非空 fallback,供 WS 与 TTS 多段下发 后端 - 回忆录任务 - transcript ingest 记录 source_id;任务成功结?
This commit is contained in:
@@ -17,7 +17,11 @@ from app.agents.chat.prompts_conversation import (
|
||||
get_opening_prompt,
|
||||
)
|
||||
from app.agents.state_schema import MemoirStateSchema
|
||||
from app.agents.chat.reply_limits import truncate_chat_segments
|
||||
from app.agents.chat.reply_limits import (
|
||||
nonempty_segments_or_fallback,
|
||||
segments_from_llm_response,
|
||||
truncate_chat_segments,
|
||||
)
|
||||
from app.core.agent_logging import (
|
||||
agent_span,
|
||||
log_agent_payload,
|
||||
@@ -135,10 +139,12 @@ class InterviewAgent:
|
||||
log_agent_payload(
|
||||
logger, "InterviewAgent.generate_response.raw_response", response_text
|
||||
)
|
||||
messages = [
|
||||
msg.strip() for msg in response_text.split("[SPLIT]") if msg.strip()
|
||||
]
|
||||
raw_list = messages if messages else [response_text.strip()]
|
||||
raw_list = segments_from_llm_response(
|
||||
response_text,
|
||||
max_segments=settings.chat_interview_max_segments,
|
||||
)
|
||||
if not raw_list:
|
||||
raw_list = [response_text.strip()]
|
||||
out = truncate_chat_segments(
|
||||
raw_list,
|
||||
max_segments=settings.chat_interview_max_segments,
|
||||
@@ -150,6 +156,7 @@ class InterviewAgent:
|
||||
: settings.chat_interview_max_chars_per_segment
|
||||
]
|
||||
]
|
||||
out = nonempty_segments_or_fallback(out, fallback=_FALLBACK_REPLY)
|
||||
log_agent_summary(
|
||||
logger,
|
||||
"InterviewAgent.generate_response segments={} conversation_id={}",
|
||||
@@ -193,10 +200,9 @@ class InterviewAgent:
|
||||
log_agent_payload(
|
||||
logger, "InterviewAgent.opening.raw_response", response_text
|
||||
)
|
||||
messages = [
|
||||
msg.strip() for msg in response_text.split("[SPLIT]") if msg.strip()
|
||||
]
|
||||
raw_list = messages if messages else [response_text.strip()]
|
||||
raw_list = segments_from_llm_response(response_text, max_segments=2)
|
||||
if not raw_list:
|
||||
raw_list = [response_text.strip()]
|
||||
out = truncate_chat_segments(
|
||||
raw_list,
|
||||
max_segments=2,
|
||||
@@ -208,7 +214,7 @@ class InterviewAgent:
|
||||
len(out),
|
||||
conversation_id,
|
||||
)
|
||||
return (
|
||||
segments = (
|
||||
out
|
||||
if out
|
||||
else [
|
||||
@@ -217,6 +223,10 @@ class InterviewAgent:
|
||||
]
|
||||
]
|
||||
)
|
||||
return nonempty_segments_or_fallback(
|
||||
segments,
|
||||
fallback="你好呀~ 又见面了,最近有没有什么事想跟我说说?",
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("生成开场白失败: {}", e, exc_info=True)
|
||||
return ["你好呀~ 又见面了,最近有没有什么事想跟我说说?"]
|
||||
|
||||
@@ -19,7 +19,11 @@ from app.core.langchain_llm import ainvoke_json_object
|
||||
from app.core.agent_logging import agent_span, log_agent_payload, log_agent_summary
|
||||
from app.core.config import settings
|
||||
from app.core.logging import get_logger
|
||||
from app.agents.chat.reply_limits import truncate_chat_segments
|
||||
from app.agents.chat.reply_limits import (
|
||||
nonempty_segments_or_fallback,
|
||||
segments_from_llm_response,
|
||||
truncate_chat_segments,
|
||||
)
|
||||
from app.features.memoir.memoir_images.json_payload import extract_json_payload
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -135,10 +139,9 @@ class ProfileAgent:
|
||||
log_agent_payload(
|
||||
logger, "ProfileAgent.followup.raw_response", response_text
|
||||
)
|
||||
messages = [
|
||||
msg.strip() for msg in response_text.split("[SPLIT]") if msg.strip()
|
||||
]
|
||||
raw_list = messages if messages else [response_text.strip()]
|
||||
raw_list = segments_from_llm_response(response_text, max_segments=3)
|
||||
if not raw_list:
|
||||
raw_list = [response_text.strip()]
|
||||
out = truncate_chat_segments(
|
||||
raw_list,
|
||||
max_segments=3,
|
||||
@@ -150,7 +153,7 @@ class ProfileAgent:
|
||||
len(out),
|
||||
conversation_id,
|
||||
)
|
||||
return (
|
||||
segments = (
|
||||
out
|
||||
if out
|
||||
else [
|
||||
@@ -159,6 +162,10 @@ class ProfileAgent:
|
||||
]
|
||||
]
|
||||
)
|
||||
return nonempty_segments_or_fallback(
|
||||
segments,
|
||||
fallback="谢谢分享!能再告诉我一些吗?",
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("生成资料跟进回复失败: {}", e)
|
||||
return ["谢谢分享!能再告诉我一些吗?"]
|
||||
@@ -193,10 +200,9 @@ class ProfileAgent:
|
||||
log_agent_payload(
|
||||
logger, "ProfileAgent.greeting.raw_response", response_text
|
||||
)
|
||||
messages = [
|
||||
msg.strip() for msg in response_text.split("[SPLIT]") if msg.strip()
|
||||
]
|
||||
raw_list = messages if messages else [response_text.strip()]
|
||||
raw_list = segments_from_llm_response(response_text, max_segments=2)
|
||||
if not raw_list:
|
||||
raw_list = [response_text.strip()]
|
||||
out = truncate_chat_segments(
|
||||
raw_list,
|
||||
max_segments=2,
|
||||
@@ -208,7 +214,7 @@ class ProfileAgent:
|
||||
len(out),
|
||||
conversation_id,
|
||||
)
|
||||
return (
|
||||
segments = (
|
||||
out
|
||||
if out
|
||||
else [
|
||||
@@ -217,6 +223,10 @@ class ProfileAgent:
|
||||
]
|
||||
]
|
||||
)
|
||||
return nonempty_segments_or_fallback(
|
||||
segments,
|
||||
fallback="你好!在开始之前,能告诉我你是哪一年出生的吗?",
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("生成资料收集开场白失败: {}", e)
|
||||
return [
|
||||
|
||||
@@ -77,7 +77,7 @@ def get_system_prompt(
|
||||
|
||||
prompt = f"""你是「岁月知己」,像老朋友一样陪用户聊人生。**回复要短**,像微信聊天,不要长篇、不要文学腔。
|
||||
|
||||
规则:先简短接住对方一句,**最多再问一个具体问题**;禁止括号与思考过程;禁止采访腔(如「我注意到」「我想了解」)。
|
||||
规则:先简短接住对方一句,**最多再问一个具体问题**;禁止括号与思考过程;禁止采访腔(如「我注意到」「我想了解」);**不要重复确认**对方刚说过或上文已能推断的信息。
|
||||
|
||||
当前阶段:{stage_name_map.get(current_stage, current_stage.value)}
|
||||
已聊话题:{covered_topics_str}
|
||||
@@ -364,7 +364,7 @@ def get_guided_conversation_prompt(
|
||||
style = RESPONSE_STYLES[conversation_turn % len(RESPONSE_STYLES)]
|
||||
style_guidance = {
|
||||
"empathy": "共情一两句即可",
|
||||
"curious": "表现好奇,追问一个具体点",
|
||||
"curious": "若还有未展开的细节可好奇问一个点;若上文已说清或可自然推断,只承接或换角度,**勿为凑问题而追问**",
|
||||
"reflection": "可一句简短感慨,勿讲大道理",
|
||||
"lighthearted": "轻松一点,别讲段子太长",
|
||||
"connection": "可提「我也有过类似感受」一句,勿编造具体经历细节",
|
||||
@@ -431,12 +431,13 @@ def get_guided_conversation_prompt(
|
||||
## 任务(短)
|
||||
1. 先简短回应一句,不要总结成长文。
|
||||
2. 用户若跳到别的人生阶段,跟着他聊,别硬拉回。
|
||||
3. 需要追问时**只问一个**具体小问题;不必每轮都问。
|
||||
4. 可用 [SPLIT] 分成**最多 2 条**消息,每条都很短。
|
||||
3. 需要追问时**只问一个**具体小问题;**不必每轮都问**;若用户已说明或语境已能推出(如谁买的、和谁),**别再为同一件事做 yes/no 确认**。
|
||||
4. 用户只回简短肯定/否定(如「是的」「对」)时,**结合上文**理解,承接即可或问**新**角度,勿重复上一句已问过的事。
|
||||
5. 可用 [SPLIT] 分成**最多 2 条**消息,每条都很短。
|
||||
{dynamic_guidance}{uncovered_hint}
|
||||
|
||||
## 禁止
|
||||
括号/思考过程;采访腔;重复确认用户档案里已有信息;别编用户没说的细节。
|
||||
括号/思考过程;采访腔;**重复确认**用户档案、**上文已说**或**强暗示下已可知**的事实(包括无信息量的「是不是他/她…」式追问);别编用户没说的细节。
|
||||
|
||||
直接输出([SPLIT] 可选,最多 2 段):"""
|
||||
|
||||
|
||||
@@ -2,6 +2,49 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
|
||||
def segments_from_llm_response(
|
||||
response_text: str,
|
||||
*,
|
||||
max_segments: int = 3,
|
||||
min_paragraph_chars: int = 12,
|
||||
) -> list[str]:
|
||||
"""
|
||||
优先按字面 [SPLIT] 拆段;若模型只输出一段、但用空行写了多段,再按段落拆。
|
||||
解决「两段话 + 换行」却未写 [SPLIT] 时仍要拆气泡 / 多段 TTS 的情况。
|
||||
"""
|
||||
text = (response_text or "").strip()
|
||||
if not text:
|
||||
return []
|
||||
primary = [p.strip() for p in text.split("[SPLIT]") if p.strip()]
|
||||
if len(primary) > 1:
|
||||
return primary[:max_segments]
|
||||
blob = primary[0] if primary else text
|
||||
if "\n" not in blob:
|
||||
return [blob]
|
||||
paras = [p.strip() for p in re.split(r"\n\s*\n+", blob) if p.strip()]
|
||||
if len(paras) < 2:
|
||||
return [blob]
|
||||
paras = [p for p in paras if len(p) >= min_paragraph_chars]
|
||||
if len(paras) < 2:
|
||||
return [blob]
|
||||
return paras[:max_segments]
|
||||
|
||||
|
||||
def nonempty_segments_or_fallback(
|
||||
segments: list[str],
|
||||
*,
|
||||
fallback: str,
|
||||
) -> list[str]:
|
||||
"""去掉空段;若全部为空白/空串则返回单条 fallback,避免 WS 下发空 text。"""
|
||||
cleaned = [s for s in segments if (s or "").strip()]
|
||||
if cleaned:
|
||||
return cleaned
|
||||
fb = (fallback or "").strip()
|
||||
return [fb] if fb else ["…"]
|
||||
|
||||
|
||||
def truncate_chat_segments(
|
||||
segments: list[str],
|
||||
|
||||
Reference in New Issue
Block a user