What this does
- Encodes a small corpus of example documents using a SentenceTransformer
- Encodes queries and computes cosine similarity to the corpus
- Prints the top-k most similar corpus sentences for each query
Requirements
- Python 3.8+
- sentence-transformers (pip install sentence-transformers)
- torch (pip install torch) — CPU or GPU build
Usage
- Install dependencies: pip install sentence-transformers torch
- Run: python semantic_search.py
Notes
- The script uses the "all-MiniLM-L6-v2" model by default.
- Tensors are kept in memory; set convert_to_tensor=True to place tensors on GPU if available.
- Edit the corpus, queries, or top_k to adapt the demo to your needs.