File size: 2,879 Bytes
ebf826c
 
 
 
 
 
 
9b5b26a
 
c086ada
8fe992b
ebf826c
 
 
 
9b5b26a
 
ebf826c
 
 
 
 
9b5b26a
ebf826c
 
 
 
c086ada
ebf826c
c086ada
ebf826c
 
c086ada
 
 
 
ebf826c
 
9b5b26a
ebf826c
9b5b26a
ebf826c
 
 
 
 
 
 
61ce6f4
c086ada
9b5b26a
 
 
ebf826c
 
 
9b5b26a
ebf826c
9b5b26a
ebf826c
9b5b26a
 
ebf826c
 
 
9b5b26a
ebf826c
8c01ffb
 
ebf826c
 
 
 
6aae614
c086ada
61ce6f4
ebf826c
 
 
 
 
 
 
61ce6f4
ebf826c
 
 
 
 
 
 
 
 
61ce6f4
ebf826c
 
 
ae7a494
a129081
 
e121372
a129081
 
ebf826c
 
13d500a
8c01ffb
ebf826c
 
 
 
 
8c01ffb
8fe992b
ebf826c
 
 
 
 
 
 
 
 
 
 
8fe992b
 
61ce6f4
ebf826c
 
 
9b5b26a
7fb947d
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from smolagents import (
    CodeAgent,
    HfApiModel,
    load_tool,
    tool,
)

import datetime
import pytz
import re

from tools.final_answer import FinalAnswerTool
from tools.web_search import DuckDuckGoSearchTool
from tools.visit_webpage import VisitWebpageTool

from Gradio_UI import GradioUI


# ==========================================================
# CUSTOM TOOLS
# ==========================================================

@tool
def calculate_min_price(prices: list[float]) -> str:
    """
    Calculates the minimum value from a list of prices.

    Args:
        prices: List of prices.
    """
    return f"The minimum price is {min(prices)}"


@tool
def extract_price_from_snippet(snippet: str) -> list[str]:
    """
    Extracts prices from a block of text.

    Args:
        snippet: Text containing prices.
    """

    pattern = (
        r"\$\d+(?:,\d{3})*(?:\.\d{2})?"
        r"|\d+(?:,\d{3})*(?:\.\d{2})?\s*"
        r"(?:USD|EUR|GBP|INR|AUD|CAD)?"
    )

    return re.findall(pattern, snippet)


@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """
    Returns current local time.

    Args:
        timezone: Valid pytz timezone.
    """

    try:
        tz = pytz.timezone(timezone)
        now = datetime.datetime.now(tz)
        return now.strftime("%Y-%m-%d %H:%M:%S")

    except Exception as e:
        return str(e)


# ==========================================================
# REQUIRED FINAL ANSWER TOOL
# ==========================================================

final_answer = FinalAnswerTool()


# ==========================================================
# WEB TOOLS
# ==========================================================

web_search = DuckDuckGoSearchTool()
visit_webpage = VisitWebpageTool()


# ==========================================================
# OPTIONAL IMAGE TOOL
# ==========================================================

image_generation_tool = load_tool(
    "agents-course/text-to-image",
    trust_remote_code=True,
)


# ==========================================================
# MODEL
# ==========================================================

import os

model = HfApiModel(
    model_id="Qwen/Qwen2.5-72B-Instruct",
    token=os.environ["HF_TOKEN"],
    max_tokens=2048,
    temperature=0.1,
)


# ==========================================================
# AGENT
# ==========================================================

agent = CodeAgent(
    model=model,
    tools=[
        final_answer,
        web_search,
        visit_webpage,
        calculate_min_price,
        extract_price_from_snippet,
        get_current_time_in_timezone,
    ],
    max_steps=12,
    verbosity_level=2,
    planning_interval=2,
)


# ==========================================================
# UI
# ==========================================================

GradioUI(agent).launch()