feat(api): 统一 LLM JSON 调用层 llm_json_call,按域 Schema 迁移 chat/memoir agents
This commit is contained in:
@@ -5,7 +5,6 @@ NarrativeAgent:生成创意标题和叙事改写。
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from app.agents.stage_constants import CHAPTER_CATEGORIES
|
||||
@@ -14,9 +13,11 @@ from app.agents.memoir.prompts import (
|
||||
get_narrative_json_prompt,
|
||||
get_narrative_merge_json_prompt,
|
||||
)
|
||||
from app.agents.memoir.schemas import MemoirTitleOutput
|
||||
from app.core.config import settings
|
||||
from app.core.langchain_llm import invoke_json_object
|
||||
from app.core.llm_call import llm_json_call
|
||||
from app.core.logging import get_logger
|
||||
from app.core.json_utils import extract_json_payload
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -44,17 +45,23 @@ class NarrativeAgent:
|
||||
user_profile=user_profile,
|
||||
birth_year=birth_year,
|
||||
)
|
||||
raw = invoke_json_object(
|
||||
default_title = f"{CHAPTER_CATEGORIES.get(stage, stage)} 回忆"
|
||||
|
||||
def _title_fallback() -> MemoirTitleOutput:
|
||||
return MemoirTitleOutput(title=default_title)
|
||||
|
||||
out = llm_json_call(
|
||||
llm,
|
||||
prompt,
|
||||
max_tokens=256,
|
||||
MemoirTitleOutput,
|
||||
max_tokens=settings.memoir_title_max_tokens,
|
||||
agent="NarrativeAgent.generate_title",
|
||||
fallback_factory=_title_fallback,
|
||||
)
|
||||
data = json.loads(extract_json_payload(raw))
|
||||
title = (data.get("title") or "").strip() if isinstance(data, dict) else ""
|
||||
title = (out.title or "").strip()
|
||||
if title:
|
||||
return title.strip('"')
|
||||
return f"{CHAPTER_CATEGORIES.get(stage, stage)} 回忆"
|
||||
return default_title
|
||||
except Exception as e:
|
||||
logger.warning("NarrativeAgent 生成标题失败: {}", e)
|
||||
return f"{CHAPTER_CATEGORIES.get(stage, stage)} 回忆"
|
||||
@@ -100,7 +107,7 @@ class NarrativeAgent:
|
||||
background_voice=background_voice,
|
||||
occupation=occupation,
|
||||
)
|
||||
max_tokens = 8192
|
||||
max_tokens = int(settings.memoir_narrative_merge_max_tokens)
|
||||
agent_name = "NarrativeAgent.generate_narrative_merge"
|
||||
else:
|
||||
prompt = get_narrative_json_prompt(
|
||||
@@ -113,7 +120,7 @@ class NarrativeAgent:
|
||||
background_voice=background_voice,
|
||||
occupation=occupation,
|
||||
)
|
||||
max_tokens = 4096
|
||||
max_tokens = int(settings.memoir_narrative_max_tokens)
|
||||
agent_name = "NarrativeAgent.generate_narrative"
|
||||
return invoke_json_object(
|
||||
llm,
|
||||
|
||||
Reference in New Issue
Block a user