mralamdari commited on
Commit
eb77524
·
verified ·
1 Parent(s): 7cd0af8

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +50 -37
agent.py CHANGED
@@ -355,49 +355,62 @@ tools = [python_repl_tool,
355
  download_file_from_url,
356
  file_reader_with_LLM,
357
  file_reader]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358
 
359
- # Build graph function
360
- def EsiAgent(provider: str = "groq"):
361
- """Build the graph"""
362
-
363
- llm = ChatOpenAI(openai_api_key=GPT_4o_API_KEY,
364
- base_url="https://openrouter.ai/api/v1",
365
- # model_name="gpt-4o",
366
- # model_name="gpt-3.5-turbo",
367
- temperature=0,
368
- max_tokens=256)
369
- # Bind tools to LLM
370
- llm_with_tools = llm.bind_tools(tools)
371
-
372
- # System message
373
- sys_msg = SystemMessage(content=SYSTEM_PROMT)
374
 
375
- def assistant(state: MessagesState):
376
- """Assistant node"""
377
- msgs = [llm_with_tools.invoke(state["messages"])]
378
- if not any(isinstance(m, SystemMessage) for m in msgs):
379
- msgs = [sys_msg] + msgs
380
- return {"messages": msgs}
381
 
382
- builder = StateGraph(MessagesState)
383
- builder.add_node("assistant", assistant)
384
- builder.add_node("tools", ToolNode(tools))
385
- builder.add_edge(START, "assistant")
386
- builder.add_conditional_edges(
387
- "assistant",
388
- tools_condition,
389
- )
390
- builder.add_edge("tools", "assistant")
391
 
392
- # Compile graph
393
- return builder.compile()
394
 
395
 
396
 
397
  if __name__ == "__main__":
398
  # question = "Where is the capital of the Iran?"
399
- graph = EsiAgent(provider="groq")
400
- messages = [HumanMessage(content=question)]
401
- messages = graph.invoke({"messages": messages})
402
- for m in messages["messages"]:
403
- m.pretty_print()
 
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)
369
+
370
+ # 3) Single system message
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
+
378
+ # 5) Build the graph
379
+ builder = StateGraph(MessagesState)
380
+ builder.add_node("assistant", assistant)
381
+ builder.add_node("tools", ToolNode(tools))
382
+ builder.add_edge(START, "assistant")
383
+ builder.add_conditional_edges("assistant", tools_condition)
384
+ builder.add_edge("tools", "assistant")
385
+
386
+ # 6) Compile once
387
+ self.graph = builder.compile()
388
+
389
+
390
+ def run(self, question: str, files=None, level: int = 1) -> str:
391
+ """
392
+ Wraps graph.invoke to run end-to-end on a plain question.
393
+ """
394
+ # Build a minimal GAIA payload (if your MessagesState expects more, adjust here)
395
+ initial_state = {
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
 
411
 
412
  if __name__ == "__main__":
413
  # question = "Where is the capital of the Iran?"
414
+ agent = EsiAgent()
415
+ answer = agent.run("What is 15% of 200 plus 7 squared?")
416
+