Skip to main content
In this lesson you’ll convert text into embeddings, store them in a vector database (Chroma), and run a simple semantic (similarity) search using LangChain and OpenAI embeddings. This pattern is useful for retrieval-augmented generation (RAG), search interfaces, and any application that needs semantic relevance rather than exact text matches.

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_KEY environment variable). Creating embeddings calls the OpenAI API.
Useful references:

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.
You can create a Chroma vector store directly from strings, or from pre-chunked documents/pages. Here we pass raw text strings:
If you have metadata (source, URL, id), pass documents with metadata instead of plain strings to make downstream identification easier.

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 the k 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:
  1. Initialize embeddings with your chosen model.
  2. Convert texts (or chunks) into embeddings and store in Chroma.
  3. For each user query, embed the query and run similarity_search(query, k=...).
  4. Use the retrieved documents as context for downstream tasks (summarization, QA, RAG).
This simple pattern powers many production retrieval pipelines: obtain relevant chunks quickly and then pass them to a language model for generation, question answering, or summarization.

Watch Video