File size: 3,729 Bytes
e555a00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# tools.py
import requests
import os
from langchain_core.tools import tool
from duckduckgo_search import DDGS
from langchain_community.tools import PythonREPLTool

import config

# --- Strumenti per l'API del Corso ---

@tool
def get_all_questions():
    """
    Recupera l'elenco di tutte le domande disponibili per la valutazione finale.
    Da usare solo una volta all'inizio per ottenere i task_id.
    """
    try:
        response = requests.get(f"{config.BASE_API_URL}/questions", headers=config.HEADERS)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return f"Errore API durante il recupero delle domande: {e}"

@tool
def download_file_by_task_id(task_id: str) -> str:
    """
    Scarica un file associato a uno specifico task_id.
    L'input deve essere il task_id.
    Salva il file localmente e restituisce il percorso del file scaricato.
    Usare questo strumento quando la domanda richiede di analizzare un file.
    """
    try:
        # Crea la directory 'downloads' se non esiste
        if not os.path.exists('downloads'):
            os.makedirs('downloads')

        response = requests.get(f"{config.BASE_API_URL}/files/{task_id}", headers=config.HEADERS)
        response.raise_for_status()

        # Estrai il nome del file dagli header se possibile, altrimenti usa un nome di default
        content_disposition = response.headers.get('content-disposition')
        if content_disposition:
            try:
                filename = content_disposition.split('filename=')[1].strip('"')
            except IndexError:
                filename = f"{task_id}_file" # Fallback
        else:
            filename = f"{task_id}_file"

        file_path = os.path.join('downloads', filename)
        with open(file_path, 'wb') as f:
            f.write(response.content)

        return f"File per il task_id '{task_id}' scaricato con successo in '{file_path}'. Contenuto del file:\n---\n{response.content.decode('utf-8', errors='ignore')[:2000]}\n---"
    except requests.exceptions.RequestException as e:
        return f"Errore API durante il download del file per task_id {task_id}: {e}"
    except Exception as e:
        return f"Errore durante il salvataggio del file per task_id {task_id}: {e}"


@tool
def submit_answer(task_id: str, answer: str) -> str:
    """
    Invia la risposta finale per un dato task_id.
    Prende come input il task_id e la stringa della risposta.
    La risposta deve essere esatta, senza prefissi o formattazione extra.
    """
    payload = {
        "task_id": task_id,
        "submitted_answer": answer,
        "username": config.HF_USERNAME
    }
    try:
        response = requests.post(f"{config.BASE_API_URL}/submit", headers=config.HEADERS, json=payload)
        response.raise_for_status()
        return f"Risposta per {task_id} inviata con successo: {response.json()}"
    except requests.exceptions.RequestException as e:
        return f"Errore API durante l'invio della risposta per {task_id}: {e}"

# --- Strumenti Generici ---

@tool
def web_search(query: str) -> str:
    """
    Esegue una ricerca web utilizzando DuckDuckGo per trovare informazioni aggiornate
    o dati non presenti nel contesto. Utilizza questo strumento quando la domanda
    riguarda eventi recenti, fatti specifici, o richiede conoscenza esterna.
    L'input è la query di ricerca.
    """
    with DDGS() as ddgs:
        results = [r for r in ddgs.text(query, max_results=5)]
        return "\n".join([f"Fonte: {r['href']}\nSnippet: {r['body']}" for r in results]) if results else "Nessun risultato trovato."

# Strumento per calcoli e esecuzione codice
python_interpreter = PythonREPLTool()