2026-03-19 10:36:55 +08:00
|
|
|
|
"""
|
|
|
|
|
|
InterviewAgent:正式访谈 Specialist
|
|
|
|
|
|
负责状态感知回复、开场白,不负责 Redis 持久化(由 Orchestrator 统一处理)
|
|
|
|
|
|
"""
|
2026-03-19 14:36:14 +08:00
|
|
|
|
|
2026-03-26 12:13:36 +08:00
|
|
|
|
from typing import Any, List, Optional
|
2026-03-19 10:36:55 +08:00
|
|
|
|
|
2026-03-20 17:25:42 +08:00
|
|
|
|
from app.agents.chat.agent_turn import AgentChatTurn
|
2026-03-26 12:13:36 +08:00
|
|
|
|
from app.agents.chat.stage_detection import keyword_fallback_primary_stage
|
2026-03-19 10:36:55 +08:00
|
|
|
|
from app.core.dependencies import get_llm_provider
|
|
|
|
|
|
from app.core.logging import get_logger
|
|
|
|
|
|
|
|
|
|
|
|
from app.agents.chat.helpers import format_history_string, get_history_messages
|
2026-03-31 23:55:26 +08:00
|
|
|
|
from app.agents.chat.personas import normalize_interview_persona
|
|
|
|
|
|
from app.agents.chat.interview_reply_length import compute_reply_plan
|
2026-03-19 10:54:48 +08:00
|
|
|
|
from app.agents.chat.prompts_conversation import (
|
|
|
|
|
|
SLOT_NAME_MAP,
|
|
|
|
|
|
get_guided_conversation_prompt,
|
|
|
|
|
|
get_opening_prompt,
|
|
|
|
|
|
)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
from app.agents.state_schema import MemoirStateSchema
|
2026-03-27 16:01:28 +08:00
|
|
|
|
from app.agents.chat.reply_limits import (
|
|
|
|
|
|
nonempty_segments_or_fallback,
|
|
|
|
|
|
segments_from_llm_response,
|
|
|
|
|
|
truncate_chat_segments,
|
|
|
|
|
|
)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
from app.core.agent_logging import (
|
|
|
|
|
|
agent_span,
|
|
|
|
|
|
log_agent_payload,
|
|
|
|
|
|
log_agent_summary,
|
|
|
|
|
|
)
|
|
|
|
|
|
from app.core.config import settings
|
2026-03-31 23:55:26 +08:00
|
|
|
|
from app.features.conversation.input_normalize import normalize_chat_input_for_agent
|
2026-03-19 10:36:55 +08:00
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
2026-03-20 17:25:42 +08:00
|
|
|
|
# LLM 不可用或调用失败时对用户展示(不暴露异常细节、不触发 TTS)
|
|
|
|
|
|
_FALLBACK_REPLY = "刚才网络不太稳,没接上。你可以再说一遍,或稍后再试。"
|
|
|
|
|
|
|
2026-03-19 10:36:55 +08:00
|
|
|
|
|
|
|
|
|
|
def _get_langchain_llm():
|
|
|
|
|
|
try:
|
|
|
|
|
|
provider = get_llm_provider()
|
|
|
|
|
|
return getattr(provider, "langchain_llm", None)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InterviewAgent:
|
|
|
|
|
|
"""正式访谈 Specialist Agent"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
self.llm = _get_langchain_llm()
|
|
|
|
|
|
|
|
|
|
|
|
def _detect_user_stage(self, user_message: str) -> str:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
"""关键词回退:与 stage_detection 一致(多阶段打分)。"""
|
|
|
|
|
|
return keyword_fallback_primary_stage(user_message)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
|
|
|
|
|
|
def _estimate_same_topic_turns(
|
|
|
|
|
|
self, history_messages: List[Any], current_filled_slots: dict
|
|
|
|
|
|
) -> int:
|
2026-03-31 23:55:26 +08:00
|
|
|
|
"""估算同一话题的连续轮数(保守:宁可多陪聊几轮再换)。"""
|
|
|
|
|
|
n_pairs = len(history_messages) // 2
|
|
|
|
|
|
if n_pairs <= 1:
|
|
|
|
|
|
return n_pairs
|
|
|
|
|
|
recent_window = min(n_pairs, 5)
|
|
|
|
|
|
recent = history_messages[-(recent_window * 2) :]
|
|
|
|
|
|
nonempty_user_turns = 0
|
|
|
|
|
|
for i in range(0, len(recent), 2):
|
|
|
|
|
|
msg = recent[i]
|
|
|
|
|
|
text = msg.content if hasattr(msg, "content") else str(msg)
|
|
|
|
|
|
if len(text.strip()) > 5:
|
|
|
|
|
|
nonempty_user_turns += 1
|
|
|
|
|
|
return nonempty_user_turns
|
|
|
|
|
|
|
|
|
|
|
|
def _resolve_text_for_model(
|
|
|
|
|
|
self,
|
|
|
|
|
|
user_message: str,
|
|
|
|
|
|
normalized_user_message: Optional[str],
|
|
|
|
|
|
) -> str:
|
|
|
|
|
|
"""模型侧净稿:编排层已归一则直接用;否则在本层补一次(含可选 LLM)。"""
|
|
|
|
|
|
if normalized_user_message is not None:
|
|
|
|
|
|
return (normalized_user_message or "").strip()
|
|
|
|
|
|
llm_n = None
|
|
|
|
|
|
if settings.chat_input_normalize_enabled and (
|
|
|
|
|
|
(settings.chat_input_normalize_mode or "").strip().lower() == "llm"
|
|
|
|
|
|
):
|
|
|
|
|
|
llm_n = self.llm
|
|
|
|
|
|
return normalize_chat_input_for_agent(user_message or "", llm=llm_n)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
|
|
|
|
|
|
async def generate_response_with_state(
|
|
|
|
|
|
self,
|
|
|
|
|
|
conversation_id: str,
|
|
|
|
|
|
user_message: str,
|
|
|
|
|
|
memoir_state: MemoirStateSchema,
|
|
|
|
|
|
user_profile_context: str = "",
|
2026-03-26 12:13:36 +08:00
|
|
|
|
detected_user_stage: Optional[str] = None,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
memory_evidence_text: str = "",
|
|
|
|
|
|
background_voice: str = "default",
|
|
|
|
|
|
normalized_user_message: Optional[str] = None,
|
2026-04-01 11:49:33 +08:00
|
|
|
|
occupation: str = "",
|
2026-03-20 17:25:42 +08:00
|
|
|
|
) -> AgentChatTurn:
|
2026-03-19 10:36:55 +08:00
|
|
|
|
"""生成状态感知的访谈回复,不持久化(由 Orchestrator 负责)"""
|
|
|
|
|
|
if not self.llm:
|
2026-03-20 17:25:42 +08:00
|
|
|
|
logger.warning("InterviewAgent: LLM 未配置,返回兜底文案")
|
|
|
|
|
|
return AgentChatTurn(messages=[_FALLBACK_REPLY], skip_tts=True)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
try:
|
2026-03-31 23:55:26 +08:00
|
|
|
|
text_for_model = self._resolve_text_for_model(
|
|
|
|
|
|
user_message, normalized_user_message
|
|
|
|
|
|
)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
empty_slots = memoir_state.empty_slots_for_current_stage()
|
|
|
|
|
|
filled_slots = {
|
|
|
|
|
|
key: value.snippet
|
2026-03-19 14:36:14 +08:00
|
|
|
|
for key, value in memoir_state.slots.get(
|
|
|
|
|
|
memoir_state.current_stage, {}
|
|
|
|
|
|
).items()
|
2026-03-19 10:36:55 +08:00
|
|
|
|
if value.snippet
|
|
|
|
|
|
}
|
2026-03-26 12:13:36 +08:00
|
|
|
|
if detected_user_stage is not None:
|
|
|
|
|
|
du = detected_user_stage
|
|
|
|
|
|
else:
|
2026-03-31 23:55:26 +08:00
|
|
|
|
du = self._detect_user_stage(text_for_model)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
history_messages = await get_history_messages(conversation_id)
|
|
|
|
|
|
conversation_turn = len(history_messages) // 2
|
|
|
|
|
|
same_topic_turns = self._estimate_same_topic_turns(
|
|
|
|
|
|
history_messages, filled_slots
|
|
|
|
|
|
)
|
|
|
|
|
|
all_stages_coverage = memoir_state.all_stages_coverage()
|
2026-03-31 23:55:26 +08:00
|
|
|
|
persona = normalize_interview_persona(settings.chat_interview_persona)
|
|
|
|
|
|
reply_plan = compute_reply_plan(
|
|
|
|
|
|
text_for_model,
|
|
|
|
|
|
background_voice=background_voice,
|
|
|
|
|
|
settings=settings,
|
|
|
|
|
|
)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
system_prompt = get_guided_conversation_prompt(
|
|
|
|
|
|
current_stage=memoir_state.current_stage,
|
|
|
|
|
|
empty_slots=empty_slots,
|
|
|
|
|
|
filled_slots=filled_slots,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
user_message=text_for_model,
|
2026-03-19 10:36:55 +08:00
|
|
|
|
conversation_turn=conversation_turn,
|
|
|
|
|
|
same_topic_turns=same_topic_turns,
|
|
|
|
|
|
all_stages_coverage=all_stages_coverage,
|
2026-03-26 12:13:36 +08:00
|
|
|
|
detected_user_stage=du,
|
2026-03-19 10:36:55 +08:00
|
|
|
|
user_profile_context=user_profile_context,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
persona=persona,
|
|
|
|
|
|
memory_evidence_text=memory_evidence_text,
|
|
|
|
|
|
reply_length_mode=reply_plan.mode.value,
|
|
|
|
|
|
background_voice=background_voice,
|
2026-04-01 11:49:33 +08:00
|
|
|
|
occupation=occupation,
|
2026-03-19 10:36:55 +08:00
|
|
|
|
)
|
|
|
|
|
|
history_string = format_history_string(history_messages)
|
2026-03-31 23:55:26 +08:00
|
|
|
|
full_prompt = f"{system_prompt}\n\n{history_string}\n\nHuman: {text_for_model}\n\nAssistant:"
|
2026-03-26 12:13:36 +08:00
|
|
|
|
log_agent_payload(
|
|
|
|
|
|
logger, "InterviewAgent.generate_response.prompt", full_prompt
|
|
|
|
|
|
)
|
2026-03-31 23:55:26 +08:00
|
|
|
|
chat_llm = self.llm.bind(max_tokens=reply_plan.max_tokens)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
with agent_span(
|
|
|
|
|
|
logger,
|
|
|
|
|
|
"InterviewAgent.generate_response.llm",
|
|
|
|
|
|
conversation_id=conversation_id,
|
|
|
|
|
|
stage=memoir_state.current_stage,
|
|
|
|
|
|
):
|
|
|
|
|
|
response = await chat_llm.ainvoke(full_prompt)
|
2026-03-19 14:36:14 +08:00
|
|
|
|
response_text = (
|
|
|
|
|
|
response.content if hasattr(response, "content") else str(response)
|
|
|
|
|
|
)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
log_agent_payload(
|
|
|
|
|
|
logger, "InterviewAgent.generate_response.raw_response", response_text
|
|
|
|
|
|
)
|
2026-03-27 16:01:28 +08:00
|
|
|
|
raw_list = segments_from_llm_response(
|
|
|
|
|
|
response_text,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
max_segments=reply_plan.max_segments,
|
2026-03-27 16:01:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
if not raw_list:
|
|
|
|
|
|
raw_list = [response_text.strip()]
|
2026-03-26 12:13:36 +08:00
|
|
|
|
out = truncate_chat_segments(
|
|
|
|
|
|
raw_list,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
max_segments=reply_plan.max_segments,
|
|
|
|
|
|
max_chars_per_segment=reply_plan.max_chars_per_segment,
|
2026-03-26 12:13:36 +08:00
|
|
|
|
)
|
|
|
|
|
|
if not out:
|
2026-03-31 23:55:26 +08:00
|
|
|
|
out = [response_text.strip()[: reply_plan.max_chars_per_segment]]
|
2026-03-27 16:01:28 +08:00
|
|
|
|
out = nonempty_segments_or_fallback(out, fallback=_FALLBACK_REPLY)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
log_agent_summary(
|
|
|
|
|
|
logger,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
"InterviewAgent.generate_response segments={} conversation_id={} "
|
|
|
|
|
|
"reply_length_mode={} max_tokens={}",
|
2026-03-26 12:13:36 +08:00
|
|
|
|
len(out),
|
|
|
|
|
|
conversation_id,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
reply_plan.mode.value,
|
|
|
|
|
|
reply_plan.max_tokens,
|
2026-03-26 12:13:36 +08:00
|
|
|
|
)
|
2026-03-20 17:25:42 +08:00
|
|
|
|
return AgentChatTurn(messages=out, skip_tts=False)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
except Exception as e:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("生成回应失败: {}", e, exc_info=True)
|
2026-03-20 17:25:42 +08:00
|
|
|
|
return AgentChatTurn(messages=[_FALLBACK_REPLY], skip_tts=True)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
|
|
|
|
|
|
async def generate_opening_message(
|
|
|
|
|
|
self,
|
|
|
|
|
|
conversation_id: str,
|
|
|
|
|
|
memoir_state: MemoirStateSchema,
|
|
|
|
|
|
user_profile_context: str = "",
|
2026-03-31 23:55:26 +08:00
|
|
|
|
background_voice: str = "default",
|
2026-04-01 11:49:33 +08:00
|
|
|
|
occupation: str = "",
|
2026-03-19 10:36:55 +08:00
|
|
|
|
) -> List[str]:
|
|
|
|
|
|
"""生成空对话开场白,不持久化(由 Orchestrator 负责)"""
|
|
|
|
|
|
if not self.llm:
|
2026-03-20 15:15:35 +08:00
|
|
|
|
return ["你好呀~ 又见面了,今天有没有哪段回忆或近况想聊聊?"]
|
2026-03-19 10:36:55 +08:00
|
|
|
|
try:
|
|
|
|
|
|
empty_slots = memoir_state.empty_slots_for_current_stage()
|
|
|
|
|
|
empty_slots_readable = [SLOT_NAME_MAP.get(s, s) for s in empty_slots]
|
2026-03-31 23:55:26 +08:00
|
|
|
|
persona = normalize_interview_persona(settings.chat_interview_persona)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
prompt = get_opening_prompt(
|
|
|
|
|
|
current_stage=memoir_state.current_stage,
|
|
|
|
|
|
empty_slots_readable=empty_slots_readable,
|
|
|
|
|
|
user_profile_context=user_profile_context,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
persona=persona,
|
|
|
|
|
|
background_voice=background_voice,
|
2026-04-01 11:49:33 +08:00
|
|
|
|
occupation=occupation,
|
2026-03-19 10:36:55 +08:00
|
|
|
|
)
|
|
|
|
|
|
full_prompt = f"{prompt}\n\nAssistant:"
|
2026-03-26 12:13:36 +08:00
|
|
|
|
log_agent_payload(logger, "InterviewAgent.opening.prompt", full_prompt)
|
|
|
|
|
|
opening_llm = self.llm.bind(max_tokens=settings.chat_opening_max_tokens)
|
|
|
|
|
|
with agent_span(
|
|
|
|
|
|
logger,
|
|
|
|
|
|
"InterviewAgent.opening.llm",
|
|
|
|
|
|
conversation_id=conversation_id,
|
|
|
|
|
|
):
|
|
|
|
|
|
response = await opening_llm.ainvoke(full_prompt)
|
2026-03-19 14:36:14 +08:00
|
|
|
|
response_text = (
|
|
|
|
|
|
response.content if hasattr(response, "content") else str(response)
|
|
|
|
|
|
)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
log_agent_payload(
|
|
|
|
|
|
logger, "InterviewAgent.opening.raw_response", response_text
|
|
|
|
|
|
)
|
2026-03-27 16:01:28 +08:00
|
|
|
|
raw_list = segments_from_llm_response(response_text, max_segments=2)
|
|
|
|
|
|
if not raw_list:
|
|
|
|
|
|
raw_list = [response_text.strip()]
|
2026-03-31 23:55:26 +08:00
|
|
|
|
open_plan = compute_reply_plan(
|
|
|
|
|
|
"x" * 50,
|
|
|
|
|
|
background_voice=background_voice,
|
|
|
|
|
|
settings=settings,
|
|
|
|
|
|
)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
out = truncate_chat_segments(
|
|
|
|
|
|
raw_list,
|
|
|
|
|
|
max_segments=2,
|
2026-03-31 23:55:26 +08:00
|
|
|
|
max_chars_per_segment=open_plan.max_chars_per_segment,
|
2026-03-26 12:13:36 +08:00
|
|
|
|
)
|
|
|
|
|
|
log_agent_summary(
|
|
|
|
|
|
logger,
|
|
|
|
|
|
"InterviewAgent.opening segments={} conversation_id={}",
|
|
|
|
|
|
len(out),
|
|
|
|
|
|
conversation_id,
|
|
|
|
|
|
)
|
2026-03-27 16:01:28 +08:00
|
|
|
|
segments = (
|
2026-03-26 12:13:36 +08:00
|
|
|
|
out
|
|
|
|
|
|
if out
|
2026-03-31 23:55:26 +08:00
|
|
|
|
else [response_text.strip()[: open_plan.max_chars_per_segment]]
|
2026-03-26 12:13:36 +08:00
|
|
|
|
)
|
2026-03-27 16:01:28 +08:00
|
|
|
|
return nonempty_segments_or_fallback(
|
|
|
|
|
|
segments,
|
|
|
|
|
|
fallback="你好呀~ 又见面了,最近有没有什么事想跟我说说?",
|
|
|
|
|
|
)
|
2026-03-19 10:36:55 +08:00
|
|
|
|
except Exception as e:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("生成开场白失败: {}", e, exc_info=True)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
return ["你好呀~ 又见面了,最近有没有什么事想跟我说说?"]
|