Generate Embeddings with Azure OpenAI
Example demonstrating embeddings using the Azure OpenAI service. Covers setup for API base, version, type, and key.
Why it matters
Leverage Azure OpenAI Service to generate embeddings for your data. This asset demonstrates how to set up API connections and create embeddings using Azure.
Outcomes
What it gets done
Configure Azure OpenAI API credentials and endpoints.
Create and list deployments for embedding models.
Generate embeddings for sample text data.
Integrate with Azure OpenAI for embedding generation.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/oai-embeddings | bash Steps
Steps in the chain
Install the necessary dependencies using pip install "openai>=0.28.1,<1.0.0"
Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value to get your api_base.
Setup the api_type and api_key. Get the key from the portal or through Microsoft Active Directory Authentication. Depending on this the api_type is either 'azure' or 'azure_ad'.
Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.
Get a key via Microsoft Active Directory Authentication. A token is valid for a period of time, after which it will expire. Refresh an expiring token by hooking into requests.auth.
Create a deployment using the 'text-similarity-curie-001' model. Create a new deployment by going to your Resource in your portal under "Resource Management" -> "Model deployments".
Look in the subscription for an already finished deployment that succeeded, since creating a new deployment takes a long time.
Send a sample embedding to the deployment.
Overview
Azure embeddings example
What it does
This example demonstrates how to use the Azure OpenAI service for embeddings. It covers the necessary setup steps, including installing dependencies and configuring API credentials.
How it connects
Use this example when you need to work with embeddings using the Azure OpenAI service in Python. Do not use this if you are not using Azure OpenAI services or if you are using a version of the OpenAI library outside the specified range (0.28.1 to <1.0.0).
Source README
Azure embeddings example
Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742
This example will cover embeddings using the Azure OpenAI service.
Setup
First, we install the necessary dependencies.
! pip install "openai>=0.28.1,<1.0.0"
For the following sections to work properly we first have to setup some things. Let's start with the api_base and api_version. To find your api_base go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value.
We next have to setup the api_type and api_key. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the api_type is either azure or azure_ad.
Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.
Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
(Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.
A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:
Deployments
In this section we are going to create a deployment that we can use to create embeddings.
Deployments: Create manually
Let's create a deployment using the text-similarity-curie-001 model. Create a new deployment by going to your Resource in your portal under "Resource Management" -> "Model deployments".
Deployments: Listing
Now because creating a new deployment takes a long time, let's look in the subscription for an already finished deployment that succeeded.
Embeddings
Now let's send a sample embedding to the deployment.
Step 1: Install dependencies
Install the necessary dependencies using pip install "openai>=0.28.1,<1.0.0"
Step 2: Setup api_base and api_version
Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value to get your api_base.
Step 3: Setup api_type and api_key
Setup the api_type and api_key. Get the key from the portal or through Microsoft Active Directory Authentication. Depending on this the api_type is either 'azure' or 'azure_ad'.
Step 4: Setup: Portal - Get API key
Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.
Step 5: Setup: Microsoft Active Directory Authentication
Get a key via Microsoft Active Directory Authentication. A token is valid for a period of time, after which it will expire. Refresh an expiring token by hooking into requests.auth.
Step 6: Deployments: Create manually
Create a deployment using the 'text-similarity-curie-001' model. Create a new deployment by going to your Resource in your portal under "Resource Management" -> "Model deployments".
Step 7: Deployments: Listing
Look in the subscription for an already finished deployment that succeeded, since creating a new deployment takes a long time.
Step 8: Embeddings
Send a sample embedding to the deployment.
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.