Nitien commited on
Commit
b15aede
·
verified ·
1 Parent(s): 978cf97

Update gaia_agent.py

Browse files
Files changed (1) hide show
  1. gaia_agent.py +51 -27
gaia_agent.py CHANGED
@@ -15,8 +15,14 @@ from langchain_openrouter import ChatOpenRouter
15
  from langgraph.graph import StateGraph, START, END
16
  from langgraph.checkpoint.memory import MemorySaver
17
  from langchain_nvidia_ai_endpoints import ChatNVIDIA
 
18
  from typing import TypedDict
 
 
19
 
 
 
 
20
  from customtools import (
21
  load_and_analyze_excel_file,
22
  extract_text_from_image,
@@ -29,7 +35,6 @@ from customtools import (
29
  transcribe_audio,
30
  modulus_tool,
31
  power_tool,
32
-
33
  )
34
  from config import (
35
  OPENROUTER_API_KEY,
@@ -48,31 +53,39 @@ from prompts import (
48
  )
49
 
50
  load_dotenv()
 
51
 
52
  memory = MemorySaver()
 
 
53
 
54
 
55
  def connect_models():
56
  """Initialize and return the LLM instance."""
57
  try:
58
- global llm
 
 
 
 
 
 
 
59
  if NVIDIA:
60
- llm = ChatNVIDIA(
61
- model=NVIDIA_MODEL,
62
- api_key= NVIDIA_API_KEY,
63
- temperature=1,
64
- top_p=1,
65
 
66
- )
67
-
68
-
69
  else:
70
- print(f"Connecting to LLM: {LLM_MODEL}")
71
- llm = ChatOpenRouter(
72
- model=LLM_MODEL,
73
- temperature=LLM_TEMPERATURE,
74
- api_key=OPENROUTER_API_KEY,
75
- )
76
  return llm
77
  except Exception as e:
78
  print(f"Error initializing LLM: {e}")
@@ -91,7 +104,7 @@ TOOLS = {
91
  "wikisearch": wikisearch,
92
  "transcribe_audio": transcribe_audio,
93
  "modulus_tool": modulus_tool,
94
- "power_tool": power_tool,
95
  }
96
 
97
 
@@ -132,18 +145,29 @@ class Step(BaseModel):
132
  class Plan(BaseModel):
133
  """Structured plan with multiple steps."""
134
  steps: List[Step]
 
 
 
 
 
 
 
 
 
 
 
 
135
  def planner_node(state: AgentState):
136
  """Planner node: breaks down question into steps."""
137
  prompt = PLANNER_PROMPT_TEMPLATE.format(question=state['question'])
138
-
139
- planner_llm = llm.with_structured_output(Plan, method="json_schema")
140
- response = planner_llm.invoke(prompt)
141
 
142
  print(f"Planner generated {len(response.steps)} steps")
143
 
144
  return {
145
  **state,
146
- "plan": [step.dict() for step in response.steps],
147
  "current_step": 0,
148
  "intermediate_results": [],
149
  "done": False,
@@ -234,9 +258,9 @@ def should_continue(state: AgentState):
234
 
235
  def finalizer_node(state: AgentState):
236
  """Finalizer node: summarizes results and generates final answer."""
237
- # Format intermediate results for the finalizer
238
  results_text = "\n".join([
239
- f"Step {i+1}: {r['step'].get('description', '')}\n Output: {str(r['output'])[:100]}..."
240
  for i, r in enumerate(state["intermediate_results"])
241
  ])
242
 
@@ -388,12 +412,12 @@ def process_questions(questions_file: str = None, questions_list: List[str] = No
388
 
389
 
390
  if __name__ == "__main__":
391
-
392
- global llm
393
  # Example questions to process
394
  questions = [
395
- """Task ID: 52e8ce1c-09bd-4537-8e2d-67d1648779b9 ; Question: The attached .csv file shows precipitation amounts, in inches, for the five boroughs of New York City in a certain year. How many inches of precipitation did the city receive in total for that year? Don’t use commas if the number has four or more digits. ; file_name: /home/nitin/.cache/huggingface/hub/datasets--gaia-benchmark--GAIA/snapshots/682dd723ee1e1697e00360edccf2366dc8418dd9/2023/test/52e8ce1c-09bd-4537-8e2d-67d1648779b9.csv
396
- """
 
 
397
 
398
  #"What is the square of the population of France in millions?",
399
  #"What is 50 plus 75?"
 
15
  from langgraph.graph import StateGraph, START, END
16
  from langgraph.checkpoint.memory import MemorySaver
17
  from langchain_nvidia_ai_endpoints import ChatNVIDIA
18
+ from langchain_community.cache import SQLiteCache
19
  from typing import TypedDict
20
+ import langchain_core
21
+ #from langchain_ollama import ChatOllama
22
 
23
+ CACHE_DB_PATH = ".langchain_llm_cache.db"
24
+ #langchain_core.globals.set_debug(True)
25
+ langchain_core.globals.set_llm_cache(SQLiteCache(database_path=CACHE_DB_PATH))
26
  from customtools import (
27
  load_and_analyze_excel_file,
28
  extract_text_from_image,
 
35
  transcribe_audio,
36
  modulus_tool,
37
  power_tool,
 
38
  )
39
  from config import (
40
  OPENROUTER_API_KEY,
 
53
  )
54
 
55
  load_dotenv()
56
+ print(f"LangChain LLM cache enabled: {CACHE_DB_PATH}")
57
 
58
  memory = MemorySaver()
59
+ llm = None
60
+ planner_llm = None
61
 
62
 
63
  def connect_models():
64
  """Initialize and return the LLM instance."""
65
  try:
66
+ global llm, planner_llm
67
+ # llm = ChatOllama(
68
+ # model="gemma4:e2b",
69
+ # base_url="http://localhost:11434/",
70
+ # temperature=0,
71
+ # )
72
+ # Reset derived clients whenever base model is reconnected.
73
+ planner_llm = None
74
  if NVIDIA:
75
+ llm = ChatNVIDIA(
76
+ model=NVIDIA_MODEL,
77
+ api_key= NVIDIA_API_KEY,
78
+ temperature=0.1,
79
+ top_p=1,
80
 
81
+ )
 
 
82
  else:
83
+ print(f"Connecting to LLM: {LLM_MODEL}")
84
+ llm = ChatOpenRouter(
85
+ model=LLM_MODEL,
86
+ temperature=LLM_TEMPERATURE,
87
+ api_key=OPENROUTER_API_KEY,
88
+ )
89
  return llm
90
  except Exception as e:
91
  print(f"Error initializing LLM: {e}")
 
104
  "wikisearch": wikisearch,
105
  "transcribe_audio": transcribe_audio,
106
  "modulus_tool": modulus_tool,
107
+ "power_tool":power_tool,
108
  }
109
 
110
 
 
145
  class Plan(BaseModel):
146
  """Structured plan with multiple steps."""
147
  steps: List[Step]
148
+
149
+
150
+ def get_planner_llm():
151
+ """Create structured planner client once and reuse it across questions."""
152
+ global planner_llm, llm
153
+ if llm is None:
154
+ llm = connect_models()
155
+ if planner_llm is None:
156
+ planner_llm = llm.with_structured_output(Plan, method="json_schema")
157
+ return planner_llm
158
+
159
+
160
  def planner_node(state: AgentState):
161
  """Planner node: breaks down question into steps."""
162
  prompt = PLANNER_PROMPT_TEMPLATE.format(question=state['question'])
163
+
164
+ response = get_planner_llm().invoke(prompt)
 
165
 
166
  print(f"Planner generated {len(response.steps)} steps")
167
 
168
  return {
169
  **state,
170
+ "plan": [step.model_dump() for step in response.steps],
171
  "current_step": 0,
172
  "intermediate_results": [],
173
  "done": False,
 
258
 
259
  def finalizer_node(state: AgentState):
260
  """Finalizer node: summarizes results and generates final answer."""
261
+ # Compact context to reduce token usage sent to finalizer.
262
  results_text = "\n".join([
263
+ f"S{i+1}: {r['step'].get('description', '')} | O: {str(r['output'])[:80]}"
264
  for i, r in enumerate(state["intermediate_results"])
265
  ])
266
 
 
412
 
413
 
414
  if __name__ == "__main__":
 
 
415
  # Example questions to process
416
  questions = [
417
+ """
418
+ Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations.
419
+ """
420
+ #Task ID: 52e8ce1c-09bd-4537-8e2d-67d1648779b9 ; Question: The attached .csv file shows precipitation amounts, in inches, for the five boroughs of New York City in a certain year. How many inches of precipitation did the city receive in total for that year? Don’t use commas if the number has four or more digits. ; file_name: /home/nitin/.cache/huggingface/hub/datasets--gaia-benchmark--GAIA/snapshots/682dd723ee1e1697e00360edccf2366dc8418dd9/2023/test/52e8ce1c-09bd-4537-8e2d-67d1648779b9.csv
421
 
422
  #"What is the square of the population of France in millions?",
423
  #"What is 50 plus 75?"