Spaces:
Sleeping
Sleeping
work on nyt tool
Browse files- nyt_tool.ipynb +16 -0
- tools/book_search.py +25 -0
nyt_tool.ipynb
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [],
|
3 |
+
"metadata": {
|
4 |
+
"kernelspec": {
|
5 |
+
"display_name": "agents_env",
|
6 |
+
"language": "python",
|
7 |
+
"name": "python3"
|
8 |
+
},
|
9 |
+
"language_info": {
|
10 |
+
"name": "python",
|
11 |
+
"version": "3.10.12"
|
12 |
+
}
|
13 |
+
},
|
14 |
+
"nbformat": 4,
|
15 |
+
"nbformat_minor": 2
|
16 |
+
}
|
tools/book_search.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import Any, Optional
|
3 |
+
from smolagents.tools import Tool
|
4 |
+
import duckduckgo_search
|
5 |
+
|
6 |
+
class NYTSearchTool(Tool):
|
7 |
+
name = "best_sellers_tool"
|
8 |
+
description = (
|
9 |
+
"Performs a search of the New York Times best seller list "
|
10 |
+
"for a specific genre and returns the best selling books"
|
11 |
+
)
|
12 |
+
|
13 |
+
inputs = {'genre': {'type': 'string', 'description': 'The genre to search'}}
|
14 |
+
output_type = "string"
|
15 |
+
|
16 |
+
def __init__(self, max_results=10, **kwargs):
|
17 |
+
super().__init__()
|
18 |
+
self.api_prefix = "https://api.nytimes.com/svc/books/v3/lists/current"
|
19 |
+
|
20 |
+
def forward(self, query: str) -> str:
|
21 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
22 |
+
if len(results) == 0:
|
23 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
24 |
+
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
25 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|