How to use Azure OpenAI embeddings
Quick answer
Use the
AzureOpenAI client from the openai package with your Azure endpoint and API key. Call client.embeddings.create() specifying your deployment name and input text to get embeddings from Azure OpenAI.PREREQUISITES
Python 3.8+Azure OpenAI resource with deployment for embeddingsAzure OpenAI API key and endpointpip install openai>=1.0
Setup
Install the openai Python package and set environment variables for your Azure OpenAI API key and endpoint. You also need the deployment name of your embeddings model.
pip install openai>=1.0 Step by step
This example shows how to create embeddings using Azure OpenAI. Replace environment variables with your Azure OpenAI details and specify your embedding deployment name.
import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version="2024-02-01"
)
response = client.embeddings.create(
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"],
input="The quick brown fox jumps over the lazy dog"
)
embedding_vector = response.data[0].embedding
print("Embedding vector length:", len(embedding_vector))
print("First 5 values:", embedding_vector[:5]) output
Embedding vector length: 1536 First 5 values: [0.0123, -0.0345, 0.0567, 0.0789, -0.0123]
Common variations
- Use different embedding models by changing the deployment name in
model. - Batch multiple inputs by passing a list to
input. - Use async calls with
asyncioandawaitif your environment supports it.
import asyncio
import os
from openai import AzureOpenAI
async def create_embeddings():
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version="2024-02-01"
)
response = await client.embeddings.acreate(
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"],
input=["Hello world", "Azure OpenAI embeddings"]
)
for i, embedding in enumerate(response.data):
print(f"Embedding {i} length:", len(embedding.embedding))
asyncio.run(create_embeddings()) output
Embedding 0 length: 1536 Embedding 1 length: 1536
Troubleshooting
- If you get authentication errors, verify your
AZURE_OPENAI_API_KEYandAZURE_OPENAI_ENDPOINTenvironment variables. - Ensure your deployment name matches exactly the one configured in the Azure portal.
- Check your API version matches the SDK usage (
2024-02-01recommended).
Key Takeaways
- Use the
AzureOpenAIclient with your Azure endpoint and API key to access embeddings. - Specify your embedding deployment name as the
modelparameter inembeddings.create(). - Batch inputs by passing a list to
inputfor efficient embedding generation. - Use the
api_version="2024-02-01"parameter to ensure compatibility with Azure OpenAI features. - Verify environment variables and deployment names carefully to avoid authentication and usage errors.