Prerequisites
Make sure you have:- Installed the required Python packages (for example, langchain and Chroma).
- Configured your OpenAI API key (for example by setting the
OPENAI_API_KEYenvironment variable). Creating embeddings calls the OpenAI API.
Step 1 — Imports and Example Documents
Import the required modules, create an embeddings object, and define a small set of example documents (headlines). These headlines will be embedded and indexed in the vector store.Step 2 — Create the Chroma Vector Store
Chroma is an open-source vector database that:- Indexes and stores vectors (embeddings) with optional metadata.
- Performs fast similarity search / retrieval over those vectors.
Step 3 — Run Semantic Similarity Searches
When you query the vector store, the query is embedded with the same model and compared to the stored vectors. Use thek parameter to control how many nearest neighbors you retrieve.
Why This Works (Concise)
- Both documents and the query are converted into embeddings by the same model (
text-embedding-3-large). - The vector database compares these vectors (e.g., using cosine similarity) and returns the nearest vectors/documents.
- This is semantic search: the system can associate concepts (e.g., player names) with relevant documents even when the exact token does not appear in the text.
Tip: The
k (top-k) parameter controls how many nearest neighbors you retrieve. Choose k based on how many documents you want to use downstream (for example, as context for a language model). You can also store metadata with each text to help identify sources.Common Parameters and Options
End-to-End Pattern
A concise end-to-end workflow:- Initialize embeddings with your chosen model.
- Convert texts (or chunks) into embeddings and store in Chroma.
- For each user query, embed the query and run
similarity_search(query, k=...). - Use the retrieved documents as context for downstream tasks (summarization, QA, RAG).
Links and References
- Kubernetes Documentation (example resource link)
- LangChain Documentation
- Chroma (vector DB)
- OpenAI Embeddings Guide