Skip to main content

TrustNest Azure OpenAI via APIM Documentation for NodeJS

In this document, you will find basic examples that uses node OpenAI client connected to the TrustNest API Manager.

Prerequisites

Ensure you have the openai node library installed. If not, install it using npm:

npm install openai

Usage

APIM JWT token and Azure APIM API key

For security and tracking purposes, requests to OpenAI API must include a JWT token and a APIM subscription key.

To do so, we use the custom headers TrustNest-Apim-Subscription-Key and Authorization.

Configure openai

import OpenAI from "openai";

const openai = new OpenAI({
apiKey: "", # Leave empty
baseURL: `https://trustnest.azure-api.net/genai-aoai-inference/v1`,
});

Common variables

const trustnest_apim_access_token = 'your-apim-access-token';
const trustnest_apim_subscription_key = "your-apim-subscription-key";
const api_version = "2023-05-15";

Create a Chat Completion

const chatcompletion_deployment = "gpt35-4k";
const openaiChatCompletionOptions = {
headers: {
Authorization: `Bearer ${trustnest_apim_access_token}`,
"TrustNest-Apim-Subscription-Key": trustnest_apim_subscription_key,
"Content-Type": "application/json",
},
path: `/deployments/${chatcompletion_deployment}/chat/completions?api-version=${api_version}`,
};

const completion = await openai.chat.completions.create(
{
messages: [
{ role: "system", content: "You are a helpful assistant." },
{
role: "user", content: "Implement basic merge sort algorithm using js.",
},
],
model: "gpt-35-turbo",
},
openaiChatCompletionOptions
);

Get embeddings

const embeddings_deployment = "text-embedding-ada-002";
const openaiEmbeddingsOptions = {
headers: {
Authorization: `Bearer ${trustnest_apim_access_token}`,
"TrustNest-Apim-Subscription-Key": trustnest_apim_subscription_key,
"Content-Type": "application/json",
},
path: `/deployments/${embeddings_deployment}/embeddings?api-version=${api_version}`,
};

const embeddings = await openai.embeddings.create(
{
input: "This is a test",
model: "text-embedding-ada-002",
},
openaiEmbeddingsOptions,
);