File size: 9,124 Bytes
5bf211e be6b61f 5bf211e be6b61f 4e0f514 be6b61f 5bf211e 58a1fee be6b61f 5bf211e 4e0f514 5bf211e be6b61f 5bf211e be6b61f 5bf211e be6b61f 5bf211e 4e0f514 5bf211e be6b61f 4e0f514 58a1fee 4e0f514 be6b61f 5bf211e be6b61f 5bf211e be6b61f 5bf211e be6b61f 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e 58a1fee be6b61f 5bf211e be6b61f 4e0f514 5bf211e be6b61f 5bf211e 4e0f514 5bf211e be6b61f 5bf211e 4e0f514 be6b61f 58a1fee 4e0f514 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e be6b61f 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e 58a1fee be6b61f 5bf211e be6b61f 4e0f514 be6b61f 5bf211e be6b61f 5bf211e 4e0f514 5bf211e be6b61f 5bf211e be6b61f 5bf211e be6b61f 5bf211e be6b61f 5bf211e 4e0f514 5bf211e be6b61f 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e 4e0f514 5bf211e be6b61f 5bf211e be6b61f 5bf211e be6b61f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
import os
import time
from typing import TypedDict, List, Optional, Annotated, Literal
import google.generativeai as genai
from langgraph.graph import StateGraph, END
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph.message import add_messages
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage, SystemMessage
from tavily import TavilyClient
from rag_store import search_knowledge
from eval_logger import log_eval
from llm_utils import generate_with_retry
from sql_db import query_database
# Config
MODEL_FAST = "gemini-2.5-flash-lite"
MODEL_SMART = "gemini-3-flash-preview"
MAX_RETRIES = 2
# ===============================
# STATE
# ===============================
class AgentState(TypedDict):
messages: Annotated[List[BaseMessage], add_messages]
query: str
final_answer: str
# Internal routing & scratchpad
next_node: str
current_tool: str
tool_outputs: List[dict] # list of {source: 'pdf'|'web'|'sql', content: ..., score: ...}
verification_notes: str
retries: int
# ===============================
# TOOLS
# ===============================
def pdf_search_tool(query: str):
"""Searches internal PDF knowledge base."""
results = search_knowledge(query, top_k=4)
return [
{
"source": "internal_pdf",
"content": r["text"],
"metadata": r["metadata"],
"score": r.get("score", 0)
}
for r in results
]
def web_search_tool(query: str):
"""Searches the web using Tavily."""
api_key = os.getenv("TAVILY_API_KEY")
if not api_key:
return [{"source": "external_web", "content": "Error: TAVILY_API_KEY not found.", "score": 0}]
try:
tavily = TavilyClient(api_key=api_key)
context = tavily.get_search_context(query=query, search_depth="advanced")
return [{
"source": "external_web",
"content": context,
"score": 0.8
}]
except Exception as e:
return [{"source": "external_web", "content": f"Web search error: {str(e)}", "score": 0}]
def text_to_sql_tool(query: str):
"""Translates natural language to SQL and executes it."""
prompt = f"""
You are an expert SQL Translator.
Table: students
Columns: id, name, course, fees (real), enrollment_date (text), gpa (real)
Task: Convert this question to a READ-ONLY SQL query (SQLite).
Question: "{query}"
Rules:
- Output ONLY the SQL query. No markdown.
- Do NOT use Markdown formatting.
"""
model = genai.GenerativeModel(MODEL_SMART)
resp = generate_with_retry(model, prompt)
sql_query = resp.text.strip().replace("```sql", "").replace("```", "").strip() if resp else ""
if not sql_query:
return [{"source": "internal_sql", "content": "Error generating SQL.", "score": 0}]
result_text = query_database(sql_query)
return [{
"source": "internal_sql",
"content": f"Query: {sql_query}\nResult: {result_text}",
"score": 1.0
}]
# ===============================
# NODES
# ===============================
# 1. SUPERVISOR
def supervisor_node(state: AgentState):
"""Decides whether to research (and which tool) or answer."""
query = state["query"]
tools_out = state.get("tool_outputs", [])
prompt = f"""
You are a Supervisor Agent.
User Query: "{query}"
Gathered Info Count: {len(tools_out)}
Decide next step:
1. "research_sql": If the query asks about quantitative student data (fees, grades, counts, names in database).
2. "research_pdf": If the query asks about policies, documents, or general university info.
3. "research_web": If internal info is missing.
4. "responder": If enough info is gathered.
Return ONLY one of: research_sql, research_pdf, research_web, responder
"""
# Heuristic: If we already searched SQL and got results, maybe go to responder or PDF
# But for now, let LLM decide based on history.
model = genai.GenerativeModel(MODEL_FAST)
resp = generate_with_retry(model, prompt)
decision = resp.text.strip().lower() if resp else "responder"
if "sql" in decision: return {**state, "next_node": "research_sql"}
if "pdf" in decision: return {**state, "next_node": "research_pdf"}
if "web" in decision: return {**state, "next_node": "research_web"}
return {**state, "next_node": "responder"}
# 2. RESEARCHER (PDF)
def researcher_pdf_node(state: AgentState):
query = state["query"]
results = pdf_search_tool(query)
current_outputs = state.get("tool_outputs", []) + results
# Removed intermediate logging to focus on final evaluation
return {**state, "tool_outputs": current_outputs}
# 3. RESEARCHER (WEB)
def researcher_web_node(state: AgentState):
query = state["query"]
results = web_search_tool(query)
current_outputs = state.get("tool_outputs", []) + results
return {**state, "tool_outputs": current_outputs}
# 4. RESEARCHER (SQL)
def researcher_sql_node(state: AgentState):
query = state["query"]
results = text_to_sql_tool(query)
current_outputs = state.get("tool_outputs", []) + results
return {**state, "tool_outputs": current_outputs}
# 5. VERIFIER
def verifier_node(state: AgentState):
"""Verifies the quality of gathered information."""
query = state["query"]
tools_out = state.get("tool_outputs", [])
# Simple verification logic
context = ""
for t in tools_out:
context += f"\n[{t['source'].upper()}]: {t['content']}..."
prompt = f"""
You are a Verifier Agent.
User Query: "{query}"
Gathered Info:
{context}
Task:
Analyze the gathered information.
- Is it relevant to the query?
- Are there conflicts?
- What key details are present?
Provide concise verification notes for the Final Responder.
"""
model = genai.GenerativeModel(MODEL_SMART)
resp = generate_with_retry(model, prompt)
notes = resp.text if resp else "Verification completed."
return {**state, "verification_notes": notes}
# 6. RESPONDER
def responder_node(state: AgentState):
query = state["query"]
tools_out = state.get("tool_outputs", [])
notes = state.get("verification_notes", "")
if not tools_out and state["retries"] < 1:
# Self-correction
return {**state, "retries": state["retries"] + 1, "next_node": "supervisor"}
context_text_list = [t['content'] for t in tools_out]
context = ""
for t in tools_out:
context += f"\n[{t['source'].upper()}]: {t['content']}..."
prompt = f"""
You are the Final Responder.
User Query: {query}
Gathered Info:
{context}
Verification Notes:
{notes}
Answer the user query. If you used SQL, summarize the data insights.
"""
model = genai.GenerativeModel(MODEL_SMART)
resp = generate_with_retry(model, prompt)
answer = resp.text if resp else "I could not generate an answer."
# === NEW: LOG FULL EVALUATION DATA ===
# We log here because we have the Query, The Context, and The Final Answer
if tools_out:
log_eval(
query=query,
retrieved_count=len(tools_out),
confidence=0.9, # dynamic confidence is hard without prob, assuming high if we have tools
answer_known=True,
source_type="mixed",
final_answer=answer,
context_list=context_text_list
)
return {
**state,
"final_answer": answer,
"messages": [AIMessage(content=answer)],
"next_node": "end"
}
# ===============================
# GRAPH BUILDER
# ===============================
def build_agentic_rag_v2_graph():
graph = StateGraph(AgentState)
memory = MemorySaver()
graph.add_node("supervisor", supervisor_node)
graph.add_node("research_pdf", researcher_pdf_node)
graph.add_node("research_web", researcher_web_node)
graph.add_node("research_sql", researcher_sql_node)
graph.add_node("verifier", verifier_node)
graph.add_node("responder", responder_node)
graph.set_entry_point("supervisor")
# Routing
graph.add_conditional_edges(
"supervisor",
lambda s: s["next_node"],
{
"research_pdf": "research_pdf",
"research_web": "research_web",
"research_sql": "research_sql",
"responder": "responder"
}
)
# Edges returning to Supervisor
graph.add_edge("research_pdf", "supervisor")
graph.add_edge("research_sql", "supervisor")
# Web -> Verifier -> Supervisor
graph.add_edge("research_web", "verifier")
graph.add_edge("verifier", "supervisor")
graph.add_conditional_edges(
"responder",
lambda s: "supervisor" if s["next_node"] == "supervisor" else "end",
{
"supervisor": "supervisor",
"end": END
}
)
return graph.compile(checkpointer=memory)
|