Building BookMatch: A Journey into AI-Powered Book Recommendations
Introduction
In the age of digital transformation, personalized experiences are the cornerstone of user engagement. Our latest project, BookMatch, is a book recommendation app that leverages the power of generative AI to provide users with tailored book suggestions. At the heart of BookMatch is OpenAI’s ChatGPT 3.5 Turbo, which curates book details and recommendations based on user interactions.
This blog will take you through the idea behind BookMatch, our tech stack, why we chose Supabase for backend operations, and how we perform prompt engineering to deliver accurate results.
The Idea Behind BookMatch
BookMatch was created to make discovering new books easier and more personal. Traditional recommendation systems often use fixed algorithms that can be limited in their suggestions and requires laborious work of creating database.
For example, if you’ve read and enjoyed a few popular novels, these systems might keep suggesting similar bestsellers, which can become repetitive and not very exciting. They usually don’t adapt well to changes in your reading preferences, making it hard to find new and diverse books.
BookMatch uses Generative AI to offer a more personalized book discovery experience. Imagine you love historical fiction and books with strong female characters. Traditional systems might not connect these interests well, leading to suggestions that don’t match what you’re looking for. Generative AI used in BookMatch analyze your recent likes, dislikes, and ratings to give you book recommendations that fit your specific tastes. For example, it might suggest a lesser-known historical novel with a strong female protagonist — something a traditional system might miss.
Generative AI is also capable of using additional parameters to fine-tune recommendations. You can incorporate factors like reading speed, preferred book length, or favorite authors to further personalize suggestions. This would allow the system to provide even more accurate recommendations based on a wider range of user preferences.
Tech Stack Overview
- Kotlin Multiplatform (KMP): We chose KMP for its ability to share business logic across platforms while allowing native UIs. For Android, we used Jetpack Compose, and for iOS, we used SwiftUI.
- Supabase: This backend as a service (BaaS) provides us with authentication (using Google OAuth) and seamless database operations.
- Generative AI with OpenAI’s ChatGPT 3.5 Turbo: Our AI engine generates detailed book recommendations based on user inputs and interactions using ChatGPT’s chat completion API.
Why Supabase?
Supabase was selected for its robust and developer-friendly platform, offering real-time capabilities, SQL-based databases, and easy integration with Google OAuth for authentication. This allowed us to quickly set up a scalable and secure backend without extensive DevOps overhead.
From Gemini to OpenAI
Initially, we experimented with Gemini 1.5 flash having attractive 1M input token limit and 8k output token limit and Gemini 1.5 pro model having 2M for input and 8k for output tokens for generating book recommendations. However, Gemini’s inability to provide accurate ISBNs or the image url directly led to difficulties in fetching book covers from external APIs. The ISBN inaccuracies resulted in broken user experiences, as book cover images are essential for visual engagement. We then transitioned to OpenAI’s ChatGPT 3.5 Turbo, which provided more reliable data and improved the overall accuracy of our recommendations.
Prompt Engineering for Curated Outputs
Prompt engineering is crucial for harnessing the full potential of generative AI. By carefully crafting prompts and system instructions, we guide the AI to generate relevant and high-quality book recommendations. Here’s how we do it:
- User Feedback Integration: We incorporate user feedback (likes, dislikes, read/unread status, and ratings) into the prompts. This helps the AI understand the user’s preferences better.
- System Instruction: We provide context and guidelines to the AI, ensuring it generates book recommendations that align with user interests and historical interactions. Below is the system instruction passed to OpenAI in our project, feel free to make your own changes when trying it.
- Dynamic Adjustments: The AI’s responses are continually refined based on ongoing user interactions, ensuring the recommendations remain fresh and pertinent as result of providing chat history in the API request.
Database Schema
Our database is designed to efficiently store user data, book details, recommendations, and interaction history (to provide context to AI models). Here’s an overview of the key tables:
- users: Stores user details and preferences.
- categories: Contains genres/categories of books.
- books: Stores generated book information.
- recommendations: Logs each recommendation generated for users.
- recommended_books: Tracks user interactions with recommended books.
- chat_history: Stores the chat history used to provide context for AI requests.
Workflow of BookMatch
- Login: Users start with a Google OAuth login. Upon successful authentication, user data is saved in the database.
- Category Selection: Users select their preferred genres from the categories table. These selections are saved locally and in the database.
- Generating Recommendations: When a user requests recommendations, a curated prompt is sent to OpenAI’s ChatGPT 3.5 Turbo. The response is processed, and unique books are inserted into the books table.
- Storing Recommendations: A new recommendation entry is created, and the recommended books are logged in the recommended_books table. The chat history is also updated.
- User Interactions: Users can like/dislike, mark as read/unread, and rate books. These interactions refine future recommendations.
- Fetching Book Covers: Book images are fetched using the Open Library Book Cover API based on the ISBNs provided by the AI.
- Filtering Recommendations: Users can filter past recommendations by selecting specific timestamps, allowing them to revisit earlier suggestions.
Challenges and Solutions
- Inaccurate ISBNs with Gemini: Switching to OpenAI solved the issue of inaccurate ISBNs, ensuring we could reliably fetch book covers.
- Bulk Book Covers Handling: Google Book API’s limitations of not providing bulk image request on multiple book instead providing ton of information on single book led us to seek alternative methods for fetching book covers. Using Open Library’s Book cover API, we could efficiently display book images.
- Genre Name Inconsistencies: The AI sometimes changes the genre names (e.g., “historical fiction” to “fiction”) or adds hyphens and underscores in genre names (e.g., “self-help”, “science_fiction”). We handle these inconsistencies by normalizing the genre names before processing the recommendations.
Pros and Cons of Using Generative AI for Book Recommendations
Pros of Using Generative AI
- Dynamic Data Generation:
- Traditional Approach: Managing book details usually means either manually entering data into a database or setting up web scraping or API integrations. This process is not only labor-intensive but also prone to errors and requires constant updates to ensure the information remains accurate.
- Generative AI: With AI, book recommendations are generated on-the-fly based on user inputs and interactions. There’s no need for a massive, pre-existing database, which means recommendations are always fresh and relevant to the user’s current preferences.
2. Simplified Data Management:
- Traditional Approach: Keeping a comprehensive and well-structured book database demands significant storage and management efforts, which can be quite a hassle.
- Generative AI: AI takes a lot of this burden away by dynamically generating book details. This reduces the need for extensive data storage and management, and the risk of outdated or incomplete data is minimized.
3. Improved Personalization:
- Traditional Approach: Involves creating detailed user profiles and implementing machine learning models to predict user preferences based on historical data. This requires a large user base and sophisticated algorithms.
- Generative AI: Continuously learns from user interactions (likes, dislikes, reads, ratings) to refine recommendations from the context provided in the request, resulting in highly personalized and evolving suggestions without the need for extensive user data.
4. Flexibility and Adaptability:
- Traditional Approach: Fixed algorithms may struggle to adapt quickly to changing user preferences and new trends.
- Generative AI: Adapts dynamically to user preferences and new information, providing a more responsive and engaging recommendation system provided the correct context.
Cons of Using Generative AI
- Dependence on Training Data / Hallucination: AI models may generate inaccurate or fictitious information, such as incorrect ISBNs or non-existent book details . The quality and accuracy of the AI’s recommendations are heavily dependent on its training data.
- Cost Intensive: Using generative AI, for the purpose book recommendation generation where it needs to produce bulk data of book details and maintain conversation history to provide unique and personalized recommendation can be expensive in terms of tokens used hence increasing the cost of using LLMs.
Despite the higher expense, this investment is justified by the value of delivering highly personalized and dynamic recommendations, with ongoing advancements expected to reduce costs over time. The problem of hallucination will get better with AI model getting smarter and techniques like Retrieval-Augmented Generation (RAG) getting better.
Conclusion
Integrating generative AI into a book recommendation system like BookMatch brings a transformative approach to personalized recommendations. While it simplifies data management and enhances personalization, it also introduces challenges such as hallucinated data and output inconsistencies. By carefully addressing these challenges and leveraging the strengths of generative AI and help of Kotlin Multiplatform (KMP) for shared logic and native UIs, we can create a dynamic and engaging book recommendation experience that evolves with the user's reading journey.
GitHub Repository
The source code for the project is available on GitHub, feel free to try and experiment with the app. If interested you can also contribute in it.
https://github.com/novumlogic/BookMatch
Resources
Here some resources which helped me as XML based Android developer to make this project happen. It has links to Jetpack compose codelabs, SwiftUI codelabs.