Adding zet tools : terminus_to_routes and create map link
Browse files- app.py +26 -32
- prompts.yaml +7 -1
- requirements.txt +2 -1
- tools/zet.py +84 -0
app.py
CHANGED
@@ -1,37 +1,24 @@
|
|
1 |
-
|
|
|
|
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
arg2: the second argument
|
18 |
-
"""
|
19 |
-
return "What magic will you build ?"
|
20 |
-
|
21 |
-
@tool
|
22 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
-
"""A tool that fetches the current local time in a specified timezone.
|
24 |
-
Args:
|
25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
26 |
-
"""
|
27 |
-
try:
|
28 |
-
# Create timezone object
|
29 |
-
tz = pytz.timezone(timezone)
|
30 |
-
# Get current time in that timezone
|
31 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
32 |
-
return f"The current local time in {timezone} is: {local_time}"
|
33 |
-
except Exception as e:
|
34 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
@@ -39,11 +26,18 @@ final_answer = FinalAnswerTool()
|
|
39 |
# 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:
|
40 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
41 |
|
42 |
-
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
46 |
-
custom_role_conversions=None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
|
@@ -55,7 +49,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer,
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,OpenAIServerModel, load_tool,tool
|
4 |
import datetime
|
5 |
import requests
|
6 |
import pytz
|
7 |
import yaml
|
8 |
from tools.final_answer import FinalAnswerTool
|
9 |
+
from tools.zet import create_link_to_public_transport_map, terminus_to_routes
|
10 |
+
from tools.web_search import DuckDuckGoSearchTool
|
11 |
+
from typing import List
|
12 |
|
13 |
from Gradio_UI import GradioUI
|
14 |
|
15 |
+
# @tool
|
16 |
+
# def get_route_for_terminus(terminus: str): -> int:
|
17 |
+
# """A tool that fetches the route number for a given terminus.
|
18 |
+
# Args:
|
19 |
+
# terminus: the terminus to search for
|
20 |
+
# """
|
21 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
|
24 |
final_answer = FinalAnswerTool()
|
|
|
26 |
# 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:
|
27 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
28 |
|
29 |
+
# model = HfApiModel(
|
30 |
+
# max_tokens=2096,
|
31 |
+
# temperature=0.5,
|
32 |
+
# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
33 |
+
# custom_role_conversions=None,
|
34 |
+
# )
|
35 |
+
|
36 |
+
model = OpenAIServerModel(
|
37 |
+
model_id="gpt-4o-mini",
|
38 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
39 |
+
temperature=0.5,
|
40 |
+
max_tokens=2096,
|
41 |
)
|
42 |
|
43 |
|
|
|
49 |
|
50 |
agent = CodeAgent(
|
51 |
model=model,
|
52 |
+
tools=[final_answer, create_link_to_public_transport_map, terminus_to_routes],
|
53 |
max_steps=6,
|
54 |
verbosity_level=1,
|
55 |
grammar=None,
|
prompts.yaml
CHANGED
@@ -1,5 +1,11 @@
|
|
1 |
"system_prompt": |-
|
2 |
-
You are an expert assistant who can solve any task using code blobs.
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
|
4 |
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
|
5 |
|
|
|
1 |
"system_prompt": |-
|
2 |
+
You are an expert assistant who can solve any task using code blobs.
|
3 |
+
|
4 |
+
Your current task is to help a user find the closest tram or bus.
|
5 |
+
The user will speak Croatian.
|
6 |
+
The user will ask you for a specific tram or bus defined by it's number or route.
|
7 |
+
Your job is to give them a link to the map with the specified tram or bus route.
|
8 |
+
|
9 |
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
|
10 |
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
|
11 |
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
markdownify
|
2 |
-
smolagents
|
3 |
requests
|
4 |
duckduckgo_search
|
5 |
pandas
|
|
|
|
1 |
markdownify
|
2 |
+
smolagents[openai,gradio]
|
3 |
requests
|
4 |
duckduckgo_search
|
5 |
pandas
|
6 |
+
IPython
|
tools/zet.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional, List
|
2 |
+
from smolagents import tool
|
3 |
+
from smolagents.tools import Tool
|
4 |
+
|
5 |
+
import duckduckgo_search
|
6 |
+
|
7 |
+
@tool
|
8 |
+
def create_link_to_public_transport_map(routes: List[str]) -> str:
|
9 |
+
"""A tool that create a link for a map with public transport routes. It can be used to display a route on a map.
|
10 |
+
Args:
|
11 |
+
routes: list of routes to display on the map
|
12 |
+
"""
|
13 |
+
try:
|
14 |
+
return f"https://ntrd.top/?routes="+"&routes=".join(routes)
|
15 |
+
except Exception as e:
|
16 |
+
return f"Error fetching link to maps for route '{routes}'"
|
17 |
+
|
18 |
+
|
19 |
+
@tool
|
20 |
+
def terminus_to_routes(terminus: str) -> List[str]:
|
21 |
+
"""
|
22 |
+
A tool that fetches the route numbers for a given terminus but works only for trams not for buses.
|
23 |
+
Example: terminus_to_routes('ZAPADNI KOLODVOR') -> [1]; terminus_to_routes('ZAPRUDE') -> [8, 14]
|
24 |
+
Args:
|
25 |
+
terminus: the terminus to search for
|
26 |
+
"""
|
27 |
+
terminus_to_route_map = {
|
28 |
+
'ZAPADNI KOLODVOR': [1],
|
29 |
+
'BORONGAJ': [1, 9, 17, 32],
|
30 |
+
'ČRNOMEREC': [2, 6, 11, 31],
|
31 |
+
'SAVIŠĆE': [2, 3, 33],
|
32 |
+
'LJUBLJANICA': [3, 9, 12, 34],
|
33 |
+
'SAVSKI MOST': [4, 7, 31],
|
34 |
+
'DUBEC': [4, 11, 34],
|
35 |
+
'PREČKO': [5, 17, 32],
|
36 |
+
'PARK MAKSIMIR': [5],
|
37 |
+
'SOPOT': [6],
|
38 |
+
'DUBRAVA': [7, 12],
|
39 |
+
'MIHALJEVAC': [8, 14, 15, 33],
|
40 |
+
'ZAPRUĐE': [8, 14],
|
41 |
+
'ŽITNJAK': [13],
|
42 |
+
'KVATERNIKOV TRG': [13],
|
43 |
+
'GRAČANSKO DOLJE': [15]
|
44 |
+
}
|
45 |
+
routes = terminus_to_route_map.get(terminus.upper(), None)
|
46 |
+
if routes:
|
47 |
+
return routes
|
48 |
+
else:
|
49 |
+
return f"No routes found for terminus '{terminus}'. Available terminus: {list(terminus_to_route_map.keys())}"
|
50 |
+
|
51 |
+
|
52 |
+
# class TerminusToRoutes(Tool):
|
53 |
+
# name = "terminus_to_route"
|
54 |
+
# description = "A tool that fetches the route numbers for a given terminus but works only for trams not for buses. Example: 'ZAPADNI KOLODVOR' -> [1]; 'ZAPRUDE' -> [8, 14]"
|
55 |
+
# inputs = {'terminus': {'type': 'string', 'description': 'The terminus to search for.'}}
|
56 |
+
# output_type = "list"
|
57 |
+
#
|
58 |
+
# def __init__(self):
|
59 |
+
# super().__init__()
|
60 |
+
# self.terminus_to_route_map = {
|
61 |
+
# 'ZAPADNI KOLODVOR': [1],
|
62 |
+
# 'BORONGAJ': [1, 9, 17, 32],
|
63 |
+
# 'ČRNOMEREC': [2, 6, 11, 31],
|
64 |
+
# 'SAVIŠĆE': [2, 3, 33],
|
65 |
+
# 'LJUBLJANICA': [3, 9, 12, 34],
|
66 |
+
# 'SAVSKI MOST': [4, 7, 31],
|
67 |
+
# 'DUBEC': [4, 11, 34],
|
68 |
+
# 'PREČKO': [5, 17, 32],
|
69 |
+
# 'PARK MAKSIMIR': [5],
|
70 |
+
# 'SOPOT': [6],
|
71 |
+
# 'DUBRAVA': [7, 12],
|
72 |
+
# 'MIHALJEVAC': [8, 14, 15, 33],
|
73 |
+
# 'ZAPRUĐE': [8, 14],
|
74 |
+
# 'ŽITNJAK': [13],
|
75 |
+
# 'KVATERNIKOV TRG': [13],
|
76 |
+
# 'GRAČANSKO DOLJE': [15]
|
77 |
+
# }
|
78 |
+
#
|
79 |
+
# def forward(self, terminus: str) -> str:
|
80 |
+
# routes = self.terminus_to_route_map.get(terminus.upper())
|
81 |
+
# if routes:
|
82 |
+
# return routes
|
83 |
+
# else:
|
84 |
+
# return f"No routes found for terminus '{terminus}'. Available terminus: {list(self.terminus_to_route_map.keys())}"
|