File size: 2,431 Bytes
a709497
 
 
5a72425
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a709497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6ecec3
 
a709497
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import json
import os
from smolagents import tool
from typing import Any, Optional
from smolagents.tools import Tool

class FinalAnswerTool(Tool):
    name = "final_answer"
    description = "Provides a final answer to the given problem."
    inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
    output_type = "any"

    def forward(self, answer: Any) -> Any:

        # Returns the final answer provided by the agent.
        # FINAL ANSWER: [YOUR FINAL ANSWER].
        
        return answer

    def __init__(self, *args, **kwargs):
        self.is_initialized = False

@tool
def fetch_questions(api_url: str) -> dict | None:
    """ A tool that fetches the questions from the agents-course API and returns the JSON as a dict.
    Args:
        api_url: The base URL of the agents-course API.
    Returns:
        A dictionary containing the questions if successful, or None if there was an error.
    """
    questions_url = f"{api_url}/questions"
    
    try:
        response = requests.get(questions_url)
        response.raise_for_status()
        return response.json()
    except requests.RequestException as e:
        print(f"Error fetching questions: {e}")
        return None
    
@tool    
def fetch_file(api_url:str, task_id: str, file_name: str) -> None:
    """ A tool that fetches a file for a given task ID from the agents-course API.
    Args:
        api_url: The base URL of the agents-course API.
        task_id: The ID of the task for which the file is to be fetched.
        file_name: The name of the file to be downloaded.
    Returns:
        None if the file is downloaded successfully, or an error message if there was an issue.
    """
    file_url = f"{api_url}/files/{task_id}"
    downsloads_dir = "files"
    os.makedirs(downsloads_dir, exist_ok=True)
    
    file_path = os.path.join(downsloads_dir, file_name)
    if not os.path.exists(file_path):
        print(f"Downloading file {file_name} for task {task_id} ...")
        file_response = requests.get(file_url, timeout=30)
        if file_response.status_code == 200:
            with open(file_path, 'wb') as f:
                f.write(file_response.content)
            print(f"File {file_name} downloaded successfully.")
        else:
            print(f"Failed to download file {file_name} for task {task_id}. Status code: {file_response.status_code}")
            return None