Spaces:
Sleeping
Sleeping
Download agentic_rag/agent_flow.py from Datamind23/AI_Research_Assis: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/Datamind23/AI_Research_Assis/resolve/main/agentic_rag/agent_flow.py
- Command line
-
hf download hf://spaces/Datamind23/AI_Research_Assis/agentic_rag/agent_flow.py
-
curl -L -o agent_flow.py https://huggingface.co/spaces/Datamind23/AI_Research_Assis/resolve/main/agentic_rag/agent_flow.py
2.67 kB
| from langgraph.graph import StateGraph, START, END | |
| from agentic_rag.tools_agent import ( | |
| agent_executor, | |
| search_web, | |
| generate_answer, | |
| ) | |
| from agentic_rag.state import MyState | |
| from agentic_rag.refinement import refined_query_generator | |
| # --- Query refinement node --- | |
| def refinement_node(state: MyState): | |
| return refined_query_generator(state) | |
| def agentic_rag_node(state: MyState): | |
| """ | |
| A node that uses the tool-enabled agent to answer the user query, | |
| falling back to web search if vector-DB relevance is low. | |
| """ | |
| # 1) Normalize & build the prompt for your tool-calling agent | |
| normalized_query = state["query"].replace("-", " ").strip() | |
| prompt_text = ( | |
| "You are a Retrieval-Augmented Generation (RAG) agent.\n" | |
| "Use the tools to:\n" | |
| "- Refine the query,\n" | |
| "- Retrieve relevant documents from the vector DB,\n" | |
| "- If the knowledge base does not have enough info (relevance < 0.5), use `search_web`,\n" | |
| "- Finally, generate an answer.\n\n" | |
| f"User query: {normalized_query}" | |
| ) | |
| # 2) Invoke the agent | |
| result = agent_executor.invoke({"input": prompt_text}) | |
| # 3) Pull out the relevance score | |
| score = 0.0 | |
| for action, observation in result.get("intermediate_steps", []): | |
| if getattr(action, "tool", None) == "score_relevance": | |
| try: | |
| score = float(observation) | |
| except: | |
| score = 0.0 | |
| # βββ 4) FALLBACK: If internal score is low, go to web search ββββββββββββββββββββ | |
| if score < 0.5: | |
| raw_snippets = search_web.invoke({"query": normalized_query}) | |
| direct_answer = generate_answer.invoke({ | |
| "query": normalized_query, | |
| "document": raw_snippets, | |
| }) | |
| return { | |
| "response": direct_answer, | |
| "refined_query": state.get("refined_query", ""), | |
| "score": score, | |
| "used_fallback": True, | |
| } | |
| # βββ 5) Otherwise, return the agentβs original RAG answer ββββββββββββββββββββββββ | |
| return { | |
| "response": result.get("output", ""), | |
| "refined_query": state.get("refined_query", ""), | |
| "score": score, | |
| "used_fallback": False, | |
| } | |
| # Build the StateGraph for the workflow | |
| graph = StateGraph(MyState) | |
| graph.add_node("refinement", refinement_node) | |
| graph.add_node("agentic", agentic_rag_node) | |
| graph.add_edge(START, "refinement") | |
| graph.add_edge("refinement", "agentic") | |
| graph.add_edge("agentic", END) | |
| flow = graph.compile() | |
| print(flow) | |