This sample shows how to use an Azure Cosmos DB Trigger and Output Binding in Azure Functions to automatically generate Azure OpenAI embeddings on new or updated data.
- Azure Cosmos DB Trigger and Output Bindings for Azure Functions in both C# and Python
- Embedding generation using Azure OpenAI SDK with text-embedding-3-small embedding model
- Preventing endless loops in Functions Triggers from in-place document updates by comparing hash values generated on the document.
- Keyless deployment of Azure Functions, Azure Cosmos DB, Azure OpenAI with managed identities and RBAC
-
Open a terminal and navigate to where you would like to clone this solution.
-
Navigate to either the
csharp
orpython
directory in this solution. -
Run the following command to ensure correct permissions to write a
local.settings.json
file locally.-
If using Windows, open a second Terminal as Administrator and run the following PowerShell command
set-executionpolicy remotesigned
-
If using Mac or Linux, open Bash and run the following command. This likely requires sudo.
chmod +x ./infra/scripts/*.sh
Note: This sample deploys using azd. To enable local debugging, a
local.settings.json
file is created in the project directory with the values for the deployed sample in Azure.
-
-
From within the
csharp
orpython
directory, deploy the sample to Azure.
azd up
- Check for a
local.settings.json
file in the directory you deployed from. If it does not exist, create a new one using thesample.settings.json
in the same directory. - Replace the placeholder text for the Cosmos DB and OpenAI Account names below. Use the correct value for
FUNCTIONS_WORKER_RUNTIME
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python", //"dotnet-isolated"
"COSMOS_CONNECTION__accountEndpoint": "https://{my-cosmos-account}.documents.azure.com:443/",
"OPENAI_ENDPOINT": "https://{my-open-ai-account}.openai.azure.com/",
"COSMOS_DATABASE_NAME": "embeddings-db",
"COSMOS_CONTAINER_NAME": "customer",
"COSMOS_VECTOR_PROPERTY": "vectors",
"COSMOS_HASH_PROPERTY": "hash",
"COSMOS_PROPERTY_TO_EMBED": "customerNotes",
"OPENAI_DEPLOYMENT_NAME": "text-3-small",
"OPENAI_DIMENSIONS": "1536"
}
}
- Open a browser to Azure Portal. Locate the resource group for the deployed sample.
- Open the deployed Azure Cosmos DB account and navigate to the
customer
container in Cosmos Data Explorer - Create a new document with the same schema as the one below and save.
Example document:
{
"id": "00001",
"customerId": "10001",
"customerNotes": "lorum ipsum."
}
-
After clicking Save, the document should reappear with a number of system properties.
-
Press F5 or refresh the browser window and it should then reappear with a hash property and vectors array stored in the document as shown below.
Note: The Functions start-up may miss the first trigger execution. If the embeddings do not appear as below. Make a small change to the same document and save to re-execute the trigger.
Run the sample locally in a debugger for either the Python or CSharp version that was deployed.
python -m venv venv
source venv/Scripts/activate
pip install -r requirements.txt
python -m venv venv
source venv\Scripts\activate
pip install -r requirements.txt
- Ensure you have the Python extension installed. If not, install it from the Extensions view (
Ctrl+Shift+X
). - Open the Command Palette (
Ctrl+Shift+P
) and typePython: Select Interpreter
. Choose the appropriate Python interpreter for your project. - Open the
function_app.py
Python file you want to debug. - Open the Run and Debug view by clicking the Run icon on the sidebar or pressing
Ctrl+Shift+D
. - Click on
create a launch.json file
link to create a new launch configuration. - Select
Python
from the list of environments. - Press
F5
to start debugging. The Azure Function will start, and execution will pause at any breakpoints you've set.
- Open the csharp project folder in VS Code.
- Ensure you have the C# extension installed. If not, install it from the Extensions view (
Ctrl+Shift+X
). - Open the
CosmosEmbeddingGenerator.cs
file to debug. - Set breakpoints by clicking in the gutter to the left of the line numbers.
- Open the Run and Debug view by clicking the Run icon on the sidebar or pressing
Ctrl+Shift+D
. - Click on
create a launch.json file
link to create a new launch configuration. - Press
F5
to start debugging. The Azure Function will start, and execution will pause at any breakpoints you've set.