-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai_tools.py
148 lines (142 loc) · 5 KB
/
ai_tools.py
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
141
142
143
144
145
146
147
148
import json
from spotify_client import SpotifyClient
from spotify_helpers import SpotifyHelpers
from logger_config import setup_logger
logger = setup_logger(__name__)
SPOTIFY_TOOLS = [
{
"type": "function",
"function": {
"name": "get_user_profile",
"description": "Get the current user's Spotify profile information including display name, ID, URI, follower count, and profile images",
"parameters": {
"type": "object",
"properties": {},
"strict": True
}
}
},
{
"type": "function",
"function": {
"name": "get_top_items",
"description": "Get user's top artists or top tracks (most frequently played artists or tracks) based on number of plays within a time period for a specific time range, if no time range is provided, default to medium_term",
"parameters": {
"type": "object",
"properties": {
"time_range": {
"type": "string",
"enum": ["short_term", "medium_term", "long_term"],
"description": "Time range for top items"
},
"item_type": {
"type": "string",
"enum": ["artists", "tracks"],
"description": "Type of items to fetch"
}
},
"required": ["time_range", "item_type"],
"strict": True
}
}
},
{
"type": "function",
"function": {
"name": "get_followed_artists",
"description": "Get list of artists the user follows",
"parameters": {
"type": "object",
"properties": {},
"strict": True
}
}
},
{
"type": "function",
"function": {
"name": "get_user_playlists",
"description": "Get user's saved and followed Spotify playlists",
"parameters": {
"type": "object",
"properties": {},
"strict": True
}
}
},
{
"type": "function",
"function": {
"name": "get_saved_podcasts",
"description": "Get user's saved podcasts",
"parameters": {
"type": "object",
"properties": {},
"strict": True
}
}
},
{
"type": "function",
"function": {
"name": "get_recently_played_tracks",
"description": "Get the chronological history of tracks the user has listened to, ordered by most recent play date first",
"parameters": {
"type": "object",
"properties": {},
"strict": True
}
}
},
{
"type": "function",
"function": {
"name": "search_item",
"description": "Search for any item type on Spotify",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"search_type": {
"type": "string",
"enum": ["track", "album", "artist", "playlist", "show", "episode", "audiobook"]
},
"filters": {
"type": "object",
"additionalProperties": True
}
},
"required": ["query", "search_type"],
"strict": True
}
}
}
]
class SpotifyFunctionHandler:
def __init__(self, access_token: str):
self.spotify_client = SpotifyClient(access_token)
self.spotify_helpers = SpotifyHelpers(self.spotify_client)
def execute_function(self, tool_call) -> dict:
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
if name == "get_top_items":
return self.spotify_helpers.get_top_items(args["time_range"], args["item_type"])
elif name == "get_user_profile":
return self.spotify_helpers.get_user_profile()
elif name == "get_followed_artists":
return self.spotify_helpers.get_followed_artists()
elif name == "get_user_playlists":
return self.spotify_helpers.get_user_playlists()
elif name == "get_saved_podcasts":
return self.spotify_helpers.get_saved_podcasts()
elif name == "get_recently_played_tracks":
return self.spotify_helpers.get_recently_played_tracks()
elif name == "search_item":
print("DEBUG! - search_item")
return self.spotify_helpers.search_item(
args["query"],
args["search_type"],
args.get("filters")
)
else:
raise ValueError(f"Unknown function: {name}")