- DB: segments 用户输入文本(Alembic 0002) - Chat: 阶段检测/阶段提示/回复限制,编排与访谈/画像 prompts 调整 - Memoir: 忠实度检查 agent,叙事与分类等链路更新 - Core: agent 日志、Alembic 启动、LangChain/日志/配置等 - Story: time_hints;Memory 检索与相关测试 - Expo: 助手头像、会话页与消息拆分、实时会话与文案/i18n - Docs/scripts/tests: 迁移脚本、LLM JSON/记忆检索文档、新增单测
2.4 KiB
2.4 KiB
LLM JSON 模式使用约定
本文档说明 Life Echo API 中 结构化输出 与 自然语言输出 的分工,以及与 DeepSeek JSON Output 的对齐方式。
实现位置
- 绑定方式:
app/core/langchain_llm.py中的bind_json_object_mode(等价于response_format={"type": "json_object"})。禁止使用已废弃的model_kwargs={"response_format": ...}绑定方式(见app/adapters/llm/deepseek.py注释)。 - 统一调用:
invoke_json_object/ainvoke_json_object(同文件)在 JSON 模式下提供空内容检测、可选一次重试与结构化日志(agent、prompt_sha12)。
何时必须使用 json_object
以下路径需要稳定 json.loads / 下游解析,必须通过 bind_json_object_mode 或 invoke_json_object / ainvoke_json_object 调用:
| 能力 | 代码入口 |
|---|---|
| 用户资料字段抽取 | ProfileAgent.extract_profile_from_message |
| 回忆录阶段与 slots 抽取 | ExtractionAgent.extract |
叙事段落 JSON(paragraphs) |
NarrativeAgent.generate_narrative |
| Story 路由 / 批量规划 | StoryRouteAgent.decide / plan_batch |
| 回忆录配图 prompt | MemoirImagePromptService.build_prompt / build_cover_prompt |
何时保持普通文本(禁止强行 JSON)
- 访谈对话、资料追问开场与跟进:自然口语与
[SPLIT]分条,使用ChatOpenAI默认输出(InterviewAgent、ProfileAgent中非 extraction 方法)。 - 章节分类、创意标题等若仍为纯文本解析:由业务选择是否后续改为小 JSON(见产品决策)。
DeepSeek 官方建议对齐
response_format:已由bind_json_object_mode设置。- Prompt 中应包含 json 字样及与解析代码一致的 字段示例(各
prompts_*.py中维护)。 max_tokens:在invoke_json_object(..., max_tokens=...)与绑定处统一传入;长叙事等场景按需调大。- 偶发空内容:由
invoke_json_object记录并重试一次;仍失败则由各 Agent 既有try/except回退。
架构注意
app/core仅承载跨切 LLM 约定,不依赖features/*或具体 Agent。- Service 层通过
get_llm_provider().langchain_llm获取 Runnable,不直接 importadapters。