Spaces:
Sleeping
Sleeping
Update tools/book_tool.py
Browse files- tools/book_tool.py +37 -17
tools/book_tool.py
CHANGED
|
@@ -6,9 +6,9 @@ class BookSearchTool(Tool):
|
|
| 6 |
name = "search_books"
|
| 7 |
description = "Search for books using the Open Library API"
|
| 8 |
inputs = {'query': {'type': 'string', 'description': 'The search query to find books.'}}
|
| 9 |
-
output_type =
|
| 10 |
|
| 11 |
-
def forward(self, query: str) ->
|
| 12 |
try:
|
| 13 |
url = f"https://openlibrary.org/search.json?q={query}&limit=5"
|
| 14 |
response = requests.get(url)
|
|
@@ -25,32 +25,52 @@ class BookSearchTool(Tool):
|
|
| 25 |
'key': book.get('key', '')
|
| 26 |
}
|
| 27 |
results.append(result)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
return
|
| 31 |
|
| 32 |
class BookDetailsTool(Tool):
|
| 33 |
name = "get_book_details"
|
| 34 |
description = "Get detailed information about a specific book from Open Library"
|
| 35 |
inputs = {'book_key': {'type': 'string', 'description': 'The Open Library key for the book.'}}
|
| 36 |
-
output_type =
|
| 37 |
|
| 38 |
-
def forward(self, book_key: str) ->
|
| 39 |
try:
|
| 40 |
url = f"https://openlibrary.org{book_key}.json"
|
| 41 |
response = requests.get(url)
|
| 42 |
response.raise_for_status()
|
| 43 |
data = response.json()
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
-
return
|
|
|
|
| 6 |
name = "search_books"
|
| 7 |
description = "Search for books using the Open Library API"
|
| 8 |
inputs = {'query': {'type': 'string', 'description': 'The search query to find books.'}}
|
| 9 |
+
output_type = "string"
|
| 10 |
|
| 11 |
+
def forward(self, query: str) -> str:
|
| 12 |
try:
|
| 13 |
url = f"https://openlibrary.org/search.json?q={query}&limit=5"
|
| 14 |
response = requests.get(url)
|
|
|
|
| 25 |
'key': book.get('key', '')
|
| 26 |
}
|
| 27 |
results.append(result)
|
| 28 |
+
|
| 29 |
+
# Format results as a string
|
| 30 |
+
formatted_results = "## Book Search Results\n\n"
|
| 31 |
+
for book in results:
|
| 32 |
+
formatted_results += f"### {book['title']}\n"
|
| 33 |
+
formatted_results += f"- Author: {book['author']}\n"
|
| 34 |
+
formatted_results += f"- Year: {book['first_publish_year']}\n"
|
| 35 |
+
formatted_results += f"- Subjects: {', '.join(book['subject'])}\n\n"
|
| 36 |
+
|
| 37 |
+
return formatted_results
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"Error searching books: {str(e)}"
|
| 41 |
|
| 42 |
class BookDetailsTool(Tool):
|
| 43 |
name = "get_book_details"
|
| 44 |
description = "Get detailed information about a specific book from Open Library"
|
| 45 |
inputs = {'book_key': {'type': 'string', 'description': 'The Open Library key for the book.'}}
|
| 46 |
+
output_type = "string"
|
| 47 |
|
| 48 |
+
def forward(self, book_key: str) -> str:
|
| 49 |
try:
|
| 50 |
url = f"https://openlibrary.org{book_key}.json"
|
| 51 |
response = requests.get(url)
|
| 52 |
response.raise_for_status()
|
| 53 |
data = response.json()
|
| 54 |
|
| 55 |
+
description = data.get('description', {}).get('value', 'No description available') \
|
| 56 |
+
if isinstance(data.get('description'), dict) \
|
| 57 |
+
else data.get('description', 'No description available')
|
| 58 |
+
|
| 59 |
+
subjects = data.get('subjects', [])[:5]
|
| 60 |
+
first_sentence = data.get('first_sentence', {}).get('value', 'Not available') \
|
| 61 |
+
if isinstance(data.get('first_sentence'), dict) \
|
| 62 |
+
else data.get('first_sentence', 'Not available')
|
| 63 |
+
|
| 64 |
+
# Format as a string
|
| 65 |
+
details = f"## {data.get('title', 'Unknown Title')}\n\n"
|
| 66 |
+
details += "### Description\n"
|
| 67 |
+
details += f"{description}\n\n"
|
| 68 |
+
details += "### Subjects\n"
|
| 69 |
+
details += ', '.join(subjects) + "\n\n"
|
| 70 |
+
details += "### First Sentence\n"
|
| 71 |
+
details += f"{first_sentence}\n"
|
| 72 |
+
|
| 73 |
+
return details
|
| 74 |
+
|
| 75 |
except Exception as e:
|
| 76 |
+
return f"Error fetching book details: {str(e)}"
|