From 0b6bbeab42c0a7b781440faa388fb21f86c596cd Mon Sep 17 00:00:00 2001 From: Howie Leung Date: Mon, 21 Oct 2024 14:26:16 -0700 Subject: [PATCH] Fixed scope in samples and readme (#31486) ### Packages impacted by this PR No code changes. Only sample and readme ### Issues associated with this PR Sample has scope incorrect for connection ### Describe the problem that is addressed by this PR Sample has scope incorrect for connection ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary) --- sdk/ai/ai-inference-rest/README.md | 3 +- .../samples-dev/chatCompletions.ts | 16 ++++++-- .../samples-dev/embeddings.ts | 16 ++++++-- .../samples-dev/getModelInfo.ts | 16 ++++++-- .../samples-dev/imageFileCompletions.ts | 40 ++++++++++++------- .../samples-dev/streamChatCompletions.ts | 16 ++++++-- .../samples-dev/streamingToolCall.ts | 29 +++++++++----- .../samples-dev/telemetry.ts | 17 ++++++-- .../samples-dev/telemetryWithToolCall.ts | 19 ++++++--- .../ai-inference-rest/samples-dev/toolCall.ts | 16 ++++++-- .../v1-beta/javascript/chatCompletions.js | 12 +++++- .../samples/v1-beta/javascript/embeddings.js | 12 +++++- .../v1-beta/javascript/getModelInfo.js | 12 +++++- .../javascript/imageFileCompletions.js | 12 +++++- .../javascript/streamChatCompletions.js | 12 +++++- .../v1-beta/javascript/streamingToolCall.js | 12 +++++- .../samples/v1-beta/javascript/telemetry.js | 12 +++++- .../javascript/telemetryWithToolCall.js | 17 ++++++-- .../samples/v1-beta/javascript/toolCall.js | 12 +++++- .../v1-beta/typescript/src/chatCompletions.ts | 16 ++++++-- .../v1-beta/typescript/src/embeddings.ts | 16 ++++++-- .../v1-beta/typescript/src/getModelInfo.ts | 16 ++++++-- .../typescript/src/imageFileCompletions.ts | 40 ++++++++++++------- .../typescript/src/streamChatCompletions.ts | 16 ++++++-- .../typescript/src/streamingToolCall.ts | 29 +++++++++----- .../v1-beta/typescript/src/telemetry.ts | 17 ++++++-- .../typescript/src/telemetryWithToolCall.ts | 19 ++++++--- .../v1-beta/typescript/src/toolCall.ts | 16 ++++++-- 28 files changed, 357 insertions(+), 129 deletions(-) diff --git a/sdk/ai/ai-inference-rest/README.md b/sdk/ai/ai-inference-rest/README.md index 4f08a6d63d4..6063851bc37 100644 --- a/sdk/ai/ai-inference-rest/README.md +++ b/sdk/ai/ai-inference-rest/README.md @@ -446,7 +446,8 @@ data: length=1024, [0.04196167, 0.029083252, ..., -0.0027484894, 0.0073127747] To generate embeddings for additional phrases, simply call `client.path("/embeddings").post` multiple times using the same `client`. -### Instrumentation (Chat Completions only) +### Instrumentation +Currently instrumentation is only supported for `Chat Completion without streaming`. To enable instrumentation, it is required to register exporter(s). Here is an example to add console as a exporter: diff --git a/sdk/ai/ai-inference-rest/samples-dev/chatCompletions.ts b/sdk/ai/ai-inference-rest/samples-dev/chatCompletions.ts index 3461407485e..cfc9bb6c8b1 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/chatCompletions.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/chatCompletions.ts @@ -50,13 +50,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples-dev/embeddings.ts b/sdk/ai/ai-inference-rest/samples-dev/embeddings.ts index b13a18939e0..ce279914511 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/embeddings.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/embeddings.ts @@ -46,13 +46,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples-dev/getModelInfo.ts b/sdk/ai/ai-inference-rest/samples-dev/getModelInfo.ts index 2b8bd8a980f..0df41cf9dc0 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/getModelInfo.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/getModelInfo.ts @@ -39,13 +39,21 @@ export async function main(): Promise { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples-dev/imageFileCompletions.ts b/sdk/ai/ai-inference-rest/samples-dev/imageFileCompletions.ts index 0a39606cccc..e9ba670b961 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/imageFileCompletions.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/imageFileCompletions.ts @@ -33,10 +33,14 @@ export async function main() { body: { messages: [ { role: "system", content: "You are a helpful assistant that describes images in details." }, - { role: "user", content: [ - { type: "text", text: "What's in this image?"}, - { type: "image_url", image_url: { - url: getImageDataUrl(imageFilePath, imageFormat)}} + { + role: "user", content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", image_url: { + url: getImageDataUrl(imageFilePath, imageFormat) + } + } ] } ], @@ -59,13 +63,13 @@ export async function main() { */ function getImageDataUrl(imageFile: string, imageFormat: string): string { try { - const imageBuffer = fs.readFileSync(imageFile); - const imageBase64 = imageBuffer.toString('base64'); - return `data:image/${imageFormat};base64,${imageBase64}`; + const imageBuffer = fs.readFileSync(imageFile); + const imageBase64 = imageBuffer.toString('base64'); + return `data:image/${imageFormat};base64,${imageBase64}`; } catch (error) { - console.error(`Could not read '${imageFile}'.`); - console.error('Set the correct path to the image file before running this sample.'); - process.exit(1); + console.error(`Could not read '${imageFile}'.`); + console.error('Set the correct path to the image file before running this sample.'); + process.exit(1); } } @@ -74,13 +78,21 @@ function getImageDataUrl(imageFile: string, imageFormat: string): string { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples-dev/streamChatCompletions.ts b/sdk/ai/ai-inference-rest/samples-dev/streamChatCompletions.ts index fa1dcf347e3..fe4663a5e5c 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/streamChatCompletions.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/streamChatCompletions.ts @@ -77,13 +77,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples-dev/streamingToolCall.ts b/sdk/ai/ai-inference-rest/samples-dev/streamingToolCall.ts index 041e0983e22..70978e5cddf 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/streamingToolCall.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/streamingToolCall.ts @@ -64,10 +64,10 @@ const getWeatherFunc = (location: string, unit: string): string => { const updateToolCalls = (toolCallArray: Array, functionArray: Array) => { const dummyFunction = { name: "", arguments: "", id: "" }; - while ( functionArray.length < toolCallArray.length ) { + while (functionArray.length < toolCallArray.length) { functionArray.push(dummyFunction); } - + let index = 0; for (const toolCall of toolCallArray) { if (toolCall.function.name) { @@ -90,7 +90,7 @@ const handleToolCalls = (functionArray: Array) => { let content = ""; switch (func.name) { - + case "get_current_weather": content = getWeatherFunc(funcArgs.location, funcArgs.unit ?? "fahrenheit"); messageArray.push({ @@ -123,7 +123,7 @@ const streamToString = async (stream: NodeJS.ReadableStream) => { export async function main() { const client = createModelClient(); - + const messages = [{ role: "user", content: "What's the weather like in Boston?" }]; let toolCallAnswer = ""; @@ -177,8 +177,7 @@ export async function main() { const messageArray = handleToolCalls(functionArray); messages.push(...messageArray); } else { - if (choice.delta?.content && choice.delta.content != '') - { + if (choice.delta?.content && choice.delta.content != '') { toolCallAnswer += choice.delta?.content; awaitingToolCallAnswer = false; } @@ -186,7 +185,7 @@ export async function main() { } } } - + console.log("Model response after tool call:"); console.log(toolCallAnswer); @@ -197,13 +196,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples-dev/telemetry.ts b/sdk/ai/ai-inference-rest/samples-dev/telemetry.ts index 780f0401981..75317b08e17 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/telemetry.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/telemetry.ts @@ -75,16 +75,25 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } + main().catch((err) => { console.error("The sample encountered an error:", err); }); diff --git a/sdk/ai/ai-inference-rest/samples-dev/telemetryWithToolCall.ts b/sdk/ai/ai-inference-rest/samples-dev/telemetryWithToolCall.ts index 870286b07a4..2c464b21074 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/telemetryWithToolCall.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/telemetryWithToolCall.ts @@ -4,7 +4,7 @@ /** * Demonstrates how to use tool calls with chat completions with telemetry. * - * @summary Get chat completions with function call. + * @summary Get chat completions with function call with instrumentation. */ import { DefaultAzureCredential } from "@azure/identity"; @@ -193,15 +193,24 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } + main().catch((err) => { console.error("The sample encountered an error:", err); }); diff --git a/sdk/ai/ai-inference-rest/samples-dev/toolCall.ts b/sdk/ai/ai-inference-rest/samples-dev/toolCall.ts index cdddd9f8949..7a3aec87f49 100644 --- a/sdk/ai/ai-inference-rest/samples-dev/toolCall.ts +++ b/sdk/ai/ai-inference-rest/samples-dev/toolCall.ts @@ -166,13 +166,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/chatCompletions.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/chatCompletions.js index f9dae4d322d..9e6e3b19366 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/chatCompletions.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/chatCompletions.js @@ -50,11 +50,19 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/embeddings.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/embeddings.js index 633e5d934bb..f3d24739414 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/embeddings.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/embeddings.js @@ -45,11 +45,19 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/getModelInfo.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/getModelInfo.js index 3f6681fa4db..f5364a23aa7 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/getModelInfo.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/getModelInfo.js @@ -39,11 +39,19 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/imageFileCompletions.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/imageFileCompletions.js index ec8cdaf6ce7..4f3c93af84b 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/imageFileCompletions.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/imageFileCompletions.js @@ -84,11 +84,19 @@ function getImageDataUrl(imageFile, imageFormat) { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamChatCompletions.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamChatCompletions.js index 5592b655fb2..2ca552d6100 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamChatCompletions.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamChatCompletions.js @@ -79,11 +79,19 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamingToolCall.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamingToolCall.js index 17fb5907015..2fd6a1254ca 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamingToolCall.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/streamingToolCall.js @@ -179,11 +179,19 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetry.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetry.js index 934fe53535d..7907e0e0dde 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetry.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetry.js @@ -83,11 +83,19 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetryWithToolCall.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetryWithToolCall.js index 0a45866e361..097e430e984 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetryWithToolCall.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/telemetryWithToolCall.js @@ -4,7 +4,7 @@ /** * Demonstrates how to use tool calls with chat completions with telemetry. * - * @summary Get chat completions with function call. + * @summary Get chat completions with function call with instrumentation. */ const { DefaultAzureCredential } = require("@azure/identity"); @@ -59,7 +59,7 @@ const getCurrentWeather = { }; const getWeatherFunc = (location, unit) => { - if (unit != "celsius") { + if (unit !== "celsius") { unit = "fahrenheit"; } return `The temperature in ${location} is 72 degrees ${unit}`; @@ -192,15 +192,24 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } + main().catch((err) => { console.error("The sample encountered an error:", err); }); diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/toolCall.js b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/toolCall.js index 914a13bad27..c4d5c104424 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/toolCall.js +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/javascript/toolCall.js @@ -160,11 +160,19 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/chatCompletions.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/chatCompletions.ts index 3461407485e..cfc9bb6c8b1 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/chatCompletions.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/chatCompletions.ts @@ -50,13 +50,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/embeddings.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/embeddings.ts index b13a18939e0..ce279914511 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/embeddings.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/embeddings.ts @@ -46,13 +46,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/getModelInfo.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/getModelInfo.ts index 2b8bd8a980f..0df41cf9dc0 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/getModelInfo.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/getModelInfo.ts @@ -39,13 +39,21 @@ export async function main(): Promise { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/imageFileCompletions.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/imageFileCompletions.ts index 0a39606cccc..e9ba670b961 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/imageFileCompletions.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/imageFileCompletions.ts @@ -33,10 +33,14 @@ export async function main() { body: { messages: [ { role: "system", content: "You are a helpful assistant that describes images in details." }, - { role: "user", content: [ - { type: "text", text: "What's in this image?"}, - { type: "image_url", image_url: { - url: getImageDataUrl(imageFilePath, imageFormat)}} + { + role: "user", content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", image_url: { + url: getImageDataUrl(imageFilePath, imageFormat) + } + } ] } ], @@ -59,13 +63,13 @@ export async function main() { */ function getImageDataUrl(imageFile: string, imageFormat: string): string { try { - const imageBuffer = fs.readFileSync(imageFile); - const imageBase64 = imageBuffer.toString('base64'); - return `data:image/${imageFormat};base64,${imageBase64}`; + const imageBuffer = fs.readFileSync(imageFile); + const imageBase64 = imageBuffer.toString('base64'); + return `data:image/${imageFormat};base64,${imageBase64}`; } catch (error) { - console.error(`Could not read '${imageFile}'.`); - console.error('Set the correct path to the image file before running this sample.'); - process.exit(1); + console.error(`Could not read '${imageFile}'.`); + console.error('Set the correct path to the image file before running this sample.'); + process.exit(1); } } @@ -74,13 +78,21 @@ function getImageDataUrl(imageFile: string, imageFormat: string): string { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamChatCompletions.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamChatCompletions.ts index fa1dcf347e3..fe4663a5e5c 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamChatCompletions.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamChatCompletions.ts @@ -77,13 +77,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamingToolCall.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamingToolCall.ts index 041e0983e22..70978e5cddf 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamingToolCall.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/streamingToolCall.ts @@ -64,10 +64,10 @@ const getWeatherFunc = (location: string, unit: string): string => { const updateToolCalls = (toolCallArray: Array, functionArray: Array) => { const dummyFunction = { name: "", arguments: "", id: "" }; - while ( functionArray.length < toolCallArray.length ) { + while (functionArray.length < toolCallArray.length) { functionArray.push(dummyFunction); } - + let index = 0; for (const toolCall of toolCallArray) { if (toolCall.function.name) { @@ -90,7 +90,7 @@ const handleToolCalls = (functionArray: Array) => { let content = ""; switch (func.name) { - + case "get_current_weather": content = getWeatherFunc(funcArgs.location, funcArgs.unit ?? "fahrenheit"); messageArray.push({ @@ -123,7 +123,7 @@ const streamToString = async (stream: NodeJS.ReadableStream) => { export async function main() { const client = createModelClient(); - + const messages = [{ role: "user", content: "What's the weather like in Boston?" }]; let toolCallAnswer = ""; @@ -177,8 +177,7 @@ export async function main() { const messageArray = handleToolCalls(functionArray); messages.push(...messageArray); } else { - if (choice.delta?.content && choice.delta.content != '') - { + if (choice.delta?.content && choice.delta.content != '') { toolCallAnswer += choice.delta?.content; awaitingToolCallAnswer = false; } @@ -186,7 +185,7 @@ export async function main() { } } } - + console.log("Model response after tool call:"); console.log(toolCallAnswer); @@ -197,13 +196,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetry.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetry.ts index 780f0401981..75317b08e17 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetry.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetry.ts @@ -75,16 +75,25 @@ async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } + main().catch((err) => { console.error("The sample encountered an error:", err); }); diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetryWithToolCall.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetryWithToolCall.ts index 870286b07a4..2c464b21074 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetryWithToolCall.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/telemetryWithToolCall.ts @@ -4,7 +4,7 @@ /** * Demonstrates how to use tool calls with chat completions with telemetry. * - * @summary Get chat completions with function call. + * @summary Get chat completions with function call with instrumentation. */ import { DefaultAzureCredential } from "@azure/identity"; @@ -193,15 +193,24 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } } + main().catch((err) => { console.error("The sample encountered an error:", err); }); diff --git a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/toolCall.ts b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/toolCall.ts index cdddd9f8949..7a3aec87f49 100644 --- a/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/toolCall.ts +++ b/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src/toolCall.ts @@ -166,13 +166,21 @@ export async function main() { */ function createModelClient() { // auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default - // (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints) + // auth scope for MaaS and MaaP is currently https://ml.azure.com + // (Do not use for Serverless API or Managed Computer Endpoints) if (key) { - return ModelClient(endpoint, new AzureKeyCredential(key)); + return ModelClient(endpoint, new AzureKeyCredential(key)); } else { - const scopes = ["https://cognitiveservices.azure.com/.default"]; + const scopes: string[] = []; + if (endpoint.includes(".models.ai.azure.com")) { + scopes.push("https://ml.azure.com"); + } + else if (endpoint.includes(".openai.azure.com/openai/deployments/")) { + scopes.push("https://cognitiveservices.azure.com"); + } + const clientOptions = { credentials: { scopes } }; - return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); + return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions); } }