OpenAIClient class
Warning
This API is now deprecated.
The Azure OpenAI client library for JavaScript beta will be retired on January 14th, 2025. Please migrate to the stable OpenAI SDK for JavaScript using the migration guide before that date.
A client for interacting with Azure OpenAI.
The client needs the endpoint of an OpenAI resource and an authentication method such as an API key or token. The API key and endpoint can be found in the OpenAI resource page. They will be located in the resource's Keys and Endpoint page.
Examples for authentication:
API Key
import { OpenAIClient } from "@azure/openai";
import { AzureKeyCredential } from "@azure/core-auth";
const endpoint = "<azure endpoint>";
const credential = new AzureKeyCredential("<api key>");
const client = new OpenAIClient(endpoint, credential);
Azure Active Directory
import { OpenAIClient } from "@azure/openai";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "<azure endpoint>";
const credential = new DefaultAzureCredential();
const client = new OpenAIClient(endpoint, credential);
Constructors
Open |
Initializes an instance of OpenAIClient for use with the non-Azure OpenAI endpoint. |
Open |
Initializes an instance of OpenAIClient for use with an Azure OpenAI resource. |
Open |
Initializes an instance of OpenAIClient for use with an Azure OpenAI resource. |
Methods
get |
Returns the transcription of an audio file in a simple JSON format. |
get |
Returns the transcription of an audio file. |
get |
Returns the translation of an audio file. |
get |
Returns the translation of an audio file. |
get |
Gets chat completions for the provided chat messages. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data. |
get |
Gets completions for the provided input prompts. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data. |
get |
Return the embeddings for a given prompt. |
get |
Creates an image given a prompt. |
stream |
Lists the chat completions tokens as they become available for a chat context. |
stream |
Lists the completions tokens as they become available for a given prompt. |
Constructor Details
OpenAIClient(KeyCredential, OpenAIClientOptions)
Initializes an instance of OpenAIClient for use with the non-Azure OpenAI endpoint.
new OpenAIClient(openAiApiKey: KeyCredential, options?: OpenAIClientOptions)
Parameters
- openAiApiKey
- KeyCredential
The API key to use when connecting to the non-Azure OpenAI endpoint.
- options
- OpenAIClientOptions
The options for configuring the client.
Remarks
OpenAIClient objects initialized with this constructor can only be used with the non-Azure OpenAI inference endpoint. To use OpenAIClient with an Azure OpenAI resource, use a constructor that accepts a resource URI and Azure authentication credential instead.
OpenAIClient(string, KeyCredential, OpenAIClientOptions)
Initializes an instance of OpenAIClient for use with an Azure OpenAI resource.
new OpenAIClient(endpoint: string, credential: KeyCredential, options?: OpenAIClientOptions)
Parameters
- endpoint
-
string
The URI for an Azure OpenAI resource, including protocol and hostname. For example: https://my-resource.openai.azure.com.
- credential
- KeyCredential
A key credential used to authenticate to an Azure OpenAI resource.
- options
- OpenAIClientOptions
The options for configuring the client.
Remarks
This constructor initializes an OpenAIClient object that can only be used with Azure OpenAI resources. To use OpenAIClient with a non-Azure OpenAI inference endpoint, use a constructor that accepts a non-Azure OpenAI API key instead.
OpenAIClient(string, TokenCredential, OpenAIClientOptions)
Initializes an instance of OpenAIClient for use with an Azure OpenAI resource.
new OpenAIClient(endpoint: string, credential: TokenCredential, options?: OpenAIClientOptions)
Parameters
- endpoint
-
string
The URI for an Azure OpenAI resource, including protocol and hostname. For example: https://my-resource.openai.azure.com.
- credential
- TokenCredential
A token credential used to authenticate with an Azure OpenAI resource.
- options
- OpenAIClientOptions
The options for configuring the client.
Method Details
getAudioTranscription(string, Uint8Array, GetAudioTranscriptionOptions)
Returns the transcription of an audio file in a simple JSON format.
function getAudioTranscription(deploymentName: string, fileContent: Uint8Array, options?: GetAudioTranscriptionOptions): Promise<AudioResultSimpleJson>
Parameters
- deploymentName
-
string
The name of the model deployment (when using Azure OpenAI) or model name (when using non-Azure OpenAI) to use for this request.
- fileContent
-
Uint8Array
The content of the audio file to transcribe.
- options
- GetAudioTranscriptionOptions
The options for this audio transcription request.
Returns
Promise<AudioResultSimpleJson>
The audio transcription result in a simple JSON format.
getAudioTranscription<Format>(string, Uint8Array, Format, GetAudioTranscriptionOptions)
Returns the transcription of an audio file.
function getAudioTranscription<Format>(deploymentName: string, fileContent: Uint8Array, format: Format, options?: GetAudioTranscriptionOptions): Promise<AudioResult<Format>>
Parameters
- deploymentName
-
string
The name of the model deployment (when using Azure OpenAI) or model name (when using non-Azure OpenAI) to use for this request.
- fileContent
-
Uint8Array
The content of the audio file to transcribe.
- format
-
Format
The format of the result object. See AudioResultFormat for possible values.
- options
- GetAudioTranscriptionOptions
The options for this audio transcription request.
Returns
Promise<AudioResult<Format>>
The audio transcription result in a format of your choice.
getAudioTranslation(string, Uint8Array, GetAudioTranslationOptions)
Returns the translation of an audio file.
function getAudioTranslation(deploymentName: string, fileContent: Uint8Array, options?: GetAudioTranslationOptions): Promise<AudioResultSimpleJson>
Parameters
- deploymentName
-
string
The name of the model deployment (when using Azure OpenAI) or model name (when using non-Azure OpenAI) to use for this request.
- fileContent
-
Uint8Array
The content of the audio file to translate.
- options
- GetAudioTranslationOptions
The options for this audio translation request.
Returns
Promise<AudioResultSimpleJson>
The audio translation result.
getAudioTranslation<Format>(string, Uint8Array, Format, GetAudioTranslationOptions)
Returns the translation of an audio file.
function getAudioTranslation<Format>(deploymentName: string, fileContent: Uint8Array, format: Format, options?: GetAudioTranslationOptions): Promise<AudioResult<Format>>
Parameters
- deploymentName
-
string
The name of the model deployment (when using Azure OpenAI) or model name (when using non-Azure OpenAI) to use for this request.
- fileContent
-
Uint8Array
The content of the audio file to translate.
- format
-
Format
The format of the result object. See AudioResultFormat for possible values.
- options
- GetAudioTranslationOptions
The options for this audio translation request.
Returns
Promise<AudioResult<Format>>
The audio translation result.
getChatCompletions(string, ChatRequestMessageUnion[], GetChatCompletionsOptions)
Gets chat completions for the provided chat messages. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data.
function getChatCompletions(deploymentName: string, messages: ChatRequestMessageUnion[], options?: GetChatCompletionsOptions): Promise<ChatCompletions>
Parameters
- deploymentName
-
string
- messages
- options
- GetChatCompletionsOptions
Returns
Promise<ChatCompletions>
getCompletions(string, string[], GetCompletionsOptions)
Gets completions for the provided input prompts. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data.
function getCompletions(deploymentName: string, prompt: string[], options?: GetCompletionsOptions): Promise<Completions>
Parameters
- deploymentName
-
string
- prompt
-
string[]
- options
- GetCompletionsOptions
Returns
Promise<Completions>
getEmbeddings(string, string[], GetEmbeddingsOptions)
Return the embeddings for a given prompt.
function getEmbeddings(deploymentName: string, input: string[], options?: GetEmbeddingsOptions): Promise<Embeddings>
Parameters
- deploymentName
-
string
- input
-
string[]
- options
- GetEmbeddingsOptions
Returns
Promise<Embeddings>
getImages(string, string, GetImagesOptions)
Creates an image given a prompt.
function getImages(deploymentName: string, prompt: string, options?: GetImagesOptions): Promise<ImageGenerations>
Parameters
- deploymentName
-
string
- prompt
-
string
- options
- GetImagesOptions
Returns
Promise<ImageGenerations>
streamChatCompletions(string, ChatRequestMessageUnion[], GetChatCompletionsOptions)
Lists the chat completions tokens as they become available for a chat context.
function streamChatCompletions(deploymentName: string, messages: ChatRequestMessageUnion[], options?: GetChatCompletionsOptions): Promise<EventStream<ChatCompletions>>
Parameters
- deploymentName
-
string
The name of the model deployment (when using Azure OpenAI) or model name (when using non-Azure OpenAI) to use for this request.
- messages
The chat context messages to use for this request.
- options
- GetChatCompletionsOptions
The chat completions options for this chat completions request.
Returns
Promise<EventStream<ChatCompletions>>
An asynchronous iterable of chat completions tokens.
streamCompletions(string, string[], GetCompletionsOptions)
Lists the completions tokens as they become available for a given prompt.
function streamCompletions(deploymentName: string, prompt: string[], options?: GetCompletionsOptions): Promise<EventStream<Omit<Completions, "usage">>>
Parameters
- deploymentName
-
string
The name of the model deployment (when using Azure OpenAI) or model name (when using non-Azure OpenAI) to use for this request.
- prompt
-
string[]
The prompt to use for this request.
- options
- GetCompletionsOptions
The completions options for this completions request.
Returns
Promise<EventStream<Omit<Completions, "usage">>>
An asynchronous iterable of completions tokens.
Azure SDK for JavaScript