File size: 4,487 Bytes
3696fe2
9b5b26a
 
 
c19d193
2d2c435
6aae614
2d2c435
 
8fe992b
9b5b26a
 
5df72d6
9b5b26a
3d1237b
9b5b26a
 
 
 
 
 
 
 
2d2c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
 
 
 
 
 
 
 
 
 
 
4d634a3
9b5b26a
 
8c01ffb
 
6aae614
ae7a494
 
3696fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13d500a
8c01ffb
 
9b5b26a
 
8c01ffb
861422e
 
9b5b26a
8c01ffb
8fe992b
2d2c435
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
9b5b26a
2d2c435
 
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
134
135
136
137
138
139
140
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import os
from tools.final_answer import FinalAnswerTool
from tools import nlu_tool, scheduler, requests_store
from smolagents import tool as tool_decorator

from Gradio_UI import GradioUI

# Below is an example of a tool that does nothing. Amaze us with your creativity !
@tool
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
    #Keep this format for the description / args / args description but feel free to modify the tool
    """A tool that does nothing yet 
    Args:
        arg1: the first argument
        arg2: the second argument
    """
    return "What magic will you build ?"


@tool_decorator
def nlu(text: str) -> dict:
    """Run NLU (intent + slots) on user text.

    Args:
        text: The user's message to analyze.
    """
    return nlu_tool.extract_intent_and_slots(text)


@tool_decorator
def propose_slots(preferred_windows: dict = None) -> list:
    """Return up to 3 candidate operator slots based on preferred windows.

    Args:
        preferred_windows: Optional list of dicts with 'start' and 'end' strings
            (natural language or ISO). Example: [{'start': 'tomorrow 09:00', 'end': 'tomorrow 12:00'}].
    """
    cust_windows = []
    if preferred_windows:
        for w in preferred_windows:
            try:
                # use dateparser to flexibly parse the start/end and normalize to Asia/Tokyo
                import dateparser
                settings = {'TIMEZONE': 'Asia/Tokyo', 'RETURN_AS_TIMEZONE_AWARE': True}
                s = dateparser.parse(w['start'], settings=settings)
                e = dateparser.parse(w['end'], settings=settings)
                if s and e:
                    cust_windows.append({'start': s, 'end': e})
            except Exception:
                continue
    slots = scheduler.find_common_slots(cust_windows)
    return slots


@tool_decorator
def create_request(payload: dict) -> dict:
    """Persist a handoff request and return stored record.

    Args:
        payload: dict containing the handoff data (customer, account, amount, date_by_when, etc.)
    """
    return requests_store.create_request(payload)


@tool_decorator
def notify_operator(message: str) -> str:
    """Stub: notify operator (logs the message into data/operator_notifications.log)

    Args:
        message: The notification content to send to operator.
    """
    os.makedirs('data', exist_ok=True)
    path = 'data/operator_notifications.log'
    with open(path, 'a') as f:
        f.write(message + "\n---\n")
    return "ok"

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """A tool that fetches the current local time in a specified timezone.
    Args:
        timezone: A string representing a valid timezone (e.g., 'America/New_York').
    """
    try:
        # Create timezone object
        tz = pytz.timezone(timezone)
        # Get current time in that timezone
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return local_time
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"


final_answer = FinalAnswerTool()


# Get OpenAI API key from environment variable
openai_api_key = os.getenv('OPENAI_API_KEY')
if not openai_api_key:
    raise ValueError("Please set the OPENAI_API_KEY environment variable")

import urllib3
# Disable SSL verification warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

model = OpenAIServerModel(
    api_key=openai_api_key,
    model_id="gpt-5-mini-2025-08-07",  # Using GPT-4 Turbo, you can change to gpt-3.5-turbo for lower cost
    max_tokens=2096,
    # verify_ssl=False,  # Disable SSL verification
    temperature=0.5,
)


# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
agent = CodeAgent(
    model=model,
    tools=[final_answer, get_current_time_in_timezone, nlu, propose_slots, create_request, notify_operator], ## add your tools here (don't remove final answer)
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)


if __name__ == '__main__':
    GradioUI(agent).launch()