Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| from typing import Any, Optional | |
| from smolagents.tools import Tool | |
| class NYTBestSellerTool(Tool): | |
| name = "nyt_best_sellers_tool" | |
| description = ( | |
| "Performs a search of the New York Times best seller list " | |
| "for a specific genre and returns the best selling books.\n" | |
| "The possible options for genre are:\n" | |
| "combined-print-and-e-book-fiction, " | |
| "combined-print-and-e-book-nonfiction, " | |
| "hardcover-fiction, " | |
| "hardcover-nonfiction, " | |
| "trade-fiction-paperback, " | |
| "paperback-nonfiction, " | |
| "advice-how-to-and-miscellaneous, " | |
| "childrens-middle-grade-hardcover, " | |
| "picture-books, " | |
| "series-books, " | |
| "young-adult-hardcover, " | |
| "audio-fiction, " | |
| "audio-nonfiction, " | |
| "business-books, " | |
| "graphic-books-and-manga, " | |
| "mass-market-monthly, " | |
| "middle-grade-paperback-monthly, " | |
| "young-adult-paperback-monthly." | |
| ) | |
| inputs = { | |
| 'genre': | |
| {'type': 'string', 'description': 'The genre to search'}, | |
| 'limit': | |
| {'type': 'integer', 'description': 'The number of results to include', 'nullable': True} | |
| } | |
| output_type = "string" | |
| def __init__(self): | |
| super().__init__() | |
| self.api_prefix = "https://api.nytimes.com/svc/books/v3/lists/current" | |
| self.api_key = os.getenv("NYT_API_KEY") | |
| self.possible_genres = [ | |
| "combined-print-and-e-book-fiction", | |
| "combined-print-and-e-book-nonfiction", | |
| "hardcover-fiction", | |
| "hardcover-nonfiction", | |
| "trade-fiction-paperback", | |
| "paperback-nonfiction", | |
| "advice-how-to-and-miscellaneous", | |
| "childrens-middle-grade-hardcover", | |
| "picture-books", | |
| "series-books", | |
| "young-adult-hardcover", | |
| "audio-fiction", | |
| "audio-nonfiction", | |
| "business-books", | |
| "graphic-books-and-manga", | |
| "mass-market-monthly", | |
| "middle-grade-paperback-monthly", | |
| "young-adult-paperback-monthly" | |
| ] | |
| def forward(self, genre: str, limit: int = 5) -> str: | |
| if isinstance(limit,str): | |
| limit = int(limit) | |
| if genre not in self.possible_genres: | |
| raise Exception( | |
| f"{genre} is not a valid genre. Please search by a genre from the list: {self.possible_genres}" | |
| ) | |
| result = requests.get( | |
| f"https://api.nytimes.com/svc/books/v3/lists/{genre}.json", | |
| params={"api-key":self.api_key} | |
| ) | |
| if result.status_code != 200: | |
| raise Exception("Error getting the best seller list. Please try again later.") | |
| book_results = result.json()['results']['books'] | |
| book_result_str = "" | |
| for ix, book_result in enumerate(book_results): | |
| rank = book_result.get("rank") | |
| if rank: | |
| book_result_str += f"rank: {rank}\n" | |
| title = book_result.get("title") | |
| if title: | |
| book_result_str += f"title: {title}\n" | |
| author = book_result.get("author") | |
| if author: | |
| book_result_str += f"author: {author}\n" | |
| description = book_result.get("description") | |
| if description: | |
| book_result_str += f"description: {description}\n" | |
| primary_isbn10 = book_result.get("primary_isbn10") | |
| if primary_isbn10: | |
| book_result_str += f"primary_isbn10: {primary_isbn10}\n" | |
| primary_isbn13 = book_result.get("primary_isbn13") | |
| if primary_isbn13: | |
| book_result_str += f"primary_isbn13: {primary_isbn13}\n" | |
| buy_links = book_result.get("buy_links") | |
| if buy_links: | |
| book_result_str += f"buy_links: " | |
| for buy_link in buy_links: | |
| book_result_str += f"{buy_link.get('name')} {buy_link.get('url')}\n" | |
| book_result_str += "=============================\n\n" | |
| if ix + 1 == limit: | |
| break | |
| return book_result_str | |