feat(api): 统一 LLM JSON 调用层 llm_json_call,按域 Schema 迁移 chat/memoir agents
This commit is contained in:
@@ -3,7 +3,6 @@ ProfileAgent:用户资料收集 Specialist
|
||||
负责提取资料、资料追问、资料收集开场白,不负责 Redis 持久化(由 Orchestrator 统一处理)
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
@@ -15,11 +14,11 @@ from app.agents.chat.prompts_profile import (
|
||||
get_profile_followup_prompt,
|
||||
get_profile_greeting_prompt,
|
||||
)
|
||||
from app.agents.chat.schemas import ProfileExtractionOutput
|
||||
from app.core.agent_logging import agent_span, log_agent_payload, log_agent_summary
|
||||
from app.core.config import settings
|
||||
from app.core.dependencies import get_llm_provider
|
||||
from app.core.json_utils import extract_json_payload
|
||||
from app.core.langchain_llm import ainvoke_json_object
|
||||
from app.core.llm_call import allm_json_call
|
||||
from app.core.logging import get_logger
|
||||
from app.agents.chat.reply_limits import (
|
||||
nonempty_segments_or_fallback,
|
||||
@@ -53,6 +52,53 @@ class ProfileAgent:
|
||||
def __init__(self):
|
||||
self.llm = _get_langchain_llm()
|
||||
|
||||
async def _invoke_chat(
|
||||
self,
|
||||
messages: List[Any],
|
||||
*,
|
||||
max_tokens: int,
|
||||
conversation_id: Optional[str],
|
||||
agent_name: str,
|
||||
) -> str:
|
||||
chat_llm = self.llm.bind(max_tokens=max_tokens)
|
||||
llm_t0 = time.perf_counter()
|
||||
with agent_span(
|
||||
logger, f"{agent_name}.llm", conversation_id=conversation_id or ""
|
||||
):
|
||||
response = await chat_llm.ainvoke(messages)
|
||||
logger.info(
|
||||
"event=chat_llm_done agent={} response_latency_ms={:.2f}",
|
||||
agent_name,
|
||||
(time.perf_counter() - llm_t0) * 1000,
|
||||
)
|
||||
return (
|
||||
response.content if hasattr(response, "content") else str(response)
|
||||
) or ""
|
||||
|
||||
async def _segments_from_response(
|
||||
self,
|
||||
response_text: str,
|
||||
*,
|
||||
max_segments: int,
|
||||
max_chars_per_segment: int,
|
||||
fallback: str,
|
||||
) -> List[str]:
|
||||
log_agent_payload(
|
||||
logger,
|
||||
"ProfileAgent._segments_from_response.raw_response",
|
||||
response_text,
|
||||
)
|
||||
raw_list = segments_from_llm_response(response_text, max_segments=max_segments)
|
||||
if not raw_list:
|
||||
raw_list = [response_text.strip()]
|
||||
out = truncate_chat_segments(
|
||||
raw_list,
|
||||
max_segments=max_segments,
|
||||
max_chars_per_segment=max_chars_per_segment,
|
||||
)
|
||||
segments = out if out else [response_text.strip()[:max_chars_per_segment]]
|
||||
return nonempty_segments_or_fallback(segments, fallback=fallback)
|
||||
|
||||
async def extract_profile_from_message(
|
||||
self,
|
||||
user_message: str,
|
||||
@@ -81,16 +127,17 @@ class ProfileAgent:
|
||||
prompt = get_profile_extraction_prompt(
|
||||
user_message, missing_fields, recent_dialogue=recent_dialogue or None
|
||||
)
|
||||
content = await ainvoke_json_object(
|
||||
parsed = await allm_json_call(
|
||||
self.llm,
|
||||
prompt,
|
||||
max_tokens=512,
|
||||
ProfileExtractionOutput,
|
||||
max_tokens=settings.chat_profile_extract_max_tokens,
|
||||
agent="ProfileAgent.extract_profile_from_message",
|
||||
fallback_factory=lambda: ProfileExtractionOutput(),
|
||||
)
|
||||
parsed = json.loads(extract_json_payload(content))
|
||||
result = {}
|
||||
if "birth_year" in parsed and parsed["birth_year"] is not None:
|
||||
raw = parsed["birth_year"]
|
||||
if parsed.birth_year is not None:
|
||||
raw = parsed.birth_year
|
||||
if isinstance(raw, int) and 1900 <= raw <= 2100:
|
||||
result["birth_year"] = raw
|
||||
elif isinstance(raw, str) and raw.isdigit():
|
||||
@@ -99,14 +146,14 @@ class ProfileAgent:
|
||||
y = 1900 + y if y >= 50 else 2000 + y
|
||||
if 1900 <= y <= 2100:
|
||||
result["birth_year"] = y
|
||||
if "birth_place" in parsed and parsed["birth_place"]:
|
||||
result["birth_place"] = str(parsed["birth_place"])
|
||||
if "grew_up_place" in parsed and parsed["grew_up_place"]:
|
||||
result["grew_up_place"] = str(parsed["grew_up_place"])
|
||||
if "occupation" in parsed and parsed["occupation"]:
|
||||
result["occupation"] = str(parsed["occupation"])
|
||||
if parsed.birth_place:
|
||||
result["birth_place"] = str(parsed.birth_place)
|
||||
if parsed.grew_up_place:
|
||||
result["grew_up_place"] = str(parsed.grew_up_place)
|
||||
if parsed.occupation:
|
||||
result["occupation"] = str(parsed.occupation)
|
||||
return result
|
||||
except (json.JSONDecodeError, Exception) as e:
|
||||
except Exception as e:
|
||||
logger.error("提取资料信息失败: {}", e)
|
||||
return {}
|
||||
|
||||
@@ -143,61 +190,33 @@ class ProfileAgent:
|
||||
"ProfileAgent.followup.prompt",
|
||||
format_history_string(messages),
|
||||
)
|
||||
chat_llm = self.llm.bind(
|
||||
max_tokens=settings.chat_profile_followup_max_tokens
|
||||
)
|
||||
llm_t0 = time.perf_counter()
|
||||
with agent_span(
|
||||
logger,
|
||||
"ProfileAgent.followup.llm",
|
||||
conversation_id=conversation_id,
|
||||
):
|
||||
logger.info(
|
||||
"event=chat_prompt_built agent=ProfileAgent.generate_profile_followup "
|
||||
"prompt_chars={} history_pairs_total={} history_pairs_windowed={}",
|
||||
_message_contents_char_count(messages),
|
||||
hw.turn_total,
|
||||
len(hw.window) // 2,
|
||||
)
|
||||
response = await chat_llm.ainvoke(messages)
|
||||
prompt_chars = _message_contents_char_count(messages)
|
||||
logger.info(
|
||||
"event=chat_llm_done agent=ProfileAgent.generate_profile_followup "
|
||||
"response_latency_ms={:.2f}",
|
||||
(time.perf_counter() - llm_t0) * 1000,
|
||||
"event=chat_prompt_built agent=ProfileAgent.generate_profile_followup "
|
||||
"prompt_chars={} history_pairs_total={} history_pairs_windowed={}",
|
||||
prompt_chars,
|
||||
hw.turn_total,
|
||||
len(hw.window) // 2,
|
||||
)
|
||||
response_text = (
|
||||
response.content if hasattr(response, "content") else str(response)
|
||||
response_text = await self._invoke_chat(
|
||||
messages,
|
||||
max_tokens=settings.chat_profile_followup_max_tokens,
|
||||
conversation_id=conversation_id,
|
||||
agent_name="ProfileAgent.generate_profile_followup",
|
||||
)
|
||||
log_agent_payload(
|
||||
logger, "ProfileAgent.followup.raw_response", response_text
|
||||
)
|
||||
raw_list = segments_from_llm_response(response_text, max_segments=3)
|
||||
if not raw_list:
|
||||
raw_list = [response_text.strip()]
|
||||
out = truncate_chat_segments(
|
||||
raw_list,
|
||||
segments = await self._segments_from_response(
|
||||
response_text,
|
||||
max_segments=3,
|
||||
max_chars_per_segment=settings.chat_interview_max_chars_per_segment,
|
||||
fallback="谢谢分享!能再告诉我一些吗?",
|
||||
)
|
||||
log_agent_summary(
|
||||
logger,
|
||||
"ProfileAgent.followup segments={} conversation_id={}",
|
||||
len(out),
|
||||
len(segments),
|
||||
conversation_id,
|
||||
)
|
||||
segments = (
|
||||
out
|
||||
if out
|
||||
else [
|
||||
response_text.strip()[
|
||||
: settings.chat_interview_max_chars_per_segment
|
||||
]
|
||||
]
|
||||
)
|
||||
return nonempty_segments_or_fallback(
|
||||
segments,
|
||||
fallback="谢谢分享!能再告诉我一些吗?",
|
||||
)
|
||||
return segments
|
||||
except Exception as e:
|
||||
logger.error("生成资料跟进回复失败: {}", e)
|
||||
return ["谢谢分享!能再告诉我一些吗?"]
|
||||
@@ -229,61 +248,33 @@ class ProfileAgent:
|
||||
log_agent_payload(
|
||||
logger, "ProfileAgent.greeting.prompt", format_history_string(messages)
|
||||
)
|
||||
chat_llm = self.llm.bind(
|
||||
max_tokens=settings.chat_profile_followup_max_tokens
|
||||
)
|
||||
llm_t0 = time.perf_counter()
|
||||
with agent_span(
|
||||
logger,
|
||||
"ProfileAgent.greeting.llm",
|
||||
conversation_id=conversation_id,
|
||||
):
|
||||
logger.info(
|
||||
"event=chat_prompt_built agent=ProfileAgent.generate_profile_greeting "
|
||||
"prompt_chars={} history_pairs_total={} history_pairs_windowed={}",
|
||||
_message_contents_char_count(messages),
|
||||
hw.turn_total,
|
||||
len(hw.window) // 2,
|
||||
)
|
||||
response = await chat_llm.ainvoke(messages)
|
||||
prompt_chars = _message_contents_char_count(messages)
|
||||
logger.info(
|
||||
"event=chat_llm_done agent=ProfileAgent.generate_profile_greeting "
|
||||
"response_latency_ms={:.2f}",
|
||||
(time.perf_counter() - llm_t0) * 1000,
|
||||
"event=chat_prompt_built agent=ProfileAgent.generate_profile_greeting "
|
||||
"prompt_chars={} history_pairs_total={} history_pairs_windowed={}",
|
||||
prompt_chars,
|
||||
hw.turn_total,
|
||||
len(hw.window) // 2,
|
||||
)
|
||||
response_text = (
|
||||
response.content if hasattr(response, "content") else str(response)
|
||||
response_text = await self._invoke_chat(
|
||||
messages,
|
||||
max_tokens=settings.chat_profile_followup_max_tokens,
|
||||
conversation_id=conversation_id,
|
||||
agent_name="ProfileAgent.generate_profile_greeting",
|
||||
)
|
||||
log_agent_payload(
|
||||
logger, "ProfileAgent.greeting.raw_response", response_text
|
||||
)
|
||||
raw_list = segments_from_llm_response(response_text, max_segments=2)
|
||||
if not raw_list:
|
||||
raw_list = [response_text.strip()]
|
||||
out = truncate_chat_segments(
|
||||
raw_list,
|
||||
segments = await self._segments_from_response(
|
||||
response_text,
|
||||
max_segments=2,
|
||||
max_chars_per_segment=settings.chat_interview_max_chars_per_segment,
|
||||
fallback="你好!在开始之前,能告诉我你是哪一年出生的吗?",
|
||||
)
|
||||
log_agent_summary(
|
||||
logger,
|
||||
"ProfileAgent.greeting segments={} conversation_id={}",
|
||||
len(out),
|
||||
len(segments),
|
||||
conversation_id,
|
||||
)
|
||||
segments = (
|
||||
out
|
||||
if out
|
||||
else [
|
||||
response_text.strip()[
|
||||
: settings.chat_interview_max_chars_per_segment
|
||||
]
|
||||
]
|
||||
)
|
||||
return nonempty_segments_or_fallback(
|
||||
segments,
|
||||
fallback="你好!在开始之前,能告诉我你是哪一年出生的吗?",
|
||||
)
|
||||
return segments
|
||||
except Exception as e:
|
||||
logger.error("生成资料收集开场白失败: {}", e)
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user