กิจกรรม
17 มี.ค. 21 - 21 มี.ค. 10
แอปอัจฉริยะ เข้าร่วมชุด meetup เพื่อสร้างโซลูชัน AI ที่ปรับขนาดได้ตามกรณีการใช้งานจริงกับนักพัฒนาและผู้เชี่ยวชาญร่วมกัน
ลงทะเบียนตอนนี้เบราว์เซอร์นี้ไม่ได้รับการสนับสนุนอีกต่อไป
อัปเกรดเป็น Microsoft Edge เพื่อใช้ประโยชน์จากคุณลักษณะล่าสุด เช่น การอัปเดตความปลอดภัยและการสนับสนุนด้านเทคนิค
As of June 2024, we recommend migrating to the OpenAI JavaScript API library 4.x, the latest version of the official OpenAI JavaScript client library that supports the Azure OpenAI Service API version 2022-12-01
and later. This article helps you bring you up to speed on the changes specific to Azure OpenAI.
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.
There are several ways to authenticate with the Azure OpenAI service 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();
This object is then passed to the second argument of the OpenAIClient
and AssistantsClient
client constructors.
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://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
azureADTokenProvider
is passed to the options object when creating the AzureOpenAI
client.
API keys are not recommended for production use because they are less secure than other authentication methods. Previously, AzureKeyCredential
objects were created as follows to authenticate OpenAIClient
or AssistantsClient
:
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.
ข้อสำคัญ
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.
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.
There are key differences between the OpenAIClient
and AssistantsClient
clients and the AzureOpenAI
client:
OpenAIClient
and AssistantsClient
, for example client.getChatCompletions
. In AzureOpenAI
, operations are grouped in nested groups, for example client.chat.completions.create
.OpenAIClient
and AssistantsClient
rename many of the names used in the Azure OpenAI service API. For example, snake case is used in the API but camel case is used in the client. In AzureOpenAI
, names are kept the same as in the Azure OpenAI service API.The following sections provide examples of how to migrate from OpenAIClient
and AssistantsClient
to AzureOpenAI
.
const result = await client.chat.completions.create({ messages, model: '', max_tokens: 100 });
Note the following:
getChatCompletions
method has been replaced with the chat.completions.create
method.messages
parameter is now passed in the options object with the messages
property.maxTokens
property has been renamed to max_tokens
and the deploymentName
parameter has been removed. Generally, the names of the properties in the options
object are the same as in the Azure OpenAI service API, following the snake case convention instead of the camel case convention used in the AssistantsClient
. This is true for all the properties across all requests and responses in the AzureOpenAI
client.deploymentName
parameter isn't needed if the client was created with the deployment
option. If the client was not created with the deployment
option, the model
property in the option object should be set with the deployment name.const stream = await client.chat.completions.create({ model: '', messages, max_tokens: 100, stream: true });
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.azureExtensionOptions
property has been replaced with the inner data_sources
property.parameters
property has been added to wrap the parameters of the extension, which mirrors the schema of the Azure OpenAI service API.import { createReadStream } from "fs";
const result = await client.audio.transcriptions.create({
model: '',
file: createReadStream(audioFilePath),
});
getAudioTranscription
method has been replaced with the audio.transcriptions.create
method.AzureOpenAI
has to be constructed with the deployment
option set to the deployment name in order to use audio operations such as audio.transcriptions.create
.model
property 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.file
property accepts various types including Buffer
, fs.ReadaStream
, and Blob
but in this example, a file is streamed from disk using fs.createReadStream
.import { createReadStream } from "fs";
const result = await client.audio.translations.create({
model: '',
file: createReadStream(audioFilePath),
});
getAudioTranslation
method has been replaced with the audio.translations.create
method.The following examples show how to migrate some of the AssistantsClient
methods.
const options = ...;
const assistantResponse = await assistantsClient.beta.assistants.create(
options
);
createAssistant
method has been replaced with the beta.assistants.create
methodThe following example shows how to migrate the createThread
method call.
const assistantThread = await assistantsClient.beta.threads.create();
createThread
method has been replaced with the beta.threads.create
methodThe following example shows how to migrate the createMessage
method call.
const threadResponse = await assistantsClient.beta.threads.messages.create(
assistantThread.id,
{
role,
content: message,
}
);
createMessage
method has been replaced with the beta.threads.messages.create
method.To run an assistant on a thread, the createRun
method is used to create a run, and then a loop is used to poll the run status until it is in a terminal state. The following example shows how to migrate the run creation and polling.
This code can be migrated and simplified by using the createAndPoll
method, which creates a run and polls it until it is in a terminal state.
const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
assistantThread.id,
{
assistant_id: assistantResponse.id,
},
{ pollIntervalMs: 500 }
);
createRun
method has been replaced with the beta.threads.runs.create
and createAndPoll
methods.createAndPoll
method is used to create a run and poll it until it is in a terminal state.Pages can be looped through by using the for await
loop.
for await (const runMessageDatum of runMessages) {
for (const item of runMessageDatum.content) {
...
}
}
The following example shows how to migrate the getEmbeddings
method call.
const embeddings = await client.embeddings.create({ input, model: '' });
getEmbeddings
method has been replaced with the embeddings.create
method.input
parameter is now passed in the options object with the input
property.deploymentName
parameter has been removed. The deploymentName
parameter isn't needed if the client was created with the deployment
option. If the client was not created with the deployment
option, the model
property in the option object should be set with the deployment name.The following example shows how to migrate the getImages
method call.
const results = await client.images.generate({ prompt, model: '', n, size });
getImages
method has been replaced with the images.generate
method.prompt
parameter is now passed in the options object with the prompt
property.deploymentName
parameter has been removed. The deploymentName
parameter isn't needed if the client was created with the deployment
option. If the client was not created with the deployment
option, the model
property in the option object should be set with the deployment name.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;
...
}
"@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.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 |
AzureOpenAI
connects to the Azure OpenAI service 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.
กิจกรรม
17 มี.ค. 21 - 21 มี.ค. 10
แอปอัจฉริยะ เข้าร่วมชุด meetup เพื่อสร้างโซลูชัน AI ที่ปรับขนาดได้ตามกรณีการใช้งานจริงกับนักพัฒนาและผู้เชี่ยวชาญร่วมกัน
ลงทะเบียนตอนนี้การฝึกอบรม
ใบรับรอง
รับรองโดย Microsoft: หลักพื้นฐานของ Azure AI - Certifications
แสดงให้เห็นแนวคิด AI พื้นฐานที่เกี่ยวข้องกับการพัฒนาซอฟต์แวร์และบริการของ Microsoft Azure เพื่อสร้างโซลูชัน AI