Files
life-echo/api/app/agents/memoir/extraction_agent.py

89 lines
2.9 KiB
Python
Raw Normal View History

2026-03-19 10:38:11 +08:00
"""
ExtractionAgent从用户消息中提取 5-stage 状态与 slots
对应现有逻辑get_state_extraction_prompt + JSON 解析
"""
2026-03-19 14:36:14 +08:00
2026-03-19 10:38:11 +08:00
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Dict
2026-03-20 15:15:35 +08:00
from app.agents.memoir.prompts import get_state_extraction_prompt
from app.agents.memoir.schemas import StateExtractionOutput
from app.agents.stage_constants import normalize_chat_stage
from app.core.config import settings
from app.core.llm_call import LLMCallError, llm_json_call
2026-03-19 10:38:11 +08:00
from app.core.logging import get_logger
logger = get_logger(__name__)
@dataclass
class ExtractionResult:
"""状态提取结果"""
2026-03-19 14:36:14 +08:00
2026-03-19 10:38:11 +08:00
detected_stage: str
slots: Dict[str, str]
class ExtractionAgent:
"""从用户消息中提取 detected_stage 和 slots"""
def extract(
self,
user_message: str,
current_stage: str,
stage_slots: Dict[str, Any],
llm: Any,
*,
language: str = "zh",
2026-03-19 10:38:11 +08:00
) -> ExtractionResult:
"""
提取结构化信息并判断阶段
llm 需支持 .invoke(prompt) 同步调用Celery 任务内使用
"""
detected_stage = current_stage
extracted_slots: Dict[str, str] = {}
if not llm:
2026-03-19 14:36:14 +08:00
return ExtractionResult(
detected_stage=detected_stage, slots=extracted_slots
)
2026-03-19 10:38:11 +08:00
try:
prompt = get_state_extraction_prompt(
user_message=user_message,
current_stage=current_stage,
stage_slots={
k: v.model_dump() if hasattr(v, "model_dump") else v
for k, v in (stage_slots or {}).items()
},
language=language,
2026-03-19 10:38:11 +08:00
)
parsed = llm_json_call(
llm,
prompt,
StateExtractionOutput,
max_tokens=settings.memoir_extraction_max_tokens,
agent="ExtractionAgent.extract",
)
raw_slots = parsed.slots or {}
2026-03-19 10:38:11 +08:00
extracted_slots = {
2026-03-19 14:36:14 +08:00
k: v if isinstance(v, str) else str(v) for k, v in raw_slots.items()
2026-03-19 10:38:11 +08:00
}
if not extracted_slots:
# 无实质 slot 时不推断阶段,避免元话语被标成任意 childhood 等(与服务端护栏一致)
detected_stage = normalize_chat_stage(
current_stage, fallback=current_stage
)
else:
raw_detected = parsed.detected_stage or current_stage
detected_stage = normalize_chat_stage(
str(raw_detected) if raw_detected is not None else None,
fallback=current_stage,
)
except LLMCallError as e:
logger.warning("ExtractionAgent LLM 解析失败: {}", e)
2026-03-19 10:38:11 +08:00
return ExtractionResult(detected_stage=detected_stage, slots=extracted_slots)