From 0152859aa0ece3374ecfe72161069ba127f08f93 Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Thu, 30 May 2024 13:16:38 -0700 Subject: [PATCH] docs(azure): update example and readme to use Entra ID (#857) --- README.md | 11 +++++++++-- examples/azure.ts | 11 ++++++++--- examples/package.json | 3 ++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b5feb43f3..89601dcce 100644 --- a/README.md +++ b/README.md @@ -367,11 +367,18 @@ To use this library with [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ class instead of the `OpenAI` class. > [!IMPORTANT] -> The Azure API shape differs from the core API shape which means that the static types for responses / params +> The Azure API shape slightly differs from the core API shape which means that the static types for responses / params > won't always be correct. ```ts -const openai = new AzureOpenAI(); +import { AzureOpenAI } from 'openai'; +import { getBearerTokenProvider, DefaultAzureCredential } from '@azure/identity'; + +const credential = new DefaultAzureCredential(); +const scope = 'https://cognitiveservices.azure.com/.default'; +const azureADTokenProvider = getBearerTokenProvider(credential, scope); + +const openai = new AzureOpenAI({ azureADTokenProvider }); const result = await openai.chat.completions.create({ model: 'gpt-4-1106-preview', diff --git a/examples/azure.ts b/examples/azure.ts index 7f57e45c3..5fe1718fa 100755 --- a/examples/azure.ts +++ b/examples/azure.ts @@ -1,14 +1,19 @@ #!/usr/bin/env -S npm run tsn -T import { AzureOpenAI } from 'openai'; +import { getBearerTokenProvider, DefaultAzureCredential } from '@azure/identity'; // Corresponds to your Model deployment within your OpenAI resource, e.g. gpt-4-1106-preview // Navigate to the Azure OpenAI Studio to deploy a model. const deployment = 'gpt-4-1106-preview'; -// Make sure to set both AZURE_OPENAI_ENDPOINT with the endpoint of your Azure resource and AZURE_OPENAI_API_KEY with the API key. -// You can find both information in the Azure Portal. -const openai = new AzureOpenAI(); +const credential = new DefaultAzureCredential(); +const scope = 'https://cognitiveservices.azure.com/.default'; +const azureADTokenProvider = getBearerTokenProvider(credential, scope); + +// Make sure to set AZURE_OPENAI_ENDPOINT with the endpoint of your Azure resource. +// You can find it in the Azure Portal. +const openai = new AzureOpenAI({ azureADTokenProvider }); async function main() { console.log('Non-streaming:'); diff --git a/examples/package.json b/examples/package.json index 04ed507b9..c8a5f7087 100644 --- a/examples/package.json +++ b/examples/package.json @@ -9,7 +9,8 @@ "express": "^4.18.2", "next": "^14.1.1", "openai": "file:..", - "zod-to-json-schema": "^3.21.4" + "zod-to-json-schema": "^3.21.4", + "@azure/identity": "^4.2.0" }, "devDependencies": { "@types/body-parser": "^1.19.3",