Open In Colab

OpenAI Embeddings#

If you’re opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

!pip install llama-index
import os

os.environ["OPENAI_API_KEY"] = "sk-..."
from llama_index.embeddings import OpenAIEmbedding
from llama_index import ServiceContext, set_global_service_context

embed_model = OpenAIEmbedding(embed_batch_size=10)

service_context = ServiceContext.from_defaults(embed_model=embed_model)

# optionally set a global service context
set_global_service_context(service_context)

Using OpenAI text-embedding-3-large and text-embedding-3-small#

Note, you may have to update your openai client: pip install -U openai

# get API key and create embeddings
from llama_index.embeddings import OpenAIEmbedding

embed_model = OpenAIEmbedding(model="text-embedding-3-large")

embeddings = embed_model.get_text_embedding(
    "Open AI new Embeddings models is great."
)
print(embeddings[:5])
[-0.011500772088766098, 0.02457442320883274, -0.01760469563305378, -0.017763426527380943, 0.029841400682926178]
print(len(embeddings))
3072
# get API key and create embeddings
from llama_index.embeddings import OpenAIEmbedding

embed_model = OpenAIEmbedding(
    model="text-embedding-3-small",
)

embeddings = embed_model.get_text_embedding(
    "Open AI new Embeddings models is awesome."
)
print(len(embeddings))
1536

Change the dimension of output embeddings#

Note: Make sure you have the latest OpenAI client

# get API key and create embeddings
from llama_index.embeddings import OpenAIEmbedding


embed_model = OpenAIEmbedding(
    model="text-embedding-3-large",
    dimensions=512,
)

embeddings = embed_model.get_text_embedding(
    "Open AI new Embeddings models with different dimensions is awesome."
)
print(len(embeddings))
512