from __future__ import annotations import json from langchain_core.language_models.chat_models import BaseChatModel from langchain_core.messages import HumanMessage, SystemMessage from graph.state import AgentState from prompts.templates import ANSWER_PROMPT def generate_answer(state: AgentState, model: BaseChatModel) -> dict: evidence = json.dumps(state.get("evidence", []), ensure_ascii=False, indent=2)[:30000] prompt = ANSWER_PROMPT.format(question=state["question"], evidence=evidence) try: response = model.invoke([SystemMessage(content=prompt), HumanMessage(content=state["question"])]) answer = str(response.content).strip() return {"draft_answer": answer, "error": ""} except Exception as exc: return {"draft_answer": "", "error": f"Answer generation failed: {exc}"}