File size: 3,915 Bytes
9b5b26a
 
 
 
c19d193
6aae614
8fe992b
9b5b26a
 
79a047a
 
 
9c71233
e8a5d48
c6121ee
 
0ae2bef
c6121ee
 
1f172b4
e8a5d48
c6121ee
 
0ae2bef
1f172b4
d10d1b1
c6121ee
d4ddfdb
 
 
 
 
 
c6121ee
1f172b4
 
0ae2bef
1f172b4
652e3aa
 
 
0ae2bef
652e3aa
 
 
 
d4ddfdb
1f172b4
 
 
 
9b5b26a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
 
6aae614
ae7a494
 
a24a3dd
ae7a494
e121372
bf6d34c
fed7828
a24a3dd
fe328e0
13d500a
8c01ffb
 
9b5b26a
 
8c01ffb
861422e
 
9b5b26a
8c01ffb
8fe992b
e8a5d48
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
9b5b26a
8c01ffb
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
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

# Initialize DuckDuckGo search tool
search_tool = DuckDuckGoSearchTool()

@tool
def find_open_restaurants_by_location(location: str, 
                                cuisine: str = None, 
                                rating: float = None, 
                                number_of_options: int = 5
                               )-> str: 
    """A tool that searches for restaurants according to location, cuisine, rating and local time. 
    Args:
        location: A string representing the location area with the optional inclusion of a zip code to search for open restaurants (e.g. Paris, Paris 75005, London, London NW1 4RY).
        cuisine: An optional string representing the cuisine type (e.g. Asian, Indian, Italian, breakfast, lunch, etc.)
        rating: An optional float representing the average customer rating of the restaurant from Google, TripAdvisor, or Yelp.
        number_of_options: An integer representing the number of return options, default set to 5.
    """
    try:
        # Build query dynamically based on provided inputs
        query_parts = [
        f"restaurants open now in {location}",
        f"serving {cuisine} cuisine" if cuisine else "",
        f"rated {rating} or higher" if rating else "",
        ]
        query = " ".join(filter(None, query_parts))
        search_results = search_tool(query)  

        if not search_results:
            return f"❌ No matching restaurants found in {location} at {local_time} fulfilling desired requests."

        # Ensure search_results is a list before iterating    
        if isinstance(search_results, list) and len(search_results) > 0:
            results_str = "\n".join(
                [f"{result.get('title', 'Unknown')} - {result.get('url', 'No URL')}" for result in search_results[:number_of_options]]
            )
        else:
            results_str = str(search_results)  # Convert to string if it's not a list

        return f"🍽️ Open restaurants in {location}:\n\n{results_str}"

    except Exception as e:
        return f"❌ Error finding restaurants: {str(e)}"


@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 f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"


final_answer = FinalAnswerTool()

# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 

model = HfApiModel(
max_tokens=2096,
temperature=0.0,
model_id= model_id,# it is possible that this model may be overloaded
custom_role_conversions=None,
)


# 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, find_open_restaurants_by_location], ## 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
)


GradioUI(agent).launch()