Spaces:
Sleeping
Sleeping
tobyvertommen Claude Sonnet 4.6 commited on
Commit ·
b5c651d
1
Parent(s): df5ced9
Fix: replace agent factory with manual llm.bind_tools() ReAct loop
Browse filesRemoves dependency on langchain.agents factory functions (not available
in installed version). Custom tool-calling loop works with any recent
langchain-groq version.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -3,12 +3,11 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
-
from langchain.agents import create_openai_tools_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
|
| 11 |
-
from langchain_core.
|
| 12 |
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -23,7 +22,7 @@ Rules for FINAL ANSWER:
|
|
| 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 |
-
-
|
| 27 |
|
| 28 |
|
| 29 |
@tool
|
|
@@ -43,33 +42,48 @@ def fetch_task_file(task_id: str) -> str:
|
|
| 43 |
|
| 44 |
class BasicAgent:
|
| 45 |
def __init__(self):
|
| 46 |
-
llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
|
| 47 |
-
tools = [
|
| 48 |
DuckDuckGoSearchRun(),
|
| 49 |
WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=3)),
|
| 50 |
PythonREPLTool(),
|
| 51 |
fetch_task_file,
|
| 52 |
]
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
MessagesPlaceholder("agent_scratchpad"),
|
| 57 |
-
])
|
| 58 |
-
agent = create_openai_tools_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 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if "FINAL ANSWER:" in raw_answer:
|
| 74 |
answer = raw_answer.split("FINAL ANSWER:")[-1].strip()
|
| 75 |
else:
|
|
@@ -194,7 +208,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 194 |
|
| 195 |
# --- Gradio Interface ---
|
| 196 |
with gr.Blocks() as demo:
|
| 197 |
-
gr.Markdown("# Agent Evaluation Runner —
|
| 198 |
gr.Markdown(
|
| 199 |
"""
|
| 200 |
**Instructions:**
|
|
@@ -202,7 +216,7 @@ with gr.Blocks() as demo:
|
|
| 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:**
|
| 206 |
**Tools:** DuckDuckGo search, Wikipedia, Python REPL, File fetcher
|
| 207 |
|
| 208 |
---
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from langchain_groq import ChatGroq
|
|
|
|
| 6 |
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
| 7 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 8 |
from langchain_experimental.tools import PythonREPLTool
|
| 9 |
+
from langchain_core.tools import tool
|
| 10 |
+
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage
|
| 11 |
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 22 |
- Strings: no articles (a/an/the), no abbreviations, write digits in plain text
|
| 23 |
- Lists: comma-separated, apply above rules per element
|
| 24 |
- Be precise — evaluated by exact match
|
| 25 |
+
- Always provide an answer, never say you cannot answer"""
|
| 26 |
|
| 27 |
|
| 28 |
@tool
|
|
|
|
| 42 |
|
| 43 |
class BasicAgent:
|
| 44 |
def __init__(self):
|
| 45 |
+
self.llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
|
| 46 |
+
self.tools = [
|
| 47 |
DuckDuckGoSearchRun(),
|
| 48 |
WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=3)),
|
| 49 |
PythonREPLTool(),
|
| 50 |
fetch_task_file,
|
| 51 |
]
|
| 52 |
+
self.tools_map = {t.name: t for t in self.tools}
|
| 53 |
+
self.llm_with_tools = self.llm.bind_tools(self.tools)
|
| 54 |
+
print("BasicAgent initialized with Groq (llama-3.3-70b-versatile) + tool binding.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
def __call__(self, question: str, task_id: str = "") -> str:
|
| 57 |
full_question = f"[Task ID: {task_id}]\n\n{question}" if task_id else question
|
| 58 |
print(f"Running agent on task {task_id}: {question[:80]}...")
|
| 59 |
+
|
| 60 |
+
messages = [
|
| 61 |
+
SystemMessage(content=SYSTEM_PROMPT),
|
| 62 |
+
HumanMessage(content=full_question),
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
for iteration in range(15):
|
| 66 |
+
response = self.llm_with_tools.invoke(messages)
|
| 67 |
+
messages.append(response)
|
| 68 |
+
|
| 69 |
+
if not response.tool_calls:
|
| 70 |
+
break
|
| 71 |
+
|
| 72 |
+
for tool_call in response.tool_calls:
|
| 73 |
+
tool_name = tool_call["name"]
|
| 74 |
+
tool_args = tool_call["args"]
|
| 75 |
+
tool_id = tool_call["id"]
|
| 76 |
+
print(f" Tool call: {tool_name}({tool_args})")
|
| 77 |
+
if tool_name in self.tools_map:
|
| 78 |
+
try:
|
| 79 |
+
result = self.tools_map[tool_name].invoke(tool_args)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
result = f"Tool error: {e}"
|
| 82 |
+
else:
|
| 83 |
+
result = f"Unknown tool: {tool_name}"
|
| 84 |
+
messages.append(ToolMessage(content=str(result), tool_call_id=tool_id))
|
| 85 |
+
|
| 86 |
+
raw_answer = response.content
|
| 87 |
if "FINAL ANSWER:" in raw_answer:
|
| 88 |
answer = raw_answer.split("FINAL ANSWER:")[-1].strip()
|
| 89 |
else:
|
|
|
|
| 208 |
|
| 209 |
# --- Gradio Interface ---
|
| 210 |
with gr.Blocks() as demo:
|
| 211 |
+
gr.Markdown("# Agent Evaluation Runner — Groq + Tool Binding")
|
| 212 |
gr.Markdown(
|
| 213 |
"""
|
| 214 |
**Instructions:**
|
|
|
|
| 216 |
1. Log in to your Hugging Face account using the button below.
|
| 217 |
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, and submit.
|
| 218 |
|
| 219 |
+
**Agent:** Custom ReAct loop with Groq (llama-3.3-70b-versatile) + bind_tools
|
| 220 |
**Tools:** DuckDuckGo search, Wikipedia, Python REPL, File fetcher
|
| 221 |
|
| 222 |
---
|