1.0.0-beta.12 • Published 2 days ago

@azure/openai v1.0.0-beta.12

Weekly downloads
-
License
MIT
Repository
-
Last release
2 days ago

Azure OpenAI client library for JavaScript

The Azure OpenAI client library for JavaScript is an adaptation of OpenAI's REST APIs that provides an idiomatic interface and rich integration with the rest of the Azure SDK ecosystem. It can connect to Azure OpenAI resources or to the non-Azure OpenAI inference endpoint, making it a great choice for even non-Azure OpenAI development.

Use the client library for Azure OpenAI to:

Azure OpenAI is a managed service that allows developers to deploy, tune, and generate content from OpenAI models on Azure resources.

Key links:

Getting started

const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");

const client = new OpenAIClient(
  "https://<resource name>.openai.azure.com/", 
  new AzureKeyCredential("<Azure API key>")
);
const { id, created, choices, usage } = await client.getCompletions("<deployment ID>", ["YOUR PROMPT HERE"]);

Currently supported environments

Prerequisites

If you'd like to use an Azure OpenAI resource, you must have an Azure subscription and Azure OpenAI access. This will allow you to create an Azure OpenAI resource and get both a connection URL as well as API keys. For more information, see Quickstart: Get started generating text using Azure OpenAI Service.

If you'd like to use the Azure OpenAI JS client library to connect to non-Azure OpenAI, you'll need an API key from a developer account at https://platform.openai.com/.

Install the @azure/openai package

Install the Azure OpenAI client client library for JavaScript with npm:

npm install @azure/openai

Create and authenticate a OpenAIClient

To configure a client for use with Azure OpenAI, provide a valid endpoint URI to an Azure OpenAI resource along with a corresponding key credential, token credential, or Azure identity credential that's authorized to use the Azure OpenAI resource. To instead configure the client to connect to OpenAI's service, provide an API key from OpenAI's developer portal.

Using an API Key from Azure

Use the Azure Portal to browse to your OpenAI resource and retrieve an API key, or use the Azure CLI snippet below:

Note: Sometimes the API key is referred to as a "subscription key" or "subscription API key."

az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>

Once you have an API key and endpoint, you can use the AzureKeyCredential class to authenticate the client as follows:

const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");

const client = new OpenAIClient("<endpoint>", new AzureKeyCredential("<API key>"));

Using an Azure Active Directory Credential

Client API key authentication is used in most of the examples, but you can also authenticate with Azure Active Directory using the Azure Identity library. To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, please install the @azure/identity package:

npm install @azure/identity

You will also need to register a new AAD application and grant access to OpenAI by assigning the "Cognitive Services User" role to your service principal (note: other roles such as "Owner" will not grant the necessary permissions, only "Cognitive Services User" will suffice to run the examples and the sample code).

Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET.

const { OpenAIClient } = require("@azure/openai");
const { DefaultAzureCredential } = require("@azure/identity");

const client = new OpenAIClient("<endpoint>", new DefaultAzureCredential());

Using an API Key from OpenAI

To instead configure the client to connect to OpenAI's service, provide an API key from OpenAI's developer portal. Once you have an API key, you can use the OpenAIKeyCredential class to authenticate the client as follows:

const { OpenAIClient, OpenAIKeyCredential } = require("@azure/openai");

const client = new OpenAIClient(new OpenAIKeyCredential("<API key>"));

Key concepts

The main concept to understand is Completions. Briefly explained, completions provides its functionality in the form of a text prompt, which by using a specific model, will then attempt to match the context and patterns, providing an output text. The following code snippet provides a rough overview:

const client = new OpenAIClient(
  "https://your-azure-openai-resource.com/",
  new AzureKeyCredential("your-azure-openai-resource-api-key"));

const { choices } = await client.getCompletions(
  "text-davinci-003", // assumes a matching model deployment or model name
  ["Hello, world!"]);

for (const choice of choices) {
  console.log(choice.text);
}

Examples

You can familiarize yourself with different APIs using Samples.

Generate Chatbot Response

This example authenticates using a DefaultAzureCredential, then generates text responses to input prompts.

const endpoint = "https://myaccount.openai.azure.com/";
const client = new OpenAIClient(endpoint, new DefaultAzureCredential());

const deploymentName = "text-davinci-003";
const prompt = ["What is Azure OpenAI?"];
console.log(`Input: ${prompt}`);

const { choices } = await client.getCompletions(deploymentName, prompt);
const completion = choices[0].text;
console.log(`Chatbot: ${completion}`);

Generate Multiple Chatbot Responses With Subscription Key

This example generates text responses to input prompts using an Azure subscription key

// Replace with your Azure OpenAI key
const key = "YOUR_AZURE_OPENAI_KEY";
const endpoint = "https://myaccount.openai.azure.com/";
const client = new OpenAIClient(endpoint, new AzureKeyCredential(key));

const examplePrompts = [
  "How are you today?",
  "What is Azure OpenAI?",
  "Why do children love dinosaurs?",
  "Generate a proof of Euler's identity",
  "Describe in single words only the good things that come into your mind about your mother.",
];

const deploymentName = "text-davinci-003";

let promptIndex = 0;
const { choices } = await client.getCompletions(deploymentName, examplePrompts);
for (const choice of choices) {
  const completion = choice.text;
  console.log(`Input: ${examplePrompts[promptIndex++]}`);
  console.log(`Chatbot: ${completion}`);
}

Summarize Text with Completion

This example generates a summarization of the given input prompt.

const endpoint = "https://myaccount.openai.azure.com/";
const client = new OpenAIClient(endpoint, new DefaultAzureCredential());

const textToSummarize = `
  Two independent experiments reported their results this morning at CERN, Europe's high-energy physics laboratory near Geneva in Switzerland. Both show convincing evidence of a new boson particle weighing around 125 gigaelectronvolts, which so far fits predictions of the Higgs previously made by theoretical physicists.

  ""As a layman I would say: 'I think we have it'. Would you agree?"" Rolf-Dieter Heuer, CERN's director-general, asked the packed auditorium. The physicists assembled there burst into applause.
 :`;

const summarizationPrompt = [`
  Summarize the following text.

  Text:
  """"""
  ${textToSummarize}
  """"""

  Summary:
`];

console.log(`Input: ${summarizationPrompt}`);

const deploymentName = "text-davinci-003";

const { choices } = await client.getCompletions(deploymentName, summarizationPrompt);
const completion = choices[0].text;
console.log(`Summarization: ${completion}`);

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

const { setLogLevel } = require("@azure/logger");

setLogLevel("info");

For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.

1.0.0-beta.12

29 days ago

1.0.0-beta.11

3 months ago

1.0.0-beta.10

4 months ago

1.0.0-beta.9

4 months ago

1.0.0-beta.8

5 months ago

1.0.0-beta.7

6 months ago

1.0.0-beta.3

10 months ago

1.0.0-beta.4

9 months ago

1.0.0-beta.5

8 months ago

1.0.0-beta.6

7 months ago

1.0.0-beta.2

11 months ago

1.0.0-beta.1

12 months ago