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

67 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
ExtractionAgent从用户消息中提取 5-stage 状态与 slots。
对应现有逻辑get_state_extraction_prompt + JSON 解析
"""
from __future__ import annotations
import json
from dataclasses import dataclass
from typing import Any, Dict
from app.core.logging import get_logger
from app.features.memoir.memoir_images.json_payload import extract_json_payload
from app.agents.memoir.prompts import get_state_extraction_prompt
logger = get_logger(__name__)
@dataclass
class ExtractionResult:
"""状态提取结果"""
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,
) -> ExtractionResult:
"""
提取结构化信息并判断阶段。
llm 需支持 .invoke(prompt) 同步调用Celery 任务内使用)。
"""
detected_stage = current_stage
extracted_slots: Dict[str, str] = {}
if not llm:
return ExtractionResult(detected_stage=detected_stage, slots=extracted_slots)
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()
},
)
response = llm.invoke(prompt)
parsed = json.loads(extract_json_payload(response.content))
detected_stage = parsed.get("detected_stage", detected_stage)
raw_slots = parsed.get("slots", {}) or {}
extracted_slots = {
k: v if isinstance(v, str) else str(v)
for k, v in raw_slots.items()
}
except (json.JSONDecodeError, Exception) as e:
logger.warning("ExtractionAgent LLM 解析失败: %s", e)
return ExtractionResult(detected_stage=detected_stage, slots=extracted_slots)