WeByT3 commited on
Commit
fb6708d
·
verified ·
1 Parent(s): 65da152

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +51 -22
tools.py CHANGED
@@ -1,8 +1,6 @@
1
  from langchain_core.tools import tool
2
- import pandas as pd
3
- import os
4
- import re
5
  import requests
 
6
 
7
 
8
  @tool
@@ -52,25 +50,56 @@ def divide(a: int, b: int) -> int:
52
  return a / b
53
 
54
  @tool
55
- def wikidata_search_tool(sparql_query: str) -> str:
56
  """
57
- A tool that allows to search in WikiData for specific information
58
- and returns the information as a json in string format
59
-
60
- Args:
61
- sparql_query: The query to search in wikidata. This must be a SPARQL query.
 
 
62
 
63
- Example:
64
- {"message": "How many albums did the Beatles produce between 1965 and 1968?"}
65
- wikidata_search_tool("SELECT (COUNT(DISTINCT ?album) AS ?albumCount) WHERE {
66
- ?album wdt:P31 wd:Q482994; # instance of album
67
- wdt:P175 wd:Q1299; # performer: The Beatles
68
- wdt:P577 ?publicationDate. # publication date
69
- FILTER(YEAR(?publicationDate) >= 1965 && YEAR(?publicationDate) <= 1968)
70
- }")
71
  """
72
- endpoint_url = "https://query.wikidata.org/sparql"
73
- headers = {"Accept": "application/sparql-results+json"}
74
- response = requests.get(endpoint_url, headers=headers, params={"query": sparql_query})
75
- response.raise_for_status()
76
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from langchain_core.tools import tool
 
 
 
2
  import requests
3
+ from datetime import datetime
4
 
5
 
6
  @tool
 
50
  return a / b
51
 
52
  @tool
53
+ def get_studio_album_count(artist_name: str, start_year: int, end_year: int): -> dict
54
  """
55
+ Search music APIs for relevant information related to music artists.
56
+ It returns a dictionary with the number of albums produced during that interval and the names.
57
+
58
+ Args:
59
+ artist_name: Name of the artist to search
60
+ start_year: The start year for searching information
61
+ end_year: The end year for searching information
62
 
63
+ Exampe:
64
+ {"message": "How many albums did The Beatles produce between 1965 and 1968 ?"}
65
+ get_studio_album_count("The Beatles", 1965, 1968)
 
 
 
 
 
66
  """
67
+ # Step 1: Get artist MBID
68
+ search_url = f"https://musicbrainz.org/ws/2/artist/?query=artist:{artist_name}&fmt=json"
69
+ artist_resp = requests.get(search_url).json()
70
+ if not artist_resp["artists"]:
71
+ return {"error": f"Artist '{artist_name}' not found."}
72
+
73
+ artist_mbid = artist_resp["artists"][0]["id"]
74
+
75
+ # Step 2: Get release-groups (albums)
76
+ album_url = f"https://musicbrainz.org/ws/2/release-group?artist={artist_mbid}&type=album&fmt=json&limit=100"
77
+ albums_resp = requests.get(album_url).json()
78
+
79
+ studio_albums = []
80
+ for release_group in albums_resp.get("release-groups", []):
81
+ title = release_group.get("title", "")
82
+ first_date = release_group.get("first-release-date", "")
83
+
84
+ # Filter by date
85
+ try:
86
+ year = int(first_date[:4])
87
+ except:
88
+ continue
89
+ if not (start_year <= year <= end_year):
90
+ continue
91
+
92
+ # Filter out secondary types (e.g. Live, Compilation)
93
+ secondary_types = release_group.get("secondary-types", [])
94
+ if secondary_types:
95
+ continue # Not a studio album
96
+
97
+ studio_albums.append({
98
+ "title": title,
99
+ "release_year": year,
100
+ })
101
+
102
+ return {
103
+ "studio_album_count": len(studio_albums),
104
+ "studio_album_list": sorted(studio_albums, key=lambda x: x["release_year"]),
105
+ }