File size: 2,534 Bytes
7310dd0
bb81171
517bc3e
bb81171
 
 
db8cd81
49c2e76
7310dd0
 
db8cd81
7310dd0
 
 
db8cd81
7310dd0
 
 
 
 
936b841
7310dd0
5cd96ae
 
c907a28
7310dd0
 
5cd96ae
7310dd0
 
 
 
5cd96ae
 
c907a28
 
7310dd0
5cd96ae
7310dd0
8c01ffb
7310dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517bc3e
7310dd0
5cd96ae
49c2e76
7310dd0
db8cd81
7310dd0
 
 
 
8fe992b
 
7310dd0
db8cd81
49c2e76
7310dd0
db8cd81
 
7310dd0
 
db8cd81
 
7310dd0
db8cd81
 
 
 
7310dd0
 
db8cd81
49c2e76
0774b2e
7310dd0
517bc3e
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
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI

# TOOL PERSONALIZZATI

@tool
def my_custom_tool(arg1: str, arg2: int) -> str:
    """Un tool di esempio che al momento non fa nulla di utile.
    
    Args:
        arg1: Il primo argomento.
        arg2: Il secondo argomento.
    
    Returns:
        Una stringa di esempio.
    """
    return "What magic will you build?"

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """Ottiene l'orario corrente in un fuso orario specificato.
    
    Args:
        timezone: Il fuso orario richiesto (es. 'Europe/Rome').
    
    Returns:
        Una stringa con l'orario locale.
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"L'ora locale in {timezone} è: {local_time}"
    except Exception as e:
        return f"Errore nel recupero dell'orario per '{timezone}': {str(e)}"

@tool
def web_search(query: str) -> str:
    """Effettua una ricerca sul web utilizzando DuckDuckGo e restituisce i risultati principali.
    
    Args:
        query: La stringa di ricerca.
    
    Returns:
        Una stringa con i risultati principali della ricerca.
    """
    try:
        search_tool = DuckDuckGoSearchTool()
        results = search_tool.run(query)
        return f"Risultati per '{query}':\n{results}"
    except Exception as e:
        return f"Errore nella ricerca web: {str(e)}"

# STRUMENTO DI RISPOSTA FINALE
final_answer = FinalAnswerTool()

# CONFIGURAZIONE DEL MODELLO AI
model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)

# IMPORT TOOL PER GENERAZIONE IMMAGINI
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

# CARICAMENTO TEMPLATE DI PROMPT
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

# CREAZIONE DELL'AGENTE AI
agent = CodeAgent(
    model=model,
    tools=[final_answer, web_search, get_current_time_in_timezone, my_custom_tool],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name="AI Helper",
    description="Un assistente AI con ricerca web e strumenti utili.",
    prompt_templates=prompt_templates
)

# AVVIO DELL'INTERFACCIA GRADIO
GradioUI(agent).launch()