-
- Imports and prerequisites
-
- Load the policy document
-
- Chunk the document by headings
-
- Load the embedding model
-
- Encode chunks and store in LanceDB
-
- Define search utilities
-
- Example queries
-
- Notes on behavior and limitations
-
- Wrap-up and resources
1) Imports and prerequisites
Install dependencies (example):- SentenceTransformers: https://www.sbert.net/
- LanceDB: https://lancedb.ai/
2) Load the policy document
Load the Markdown file (here:kodekloud_airlines_policy.md) and print a short preview to confirm successful load.

3) Chunk the document
Embedding an entire long document at once reduces retrieval granularity. The recommended approach is to split into semantic chunks (for example, by headings) and embed each chunk independently. The followingchunk_by_headings function groups content by top-level headings (#, ##, ###) and discards very short buffers. It returns a list of dictionaries with section and text keys.
4) Load the embedding model
We use theall-MiniLM-L6-v2 SentenceTransformer (compact, high-quality for semantic search). If you pull from the Hugging Face Hub frequently, consider setting HF_TOKEN as an environment variable to avoid unauthenticated rate limits.
If you see warnings about unauthenticated Hugging Face Hub requests, set a
HF_TOKEN environment variable to increase rate limits and speed up downloads.5) Encode chunks and store in LanceDB
Create (or recreate) a local LanceDB store, encode each chunk into a normalized embedding vector, and store rows withsection, text, and vector fields.
6) Define search utilities
Create two helper functions:search_policy(question, k)— performs a vector search in LanceDB and returns top-k hits as a pandas DataFramepretty_print_results(question, k, preview_chars)— prints results with section context and distance score
- Using
normalize_embeddings=Trueensures cosine similarity is computed as a simple dot product in many vector DBs. - The
_distancefield returned by LanceDB is typically the computed metric (lower is closer depending on configuration).
7) Example queries
Try a few representative questions to demonstrate semantic search behavior.- Example: “What is the cabin baggage weight limit?”


8) Notes on behavior and limitations
- Embeddings provide semantic matching, not exact keyword/string matching: paraphrases and related concepts can match.
- Retrieval quality depends on chunking strategy, model selection, and the currency of the embedded document.
- Ambiguous queries may return plausible but incorrect sections. Combining retrieval with a downstream RAG (Retrieval-Augmented Generation) pipeline, intent classification, or rule-based filters can improve precision.
- If the source document changes, re-embed the affected chunks and update the vector store to reflect the latest content.
9) Wrap-up and resources
What we covered:- Loading a Markdown policy document
- Chunking by headings for semantic units
- Encoding chunks with a SentenceTransformer
- Storing vectors in LanceDB
- Performing semantic search and displaying top matches
- SentenceTransformers: https://www.sbert.net/
- LanceDB: https://lancedb.ai/
- RAG (Retrieval-Augmented Generation) concepts: https://learn.kodekloud.com/user/courses/fundamentals-of-rag