Azure OpenAI Service provides access to powerful language models that enable a wide range of AI-driven applications, from natural language understanding to text generation. In this blog post, we’ll walk through how to connect to Azure OpenAI using JavaScript and the @azure/openai package. We'll explain the code in detail to help you get started quickly.
Source code available in gist: https://gist.github.com/kamiljaneczek/07f55d4055beede32487a8df6ab3a738
Before we dive into the code, make sure you have the following:
Setting Up the Environment First, let’s set up our environment by installing the necessary packages. Run the following commands in your terminal:
npm install @azure/openai dotenv
The @azure/openai package is the official SDK for interacting with Azure OpenAI, and dotenv is used for loading environment variables from a .env file.
Here's the code to connect to Azure OpenAI and get a chat completion response:
import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
import dotenv from "dotenv";
dotenv.config();
const client = new OpenAIClient(
process.env.AZURE_OPENAI_ENDPOINT as string,
new AzureKeyCredential(process.env.AZURE_OPENAI_KEY as string)
);
const messages = [
{ role: "user", content: "What is the biggest city in Canada?" },
];
const { id, created, choices, usage } = await client.getChatCompletions(
"gpt-4",
messages
);
if (!choices || choices.length === 0) {
throw new Error("No completions returned from the API");
} else {
console.log("id: ", id);
console.log("created: ", created);
console.log("usage: ", usage);
console.log("reply: ", choices[0].message!.content);
}
Explanation
import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
import dotenv from "dotenv";
We import the OpenAIClient and AzureKeyCredential from the @azure/openai package. We also import dotenv to handle environment variables.
dotenv.config();
The dotenv.config() function loads environment variables from a .env file into process.env. Make sure you have a .env file in your project root with the following content:
AZURE_OPENAI_ENDPOINT=<Your Azure OpenAI Endpoint>
AZURE_OPENAI_KEY=<Your Azure OpenAI Key>
const client = new OpenAIClient(
process.env.AZURE_OPENAI_ENDPOINT as string,
new AzureKeyCredential(process.env.AZURE_OPENAI_KEY as string)
);
We create a new instance of OpenAIClient using the endpoint and key stored in environment variables. The AzureKeyCredential is used to securely pass the API key.
const messages = [
{ role: "user", content: "What is the biggest city in Canada?" },
];
We define an array of messages to send to the OpenAI model. Each message has a role (e.g., "user") and content (the actual message).
const { id, created, choices, usage } = await client.getChatCompletions(
"gpt-4",
messages
);
We use the getChatCompletions method of the client to send the messages to the "gpt-4" model and receive the response. The method returns an object containing the response id, created timestamp, choices, and usage information.
if (!choices || choices.length === 0) {
throw new Error("No completions returned from the API");
} else {
console.log("id: ", id);
console.log("created: ", created);
console.log("usage: ", usage);
console.log("reply: ", choices[0].message!.content);
}
We check if the choices array is empty. If it is, we throw an error. Otherwise, we log the response details, including the reply from the model.
Connecting to Azure OpenAI is straightforward with the @azure/openai package. By following the steps outlined above, you can quickly set up your environment and start interacting with powerful AI models. Whether you’re building a chatbot, generating content, or performing any other language-related tasks, Azure OpenAI provides the tools you need.
Feel free to experiment with different models and messages to explore the full capabilities of Azure OpenAI. Happy coding!
Social share
About the autor
Low code enthusiast, automation advocate, open-source supporter, digital transformation lead consultant, skilled Pega LSA holding LSA certification since 2018, Pega expert and JavaScript full-stack developer as well as people manager. 13+ years of experience in the IT field with a focus on designing and implementing large scale IT systems for world's biggest companies. Professional knowledge of: software design, enterprise architecture, project management and project delivery methods, BPM, CRM, Low-code platforms and Pega 8/23/24 suite.
Releated Posts