Skip to content
background-image background-image

Using Azure OpenAI

This example shows how to use @azure/openai module.

Most APIs require authorization provided by @azure/identity module which is imported as azure.identity

Authorization using DefaultAzureCredential class is not supported, you can use ClientSecretCredential

@azure/openai module is imported as azure.openai

Authorize using Azure API Key

const { OpenAIClient, AzureKeyCredential } = azure.openai;

const openAIClient = new OpenAIClient(
  "https://<resource name>.openai.azure.com/",
  new AzureKeyCredential("<Azure API key>")
);

Authorize using Client Secret Credential

const { OpenAIClient } = azure.openai;
const { ClientSecretCredential } = azure.identity;

const clientSecretCredential = new ClientSecretCredential(
  "<tentant-id>",
  "<client-id>",
  "<client-secret>"
);

const openAIClient = new OpenAIClient(
  "https://<resource name>.openai.azure.com/",
  clientSecretCredential
);

Ask a question

// openAIClient can be obtained usin examples above
const { id, created, choices, usage } = await openAiClient.getCompletions(
  "<deployment ID>",
  ["YOUR PROMPT HERE"]
);

See Azure OpenAI client library for JavaScript for full examples.