feat(api): 统一 LLM JSON 调用层 llm_json_call,按域 Schema 迁移 chat/memoir agents
This commit is contained in:
@@ -15,7 +15,7 @@ from app.agents.memoir.prompts import (
|
||||
)
|
||||
from app.agents.memoir.story_route_payload import build_route_candidate_json
|
||||
from app.core.config import settings
|
||||
from app.core.langchain_llm import invoke_json_object
|
||||
from app.core.llm_call import LLMCallError, llm_json_call
|
||||
from app.core.logging import get_logger
|
||||
from app.features.story.models import Story
|
||||
|
||||
@@ -132,23 +132,23 @@ class StoryRouteAgent:
|
||||
batch_transcript=batch_transcript,
|
||||
candidate_stories_json=payload,
|
||||
)
|
||||
try:
|
||||
raw = invoke_json_object(
|
||||
llm,
|
||||
prompt,
|
||||
max_tokens=1024,
|
||||
agent="StoryRouteAgent.decide",
|
||||
).strip()
|
||||
data = json.loads(raw)
|
||||
decision = StoryRouteDecision.model_validate(data)
|
||||
except Exception as e:
|
||||
logger.warning("StoryRouteAgent 解析失败: {}", e)
|
||||
|
||||
def _decide_fallback() -> StoryRouteDecision:
|
||||
return StoryRouteDecision(
|
||||
decision="new_story",
|
||||
new_story_title=None,
|
||||
reason="parse_error",
|
||||
)
|
||||
|
||||
decision = llm_json_call(
|
||||
llm,
|
||||
prompt,
|
||||
StoryRouteDecision,
|
||||
max_tokens=settings.memoir_story_route_max_tokens,
|
||||
agent="StoryRouteAgent.decide",
|
||||
fallback_factory=_decide_fallback,
|
||||
)
|
||||
|
||||
if decision.decision == "append_story":
|
||||
tid = decision.target_story_id
|
||||
if not tid or tid not in valid_story_ids:
|
||||
@@ -188,15 +188,14 @@ class StoryRouteAgent:
|
||||
candidate_stories_json=payload,
|
||||
)
|
||||
try:
|
||||
raw = invoke_json_object(
|
||||
plan = llm_json_call(
|
||||
llm,
|
||||
prompt,
|
||||
max_tokens=4096,
|
||||
StoryBatchPlan,
|
||||
max_tokens=settings.memoir_story_batch_plan_max_tokens,
|
||||
agent="StoryRouteAgent.plan_batch",
|
||||
).strip()
|
||||
data = json.loads(raw)
|
||||
plan = StoryBatchPlan.model_validate(data)
|
||||
except Exception as e:
|
||||
)
|
||||
except LLMCallError as e:
|
||||
logger.warning("StoryRouteAgent.plan_batch 解析失败: {}", e)
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user