feat(api+app): 对话阶段化、回忆录流水线与客户端会话体验

- DB: segments 用户输入文本(Alembic 0002)
- Chat: 阶段检测/阶段提示/回复限制,编排与访谈/画像 prompts 调整
- Memoir: 忠实度检查 agent,叙事与分类等链路更新
- Core: agent 日志、Alembic 启动、LangChain/日志/配置等
- Story: time_hints;Memory 检索与相关测试
- Expo: 助手头像、会话页与消息拆分、实时会话与文案/i18n
- Docs/scripts/tests: 迁移脚本、LLM JSON/记忆检索文档、新增单测
This commit is contained in:
Kevin
2026-03-26 12:13:36 +08:00
parent 49b089354c
commit a3f61fcc0f
94 changed files with 3332 additions and 672 deletions

View File

@@ -2,7 +2,7 @@ import json
import re
from typing import Any, Optional
from app.core.langchain_llm import bind_json_object_mode
from app.core.langchain_llm import invoke_json_object
from app.core.logging import get_logger
from .json_payload import extract_json_payload
@@ -61,13 +61,18 @@ class MemoirImagePromptService:
if self.llm:
raw_response = None
try:
json_llm = bind_json_object_mode(self.llm, max_tokens=512)
response = json_llm.invoke(
prompt_text = (
"Return JSON only with keys prompt, style, size. "
"Convert the memoir scene into an image-generation prompt.\n"
"Convert the memoir scene into an image-generation prompt. "
"The API uses response_format=json_object.\n"
+ json.dumps(llm_input, ensure_ascii=False)
)
raw_response = response.content
raw_response = invoke_json_object(
self.llm,
prompt_text,
max_tokens=512,
agent="MemoirImagePromptService.build_prompt",
)
parsed = json.loads(extract_json_payload(raw_response))
return {
"prompt": _ensure_style_in_prompt(
@@ -79,7 +84,7 @@ class MemoirImagePromptService:
}
except Exception as exc:
logger.warning(
"图片 prompt 生成回退到默认模板: chapter_category=%s, title=%s, error=%s",
"图片 prompt 生成回退到默认模板: chapter_category={}, title={}, error={}",
chapter_category,
chapter_title,
exc,
@@ -122,14 +127,20 @@ class MemoirImagePromptService:
if self.llm:
try:
json_llm = bind_json_object_mode(self.llm, max_tokens=512)
response = json_llm.invoke(
prompt_text = (
"Return JSON only with keys prompt, style, size. "
"Create an image-generation prompt for a memoir chapter COVER. "
"Emphasize: hero composition, evocative scene, chapter cover aesthetic.\n"
"Emphasize: hero composition, evocative scene, chapter cover aesthetic. "
"The API uses response_format=json_object.\n"
+ json.dumps(llm_input, ensure_ascii=False)
)
parsed = json.loads(extract_json_payload(response.content))
raw = invoke_json_object(
self.llm,
prompt_text,
max_tokens=512,
agent="MemoirImagePromptService.build_cover_prompt",
)
parsed = json.loads(extract_json_payload(raw))
return {
"prompt": _ensure_style_in_prompt(
parsed["prompt"], parsed.get("style", style)
@@ -140,7 +151,7 @@ class MemoirImagePromptService:
}
except Exception as exc:
logger.warning(
"封面 prompt 生成回退到默认模板: chapter_category=%s, title=%s, error=%s",
"封面 prompt 生成回退到默认模板: chapter_category={}, title={}, error={}",
chapter_category,
chapter_title,
exc,