Retrieval-Augmented Generation with LangChain

Nov 12 2024 · Python 3.12, LangChain 0.3.x, JupyterLab 4.2.4

Lesson 02: Working with Embeddings & Vector Databases

Vector Embeddings Demo

Episode complete

Play next episode

Next
Transcript

Demo: Exploring Embeddings with OpenAI and LangChain

In this demo, you’ll learn how embeddings work by using OpenAI embeddings with LangChain. Start by setting up LangChain:

pip install langchain

LangChain is a Python library, so you’ve used pip to install it. Note that LangChain’s real strength comes from its seamless integration with various external components. The basic installation provides core functionality, but you need additional dependencies to work with providers like OpenAI.

Embedding Models and Vector Databases

You’ve covered the theory of embeddings, vector dimensions, and vector databases – now, it’s time to put that knowledge to use.

From your terminal, navigate to your notebooks directory (or any preferred location). Open Jupyter Lab:

jupyter lab

Open a terminal within Jupyter Lab. Store your API key in an environment variable. This keeps it safe and makes it easily accessible in your code:

export OPENAI_API_KEY="<insert-your-api-key-here>"

This approach is preferable to hard coding the key in your code, which poses a security risk. Although Jupyter Notebook lets you enter the key interactively, this gets tedious during development and testing.

Remember, you initially installed just the core LangChain library. To leverage OpenAI’s models, you need an additional dependency:

pip install langchain-openai

Open a new notebook in Jupyter Lab. Add the follow code:

import os
from langchain_openai import OpenAIEmbeddings

openai_embedding = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])

This imports the os module, retrieves your stored OpenAI API key, and initializes the OpenAI embedding model. Press Shift-Enter and click the Run button on the menu below the tab.

Use your OpenAI model to embed some simple text. This essentially transforms your text data into a numerical vector format that the model can understand and process.

embeddings = openai_embedding.embed_documents(
  [
    "RAG gives me AI super powers",
    "Thanks, Kodeco!",
  ]
)

The embed_documents function converts your text into numerical vector representations (embeddings). Check the Embedding Dimensionality. Enter the following code into a new cell and execute it:

len(embeddings[0])

This outputs 1536, indicating that the OpenAI model you’re currently using represents each piece of text as a vector in a 1536-dimensional space. While printing all 1536 dimensions would be overwhelming, this code snippet shows you the first 10 dimensions:

embeddings[0][:10]

Run this cell, and you should see output like the following:

[-0.02180216647684574,
-0.03175415098667145,
0.004589573014527559,
-0.014155137352645397,
0.001597367925569415,
0.010148582980036736,
-0.020595453679561615,
-0.009335068985819817,
-0.03324558958411217,
-0.025300273671746254]

These are the first 10 dimensions of the embedding for the first text string (“RAG gives me AI super powers”). Each number represents a coordinate in the high-dimensional space, and the overall position of the embedding in this space captures the semantic meaning of the text.

If you’re curious about the specific OpenAI model being used, you can print its name:

print(openai_embedding.model)

Running this prints the name of the specific OpenAI model being used for embeddings. As of now, it’s likely text-embedding-ada-002, but OpenAI might change its default models in the future. Thetext-embedding-ada-002 model doesn’t allow you to customize the number of dimensions in the embeddings. If your OpenAI subscription includes access to other models, you might want to experiment with them. For instance, if you have access to the text-embedding-3 series, you could specify a model and its dimensionality like this:

openai_embedding = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'],
  model='text-embedding-3-small', dimensions=1024)

If you re-run the len(embeddings[0]) cell after making this change, you should see the output 1024, reflecting the new dimensionality. With a solid understanding of embeddings, you’re now ready to dive into the world of vector databases and how they’re used to efficiently store and retrieve these embeddings for your RAG application.

See forum comments
Cinema mode Download course materials from Github
Previous: Vector Dimensions & Embeddings Next: Introducing Chroma Database