Skip to main content

Azure OpenAI

Let's load the Azure OpenAI Embedding class with environment variables set to indicate to use Azure endpoints.

import os

os.environ["AZURE_OPENAI_API_KEY"] = "..."
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://<your-endpoint>.openai.azure.com/"
from langchain_openai import AzureOpenAIEmbeddings

embeddings = AzureOpenAIEmbeddings(
azure_deployment="<your-embeddings-deployment-name>",
openai_api_version="2023-05-15",
)
API Reference:AzureOpenAIEmbeddings
text = "this is a test document"
query_result = embeddings.embed_query(text)
doc_result = embeddings.embed_documents([text])
doc_result[0][:5]
[-0.012222584727053133,
0.0072103982392216145,
-0.014818063280923775,
-0.026444746872933557,
-0.0034330499700826883]

[Legacy] When using openai<1โ€‹

# set the environment variables needed for openai package to know to reach out to azure
import os

os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_BASE"] = "https://<your-endpoint.openai.azure.com/"
os.environ["OPENAI_API_KEY"] = "your AzureOpenAI key"
os.environ["OPENAI_API_VERSION"] = "2023-05-15"
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(deployment="your-embeddings-deployment-name")
API Reference:OpenAIEmbeddings
text = "This is a test document."
query_result = embeddings.embed_query(text)
doc_result = embeddings.embed_documents([text])

Was this page helpful?


You can also leave detailed feedback on GitHub.