Movie Recommendation Bot
Finding relevant movies based on vague preferences often requires sifting through hundreds of database entries. Traditional keyword-based search fails when users describe movie themes or feelings rather than exact titles or actors.
This guide details the design and deployment of an AI-powered Movie Recommendation Bot that combines semantic vector search in MongoDB Atlas with GPT-2 generation to deliver natural language movie recommendations through an interactive Gradio interface.
Navigate this post
Understanding Vector Search for Recommendations
Traditional database queries search for exact text matches, such as filtering by genre or director. In contrast, semantic vector search converts text descriptions into numerical vector embeddings, allowing database systems to compare the underlying meaning of queries and movie descriptions.
- Vector Search
-
A database querying method that calculates mathematical similarity between numerical representations of text rather than looking for exact keyword matches.
- Sentence Transformers
-
Deep learning models engineered to map sentences and paragraphs into numerical vector spaces where semantically similar texts sit close together - like arranging books on a library shelf by topic rather than title.
- MongoDB Atlas Vector Search
-
A cloud database service that indexes vector embeddings alongside document fields, enabling fast k-nearest neighbor (k-NN) similarity searches (finding the most similar movie descriptions in mathematical space).
- GPT-2 (Generative Pre-trained Transformer 2)
-
An open-source text generation model used to synthesize retrieved movie metadata into natural, conversational user responses.
By pairing semantic retrieval with a generative language model, the system retrieves relevant movie candidates first and then formats the final recommendations into friendly, readable summaries.
Semantic Search Advantage
Vector search captures contextual intent, enabling queries like "heartwarming sci-fi about space exploration" to return relevant movies even if those exact words do not appear in the title.
System Architecture and Data Pipeline
The recommendation engine processes queries through a multi-step retrieval and generation pipeline.
---
title: "Movie Recommendation Data Flow"
---
flowchart TB
A["User Input Query"] --> B["Gradio Interface"]
B --> C["Query Preprocessing"]
C --> D["Generate Vector Embedding"]
D --> E["Vector Similarity Search"]
E --> F["Retrieve Relevant Movies"]
F --> G["Format Context Prompt"]
G --> H["GPT-2 Language Model"]
H --> I["Format Output Response"]
I --> J["Display in Gradio UI"]
subgraph "Knowledge Base Indexing"
K["Load Movie Dataset"] --> L["Generate Text Embeddings"]
L --> M["Index in MongoDB Atlas"]
end
M -.-> E
classDef blue fill:#e3f2fd,stroke:#0066cc,stroke-width:2px,color:#000000;
classDef green fill:#e5ffe5,stroke:#388e3c,stroke-width:2px,color:#000000;
classDef yellow fill:#fff9e5,stroke:#f39c12,stroke-width:2px,color:#000000;
classDef purple fill:#f3e5f5,stroke:#8e24aa,stroke-width:2px,color:#000000;
class A,B,C,J blue
class D,E,F yellow
class G,H,I green
class K,L,M purple
The table below describes each major component in the technical stack and its corresponding operational role.
| Component | Software Library | Functional Responsibility | Infrastructure Layer |
|---|---|---|---|
| Interface | gradio |
Captures user queries & displays chat output | Hugging Face Spaces |
| Embeddings | sentence-transformers |
Converts query strings into dense vector arrays | Client Inference |
| Vector Index | MongoDB Atlas | Stores movie embeddings & computes similarity | Azure Cloud |
| Language Model | transformers (GPT-2) |
Formats candidate context into natural responses | Cloud Container |
The pipeline ensures that unstructured user requests are converted into numerical vectors before querying the database, eliminating the risk of empty search results caused by keyword mismatches.
The Python code snippet below demonstrates how the application connects to MongoDB Atlas and executes vector similarity queries.
from pymongo import MongoClient
# Connect to MongoDB Atlas cluster
client = MongoClient(MONGO_URI) # (1)
collection = client["movie_db"]["movies"]
# Execute vector search pipeline
results = collection.aggregate(
[
{
"$vectorSearch": {
"index": "vector_index",
"path": "embedding",
"queryVector": query_embedding,
"numCandidates": 50,
"limit": 5, # (2)
}
}
]
)
- Establishes a secure connection to the MongoDB Atlas vector database instance.
- Returns the top 5 nearest neighbor movie documents based on vector cosine similarity.
Optimizing Index Performance
Configure the numCandidates parameter in MongoDB vector search to at least 10 times your target output limit to maintain search accuracy while controlling memory usage.
Design Trade-Offs and System Constraints
Operating within free-tier cloud environments requires balancing model capabilities against hardware limitations.
The bot operates on Hugging Face free-tier spaces, which impose strict CPU and memory boundaries. Consequently, response generation is capped at 150 tokens per query to ensure quick response times and prevent container timeouts.
-
:material-cpu:{ .lg .middle } Resource Efficiency
Uses lightweight open-source models (GPT-2 and Sentence Transformers) optimized for CPU. -
Latency Management
Enforces a 150-token response threshold to maintain responsive turnarounds on public tiers. -
Decoupled Architecture
Separates vector indexing (MongoDB Atlas) from inference (Hugging Face) for scaling. -
Natural Language Search
Translates qualitative user descriptions into high-accuracy semantic vector searches.
Future enhancements with dedicated GPU resources could expand context lengths to 1,000+ tokens, integrate multi-turn conversation memory, and deploy larger foundational models like Meta Llama.
Interactive Bot Access
The application is deployed live and open-sourced for community experimentation.
- Live Application: Hugging Face Space Demo
- Source Code Repository: GitHub Repository
Conclusion
Combining vector search retrieval with open-source language models provides a practical blueprint for building intelligent recommendation systems. By using MongoDB Atlas and Hugging Face, developers can deliver contextual semantic search capabilities without relying on expensive proprietary API services.
References and further reading
Open the complete reference catalog
Primary Sources
- MongoDB Atlas Documentation, "Vector Search Overview and Indexing"
- Sentence-Transformers Documentation, "Multilingual Sentence and Image Embeddings"
- Hugging Face Documentation, "GPT-2 Architecture and Inference"
- Kunal Pathak, "Movie Recommendation Bot Source Repository"
Related Site Guides
- Decoding the AI Jargon - Machine learning vocabulary guide