📘 Introduction

RAG systems need a way to search text by meaning, not only by exact words. If a user asks about refund rules, the system should still find a document that says cancellation policy, payment reversal, or billing issue. Embeddings make this kind of semantic search possible.

In this tutorial, you will learn how LangChain embeddings turn text into vectors. We will use OpenAIEmbeddings to embed a single query, inspect vector length, preview vector values, embed multiple documents, and compare text similarity in a beginner-friendly way.

🎯
Embeddings are the bridge between text splitting and retrieval. After documents are split into chunks, embeddings help your app find the chunks that are closest in meaning to a user question.

💡 What are embeddings?

An embedding is a list of numbers that represents the meaning of a piece of text. Similar texts should end up with vectors that are close to each other, while unrelated texts should be farther apart.

You usually do not read the numbers manually. Your application stores them, compares them, and uses them to find related text. That is why embeddings are so important for RAG, semantic search, recommendations, clustering, and document retrieval.

ConceptBeginner-friendly meaningExample
TextThe human-readable inputHow do refunds work?
EmbeddingA vector representation of meaning[0.02, -0.14, 0.08, ...]
SimilarityHow close two vectors areA refund question matches a billing policy

🧠 Where embeddings fit in a RAG pipeline

Embeddings usually come after document loading and text splitting. First, your app loads content. Then it splits long documents into chunks. Next, it embeds those chunks and stores the vectors in a vector store. Later, when a user asks a question, the question is embedded too, and the vector store finds similar chunks.

Load documents -> Split into chunks -> Create embeddings -> Store vectors -> Retrieve relevant chunks -> Generate answer
💡
Embeddings do not generate answers. They help your application find relevant text so a model can answer with better context.

✅ Prerequisites

Before getting started, make sure you have:

☑️ Python installed
☑️ Basic Python knowledge
☑️ An OpenAI API key
☑️ A terminal or command prompt

⚙️1️⃣ Create a project folder

Create a new local project folder for this tutorial:

mkdir langchain-embeddings-rag
cd langchain-embeddings-rag

🐍2️⃣ Create a virtual environment

Create and activate a virtual environment:

python -m venv .venv
source .venv/bin/activate

On Windows, activate it with:

.venv\Scripts\activate

📦3️⃣ Install libraries

Install the packages we need for this tutorial:

pip install -U langchain-openai langchain-core

🔐4️⃣ Set your API key

Set your OpenAI API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.

export OPENAI_API_KEY="your_api_key_here"

On Windows PowerShell, use:

$env:OPENAI_API_KEY="your_api_key_here"
🔐
Keep API keys outside your Python files. Environment variables are enough for this tutorial and much safer than hardcoding secrets.

🔢5️⃣ Embed a single query

Create a file named embed_query.py. This first example embeds one question and prints basic information about the vector.

from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

query = "How do refunds work for annual plans?"
vector = embeddings.embed_query(query)

print(f"Text: {query}")
print(f"Vector type: {type(vector)}")
print(f"Vector length: {len(vector)}")
print("First 5 values:")
print(vector[:5])

embed_query.py

Run the script:

python embed_query.py

You should see a Python list of floating-point numbers. The exact values can change, but the important idea is that the text has been converted into a vector your application can compare with other vectors.

Text: How do refunds work for annual plans?
Vector type: <class 'list'>
Vector length: 1536
First 5 values:
[0.0123, -0.0045, 0.0211, -0.0188, 0.0067]
🎓
Want the full RAG preparation workflow? In the Academy section, we embed multiple documents, compare similarities, and connect embeddings to the next retrieval step.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In