from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage from .state import AgentState from services.prompt_manager import PromptManager from services.template_matcher import TemplateMatcher def process_input(state: AgentState) -> AgentState: """处理用户输入""" state.current_step = "processed" return state def generate_response(state: AgentState, model) -> AgentState: """使用 LLM 生成回复""" if state.messages: response = model.invoke(state.messages) state.messages.append(response) return state def normalize_input(state: AgentState, model) -> AgentState: """将用户输入规范化为标准英文语句""" if not state.messages: return state last_message = state.messages[-1] if not isinstance(last_message, HumanMessage): return state prompt_manager = PromptManager() system_prompt = SystemMessage( content=prompt_manager.get("system", "english_normalizer") ) response = model.invoke([system_prompt, HumanMessage(content=last_message.content)]) normalized = response.content if hasattr(response, "content") else str(response) state.context["original_input"] = last_message.content state.context["normalized_input"] = normalized matcher = TemplateMatcher() state.context["table_match"] = matcher.match(normalized) return state