A simple example demonstrating semantic search using open-source embeddings and the sentence-transformers library. This project searches through a collection of Tarantino movies using natural language queries.
Semantic search understands the meaning behind your query rather than just matching keywords. Instead of searching for exact word matches, it finds content that is conceptually similar to your query using vector embeddings.
- Text Encoding: The
sentence-transformerslibrary converts movie descriptions into high-dimensional vectors (embeddings) that capture semantic meaning - Query Processing: Your search query is converted into the same vector space
- Similarity Matching: The system finds movies with embeddings most similar to your query using cosine similarity
- Results: Returns the best matching movie based on semantic similarity scores
- Uses the
all-MiniLM-L6-v2model for fast, accurate embeddings - Searches through Tarantino movie database with English descriptions
- Demonstrates vector similarity scoring
- Lightweight and easy to understand implementation
- Python 3.12+
- uv package manager
curl -LsSf https://astral.sh/uv/install.sh | sh# Clone or navigate to the project directory
cd search
# Install dependencies and run
uv run main.pyThat's it! uv automatically:
- Creates a virtual environment
- Installs all dependencies from
pyproject.toml - Runs the script
The current example searches for "fading actor in decline" and finds the most semantically similar Tarantino movie:
query = "fading actor in decline"
# Results in finding "Once Upon a Time in Hollywood"
# which features an actor facing career declineEdit main.py and modify the query:
query = "violent revenge story" # Will likely match Kill Bill
query = "war movie" # Will likely match Inglourious Basterds
query = "western" # Will likely match Django UnchainedReplace the movie data in data.py with your own content:
your_data = [
{
"title": "Your Title",
"description": "Your description here...",
# Add any other fields you want to search
}
]Then update the text formatting in main.py to match your data structure.
- sentence-transformers: Provides pre-trained models for text embeddings
- numpy: Numerical operations for vector similarity calculations
This implementation runs on CPU using .cpu().numpy() to ensure compatibility across all systems. While GPU acceleration with NVIDIA CUDA would be faster for larger datasets, CPU processing is:
- Universal: Works on Mac (Apple Silicon/Intel) and Linux
- Educational: Easier setup for learning - no GPU drivers required
- Sufficient: Fast enough for small to medium datasets like this movie collection
For production applications with larger datasets, consider GPU acceleration if available.
This project demonstrates key concepts in modern AI applications:
- Vector Embeddings: How text becomes numerical representations
- Semantic Understanding: Moving beyond keyword matching
- Similarity Search: Finding related content in high-dimensional space
- Open Source AI: Using freely available, high-quality models
Perfect for learning the foundations of semantic search before scaling to larger datasets or more complex applications.
- Experiment with different sentence transformer models
- Add more sophisticated ranking algorithms
- Implement vector databases for larger datasets (Pinecone, Weaviate, ChromaDB)
- Build a web interface with FastAPI or Flask