> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Generating Embeddings

> Explains how to create text embeddings, convert documents to numeric vectors, and use them for semantic search and retrieval augmented generation workflows.

We are now in the third phase of the workflow: generating embeddings. In this lesson we convert text chunks into numeric vectors (embeddings). Before returning to the PDF-based workflow where we already loaded and chunked documents, let's take a short detour to understand embeddings as a standalone concept — this will clarify how retrieval-augmented generation (RAG) works and why embeddings are central to semantic search.

Embeddings are numeric vector representations of text that preserve semantic meaning and context. Once converted, these vectors enable similarity comparisons (for example, via cosine similarity) so you can retrieve semantically related documents from a vector store.

## Step 1 — Initialize the embeddings model

First, import and initialize the embeddings object. In this example we use LangChain's `OpenAIEmbeddings` with the `text-embedding-3-large` model:

```python theme={null}
from langchain.embeddings import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
```

## Step 2 — Prepare documents to embed

Create a small list of documents. In a real pipeline these would be the chunks extracted from your PDFs or other sources; here we use example news headlines for illustration:

```python theme={null}
docs = [
    "Thrilling Finale Awaits: The Countdown to the Cricket World Cup Championship",
    "Global Giants Clash: Football World Cup Semi-Finals Set the Stage for Epic Showdowns",
    "Record Crowds and Unforgettable Moments: Highlights from the Cricket World Cup",
    "From Underdogs to Contenders: Football World Cup Surprises and Breakout Stars"
]
```

## Step 3 — Convert text to embeddings

Call `embed_documents` to convert the list of strings into a list of numeric vectors:

```python theme={null}
embed_docs = embeddings.embed_documents(docs)
```

You can verify that the number of returned embeddings matches the number of input documents:

```python theme={null}
len(embed_docs)
```

```plaintext theme={null}
4
```

Each element in `embed_docs` is an array of floating-point numbers (the embedding vector). Inspecting the first embedding (truncated) might look like this:

```python theme={null}
embed_docs[0][:16]
```

```plaintext theme={null}
[-0.03236965773282924, -0.03388830823998, -0.01959451046452, 0.00590366532974,
 0.00387870070599, 0.00891787786988, -0.01084504675850, -0.02438912601325,
 0.02694678889459, -0.02784329818992, 0.00495443632714, -0.00387356763555,
 0.01617953250937, -0.05444381939771, 0.00328312295737, 0.01673573782502]
```

The dimensionality of each vector depends on the chosen embedding model. For `text-embedding-3-large` the vectors have 3072 dimensions:

```python theme={null}
len(embed_docs[0])
```

```plaintext theme={null}
3072
```

<Callout icon="lightbulb" color="#1CB2FE">
  Embeddings are not meant to be human-readable. They are high-dimensional numeric representations used by algorithms to compute semantic similarity (for example, via cosine similarity) during semantic search or retrieval.
</Callout>

## Quick reference — embedding workflow

| Step              | Purpose                      | Example / Result                                             |
| ----------------- | ---------------------------- | ------------------------------------------------------------ |
| Initialize model  | Create an embeddings client  | `OpenAIEmbeddings(model="text-embedding-3-large")`           |
| Prepare documents | Text chunks to convert       | `docs = ["headline1", "headline2", ...]`                     |
| Embed documents   | Convert to vectors           | `embed_docs = embeddings.embed_documents(docs)`              |
| Validate          | Ensure counts and dims match | `len(embed_docs) == len(docs)`; `len(embed_docs[0]) == 3072` |

## What to do with these vectors

Once you have these vectors you typically:

* Store them in a vector database (ANN index) for efficient similarity search.
* Use similarity metrics (cosine similarity, dot product) to retrieve the most relevant chunks for a given user query.
* Combine retrieved context with a generative model for retrieval-augmented generation (RAG) — e.g., building a Q\&A chatbot over your PDF corpus.

For vector storage and retrieval, consider vector databases or specialized libraries that support approximate nearest neighbor (ANN) search and persistence.

## Links and references

* [Vector Database for GenAI](https://learn.kodekloud.com/user/courses/vector-database-for-genai)
* LangChain embeddings docs — see relevant model usage and parameters
* OpenAI embeddings documentation — model options and dimensionality

In the next part of this lesson we'll introduce a vector database and demonstrate a simple similarity search. After covering retrieval and storage, we'll continue building the Q\&A chatbot for the PDF.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/e47b44c9-65c3-46f8-8bed-b075a18ab12b/lesson/187f5776-2d66-4326-bee6-86ba7569e581" />
</CardGroup>
