Wrap Azure Assistants API with NodeJS App

Arik Bidny
3 min readApr 19, 2024

--

Assistants API, a new feature of Azure OpenAI Service, is now available in public preview. The Assistants API simplifies the process for developers to create applications with sophisticated copilot-like experiences. These assistants can sift through data, suggest solutions, and automate tasks.

Whether you’re building chatbots, virtual assistants, or intelligent recommendation systems, the Assistants API empowers you to create powerful conversational experiences that enhance user interactions and productivity.

Why Wrap Azure Assistant API?

The Azure Assistant API provides advanced AI features that can be easily integrated into applications to create intelligent and responsive agents. By using a Node.js application to encapsulate this API, developers can leverage JavaScript’s user-friendly syntax and Node.js’s capabilities to build applications that are efficient, scalable, and require minimal maintenance.

Show me the Code!

To get started with your first Azure OpenAI Assistants, you will need the following:

To start using Azure assistants API, please refer to the documents provided below: https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/assistant

Create a NodeJS Express APP

Make sure Node.js is installed on your system. if not, download and install it from — https://nodejs.org/en/download

Required packages:

  • @azure/openai-assistants
  • express
  • dotenv
  • nodemon

Assistants Controller:

Here is a core part of the app, assistantController responsible for creating a static assistant, dynamic assistant (which receives parameters from a client), thread creation, and adding messages to the thread.

Create an assistant function

// @desc Create an assistant
// @route POST /api/assistant
// @access Public
exports.createAssistant = catchAsyncErrors(async (req, res, next) => {
console.log("Creating a new assistant...");
const assistant = await assistantsClient.createAssistant({
model: "<Deployment Name>",
name: "<AssistantName>",
instructions:
"Assistant Instructions",
tools: [{ type: "code_interpreter" }], // I'm using code_interpreter tool
});
assistantId = assistant.id;
console.log(assistantId);

res.status(200).json({
success: true,
assistant: assistant,
});
});

Create a thread function

// @desc Get an assistant
// @route GET /api/thread
// @access Public
exports.createThread = catchAsyncErrors(async (req, res, next) => {
console.log("Creating a new thread...");
const thread = await assistantsClient.createThread();

res.status(200).json({
success: true,
threadId: thread.id,
});
});

Add a message to the thread function

exports.addMessage = catchAsyncErrors(async (req, res, next) => {
const { message, threadId } = req.body;
addMessage(threadId, message).then((message) => {
console.log(message);
console.log(message.id);

runAssistant(threadId).then((run) => {
console.log(run);
console.log(run.id);
const runId = run.id;

// Check the status
pollingInterval = setInterval(() => {
checkingStatus(res, threadId, run.id);
}, 5000);
});
});
});

Check assistant status function

async function checkingStatus(res, threadId, runId) {
const runObject = await assistantsClient.getRun(threadId, runId);
console.log(runObject.status);

if (runObject.status == "completed") {
clearInterval(pollingInterval);

const messageList = await assistantsClient.listMessages(threadId);
let messages = [];

console.log(messageList);

messageList.data.forEach((message) => {
messages.push(message.content);
});

res.json({ messages });
}
}

Conclusion

By wrapping the Azure Assistant API in a Node.js application, developers can easily integrate sophisticated AI functionalities into their projects. This guide provides a simple yet powerful approach to manage API communications, helping you focus on building more intelligent, responsive applications.

For the complete code, please visit my GitHub repository:

https://github.com/arikbidny/assistants-api-nodejs-app

--

--