-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
183 lines (146 loc) · 6.31 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import requests
import cohere
from typing import List, Dict, Optional
import logging
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
class GoogleBooksAssistant:
def __init__(self, cohere_api_key: str, google_books_api_key: str):
"""
Initialize the Google Books AI Assistant.
Args:
cohere_api_key (str): Your Cohere API key
google_books_api_key (str): Your Google Books API key
"""
self.cohere_client = cohere.Client(cohere_api_key)
self.google_books_api_key = google_books_api_key
self.google_books_base_url = "https://www.googleapis.com/books/v1/volumes"
# Configure logging
logging.basicConfig(level=logging.INFO)
self.logger = logging.getLogger(__name__)
def search_books(self, query: str, max_results: int = 5) -> List[Dict]:
"""
Search for books using the Google Books API.
Args:
query (str): Search query
max_results (int): Maximum number of results to return
Returns:
List[Dict]: List of book information dictionaries
"""
try:
params = {
'q': query,
'key': self.google_books_api_key,
'maxResults': max_results
}
response = requests.get(self.google_books_base_url, params=params)
response.raise_for_status()
results = response.json()
books = []
for item in results.get('items', []):
volume_info = item.get('volumeInfo', {})
books.append({
'title': volume_info.get('title'),
'authors': volume_info.get('authors', []),
'description': volume_info.get('description', ''),
'categories': volume_info.get('categories', []),
'preview_link': volume_info.get('previewLink'),
'page_count': volume_info.get('pageCount')
})
return books
except requests.exceptions.RequestException as e:
self.logger.error(f"Error searching Google Books API: {str(e)}")
return []
def analyze_books(self, books: List[Dict], question: str) -> str:
"""
Analyze books using Cohere's LLM to answer questions about them.
Args:
books (List[Dict]): List of book information
question (str): User's question about the books
Returns:
str: AI-generated response analyzing the books
"""
try:
# Create a context from the books data
context = "\n".join([
f"Book: {book['title']}\n"
f"Authors: {', '.join(book['authors'])}\n"
f"Description: {book['description']}\n"
f"Categories: {', '.join(book['categories'])}\n"
for book in books
])
# Create prompt for the LLM
prompt = f"""Based on the following books information:
{context}
Question: {question}
Please provide a detailed analysis of these books in relation to the question. Include relevant comparisons, themes, and insights."""
# Generate response using Cohere
response = self.cohere_client.generate(
model='command',
prompt=prompt,
max_tokens=500,
temperature=0.7,
k=0,
stop_sequences=[],
return_likelihoods='NONE'
)
return response.generations[0].text.strip()
except Exception as e:
self.logger.error(f"Error analyzing books with Cohere: {str(e)}")
return "I apologize, but I encountered an error while analyzing the books."
def recommend_similar_books(self, book_title: str, max_recommendations: int = 3) -> List[Dict]:
"""
Recommend similar books based on a given book title.
Args:
book_title (str): Title of the reference book
max_recommendations (int): Maximum number of recommendations
Returns:
List[Dict]: List of recommended book information
"""
try:
# First, search for the reference book
reference_book = self.search_books(book_title, max_results=1)
if not reference_book:
return []
# Extract categories and relevant terms from the reference book
categories = reference_book[0].get('categories', [])
# Search for books in similar categories
similar_books = []
for category in categories:
query = f"subject:{category}"
books = self.search_books(query, max_results=max_recommendations)
# Filter out the reference book and duplicates
books = [book for book in books if book['title'] != book_title]
similar_books.extend(books)
# Limit to max_recommendations
return similar_books[:max_recommendations]
except Exception as e:
self.logger.error(f"Error recommending similar books: {str(e)}")
return []
def main():
"""
Example usage of the GoogleBooksAssistant
"""
# Initialize the assistant with API keys
assistant = GoogleBooksAssistant(
cohere_api_key=os.getenv("COHERE_API_KEY"),
google_books_api_key=os.getenv("GOOGLE_BOOKS_API_KEY")
)
# Example: Search for books about artificial intelligence
books = assistant.search_books("artificial intelligence ethics")
# Example: Analyze the books
analysis = assistant.analyze_books(
books,
"What are the main ethical concerns discussed in these books regarding AI?"
)
# Example: Get recommendations
recommendations = assistant.recommend_similar_books("Superintelligence by Nick Bostrom")
# Print results
print("Analysis:", analysis)
print("\nRecommended Books:")
for book in recommendations:
print(f"- {book['title']} by {', '.join(book['authors'])}")
if __name__ == "__main__":
main()