mralamdari commited on
Commit
e98d033
·
verified ·
1 Parent(s): f7e671b

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +82 -12
agent.py CHANGED
@@ -26,6 +26,7 @@ import mimetypes
26
  from PIL import Image
27
  from langchain_core.tools import tool
28
  from tavily import TavilyClient
 
29
  from dataclasses import dataclass, field
30
  from langchain_openai import ChatOpenAI
31
  from langgraph.graph import StateGraph, START, END, MessagesState
@@ -38,10 +39,11 @@ from langchain_community.document_loaders import ArxivLoader
38
  from langgraph.prebuilt import ToolNode, tools_condition
39
  from langchain_core.messages import SystemMessage, HumanMessage
40
 
41
- GPT_4o_API_KEY = os.environ["META_LLAMA_4_MAVERICK"]
42
  # GPT_4o_API_KEY = os.environ["GPT_4o_API_KEY"]
43
- TAVILY_API_KEY = os.environ["TAVILY_API_KEY"]
44
- Pixtral12B_API = os.environ["Pixtral12B_API"]
 
 
45
 
46
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
47
  tavily = TavilyClient(api_key=TAVILY_API_KEY)
@@ -345,24 +347,39 @@ def file_reader(file_path: str) -> str:
345
  return f"Error analyzing text file: {str(e)}"
346
 
347
 
 
 
 
 
 
 
 
348
  tools = [python_repl_tool,
349
- web_search_with_LLM,
350
  web_search,
351
  vision_caption,
352
- vision_caption_with_LLM,
353
  arvix_search,
354
  wiki_search,
355
  download_file_from_url,
356
- file_reader_with_LLM,
357
  file_reader]
 
 
 
358
  class EsiAgent:
359
- def __init__(self):
360
  # 1) Instantiate the LLM
361
  llm = ChatOpenAI(
362
- openai_api_key=GPT_4o_API_KEY,
363
- base_url="https://openrouter.ai/api/v1",
 
 
 
 
364
  temperature=0,
365
- max_tokens=256
 
366
  )
367
  # 2) Bind your tools
368
  llm_with_tools = llm.bind_tools(tools)
@@ -371,7 +388,11 @@ class EsiAgent:
371
  self.sys_msg = SystemMessage(content=SYSTEM_PROMPT)
372
 
373
  # 4) Assistant node
 
374
  def assistant(state):
 
 
 
375
  # Prepend sys_msg only if it's not already there
376
  return {"messages": [llm_with_tools.invoke(state["messages"])]}
377
 
@@ -396,15 +417,64 @@ class EsiAgent:
396
  "messages": [HumanMessage(content=question)] + [self.sys_msg]
397
 
398
  }
399
-
400
  final_state = self.graph.invoke(initial_state)
401
  # Extract final assistant message
402
  last_msg = final_state["messages"][-1].content
403
-
404
  # If you need to strip off "FINAL ANSWER: " prefix, do it here:
405
  if last_msg.startswith("FINAL ANSWER:"):
406
  return last_msg[len("FINAL ANSWER:"):].strip()
407
  return last_msg.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
 
409
 
410
 
 
26
  from PIL import Image
27
  from langchain_core.tools import tool
28
  from tavily import TavilyClient
29
+ from langchain_groq import ChatGroq
30
  from dataclasses import dataclass, field
31
  from langchain_openai import ChatOpenAI
32
  from langgraph.graph import StateGraph, START, END, MessagesState
 
39
  from langgraph.prebuilt import ToolNode, tools_condition
40
  from langchain_core.messages import SystemMessage, HumanMessage
41
 
 
42
  # GPT_4o_API_KEY = os.environ["GPT_4o_API_KEY"]
43
+ GPT_4o_API_KEY = os.environ["META_LLAMA_4_MAVERICK"]
44
+ GROQ_API_KEY = os.environ["GROQ_API_KEY"]
45
+ TAVILY_API_KEY = os.environ["TAVILY_API_KEY"]
46
+ Pixtral12B_API = os.environ["Pixtral12B_API"]
47
 
48
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
49
  tavily = TavilyClient(api_key=TAVILY_API_KEY)
 
347
  return f"Error analyzing text file: {str(e)}"
348
 
349
 
350
+
351
+
352
+
353
+
354
+
355
+
356
+
357
  tools = [python_repl_tool,
358
+ # web_search_with_LLM,
359
  web_search,
360
  vision_caption,
361
+ # vision_caption_with_LLM,
362
  arvix_search,
363
  wiki_search,
364
  download_file_from_url,
365
+ # file_reader_with_LLM,
366
  file_reader]
367
+
368
+
369
+
370
  class EsiAgent:
371
+ def __init__(self, level=1):
372
  # 1) Instantiate the LLM
373
  llm = ChatOpenAI(
374
+ base_url="https://api.groq.com/openai/v1", # Groq's endpoint
375
+ openai_api_key=GROQ_API_KEY,
376
+ # model_name='qwen-qwq-32b',
377
+ # model_name="mixtral-8x7b-32768",
378
+ model_name="meta-llama/llama-4-scout-17b-16e-instruct", #, llama3-70b-8192, etc.
379
+ # or gemma-7b-it, llama3-70b-8192, etc.
380
  temperature=0,
381
+ # max_tokens=128
382
+ max_tokens=512
383
  )
384
  # 2) Bind your tools
385
  llm_with_tools = llm.bind_tools(tools)
 
388
  self.sys_msg = SystemMessage(content=SYSTEM_PROMPT)
389
 
390
  # 4) Assistant node
391
+ self.tool_numbers = level * 3
392
  def assistant(state):
393
+ if self.tool_numbers == 0:
394
+ return state["messages"]
395
+ self.tool_numbers -= 1
396
  # Prepend sys_msg only if it's not already there
397
  return {"messages": [llm_with_tools.invoke(state["messages"])]}
398
 
 
417
  "messages": [HumanMessage(content=question)] + [self.sys_msg]
418
 
419
  }
 
420
  final_state = self.graph.invoke(initial_state)
421
  # Extract final assistant message
422
  last_msg = final_state["messages"][-1].content
 
423
  # If you need to strip off "FINAL ANSWER: " prefix, do it here:
424
  if last_msg.startswith("FINAL ANSWER:"):
425
  return last_msg[len("FINAL ANSWER:"):].strip()
426
  return last_msg.strip()
427
+
428
+ # class EsiAgent:
429
+ # def __init__(self):
430
+ # # 1) Instantiate the LLM
431
+ # llm = ChatOpenAI(
432
+ # openai_api_key=GPT_4o_API_KEY,
433
+ # base_url="https://openrouter.ai/api/v1",
434
+ # temperature=0,
435
+ # max_tokens=256
436
+ # )
437
+ # # 2) Bind your tools
438
+ # llm_with_tools = llm.bind_tools(tools)
439
+
440
+ # # 3) Single system message
441
+ # self.sys_msg = SystemMessage(content=SYSTEM_PROMPT)
442
+
443
+ # # 4) Assistant node
444
+ # def assistant(state):
445
+ # # Prepend sys_msg only if it's not already there
446
+ # return {"messages": [llm_with_tools.invoke(state["messages"])]}
447
+
448
+ # # 5) Build the graph
449
+ # builder = StateGraph(MessagesState)
450
+ # builder.add_node("assistant", assistant)
451
+ # builder.add_node("tools", ToolNode(tools))
452
+ # builder.add_edge(START, "assistant")
453
+ # builder.add_conditional_edges("assistant", tools_condition)
454
+ # builder.add_edge("tools", "assistant")
455
+
456
+ # # 6) Compile once
457
+ # self.graph = builder.compile()
458
+
459
+
460
+ # def run(self, question: str, files=None, level: int = 1) -> str:
461
+ # """
462
+ # Wraps graph.invoke to run end-to-end on a plain question.
463
+ # """
464
+ # # Build a minimal GAIA payload (if your MessagesState expects more, adjust here)
465
+ # initial_state = {
466
+ # "messages": [HumanMessage(content=question)] + [self.sys_msg]
467
+
468
+ # }
469
+
470
+ # final_state = self.graph.invoke(initial_state)
471
+ # # Extract final assistant message
472
+ # last_msg = final_state["messages"][-1].content
473
+
474
+ # # If you need to strip off "FINAL ANSWER: " prefix, do it here:
475
+ # if last_msg.startswith("FINAL ANSWER:"):
476
+ # return last_msg[len("FINAL ANSWER:"):].strip()
477
+ # return last_msg.strip()
478
 
479
 
480