tobyvertommen Claude Sonnet 4.6 commited on
Commit
45b0018
·
1 Parent(s): 2bd5204

Fix: switch to create_tool_calling_agent + AgentExecutor

Browse files

Replaces LangGraph create_react_agent which caused malformed tool calls
on Groq. LangChain's tool-calling agent uses proper JSON tool format.
Also adds handle_parsing_errors=True and enforces non-empty answers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -3,38 +3,39 @@ import gradio as gr
3
  import requests
4
  import pandas as pd
5
  from langchain_groq import ChatGroq
6
- from langgraph.prebuilt import create_react_agent
7
  from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
8
  from langchain_community.utilities import WikipediaAPIWrapper
9
  from langchain_experimental.tools import PythonREPLTool
10
  from langchain.tools import tool
11
- from langchain_core.messages import SystemMessage, HumanMessage
12
 
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
  SYSTEM_PROMPT = """You are a general AI assistant tasked with answering questions accurately.
17
 
18
- Reason step by step, then finish your answer with exactly this format:
19
  FINAL ANSWER: [your answer]
20
 
21
  Rules for FINAL ANSWER:
22
  - Numbers: no commas, no units unless specified
23
  - Strings: no articles (a/an/the), no abbreviations, write digits in plain text
24
  - Lists: comma-separated, apply above rules per element
25
- - Be precise and concise the answer is evaluated by exact match"""
 
26
 
27
 
28
  @tool
29
  def fetch_task_file(task_id: str) -> str:
30
- """Fetch a file associated with a GAIA task by its task_id. Use this when the question references an attachment, file, or additional data."""
31
  try:
32
  response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
33
  if response.status_code == 200:
34
  content_type = response.headers.get("content-type", "")
35
- if any(t in content_type for t in ["text", "json", "csv", "xml"]):
36
  return response.text[:8000]
37
- return f"Binary file ({content_type}) — cannot read as text"
38
  return "No file found for this task"
39
  except Exception as e:
40
  return f"Error fetching file: {e}"
@@ -49,17 +50,26 @@ class BasicAgent:
49
  PythonREPLTool(),
50
  fetch_task_file,
51
  ]
52
- self.agent = create_react_agent(llm, tools)
53
- print("BasicAgent initialized with LangGraph + Groq (llama-3.3-70b-versatile).")
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  def __call__(self, question: str, task_id: str = "") -> str:
56
  full_question = f"[Task ID: {task_id}]\n\n{question}" if task_id else question
57
  print(f"Running agent on task {task_id}: {question[:80]}...")
58
- result = self.agent.invoke(
59
- {"messages": [SystemMessage(content=SYSTEM_PROMPT), HumanMessage(content=full_question)]},
60
- {"recursion_limit": 50},
61
- )
62
- raw_answer = result["messages"][-1].content
63
  if "FINAL ANSWER:" in raw_answer:
64
  answer = raw_answer.split("FINAL ANSWER:")[-1].strip()
65
  else:
@@ -184,7 +194,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
184
 
185
  # --- Gradio Interface ---
186
  with gr.Blocks() as demo:
187
- gr.Markdown("# Agent Evaluation Runner — LangGraph + Groq")
188
  gr.Markdown(
189
  """
190
  **Instructions:**
@@ -192,7 +202,7 @@ with gr.Blocks() as demo:
192
  1. Log in to your Hugging Face account using the button below.
193
  2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, and submit.
194
 
195
- **Agent:** LangGraph ReAct agent with Groq (llama-3.3-70b-versatile)
196
  **Tools:** DuckDuckGo search, Wikipedia, Python REPL, File fetcher
197
 
198
  ---
 
3
  import requests
4
  import pandas as pd
5
  from langchain_groq import ChatGroq
6
+ from langchain.agents import create_tool_calling_agent, AgentExecutor
7
  from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
8
  from langchain_community.utilities import WikipediaAPIWrapper
9
  from langchain_experimental.tools import PythonREPLTool
10
  from langchain.tools import tool
11
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
12
 
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
  SYSTEM_PROMPT = """You are a general AI assistant tasked with answering questions accurately.
17
 
18
+ Use tools to research and reason step by step. When you have a final answer, output ONLY:
19
  FINAL ANSWER: [your answer]
20
 
21
  Rules for FINAL ANSWER:
22
  - Numbers: no commas, no units unless specified
23
  - Strings: no articles (a/an/the), no abbreviations, write digits in plain text
24
  - Lists: comma-separated, apply above rules per element
25
+ - Be precise — evaluated by exact match
26
+ - Never say "I cannot answer" — always attempt an answer"""
27
 
28
 
29
  @tool
30
  def fetch_task_file(task_id: str) -> str:
31
+ """Fetch a file associated with a GAIA task by its task_id. Use when the question references an attachment, file, image, or additional data."""
32
  try:
33
  response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
34
  if response.status_code == 200:
35
  content_type = response.headers.get("content-type", "")
36
+ if any(t in content_type for t in ["text", "json", "csv", "xml", "python"]):
37
  return response.text[:8000]
38
+ return f"Binary file detected ({content_type}) — cannot read as text"
39
  return "No file found for this task"
40
  except Exception as e:
41
  return f"Error fetching file: {e}"
 
50
  PythonREPLTool(),
51
  fetch_task_file,
52
  ]
53
+ prompt = ChatPromptTemplate.from_messages([
54
+ ("system", SYSTEM_PROMPT),
55
+ ("human", "{input}"),
56
+ MessagesPlaceholder("agent_scratchpad"),
57
+ ])
58
+ agent = create_tool_calling_agent(llm, tools, prompt)
59
+ self.executor = AgentExecutor(
60
+ agent=agent,
61
+ tools=tools,
62
+ max_iterations=15,
63
+ handle_parsing_errors=True,
64
+ verbose=True,
65
+ )
66
+ print("BasicAgent initialized with LangChain tool-calling agent + Groq (llama-3.3-70b-versatile).")
67
 
68
  def __call__(self, question: str, task_id: str = "") -> str:
69
  full_question = f"[Task ID: {task_id}]\n\n{question}" if task_id else question
70
  print(f"Running agent on task {task_id}: {question[:80]}...")
71
+ result = self.executor.invoke({"input": full_question})
72
+ raw_answer = result.get("output", "")
 
 
 
73
  if "FINAL ANSWER:" in raw_answer:
74
  answer = raw_answer.split("FINAL ANSWER:")[-1].strip()
75
  else:
 
194
 
195
  # --- Gradio Interface ---
196
  with gr.Blocks() as demo:
197
+ gr.Markdown("# Agent Evaluation Runner — LangChain + Groq")
198
  gr.Markdown(
199
  """
200
  **Instructions:**
 
202
  1. Log in to your Hugging Face account using the button below.
203
  2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, and submit.
204
 
205
+ **Agent:** LangChain tool-calling agent with Groq (llama-3.3-70b-versatile)
206
  **Tools:** DuckDuckGo search, Wikipedia, Python REPL, File fetcher
207
 
208
  ---