Spaces:
Sleeping
Sleeping
added agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from smolagent import Agent, Tool
|
4 |
+
|
5 |
+
# ------------------------
|
6 |
+
# Constants
|
7 |
+
# ------------------------
|
8 |
+
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
+
|
10 |
+
# ------------------------
|
11 |
+
# Tool definitions
|
12 |
+
# ------------------------
|
13 |
+
def fetch_questions() -> list:
|
14 |
+
"""Fetch the full list of GAIA evaluation questions."""
|
15 |
+
response = requests.get(f"{API_URL}/questions", timeout=15)
|
16 |
+
response.raise_for_status()
|
17 |
+
return response.json()
|
18 |
+
|
19 |
+
def fetch_random_question() -> dict:
|
20 |
+
"""Fetch a single random GAIA question."""
|
21 |
+
response = requests.get(f"{API_URL}/random-question", timeout=15)
|
22 |
+
response.raise_for_status()
|
23 |
+
return response.json()
|
24 |
+
|
25 |
+
def fetch_file(task_id: str) -> bytes:
|
26 |
+
"""Download a file associated with a given task_id."""
|
27 |
+
response = requests.get(f"{API_URL}/files/{task_id}", timeout=15)
|
28 |
+
response.raise_for_status()
|
29 |
+
return response.content
|
30 |
+
|
31 |
+
def submit_answers(username: str, agent_code: str, answers: list) -> dict:
|
32 |
+
"""Submit the agent's answers to GAIA and return the scoring."""
|
33 |
+
payload = {
|
34 |
+
"username": username,
|
35 |
+
"agent_code": agent_code,
|
36 |
+
"answers": answers
|
37 |
+
}
|
38 |
+
response = requests.post(f"{API_URL}/submit", json=payload, timeout=60)
|
39 |
+
response.raise_for_status()
|
40 |
+
return response.json()
|
41 |
+
|
42 |
+
# ------------------------
|
43 |
+
# SmolAgent setup
|
44 |
+
# ------------------------
|
45 |
+
# Define the tools for the agent
|
46 |
+
tools = [
|
47 |
+
Tool(
|
48 |
+
name="fetch_questions",
|
49 |
+
function=fetch_questions,
|
50 |
+
description="Fetch the list of evaluation questions from GAIA."
|
51 |
+
),
|
52 |
+
Tool(
|
53 |
+
name="fetch_random_question",
|
54 |
+
function=fetch_random_question,
|
55 |
+
description="Fetch one random question from GAIA."
|
56 |
+
),
|
57 |
+
Tool(
|
58 |
+
name="fetch_file",
|
59 |
+
function=fetch_file,
|
60 |
+
description="Download a file for a given GAIA task_id."
|
61 |
+
),
|
62 |
+
Tool(
|
63 |
+
name="submit_answers",
|
64 |
+
function=submit_answers,
|
65 |
+
description="Submit the answers payload and get the score back."
|
66 |
+
),
|
67 |
+
]
|
68 |
+
|
69 |
+
def create_agent() -> Agent:
|
70 |
+
"""
|
71 |
+
Factory that returns a configured SmolAgent instance.
|
72 |
+
Assumes OPENAI_API_KEY is set in the environment for LLM access.
|
73 |
+
"""
|
74 |
+
# Initialize OpenAI key for the LLM
|
75 |
+
import openai
|
76 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
77 |
+
|
78 |
+
|
79 |
+
agent = Agent(
|
80 |
+
tools=tools,
|
81 |
+
llm="openai", # Use OpenAI backend
|
82 |
+
model="gpt-3.5-turbo", # LLM model
|
83 |
+
prompt_template=(
|
84 |
+
"Here is a GAIA question: {question}\n"
|
85 |
+
"Provide the exact answer, nothing else."
|
86 |
+
)
|
87 |
+
)
|
88 |
+
return agent
|