How to set embed model in LlamaIndex
Quick answer
In
LlamaIndex, set the embedding model by passing a custom embedding object to the ServiceContext or directly to the index constructor. Use embedding classes like OpenAIEmbedding with your preferred model name to specify the embed model.PREREQUISITES
Python 3.8+OpenAI API key or other embedding API keypip install llama-index openai
Setup
Install llama-index and openai packages, and set your API key as an environment variable.
- Install packages:
pip install llama-index openai - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install llama-index openai Step by step
Use the OpenAIEmbedding class from llama_index.embeddings.openai to specify the embedding model, then pass it to ServiceContext and create the index.
import os
from llama_index import SimpleDirectoryReader, ServiceContext, GPTVectorStoreIndex
from llama_index.embeddings.openai import OpenAIEmbedding
# Set your OpenAI API key in environment variable
api_key = os.environ["OPENAI_API_KEY"]
# Create embedding instance with desired model
embedding = OpenAIEmbedding(model_name="text-embedding-3-large", openai_api_key=api_key)
# Create service context with custom embedding
service_context = ServiceContext.from_defaults(embed_model=embedding)
# Load documents
documents = SimpleDirectoryReader("./data").load_data()
# Create index with custom embedding via service context
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
# Query example
query_engine = index.as_query_engine()
response = query_engine.query("What is LlamaIndex?")
print(response.response) output
LlamaIndex is a data framework for building LLM applications by connecting your data with large language models.
Common variations
- Use other embedding providers by implementing the
BaseEmbeddinginterface. - Pass the embedding directly to index constructors that accept an
embed_modelparameter. - Use async methods if supported by your embedding provider.
from llama_index import GPTVectorStoreIndex
from llama_index.embeddings.openai import OpenAIEmbedding
embedding = OpenAIEmbedding(model_name="text-embedding-3-large", openai_api_key=os.environ["OPENAI_API_KEY"])
# Directly pass embed_model to index
index = GPTVectorStoreIndex.from_documents(documents, embed_model=embedding) Troubleshooting
- If you get authentication errors, verify your API key is correctly set in
os.environ. - If embeddings are not generated, check the model name is valid and supported by your embedding provider.
- For slow responses, ensure network connectivity and consider using smaller embedding models.
Key Takeaways
- Use
OpenAIEmbeddingor a compatible embedding class to specify your embed model in LlamaIndex. - Pass the embedding instance to
ServiceContext.from_defaults(embed_model=...)for centralized configuration. - You can also pass the embed model directly to index constructors that accept an
embed_modelparameter. - Always set your API keys securely via environment variables to avoid authentication issues.
- Check model names against your embedding provider's documentation to ensure compatibility.