| import requests |
| from smolagents import ToolCallingAgent, TransformersModel, Tool, DuckDuckGoSearchTool |
|
|
| |
| |
| |
| model = TransformersModel( |
| model_id="meta-llama/Llama-3.2-3B-Instruct" |
| ) |
|
|
| |
| |
| |
| class WebSearchTool(Tool): |
| def __init__(self): |
| super().__init__() |
| self.name = "web_search" |
| self.description = "Search the web using DuckDuckGo" |
| self._ddg = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0) |
|
|
| def __call__(self, query: str): |
| return self._ddg(query=query) |
|
|
| |
| |
| |
| agent = ToolCallingAgent( |
| model=model, |
| tools=[WebSearchTool()], |
| max_steps=8 |
| ) |
|
|
| |
| |
| |
| BASE_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| def get_questions(): |
| r = requests.get(f"{BASE_URL}/questions") |
| r.raise_for_status() |
| return r.json() |
|
|
| def get_random_question(): |
| r = requests.get(f"{BASE_URL}/random-question") |
| r.raise_for_status() |
| return r.json() |
|
|
| def submit_answers(username, agent_code, answers): |
| payload = {"username": username, "agent_code": agent_code, "answers": answers} |
| r = requests.post(f"{BASE_URL}/submit", json=payload) |
| r.raise_for_status() |
| return r.json() |
|
|
| |
| |
| |
| def generate_answer(question): |
| prompt = f""" |
| Answer this question accurately and concisely. |
| Do not include reasoning or explanations. |
| Question: {question} |
| """ |
| try: |
| return agent.run(prompt).strip() |
| except Exception as e: |
| return f"Error: {e}" |
|
|
| |
| |
| |
| if __name__ == "__main__": |
| |
| username = "<YOUR_HF_USERNAME>" |
| agent_code = "https://huggingface.co/spaces/<YOUR_HF_USERNAME>/smolagents-final/tree/main" |
|
|
| |
| questions = get_questions() |
| print(f"Retrieved {len(questions)} GAIA questions.\n") |
|
|
| |
| answers = [] |
| for q in questions: |
| print(f"[{q['task_id']}] {q['question']}") |
| ans = generate_answer(q["question"]) |
| print(f" β {ans}") |
| answers.append({"task_id": q["task_id"], "submitted_answer": ans}) |
|
|
| |
| print("\nSubmitting answers...") |
| result = submit_answers(username, agent_code, answers) |
| print(result) |
|
|