Create agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# agent.py
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
from typing import List, Dict
|
6 |
+
|
7 |
+
def get_questions() -> List[Dict]:
|
8 |
+
response = requests.get("https://gaia-course-api.huggingface.co/questions")
|
9 |
+
return response.json()
|
10 |
+
|
11 |
+
def get_file(task_id: str) -> str:
|
12 |
+
response = requests.get(f"https://gaia-course-api.huggingface.co/files/{task_id}")
|
13 |
+
file_path = f"{task_id}.txt"
|
14 |
+
with open(file_path, "w") as f:
|
15 |
+
f.write(response.text)
|
16 |
+
return file_path
|
17 |
+
|
18 |
+
def reasoning_prompt(question: str) -> str:
|
19 |
+
"""
|
20 |
+
Chain-of-thought + Zero-shot reasoning
|
21 |
+
"""
|
22 |
+
# مثال بسيط يمكن تحسينه لاحقًا
|
23 |
+
if "capital" in question.lower():
|
24 |
+
if "france" in question.lower():
|
25 |
+
return "Paris"
|
26 |
+
if "germany" in question.lower():
|
27 |
+
return "Berlin"
|
28 |
+
if "sum" in question.lower():
|
29 |
+
numbers = re.findall(r"\\d+", question)
|
30 |
+
if len(numbers) >= 2:
|
31 |
+
return str(sum(map(int, numbers)))
|
32 |
+
return "I don't know"
|
33 |
+
|
34 |
+
def solve_question(question: Dict) -> str:
|
35 |
+
q_text = question.get("question", "")
|
36 |
+
return reasoning_prompt(q_text)
|
37 |
+
|
38 |
+
def run_agent() -> List[Dict]:
|
39 |
+
questions = get_questions()
|
40 |
+
answers = []
|
41 |
+
for q in questions:
|
42 |
+
ans = solve_question(q)
|
43 |
+
answers.append({
|
44 |
+
"task_id": q["task_id"],
|
45 |
+
"submitted_answer": ans.strip()
|
46 |
+
})
|
47 |
+
return answers
|