|
|
@@ -1,17 +1,18 @@
|
|
|
-from anthropic import Anthropic
|
|
|
+import google.generativeai as genai
|
|
|
from typing import List, Dict, Any
|
|
|
import json
|
|
|
from app.config import get_settings
|
|
|
|
|
|
|
|
|
class BookRecommender:
|
|
|
- """AI-powered book recommendation engine using Claude."""
|
|
|
+ """AI-powered book recommendation engine using Gemini."""
|
|
|
|
|
|
def __init__(self):
|
|
|
settings = get_settings()
|
|
|
- if not settings.anthropic_api_key:
|
|
|
- raise ValueError("ANTHROPIC_API_KEY not configured")
|
|
|
- self.client = Anthropic(api_key=settings.anthropic_api_key)
|
|
|
+ if not settings.gemini_api_key:
|
|
|
+ raise ValueError("GEMINI_API_KEY not configured")
|
|
|
+ genai.configure(api_key=settings.gemini_api_key)
|
|
|
+ self.model = genai.GenerativeModel('gemini-pro')
|
|
|
|
|
|
async def generate_recommendations(
|
|
|
self,
|
|
|
@@ -31,7 +32,7 @@ class BookRecommender:
|
|
|
# Build context from reading history
|
|
|
history_text = self._format_reading_history(reading_history)
|
|
|
|
|
|
- # Create prompt for Claude
|
|
|
+ # Create prompt for Gemini
|
|
|
prompt = f"""Based on this reading history, recommend {num_recommendations} audiobooks that this person would enjoy.
|
|
|
|
|
|
Reading History:
|
|
|
@@ -47,15 +48,11 @@ For each recommendation, provide:
|
|
|
Format your response as a JSON array with objects containing: title, author, description, reason, genres.
|
|
|
Only respond with the JSON array, no additional text."""
|
|
|
|
|
|
- # Call Claude API
|
|
|
- message = self.client.messages.create(
|
|
|
- model="claude-3-5-sonnet-20241022",
|
|
|
- max_tokens=2048,
|
|
|
- messages=[{"role": "user", "content": prompt}]
|
|
|
- )
|
|
|
+ # Call Gemini API
|
|
|
+ response = self.model.generate_content(prompt)
|
|
|
|
|
|
# Parse response
|
|
|
- response_text = message.content[0].text
|
|
|
+ response_text = response.text
|
|
|
try:
|
|
|
recommendations = json.loads(response_text)
|
|
|
return recommendations
|
|
|
@@ -116,10 +113,6 @@ Only respond with the JSON array, no additional text."""
|
|
|
|
|
|
Provide a thoughtful 2-3 paragraph explanation connecting specific aspects of the recommended book to their reading preferences."""
|
|
|
|
|
|
- message = self.client.messages.create(
|
|
|
- model="claude-3-5-sonnet-20241022",
|
|
|
- max_tokens=512,
|
|
|
- messages=[{"role": "user", "content": prompt}]
|
|
|
- )
|
|
|
+ response = self.model.generate_content(prompt)
|
|
|
|
|
|
- return message.content[0].text
|
|
|
+ return response.text
|