Spaces:
Sleeping
Sleeping
fix
Browse files
agent.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
import os
|
4 |
import requests
|
5 |
from smolagents import CodeAgent, tool, OpenAIServerModel
|
@@ -18,37 +16,34 @@ def fetch_questions() -> list:
|
|
18 |
"""
|
19 |
Fetch the full list of GAIA evaluation questions.
|
20 |
|
21 |
-
:return: A list of question
|
22 |
"""
|
23 |
-
|
24 |
-
|
25 |
-
return
|
26 |
-
|
27 |
|
28 |
@tool
|
29 |
def fetch_random_question() -> dict:
|
30 |
"""
|
31 |
Fetch a single random GAIA question.
|
32 |
|
33 |
-
:return: A dict
|
34 |
"""
|
35 |
-
|
36 |
-
|
37 |
-
return
|
38 |
-
|
39 |
|
40 |
@tool
|
41 |
def fetch_file(task_id: str) -> bytes:
|
42 |
"""
|
43 |
-
Download a file associated with a given
|
44 |
|
45 |
-
:param task_id: The ID of the GAIA task whose file
|
46 |
-
:return:
|
47 |
"""
|
48 |
-
|
49 |
-
|
50 |
-
return
|
51 |
-
|
52 |
|
53 |
@tool
|
54 |
def submit_answers(
|
@@ -57,37 +52,35 @@ def submit_answers(
|
|
57 |
answers: list
|
58 |
) -> dict:
|
59 |
"""
|
60 |
-
Submit the agent's answers
|
61 |
|
62 |
-
:param username: Your
|
63 |
-
:param agent_code: URL to your
|
64 |
-
:param answers: List of dicts
|
65 |
-
:return:
|
66 |
"""
|
67 |
payload = {
|
68 |
"username": username,
|
69 |
"agent_code": agent_code,
|
70 |
"answers": answers
|
71 |
}
|
72 |
-
|
73 |
-
|
74 |
-
return
|
75 |
-
|
76 |
|
77 |
def create_agent() -> CodeAgent:
|
78 |
"""
|
79 |
-
|
80 |
-
|
81 |
Expects OPENAI_API_KEY in the environment.
|
82 |
"""
|
83 |
-
|
84 |
agent = CodeAgent(
|
85 |
tools=[fetch_questions, fetch_random_question, fetch_file, submit_answers],
|
86 |
-
model=
|
87 |
prompt_template=(
|
88 |
"Here is a GAIA question:\n"
|
89 |
"{question}\n"
|
90 |
-
"
|
91 |
)
|
92 |
)
|
93 |
return agent
|
|
|
|
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
from smolagents import CodeAgent, tool, OpenAIServerModel
|
|
|
16 |
"""
|
17 |
Fetch the full list of GAIA evaluation questions.
|
18 |
|
19 |
+
:return: A list of question dicts, each with 'task_id' and 'question'.
|
20 |
"""
|
21 |
+
resp = requests.get(f"{API_URL}/questions", timeout=15)
|
22 |
+
resp.raise_for_status()
|
23 |
+
return resp.json()
|
|
|
24 |
|
25 |
@tool
|
26 |
def fetch_random_question() -> dict:
|
27 |
"""
|
28 |
Fetch a single random GAIA question.
|
29 |
|
30 |
+
:return: A dict containing 'task_id' and 'question'.
|
31 |
"""
|
32 |
+
resp = requests.get(f"{API_URL}/random-question", timeout=15)
|
33 |
+
resp.raise_for_status()
|
34 |
+
return resp.json()
|
|
|
35 |
|
36 |
@tool
|
37 |
def fetch_file(task_id: str) -> bytes:
|
38 |
"""
|
39 |
+
Download a file associated with a given GAIA task.
|
40 |
|
41 |
+
:param task_id: The ID of the GAIA task whose file to download.
|
42 |
+
:return: Raw bytes of the file.
|
43 |
"""
|
44 |
+
resp = requests.get(f"{API_URL}/files/{task_id}", timeout=15)
|
45 |
+
resp.raise_for_status()
|
46 |
+
return resp.content
|
|
|
47 |
|
48 |
@tool
|
49 |
def submit_answers(
|
|
|
52 |
answers: list
|
53 |
) -> dict:
|
54 |
"""
|
55 |
+
Submit the agent's answers and get back the scoring.
|
56 |
|
57 |
+
:param username: Your HF username for the submission.
|
58 |
+
:param agent_code: URL to your Space code (for verification).
|
59 |
+
:param answers: List of dicts with 'task_id' and 'submitted_answer'.
|
60 |
+
:return: Dict with keys 'score', 'correct_count', 'total_attempted', 'message', etc.
|
61 |
"""
|
62 |
payload = {
|
63 |
"username": username,
|
64 |
"agent_code": agent_code,
|
65 |
"answers": answers
|
66 |
}
|
67 |
+
resp = requests.post(f"{API_URL}/submit", json=payload, timeout=60)
|
68 |
+
resp.raise_for_status()
|
69 |
+
return resp.json()
|
|
|
70 |
|
71 |
def create_agent() -> CodeAgent:
|
72 |
"""
|
73 |
+
Build and return a configured CodeAgent using OpenAI GPT-3.5 Turbo.
|
|
|
74 |
Expects OPENAI_API_KEY in the environment.
|
75 |
"""
|
76 |
+
model = OpenAIServerModel(model_name="gpt-3.5-turbo")
|
77 |
agent = CodeAgent(
|
78 |
tools=[fetch_questions, fetch_random_question, fetch_file, submit_answers],
|
79 |
+
model=model,
|
80 |
prompt_template=(
|
81 |
"Here is a GAIA question:\n"
|
82 |
"{question}\n"
|
83 |
+
"Respond with only the exact answer (exact-match), no extra text."
|
84 |
)
|
85 |
)
|
86 |
return agent
|