Download app/agent/nodes.py from acpcchatbotadmin/acpcchatbot_testing_first: direct link, hf CLI and curl.
- Browser
- Download file 7.75 kB
-
https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/agent/nodes.py
- Command line
-
hf download hf://spaces/acpcchatbotadmin/acpcchatbot_testing_first/app/agent/nodes.py
-
curl -L -o nodes.py https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/agent/nodes.py
7.75 kB
| """ | |
| LangGraph node functions for the Agentic RAG pipeline. | |
| Each node receives AgentState and returns a partial state update. | |
| """ | |
| import json | |
| import logging | |
| from langchain_core.documents import Document | |
| from langchain_core.messages import HumanMessage, AIMessage, SystemMessage | |
| from langchain_openai import ChatOpenAI | |
| from app.agent.state import AgentState | |
| from app.agent.prompts import ( | |
| SYSTEM_PROMPT, | |
| INTENT_CLASSIFICATION_PROMPT, | |
| RELEVANCE_GRADING_PROMPT, | |
| HALLUCINATION_CHECK_PROMPT, | |
| QUERY_REWRITE_PROMPT, | |
| GENERATION_PROMPT, | |
| ) | |
| from app.retrieval.retriever import retrieve_and_rerank, format_sources | |
| from app.config import settings | |
| logger = logging.getLogger(__name__) | |
| def _get_llm(temperature: float = 0.0) -> ChatOpenAI: | |
| return ChatOpenAI( | |
| model=settings.llm_model, | |
| api_key=settings.openai_api_key, | |
| base_url=settings.openai_api_base, | |
| temperature=temperature, | |
| default_headers={ | |
| "HTTP-Referer": "https://acpc.gujarat.gov.in", | |
| "X-Title": "ACPC Chatbot", | |
| }, | |
| ) | |
| def classify_intent(state: AgentState) -> dict: | |
| """Node: Classify whether the query is ACPC-related, off-topic, or injection.""" | |
| query = state["query"] | |
| llm = _get_llm(temperature=0.0) | |
| prompt = INTENT_CLASSIFICATION_PROMPT.format(query=query) | |
| response = llm.invoke([HumanMessage(content=prompt)]) | |
| classification = response.content.strip().upper() | |
| if classification not in ("ACPC_RELATED", "OFF_TOPIC", "INJECTION"): | |
| # Default to ACPC_RELATED if response is unexpected | |
| classification = "ACPC_RELATED" | |
| logger.info("Intent classification: %s for query: %s", classification, query[:80]) | |
| if classification in ("OFF_TOPIC", "INJECTION"): | |
| return {"route": "fallback"} | |
| return {"route": "retrieve"} | |
| def retrieve(state: AgentState) -> dict: | |
| """Node: Retrieve documents from Qdrant using hybrid search + Cohere rerank.""" | |
| query = state["query"] | |
| logger.info("Retrieving documents for query: %s", query[:80]) | |
| documents = retrieve_and_rerank(query) | |
| logger.info("Retrieved %d documents", len(documents)) | |
| return {"retrieved_documents": documents} | |
| def grade_documents(state: AgentState) -> dict: | |
| """Node: Grade each retrieved document for relevance to the query.""" | |
| query = state["query"] | |
| documents = state["retrieved_documents"] | |
| if not documents: | |
| return {"relevant_documents": [], "confidence": 0.0} | |
| llm = _get_llm(temperature=0.0) | |
| relevant = [] | |
| for doc in documents: | |
| prompt = RELEVANCE_GRADING_PROMPT.format( | |
| document=doc.page_content[:1000], | |
| question=query, | |
| ) | |
| try: | |
| response = llm.invoke([HumanMessage(content=prompt)]) | |
| result = json.loads(response.content.strip()) | |
| if result.get("relevant") == "yes": | |
| relevant.append(doc) | |
| except (json.JSONDecodeError, Exception) as e: | |
| logger.warning("Grading error for doc: %s", e) | |
| # On error, include the document (fail open for retrieval) | |
| relevant.append(doc) | |
| confidence = len(relevant) / len(documents) if documents else 0.0 | |
| logger.info("Grading complete: %d/%d relevant (confidence=%.2f)", len(relevant), len(documents), confidence) | |
| return {"relevant_documents": relevant, "confidence": confidence} | |
| def rewrite_query(state: AgentState) -> dict: | |
| """Node: Rewrite the query for better retrieval and increment retry count.""" | |
| original_query = state["query"] | |
| llm = _get_llm(temperature=0.3) | |
| prompt = QUERY_REWRITE_PROMPT.format(query=original_query) | |
| response = llm.invoke([HumanMessage(content=prompt)]) | |
| rewritten = response.content.strip() | |
| retry_count = state.get("retry_count", 0) + 1 | |
| logger.info("Query rewritten (retry %d): '%s' -> '%s'", retry_count, original_query[:60], rewritten[:60]) | |
| return {"query": rewritten, "retry_count": retry_count} | |
| def generate(state: AgentState) -> dict: | |
| """Node: Generate an answer from relevant documents using gpt-4o.""" | |
| query = state["query"] | |
| relevant_docs = state["relevant_documents"] | |
| history = state.get("messages", []) | |
| # Build context string from relevant documents | |
| context_parts = [] | |
| for i, doc in enumerate(relevant_docs): | |
| meta = doc.metadata or {} | |
| url = meta.get("url", "") | |
| source_line = f"[Source {i+1}: {meta.get('file_name', 'unknown')}]" | |
| if url: | |
| source_line += f" {url}" | |
| context_parts.append(f"{source_line}\n{doc.page_content}") | |
| context = "\n\n---\n\n".join(context_parts) | |
| generation_content = GENERATION_PROMPT.format(context=context, question=query) | |
| # Build messages: system + trimmed history + current generation prompt | |
| messages = [SystemMessage(content=SYSTEM_PROMPT)] | |
| # Add conversation history (excluding the current user message, already in query) | |
| for msg in history[:-1]: # Skip last (current user message) | |
| messages.append(msg) | |
| messages.append(HumanMessage(content=generation_content)) | |
| llm = _get_llm(temperature=0.1) | |
| response = llm.invoke(messages) | |
| generation = response.content.strip() | |
| sources = format_sources(relevant_docs) | |
| logger.info("Generation complete (%d chars, %d sources)", len(generation), len(sources)) | |
| return {"generation": generation, "sources": sources} | |
| def check_hallucination(state: AgentState) -> dict: | |
| """Node: Verify that the generated answer is grounded in the retrieved context.""" | |
| generation = state["generation"] | |
| relevant_docs = state["relevant_documents"] | |
| context = "\n\n".join(doc.page_content[:800] for doc in relevant_docs) | |
| prompt = HALLUCINATION_CHECK_PROMPT.format(documents=context, answer=generation) | |
| llm = _get_llm(temperature=0.0) | |
| try: | |
| response = llm.invoke([HumanMessage(content=prompt)]) | |
| result = json.loads(response.content.strip()) | |
| is_grounded = result.get("grounded", True) | |
| except (json.JSONDecodeError, Exception) as e: | |
| logger.warning("Hallucination check error: %s — defaulting to grounded=True", e) | |
| is_grounded = True | |
| if is_grounded: | |
| logger.info("Hallucination check passed") | |
| return {"route": "grounded"} | |
| else: | |
| logger.warning("Hallucination check FAILED — routing to fallback") | |
| return {"route": "not_grounded"} | |
| def fallback(state: AgentState) -> dict: | |
| """Node: Return the hardcoded fallback response (never LLM-generated).""" | |
| return { | |
| "generation": settings.fallback_response, | |
| "sources": [], | |
| "confidence": 0.0, | |
| } | |
| def respond(state: AgentState) -> dict: | |
| """Terminal node: Package the final response into messages.""" | |
| generation = state.get("generation", settings.fallback_response) | |
| return {"messages": [AIMessage(content=generation)]} | |
| # ---- Edge condition functions ---- | |
| def route_after_classify(state: AgentState) -> str: | |
| return state.get("route", "retrieve") | |
| def route_after_grade(state: AgentState) -> str: | |
| relevant = state.get("relevant_documents", []) | |
| retry_count = state.get("retry_count", 0) | |
| confidence = state.get("confidence", 0.0) | |
| if relevant and confidence >= settings.confidence_threshold: | |
| return "generate" | |
| elif retry_count < settings.max_retrieval_retries: | |
| return "rewrite" | |
| else: | |
| return "fallback" | |
| def route_after_hallucination(state: AgentState) -> str: | |
| return "respond" if state.get("route") == "grounded" else "fallback" | |