Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies only to:
Foundry (classic) portal. This article isn't available for the new Foundry portal. Learn more about the new portal.
Note
Links in this article might open content in the new Microsoft Foundry documentation instead of the Foundry (classic) documentation you're viewing now.
Important
For the latest API examples refer to the API lifecycle article.
Authenticating the client
There are several ways to authenticate API requests to Azure OpenAI. We highly recommend using Microsoft Entra ID tokens. See the Azure Identity documentation for more information.
Microsoft Entra ID
There are several ways to authenticate with the Azure OpenAI using Microsoft Entra ID tokens. The default way is to use the DefaultAzureCredential class from the @azure/identity package, but your application might be using a different credential class. For the purposes of this guide, we assume that you are using the DefaultAzureCredential class. A credential can be created as follows:
import { DefaultAzureCredential } from "@azure/identity";
const credential = new DefaultAzureCredential();
Pass this object to the second argument of the OpenAIClient constructor.
In order to authenticate the AzureOpenAI client, however, we need to use the getBearerTokenProvider function from the @azure/identity package. This function creates a token provider that AzureOpenAI uses internally to obtain tokens for each request. The token provider is created as follows:
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
const credential = new DefaultAzureCredential();
const scope = "https://ai.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
azureADTokenProvider is passed to the options object when creating the AzureOpenAI client.
(Highly Discouraged) API Key
Don't use API keys in production because they're less secure than other authentication methods. Previously, you created AzureKeyCredential objects as follows to authenticate OpenAIClient:
import { AzureKeyCredential } from "@azure/openai";
const apiKey = new AzureKeyCredential("your API key");
On the other hand, AzureOpenAI can be authenticated with an API key by setting the AZURE_OPENAI_API_KEY environment variable or by setting the apiKey string property in the options object when creating the AzureOpenAI client.
Important
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If you use an API key, store it securely in Azure Key Vault. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
Constructing the client
import { AzureOpenAI } from "openai";
const deployment = "Your Azure OpenAI deployment";
const apiVersion = "2024-04-01-preview";
const options = { azureADTokenProvider, deployment, apiVersion }
const client = new AzureOpenAI(options);
The endpoint of the Azure OpenAI resource can be specified by setting the endpoint option but it can also be loaded by the client from the environment variable AZURE_OPENAI_ENDPOINT. This is the recommended way to set the endpoint because it allows the client to be used in different environments without changing the code and also to protect the endpoint from being exposed in the code.
The API version is required to be specified, this is necessary to ensure that existing code doesn't break between preview API versions. Refer to API versioning documentation to learn more about Azure OpenAI API versions. Additionally, the deployment property isn't required but it's recommended to be set. Once deployment is set, it's used as the default deployment for all operations that require it. If the client isn't created with the deployment option, the model property in the options object should be set with the deployment name. However, audio operations such as audio.transcriptions.create require the client to be created with the deployment option set to the deployment name.
API differences
The OpenAIClient and AzureOpenAI clients have key differences:
OpenAIClientpresents operations as a flat list of methods, such asclient.getChatCompletions.AzureOpenAIgroups operations in nested groups, such asclient.chat.completions.create.OpenAIClientuses different names than the Azure OpenAI API. For example, the API uses snake case, but the client uses camel case.AzureOpenAIuses the same names as the Azure OpenAI API.
Migration examples
The following sections provide examples of how to migrate from OpenAIClient to AzureOpenAI.
Chat completions
const result = await client.chat.completions.create({ messages, model: '', max_tokens: 100 });
Note the following:
- The
getChatCompletionsmethod has been replaced with thechat.completions.createmethod. - The
messagesparameter is now passed in the options object with themessagesproperty. - Rename the
maxTokensproperty tomax_tokensand remove thedeploymentNameparameter. Generally, the names of the properties in theoptionsobject are the same as in the Azure OpenAI API. The API uses snake case instead of the camel case convention used inOpenAIClient. This naming convention applies to all properties across all requests and responses in theAzureOpenAIclient. - The
deploymentNameparameter isn't needed if the client was created with thedeploymentoption. If the client was not created with thedeploymentoption, themodelproperty in the option object should be set with the deployment name.
Streaming chat completions
const stream = await client.chat.completions.create({ model: '', messages, max_tokens: 100, stream: true });
Azure On Your Data
import "@azure/openai/types";
const azureSearchEndpoint = "Your Azure Search resource endpoint";
const azureSearchIndexName = "Your Azure Search index name";
const result = await client.chat.completions.create({ model: '', messages, data_sources: [{
type: "azure_search",
parameters: {
endpoint: azureSearchEndpoint,
index_name: azureSearchIndexName,
authentication: {
type: "system_assigned_managed_identity",
}
}
}]
});
"@azure/openai/types"is imported which adds Azure-specific definitions (for example,data_sources) to the client types.- The
azureExtensionOptionsproperty has been replaced with the innerdata_sourcesproperty. - The
parametersproperty has been added to wrap the parameters of the extension, which mirrors the schema of the Azure OpenAI API. - Camel case properties have been replaced with snake case properties.
Audio transcription
import { createReadStream } from "fs";
const result = await client.audio.transcriptions.create({
model: '',
file: createReadStream(audioFilePath),
});
- The
getAudioTranscriptionmethod has been replaced with theaudio.transcriptions.createmethod. - The
AzureOpenAIhas to be constructed with thedeploymentoption set to the deployment name in order to use audio operations such asaudio.transcriptions.create. - The
modelproperty is required to be set in the options object but its value isn't used in the operation so feel free to set it to any value. - The
fileproperty accepts various types includingBuffer,fs.ReadaStream, andBlobbut in this example, a file is streamed from disk usingfs.createReadStream.
Audio translation
import { createReadStream } from "fs";
const result = await client.audio.translations.create({
model: '',
file: createReadStream(audioFilePath),
});
- The
getAudioTranslationmethod has been replaced with theaudio.translations.createmethod. - All other changes are the same as in the audio transcription example.
Embeddings
The following example shows how to migrate the getEmbeddings method call.
const embeddings = await client.embeddings.create({ input, model: '' });
- The
getEmbeddingsmethod has been replaced with theembeddings.createmethod. - The
inputparameter is now passed in the options object with theinputproperty. - The
deploymentNameparameter has been removed. ThedeploymentNameparameter isn't needed if the client was created with thedeploymentoption. If the client was not created with thedeploymentoption, themodelproperty in the option object should be set with the deployment name.
Image generation
The following example shows how to migrate the getImages method call.
const results = await client.images.generate({ prompt, model: '', n, size });
- The
getImagesmethod has been replaced with theimages.generatemethod. - The
promptparameter is now passed in the options object with thepromptproperty. - The
deploymentNameparameter has been removed. ThedeploymentNameparameter isn't needed if the client was created with thedeploymentoption. If the client was not created with thedeploymentoption, themodelproperty in the option object should be set with the deployment name.
Content filter
Content filter results are part of the chat completions response types in OpenAIClient. However, AzureOpenAI does not have a direct equivalent to the contentFilterResults property in the ChatCompletion.Choice interface. The content filter results can be accessed by importing "@azure/openai/types" and accessing the content_filter_results property. The following example shows how to access the content filter results.
import "@azure/openai/types";
const result = await client.chat.completions.create({ model: '', messages });
for (const choice of results.choices) {
const filterResults = choice.content_filter_results;
if (!filterResults) {
console.log("No content filter is found");
return;
}
if (filterResults.error) {
console.log(
`Content filter ran into the error ${filterResults.error.code}: ${filterResults.error.message}`);
}
const { hate, sexual, self_harm, violence } = filterResults;
...
}
- Camel case properties have been replaced with snake case properties.
"@azure/openai/types"is imported which adds Azure-specific definitions (for example, content_filter_results) to the client types, see the Azure types section for more information.
Comparing Types
The following table explores several type names from @azure/openai and shows their nearest openai equivalent. The names differences illustrate several of the above-mentioned changes. This table provides an overview, and more detail and code samples are provided in the following sections.
| Old Type Name | Nearest New Type | Symbol Type | Change description |
|---|---|---|---|
OpenAIClient |
AzureOpenAI |
Class | This class replaces the former and has no methods in common with it. See the section on AzureOpenAI below. |
AudioResult |
Transcription/Transcription |
Interface | Depending on the calling operation, the two interfaces replace the former one |
AudioResultFormat |
inline union type of the response_format property |
Alias | It doesn't exist |
AudioResultSimpleJson |
Transcription/Transcription |
Interface | Depending on the calling operation, the two interfaces replace the former one |
AudioResultVerboseJson |
N/A | Interface | |
AudioSegment |
N/A | Interface | |
AudioTranscriptionTask |
N/A | Alias | |
AzureChatEnhancementConfiguration, AzureChatEnhancements, AzureChatExtensionConfiguration, AzureChatExtensionConfigurationUnion, AzureChatExtensionDataSourceResponseCitation, AzureChatExtensionsMessageContext, AzureChatExtensionType, AzureChatGroundingEnhancementConfiguration, AzureChatOCREnhancementConfiguration, AzureCosmosDBChatExtensionConfiguration, AzureCosmosDBFieldMappingOptions, AzureExtensionsOptions, AzureGroundingEnhancement, AzureGroundingEnhancementCoordinatePoint, AzureGroundingEnhancementLine, AzureGroundingEnhancementLineSpan, AzureMachineLearningIndexChatExtensionConfiguration, AzureSearchChatExtensionConfiguration, AzureSearchIndexFieldMappingOptions, AzureSearchQueryType, ContentFilterBlocklistIdResult, ContentFilterCitedDetectionResult, ContentFilterDetectionResult, ContentFilterErrorResults, ContentFilterResult, ContentFilterResultDetailsForPrompt, ContentFilterResultsForChoice, ContentFilterSeverity, ContentFilterResultsForPrompt, ContentFilterSuccessResultDetailsForPrompt, ContentFilterSuccessResultsForChoice, ElasticsearchChatExtensionConfiguration, ElasticsearchIndexFieldMappingOptions, ElasticsearchQueryType, ImageGenerationContentFilterResults, ImageGenerationPromptFilterResults, OnYourDataAccessTokenAuthenticationOptions, OnYourDataApiKeyAuthenticationOptions, OnYourDataAuthenticationOptions, OnYourDataAuthenticationOptionsUnion, OnYourDataConnectionStringAuthenticationOptions, OnYourDataDeploymentNameVectorizationSource, OnYourDataEncodedApiKeyAuthenticationOptions, OnYourDataEndpointVectorizationSource, OnYourDataKeyAndKeyIdAuthenticationOptions, OnYourDataModelIdVectorizationSource, OnYourDataSystemAssignedManagedIdentityAuthenticationOptions, OnYourDataUserAssignedManagedIdentityAuthenticationOptions, OnYourDataVectorizationSource, OnYourDataVectorizationSourceType, OnYourDataVectorizationSourceUnion, PineconeChatExtensionConfiguration, PineconeFieldMappingOptions |
N/A | Interfaces and Aliases | See the Azure types section below |
AzureKeyCredential |
N/A | Class | The API key can be provided as a string value |
ChatChoice |
ChatCompletion.Choice |
Interface | |
ChatChoiceLogProbabilityInfo |
Logprobs |
Interface | |
ChatCompletions |
ChatCompletion and ChatCompletionChunk |
Interface | |
ChatCompletionsFunctionToolCall |
ChatCompletionMessageToolCall |
Interface | |
ChatRequestFunctionMessage |
ChatCompletionFunctionMessageParam |
Interface | |
ChatRequestMessage |
ChatCompletionMessageParam |
Interface | |
ChatRequestMessageUnion |
ChatCompletionMessageParam |
||
ChatRequestSystemMessage |
ChatCompletionSystemMessageParam |
Interface | |
ChatRequestToolMessage |
ChatCompletionToolMessageParam |
Interface | |
ChatRequestUserMessage |
ChatCompletionUserMessageParam |
Interface | |
ChatResponseMessage |
Delta / ChatCompletionMessage |
Interface | |
ChatRole |
N/A | Alias | |
ChatTokenLogProbabilityInfo |
TopLogprob |
Interface | |
ChatTokenLogProbabilityResult |
ChatCompletionTokenLogprob |
Interface | |
Choice |
Choice |
Interface | |
Completions |
Completion |
Interface | |
CompletionsFinishReason |
N/A | Alias | |
CompletionsLogProbabilityModel |
Logprobs |
Interface | |
CompletionsUsage |
CompletionUsage |
Interface | |
EmbeddingItem |
Embedding |
Interface | |
Embeddings |
CreateEmbeddingResponse |
Interface | |
EmbeddingsUsage |
CreateEmbeddingResponse.Usage |
Interface | |
EventStream |
Stream |
Interface | |
FunctionCall |
FunctionCall |
Interface | |
FunctionCallPreset |
N/A | Alias | |
FunctionDefinition |
Function |
Interface | |
FunctionName |
N/A | Alias | |
GetAudioTranscriptionOptions |
TranscriptionCreateParams |
Interface | |
GetAudioTranslationOptions |
TranslationCreateParams |
Interface | |
GetChatCompletionsOptions |
ChatCompletionCreateParamsNonStreaming and ChatCompletionCreateParamsStreaming |
Interface | |
GetCompletionsOptions |
CompletionCreateParams |
Interface | |
GetEmbeddingsOptions |
EmbeddingCreateParams |
Interface | |
GetImagesOptions |
ImageGenerateParams |
Interface | |
ImageGenerationData |
Image |
Interface | |
ImageGenerationQuality |
N/A | Alias | |
ImageGenerationResponseFormat |
N/A | Alias | |
ImageGenerations |
ImagesResponse |
Interface | |
ImageGenerationStyle |
N/A | Alias | |
ImageSize |
N/A | Alias | |
MaxTokensFinishDetails |
N/A | Interface | |
OpenAIClientOptions |
AzureClientOptions |
Interface | |
OpenAIError |
OpenAIError |
Interface | |
OpenAIKeyCredential |
N/A | Class | |
StopFinishDetails |
N/A | Interface |
Azure types
AzureOpenAI connects to the Azure OpenAI and can call all the operations available in the service. However, the types of the requests and responses are inherited from the OpenAI and are not yet updated to reflect the additional features supported exclusively by the Azure OpenAI service. TypeScript users will need to import "@azure/openai/types" from @azure/openai@2.0.0-beta.1 which will merge Azure-specific definitions into existing types. Examples in the Migration examples section show how to do this.