File size: 3,396 Bytes
8cf6c08
9b5b26a
 
c19d193
cfbddd7
 
6aae614
febd08c
cfbddd7
febd08c
0306084
8fe992b
9b5b26a
 
cfbddd7
 
 
9b5b26a
cfbddd7
b336ce8
 
9b5b26a
b336ce8
 
9b5b26a
b336ce8
cfbddd7
9b5b26a
 
 
cfbddd7
9b5b26a
cfbddd7
9b5b26a
 
 
 
cfbddd7
9b5b26a
cfbddd7
8c01ffb
0306084
 
 
 
 
 
 
 
 
 
 
5db5b98
0c552d0
5db5b98
0c552d0
5db5b98
 
 
 
 
 
8c01ffb
cfbddd7
ae7a494
cfbddd7
febd08c
cfbddd7
9c9c8ff
ae7a494
cfbddd7
 
 
 
13d500a
8c01ffb
cfbddd7
8c01ffb
8cf6c08
cfbddd7
 
 
 
 
 
 
8c01ffb
cfbddd7
861422e
cfbddd7
 
 
8c01ffb
8fe992b
cfbddd7
febd08c
 
cfbddd7
 
05ae7f4
5db5b98
cfbddd7
 
 
 
8c01ffb
 
 
 
 
 
cfbddd7
8fe992b
 
cfbddd7
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from smolagents import CodeAgent, HfApiModel, load_tool, tool
import datetime
import pytz
import yaml

# ==== Import des tools définis dans tools/ ====
from tools.final_answer import FinalAnswerTool
#from tools.web_search import DuckDuckGoSearchTool
from tools.visit_webpage import VisitWebpageTool
from tools.http_search import HttpSearchTool
from tools.image_wrapper import save_image_to_tmp

from Gradio_UI import GradioUI


# ==== Nouveaux tools Python ====

@tool
def my_custom_tool(arg1: str, arg2: int) -> str:
    """Tool de démonstration.
    Retourne une phrase en français décrivant l'appel effectué.
    Args:
        arg1: premier argument textuel.
        arg2: second argument numérique.
    """
    return f'Le tool my_custom_tool a été appelé avec arg1="{arg1}" et arg2={arg2}.'


@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """Retourne l'heure locale actuelle dans un fuseau horaire donné.
    Args:
        timezone: Fuseau horaire valide (ex: 'Europe/Paris', 'America/New_York').
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"L'heure locale actuelle dans {timezone} est : {local_time}"
    except Exception as e:
        return f"Erreur pour le fuseau '{timezone}' : {str(e)}"

@tool
def generate_image_and_save(prompt: str) -> str:
    """Génère une image via image_generation_tool, la sauvegarde dans /tmp
    et renvoie le chemin du fichier.
    Args:
        prompt: description textuelle de l'image à générer.
    """
    img = image_generation_tool(prompt=prompt)
    filepath = save_image_to_tmp(img)
    return f"Image sauvegardée dans : {filepath}"

@tool
def generate_image(prompt: str) -> object:
    """Génère une image à partir d'un prompt et la renvoie directement.
    L'UI Gradio pourra l'afficher si elle gère les objets image.
    Args:
        prompt: description textuelle de l'image.
    """
    img = image_generation_tool(prompt=prompt)
    return img


# ==== Instanciation des tools existants ====

final_answer = FinalAnswerTool()
#web_search = DuckDuckGoSearchTool()
visit_webpage = VisitWebpageTool()
http_search = HttpSearchTool()

# Tool Hub text->image (prévu dans le cours)
image_generation_tool = load_tool(
    "agents-course/text-to-image",
    trust_remote_code=True,
)

# ==== Modèle LLM (Qwen2.5 Coder 32B Instruct) ====

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
    custom_role_conversions=None,
)

# ==== Prompts (prompts.yaml) ====

with open("prompts.yaml", "r") as stream:
    prompt_templates = yaml.safe_load(stream)

# ==== Création de l’agent ====

agent = CodeAgent(
    model=model,
    tools=[
        #web_search,                 # DuckDuckGo web search
        http_search,
        visit_webpage,              # lecture de page web
        image_generation_tool,      # génération d’image
        generate_image_and_save,
        generate_image,
        my_custom_tool,             # tool custom
        get_current_time_in_timezone,
        final_answer,               # final_answer doit être présent
    ],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates,
)

GradioUI(agent).launch()