None of the prompts that embed the raw user message (classify_intent, direct_response, synthesize_response, and the RAG grade/rewrite/generate steps) distinguished user-typed content from instructions to the model. Wrap the user message in a <user_message> tag (app/llm.py::wrap_user_input, added in the previous commit) and prepend PROMPT_INJECTION_GUARD, an explicit instruction telling the model to treat that block strictly as data, never as commands - even if it claims system/developer/admin authority. wrap_user_input also strips any literal occurrence of the tag from the input so a crafted message can't fake a closing tag and escape the wrapper. This is a meaningful mitigation, not a hard guarantee - documented as such in the README's "Known limitations" section. Refs #15
317 lines
12 KiB
Python
317 lines
12 KiB
Python
"""Main agentic workflow.
|
|
|
|
Six nodes (the dedicated RAG subgraph in app/rag/subgraph.py is separate and not counted
|
|
here):
|
|
|
|
classify_intent - entry point; LLM decomposes the query into an ordered task queue
|
|
(autonomous decision + subtask decomposition)
|
|
direct_response - short-circuit for greetings / out-of-scope messages
|
|
rag_answer - delegates to the RAG subgraph; auto-escalates to a ticket on
|
|
low-confidence answers (autonomous decision)
|
|
calculate_leave - deterministic PTO calculator tool
|
|
create_ticket - support-ticket tool (user-requested or auto-escalated)
|
|
synthesize_response - combines whatever sub-task results were collected into one answer
|
|
|
|
State (intermediate results, task queue, and a full execution trace) is threaded through
|
|
every node via `AgentState` - see app/state.py.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from langgraph.graph import END, StateGraph
|
|
|
|
from app.llm import PROMPT_INJECTION_GUARD, get_chat_llm, structured_json_call, wrap_user_input
|
|
from app.rag.subgraph import build_rag_subgraph, run_rag
|
|
from app.rag.vectorstore import load_or_build_index
|
|
from app.state import AgentState
|
|
from app.tools.calculator import compute_leave_balance
|
|
from app.tools.ticketing import create_ticket_record
|
|
from app.tracing import trace_step as _trace
|
|
|
|
TASK_NODE_MAP = {
|
|
"policy_question": "rag_answer",
|
|
"leave_balance": "calculate_leave",
|
|
"create_ticket": "create_ticket",
|
|
}
|
|
|
|
_ROUTE_TARGETS = {node: node for node in set(TASK_NODE_MAP.values()) | {"synthesize_response"}}
|
|
|
|
_CLASSIFY_PROMPT = """You are the routing brain of an internal company-policy assistant. \
|
|
Decompose the user's message into an ORDERED list of task types drawn from:
|
|
- "policy_question": needs to look up a RULE or GENERAL FACT in internal HR/IT/Finance/Legal \
|
|
policy docs (e.g. "how many days can I carry over", "what's the password policy")
|
|
- "leave_balance": the user gave a SPECIFIC hire date AND a specific number of days already \
|
|
taken, and wants their own remaining balance computed. Do NOT use this for general questions \
|
|
about the PTO policy/rules - only when the message itself states a real hire date and a real \
|
|
days-taken number.
|
|
- "create_ticket": the user explicitly wants to open a support/HR/IT ticket
|
|
|
|
If the message is a greeting, small talk, or unrelated to company policy/HR/IT (e.g. weather, \
|
|
jokes, general trivia, anything with no plausible connection to an internal company policy \
|
|
document), return an empty task list. Do NOT force-fit an unrelated question into \
|
|
"policy_question" just because some policy document uses similar words.
|
|
|
|
Respond with ONLY a JSON object of this shape:
|
|
{{
|
|
"tasks": ["policy_question"],
|
|
"rag_query": "self-contained question to search policy docs for, or empty string",
|
|
"calc_args": {{"hire_date": "2022-03-10", "days_taken": 4, "as_of_date": "2024-03-10"}},
|
|
"ticket_args": {{"category": "short_snake_case_category", "summary": "...", "priority": "low|normal|high"}}
|
|
}}
|
|
Only include "calc_args" at all if "leave_balance" is in tasks AND the message gave a real \
|
|
hire date and days-taken number (use those exact values, never placeholders). Only include \
|
|
"ticket_args" if "create_ticket" is in tasks. A single message can contain multiple tasks.
|
|
|
|
Example - pure policy question, no personal dates given: \
|
|
{{"tasks": ["policy_question"], "rag_query": "PTO carryover limit"}}
|
|
|
|
Example - unrelated small talk, must NOT be routed anywhere: \
|
|
{{"tasks": []}} (for a message like "What's the weather like today?" or "tell me a joke")
|
|
|
|
{guard}
|
|
|
|
User message:
|
|
{query}
|
|
"""
|
|
|
|
_DIRECT_RESPONSE_PROMPT = """This assistant only answers questions about internal company \
|
|
policy (HR, IT, Finance, Legal), calculates PTO/vacation balances, and opens support \
|
|
tickets. Reply briefly (1-3 sentences) and politely: greet back if it's a greeting, \
|
|
otherwise explain you can't help with that and suggest what you *can* help with.
|
|
|
|
{guard}
|
|
|
|
The user said:
|
|
{query}
|
|
"""
|
|
|
|
_SYNTHESIZE_PROMPT = """Combine the following internal results into ONE clear, friendly \
|
|
answer for the employee who asked the original question below. When citing a policy, \
|
|
mention the source filename in parentheses exactly as given, e.g. "(pto_policy.md)" - do \
|
|
NOT invent a URL or hyperlink for it, these are internal filenames only. Preserve any \
|
|
ticket IDs mentioned in the results. Do not invent facts that are not in the results.
|
|
|
|
{guard}
|
|
|
|
Original question:
|
|
{query}
|
|
|
|
Results:
|
|
{context}
|
|
"""
|
|
|
|
|
|
def _classify_intent(llm):
|
|
def node(state: AgentState) -> dict:
|
|
start = time.time()
|
|
default = {
|
|
"tasks": ["policy_question"],
|
|
"rag_query": state["user_query"],
|
|
"calc_args": {},
|
|
"ticket_args": {},
|
|
}
|
|
prompt = _CLASSIFY_PROMPT.format(
|
|
guard=PROMPT_INJECTION_GUARD, query=wrap_user_input(state["user_query"])
|
|
)
|
|
result = structured_json_call(llm, prompt, default)
|
|
tasks = [t for t in result.get("tasks", []) if t in TASK_NODE_MAP]
|
|
update = {
|
|
"pending_tasks": tasks,
|
|
"rag_query": result.get("rag_query") or state["user_query"],
|
|
"calc_args": result.get("calc_args", {}) or {},
|
|
"ticket_args": result.get("ticket_args", {}) or {},
|
|
"results": {},
|
|
"escalate": False,
|
|
}
|
|
update.update(_trace(state, "classify_intent", f"tasks={tasks}", start))
|
|
return update
|
|
|
|
return node
|
|
|
|
|
|
def _direct_response(llm):
|
|
def node(state: AgentState) -> dict:
|
|
start = time.time()
|
|
prompt = _DIRECT_RESPONSE_PROMPT.format(
|
|
guard=PROMPT_INJECTION_GUARD, query=wrap_user_input(state["user_query"])
|
|
)
|
|
response = llm.invoke(prompt)
|
|
answer = response.content if hasattr(response, "content") else str(response)
|
|
update = {"final_answer": answer}
|
|
update.update(_trace(state, "direct_response", "out-of-scope / greeting", start))
|
|
return update
|
|
|
|
return node
|
|
|
|
|
|
def _rag_answer(rag_subgraph):
|
|
def node(state: AgentState) -> dict:
|
|
start = time.time()
|
|
query = state.get("rag_query") or state["user_query"]
|
|
rag_result = run_rag(rag_subgraph, query)
|
|
|
|
results = dict(state.get("results", {}))
|
|
results["policy_question"] = {
|
|
"answer": rag_result.get("answer", ""),
|
|
"sources": rag_result.get("sources", []),
|
|
"confidence": rag_result.get("confidence", "low"),
|
|
}
|
|
|
|
remaining = list(state.get("pending_tasks", []))[1:]
|
|
escalate = False
|
|
escalate_reason = ""
|
|
ticket_args = state.get("ticket_args", {})
|
|
if rag_result.get("confidence") == "low" and "create_ticket" not in remaining:
|
|
remaining = remaining + ["create_ticket"]
|
|
escalate = True
|
|
escalate_reason = "RAG subgraph could not confidently answer the policy question."
|
|
ticket_args = {
|
|
"category": "general_policy_question",
|
|
"summary": f"Could not confidently answer policy question: {query}",
|
|
"priority": "normal",
|
|
}
|
|
|
|
sub_trace = [
|
|
{"node": f"rag.{t['node']}", "detail": t["detail"], "duration_ms": t["duration_ms"]}
|
|
for t in rag_result.get("trace", [])
|
|
]
|
|
|
|
update = {
|
|
"results": results,
|
|
"pending_tasks": remaining,
|
|
"escalate": escalate,
|
|
"escalate_reason": escalate_reason,
|
|
"ticket_args": ticket_args,
|
|
"trace": state.get("trace", []) + sub_trace,
|
|
}
|
|
update.update(_trace(update, "rag_answer", f"confidence={rag_result.get('confidence')}", start))
|
|
return update
|
|
|
|
return node
|
|
|
|
|
|
def _calculate_leave():
|
|
def node(state: AgentState) -> dict:
|
|
start = time.time()
|
|
args = state.get("calc_args") or {}
|
|
try:
|
|
data = compute_leave_balance(**args)
|
|
summary = (
|
|
f"Accrued {data['accrued_days']} days, already taken {data['days_taken']}, "
|
|
f"remaining {data['remaining_days']} days (as of {data['as_of_date']})."
|
|
)
|
|
error = None
|
|
except Exception as exc: # noqa: BLE001 - deliberately broad: surfaces to the user
|
|
data = None
|
|
error = str(exc)
|
|
summary = f"Could not compute leave balance: missing or invalid input ({error})."
|
|
|
|
results = dict(state.get("results", {}))
|
|
results["leave_balance"] = {"data": data, "summary": summary, "error": error}
|
|
remaining = list(state.get("pending_tasks", []))[1:]
|
|
update = {"results": results, "pending_tasks": remaining}
|
|
update.update(_trace(state, "calculate_leave", summary, start))
|
|
return update
|
|
|
|
return node
|
|
|
|
|
|
def _create_ticket():
|
|
def node(state: AgentState) -> dict:
|
|
start = time.time()
|
|
args = state.get("ticket_args") or {}
|
|
category = args.get("category", "general_inquiry")
|
|
summary = args.get("summary", state["user_query"])
|
|
priority = args.get("priority", "normal")
|
|
ticket = create_ticket_record(
|
|
category=category, summary=summary, user_query=state["user_query"], priority=priority
|
|
)
|
|
results = dict(state.get("results", {}))
|
|
results["create_ticket"] = ticket
|
|
remaining = list(state.get("pending_tasks", []))[1:]
|
|
update = {"results": results, "pending_tasks": remaining}
|
|
update.update(
|
|
_trace(state, "create_ticket", f"opened {ticket['ticket_id']} ({category})", start)
|
|
)
|
|
return update
|
|
|
|
return node
|
|
|
|
|
|
def _synthesize_response(llm):
|
|
def node(state: AgentState) -> dict:
|
|
start = time.time()
|
|
results = state.get("results", {})
|
|
parts = []
|
|
if "policy_question" in results:
|
|
r = results["policy_question"]
|
|
parts.append(
|
|
f"Policy answer (confidence={r['confidence']}, sources={r['sources']}):\n{r['answer']}"
|
|
)
|
|
if "leave_balance" in results:
|
|
parts.append(f"Leave balance calculation:\n{results['leave_balance']['summary']}")
|
|
if "create_ticket" in results:
|
|
t = results["create_ticket"]
|
|
parts.append(
|
|
f"Support ticket created: {t['ticket_id']} "
|
|
f"(category={t['category']}, priority={t['priority']}, status={t['status']})."
|
|
)
|
|
context = "\n\n".join(parts) if parts else "No sub-task results were produced."
|
|
|
|
response = llm.invoke(
|
|
_SYNTHESIZE_PROMPT.format(
|
|
guard=PROMPT_INJECTION_GUARD,
|
|
query=wrap_user_input(state["user_query"]),
|
|
context=context,
|
|
)
|
|
)
|
|
answer = response.content if hasattr(response, "content") else str(response)
|
|
update = {"final_answer": answer}
|
|
update.update(_trace(state, "synthesize_response", "combined final answer", start))
|
|
return update
|
|
|
|
return node
|
|
|
|
|
|
def _route_after_classify(state: AgentState) -> str:
|
|
tasks = state.get("pending_tasks") or []
|
|
if not tasks:
|
|
return "direct_response"
|
|
return TASK_NODE_MAP[tasks[0]]
|
|
|
|
|
|
def _route_dispatch(state: AgentState) -> str:
|
|
tasks = state.get("pending_tasks") or []
|
|
if tasks:
|
|
return TASK_NODE_MAP[tasks[0]]
|
|
return "synthesize_response"
|
|
|
|
|
|
def build_graph(llm=None, store=None):
|
|
"""Compile the main agent graph. Pass `llm`/`store` explicitly to inject fakes in tests."""
|
|
llm = llm or get_chat_llm()
|
|
store = store or load_or_build_index()
|
|
rag_subgraph = build_rag_subgraph(llm, store)
|
|
|
|
graph = StateGraph(AgentState)
|
|
graph.add_node("classify_intent", _classify_intent(llm))
|
|
graph.add_node("direct_response", _direct_response(llm))
|
|
graph.add_node("rag_answer", _rag_answer(rag_subgraph))
|
|
graph.add_node("calculate_leave", _calculate_leave())
|
|
graph.add_node("create_ticket", _create_ticket())
|
|
graph.add_node("synthesize_response", _synthesize_response(llm))
|
|
|
|
graph.set_entry_point("classify_intent")
|
|
graph.add_conditional_edges(
|
|
"classify_intent",
|
|
_route_after_classify,
|
|
{"direct_response": "direct_response", **_ROUTE_TARGETS},
|
|
)
|
|
for source in ("rag_answer", "calculate_leave", "create_ticket"):
|
|
graph.add_conditional_edges(source, _route_dispatch, _ROUTE_TARGETS)
|
|
|
|
graph.add_edge("direct_response", END)
|
|
graph.add_edge("synthesize_response", END)
|
|
|
|
return graph.compile()
|