Spaces:
Sleeping
Sleeping
fixes
Browse files
agent.py
CHANGED
@@ -1,6 +1,8 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
-
from smolagents import
|
4 |
|
5 |
# ------------------------
|
6 |
# Constants
|
@@ -10,26 +12,58 @@ API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
10 |
# ------------------------
|
11 |
# Tool definitions
|
12 |
# ------------------------
|
|
|
|
|
13 |
def fetch_questions() -> list:
|
14 |
-
"""
|
|
|
|
|
|
|
|
|
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 |
-
"""
|
|
|
|
|
|
|
|
|
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 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
27 |
response = requests.get(f"{API_URL}/files/{task_id}", timeout=15)
|
28 |
response.raise_for_status()
|
29 |
return response.content
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
payload = {
|
34 |
"username": username,
|
35 |
"agent_code": agent_code,
|
@@ -39,50 +73,21 @@ def submit_answers(username: str, agent_code: str, answers: list) -> dict:
|
|
39 |
response.raise_for_status()
|
40 |
return response.json()
|
41 |
|
42 |
-
|
43 |
-
|
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
|
72 |
-
|
|
|
73 |
"""
|
74 |
-
|
75 |
-
|
76 |
-
|
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
|
85 |
-
"
|
|
|
86 |
)
|
87 |
)
|
88 |
return agent
|
|
|
1 |
+
|
2 |
+
|
3 |
import os
|
4 |
import requests
|
5 |
+
from smolagents import CodeAgent, tool, OpenAIServerModel
|
6 |
|
7 |
# ------------------------
|
8 |
# Constants
|
|
|
12 |
# ------------------------
|
13 |
# Tool definitions
|
14 |
# ------------------------
|
15 |
+
|
16 |
+
@tool
|
17 |
def fetch_questions() -> list:
|
18 |
+
"""
|
19 |
+
Fetch the full list of GAIA evaluation questions.
|
20 |
+
|
21 |
+
:return: A list of question dictionaries, each with 'task_id' and 'question'.
|
22 |
+
"""
|
23 |
response = requests.get(f"{API_URL}/questions", timeout=15)
|
24 |
response.raise_for_status()
|
25 |
return response.json()
|
26 |
|
27 |
+
|
28 |
+
@tool
|
29 |
def fetch_random_question() -> dict:
|
30 |
+
"""
|
31 |
+
Fetch a single random GAIA question.
|
32 |
+
|
33 |
+
:return: A dict with keys 'task_id' and 'question'.
|
34 |
+
"""
|
35 |
response = requests.get(f"{API_URL}/random-question", timeout=15)
|
36 |
response.raise_for_status()
|
37 |
return response.json()
|
38 |
|
39 |
+
|
40 |
+
@tool
|
41 |
def fetch_file(task_id: str) -> bytes:
|
42 |
+
"""
|
43 |
+
Download a file associated with a given task_id.
|
44 |
+
|
45 |
+
:param task_id: The ID of the GAIA task whose file you want to download.
|
46 |
+
:return: The file content as raw bytes.
|
47 |
+
"""
|
48 |
response = requests.get(f"{API_URL}/files/{task_id}", timeout=15)
|
49 |
response.raise_for_status()
|
50 |
return response.content
|
51 |
|
52 |
+
|
53 |
+
@tool
|
54 |
+
def submit_answers(
|
55 |
+
username: str,
|
56 |
+
agent_code: str,
|
57 |
+
answers: list
|
58 |
+
) -> dict:
|
59 |
+
"""
|
60 |
+
Submit the agent's answers to GAIA and return the scoring.
|
61 |
+
|
62 |
+
:param username: Your Hugging Face username (identifies the submission).
|
63 |
+
:param agent_code: URL to your HF Space code (for verification).
|
64 |
+
:param answers: List of dicts each with 'task_id' and 'submitted_answer'.
|
65 |
+
:return: A dict containing 'score', 'correct_count', 'total_attempted', 'message', etc.
|
66 |
+
"""
|
67 |
payload = {
|
68 |
"username": username,
|
69 |
"agent_code": agent_code,
|
|
|
73 |
response.raise_for_status()
|
74 |
return response.json()
|
75 |
|
76 |
+
|
77 |
+
def create_agent() -> CodeAgent:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
"""
|
79 |
+
Factory that returns a configured CodeAgent instance using OpenAI.
|
80 |
+
|
81 |
+
Expects OPENAI_API_KEY in the environment.
|
82 |
"""
|
83 |
+
openai_model = OpenAIServerModel(model_name="gpt-3.5-turbo")
|
84 |
+
agent = CodeAgent(
|
85 |
+
tools=[fetch_questions, fetch_random_question, fetch_file, submit_answers],
|
86 |
+
model=openai_model,
|
|
|
|
|
|
|
|
|
|
|
87 |
prompt_template=(
|
88 |
+
"Here is a GAIA question:\n"
|
89 |
+
"{question}\n"
|
90 |
+
"Provide ONLY the exact answer (exact-match) with no extra text."
|
91 |
)
|
92 |
)
|
93 |
return agent
|