配置 SSOT(TOML + .env) 统一错误契约 Auth 与事务边界 Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client 可观测性(OpenTelemetry + LGTM)
90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
"""
|
||
ExtractionAgent:从用户消息中提取 5-stage 状态与 slots。
|
||
对应现有逻辑:get_state_extraction_prompt + JSON 解析
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
from typing import Any, Dict
|
||
|
||
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
|
||
from app.core.logging import get_logger
|
||
from app.features.memoir.constants import memoir
|
||
|
||
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,
|
||
*,
|
||
language: str = "zh",
|
||
) -> 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()
|
||
},
|
||
language=language,
|
||
)
|
||
parsed = llm_json_call(
|
||
llm,
|
||
prompt,
|
||
StateExtractionOutput,
|
||
max_tokens=memoir.extraction_max_tokens,
|
||
agent="ExtractionAgent.extract",
|
||
)
|
||
raw_slots = parsed.slots or {}
|
||
extracted_slots = {
|
||
k: v if isinstance(v, str) else str(v) for k, v in raw_slots.items()
|
||
}
|
||
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)
|
||
|
||
return ExtractionResult(detected_stage=detected_stage, slots=extracted_slots)
|