rsobieski commited on
Commit
0ea9e1b
·
verified ·
1 Parent(s): afa8be7

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +27 -39
agent.py CHANGED
@@ -1,75 +1,63 @@
1
- """LangGraph agent using retriever fallback with LLM + tools."""
2
  import os
3
- from langgraph.graph import StateGraph, MessagesState
4
- from langgraph.prebuilt import ToolNode, tools_condition
5
  from langchain_core.messages import HumanMessage, AIMessage
6
- from langchain_google_genai import ChatGoogleGenerativeAI
7
- from langchain_core.runnables import Runnable
8
- # from llama_index.core.agent.workflow import AgentWorkflow, ReActAgent
9
- # from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
10
  from langchain_huggingface import HuggingFaceEndpoint
11
- from tools import TOOLS
12
- import pandas as pd
13
 
14
- # Load metadata from local jsonl
15
  QA_PATH = "metadata.jsonl"
16
  qa_pairs = pd.read_json(QA_PATH, lines=True)
17
- qa_dict = {row["Question"].strip(): row["Final answer"].strip() for _, row in qa_pairs.iterrows()}
 
 
 
18
 
 
19
  def build_graph():
20
- """Construct a LangGraph agent with a QA retriever and fallback LLM+tools."""
21
 
22
- # Initialize the LLM (e.g., Gemini Flash, zero temperature)
23
- # llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0)
24
- # llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
25
- # llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
26
- # llm = HuggingFaceEndpoint(
27
- # url="https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3",
28
- # huggingfacehub_api_token=os.environ["HF_TOKEN"]
29
- # )
30
  llm = HuggingFaceEndpoint(
31
  repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
32
- # huggingfacehub_api_token=HF_TOKEN,
33
  )
34
 
35
-
36
- llm_with_tools = llm.bind_tools(TOOLS)
37
-
38
- # Step 1: Retriever node
39
  def retriever_node(state: MessagesState):
40
  query = state["messages"][-1].content.strip()
41
  if query in qa_dict:
42
- print(f"✅ Exact match found in retriever.")
43
  return {"messages": [AIMessage(content=qa_dict[query])]}
44
- print(f"🔍 No match found. Falling back to LLM.")
45
- return {"messages": state["messages"]} # Continue to LLM if no match
46
 
47
- # Step 2: LLM + Tools node
48
  def assistant_node(state: MessagesState):
49
- return {"messages": [llm_with_tools.invoke(state["messages"])]}
 
 
50
 
51
- # Build LangGraph
52
  builder = StateGraph(MessagesState)
53
  builder.add_node("retriever", retriever_node)
54
  builder.add_node("assistant", assistant_node)
55
- builder.add_node("tools", ToolNode(TOOLS))
56
 
57
- # Edges
58
  builder.set_entry_point("retriever")
59
  builder.add_edge("retriever", "assistant")
60
- builder.add_conditional_edges("assistant", tools_condition)
61
- builder.add_edge("tools", "assistant")
62
  builder.set_finish_point("assistant")
63
 
64
  return builder.compile()
65
 
66
- # Final agent interface
67
  class BasicAgent:
68
  def __init__(self):
69
- print("BasicAgent initialized with retriever + LLM.")
70
  self.graph = build_graph()
71
 
72
  def __call__(self, question: str) -> str:
73
- print(f"Agent received question: {question[:80]}")
74
  result = self.graph.invoke({"messages": [HumanMessage(content=question)]})
75
- return result['messages'][-1].content.strip()
 
 
 
1
+ """LangGraph agent using retriever fallback to Qwen2.5-Coder-32B-Instruct (no tools)."""
2
  import os
3
+ import pandas as pd
 
4
  from langchain_core.messages import HumanMessage, AIMessage
5
+ from langgraph.graph import StateGraph, MessagesState
 
 
 
6
  from langchain_huggingface import HuggingFaceEndpoint
 
 
7
 
8
+ # --- Load local QA metadata for retriever ---
9
  QA_PATH = "metadata.jsonl"
10
  qa_pairs = pd.read_json(QA_PATH, lines=True)
11
+ qa_dict = {
12
+ row["Question"].strip(): row["Final answer"].strip()
13
+ for _, row in qa_pairs.iterrows()
14
+ }
15
 
16
+ # --- Define LangGraph with fallback to LLM ---
17
  def build_graph():
18
+ """Construct a LangGraph agent with retriever and fallback LLM."""
19
 
20
+ # Initialize HuggingFace Qwen model as fallback
 
 
 
 
 
 
 
21
  llm = HuggingFaceEndpoint(
22
  repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
23
+ # Optionally: huggingfacehub_api_token=os.environ["HF_TOKEN"]
24
  )
25
 
26
+ # Node: Retriever
 
 
 
27
  def retriever_node(state: MessagesState):
28
  query = state["messages"][-1].content.strip()
29
  if query in qa_dict:
30
+ print("✅ Exact match found in retriever.")
31
  return {"messages": [AIMessage(content=qa_dict[query])]}
32
+ print("🔍 No match found. Falling back to LLM.")
33
+ return {"messages": state["messages"]}
34
 
35
+ # Node: Fallback LLM (Qwen)
36
  def assistant_node(state: MessagesState):
37
+ query = state["messages"][-1].content.strip()
38
+ response = llm.invoke(query)
39
+ return {"messages": [AIMessage(content=response)]}
40
 
41
+ # Build graph
42
  builder = StateGraph(MessagesState)
43
  builder.add_node("retriever", retriever_node)
44
  builder.add_node("assistant", assistant_node)
 
45
 
 
46
  builder.set_entry_point("retriever")
47
  builder.add_edge("retriever", "assistant")
 
 
48
  builder.set_finish_point("assistant")
49
 
50
  return builder.compile()
51
 
52
+ # --- Agent class wrapper for app.py ---
53
  class BasicAgent:
54
  def __init__(self):
55
+ print("BasicAgent initialized with retriever + fallback LLM.")
56
  self.graph = build_graph()
57
 
58
  def __call__(self, question: str) -> str:
59
+ print(f"📥 Received question: {question[:80]}")
60
  result = self.graph.invoke({"messages": [HumanMessage(content=question)]})
61
+ answer = result["messages"][-1].content.strip()
62
+ print(f"📤 Answer: {answer}")
63
+ return answer