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

OpenAIClient(KeyCredential, OpenAIClientOptions)

Initializes an instance of OpenAIClient for use with the non-Azure OpenAI endpoint.

OpenAIClient(string, KeyCredential, OpenAIClientOptions)

Initializes an instance of OpenAIClient for use with an Azure OpenAI resource.

OpenAIClient(string, TokenCredential, OpenAIClientOptions)

Initializes an instance of OpenAIClient for use with an Azure OpenAI resource.

Methods

getAudioTranscription(string, Uint8Array, GetAudioTranscriptionOptions)

Returns the transcription of an audio file in a simple JSON format.

getAudioTranscription<Format>(string, Uint8Array, Format, GetAudioTranscriptionOptions)

Returns the transcription of an audio file.

getAudioTranslation(string, Uint8Array, GetAudioTranslationOptions)

Returns the translation of an audio file.

getAudioTranslation<Format>(string, Uint8Array, Format, GetAudioTranslationOptions)

Returns the translation of an audio file.

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.

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.

getEmbeddings(string, string[], GetEmbeddingsOptions)

Return the embeddings for a given prompt.

getImages(string, string, GetImagesOptions)

Creates an image given a prompt.

streamChatCompletions(string, ChatRequestMessageUnion[], GetChatCompletionsOptions)

Lists the chat completions tokens as they become available for a chat context.

streamCompletions(string, string[], GetCompletionsOptions)

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

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

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

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[]

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[]

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

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

ChatRequestMessageUnion[]

The chat context messages to use for this request.

options
GetChatCompletionsOptions

The chat completions options for this chat completions request.

Returns

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.