atlas by clearpeople

Using OpenAI NodeJS library in SharePoint calling Azure OpenAI service

  

Unless you are in a desert island with no internet connection, you have heard about ChatGPT. In its own words (and I really mean it, as I have asked ChatGPT for a short summary about it):

ChatGPT is a large language model developed by OpenAI, based on the GPT-3.5 architecture. It's designed to understand and generate human-like language, allowing it to have conversations with humans on a wide range of topics. As an AI assistant, ChatGPT can help answer questions, provide recommendations, generate text, and more. Its knowledge is based on a vast amount of data it has been trained on, which includes various types of text from the internet.

While working in the new release of Atlas (4.3), I have been lucky to work with ChatGPT and integrate it in our product, to improve Governance and others features. You can find more information here (can you imagine a cooler codename than “Prometheus” for something like this? 😃).

OpenAI is the startup behind chatGPT and other AI services, like DALL-E 2 (another famous one that allows you to generate images from some text). Microsoft is partnering with OpenAI, and has a “wrapper” in Azure, called Azure OpenAI Service. This Azure service is still in preview, and you must get approval from Microsoft to get access.

OpenAI and Azure OpenAI service, exposes an API to consume the service from your own solutions. Azure OpenAI service does not provide any Javascript library that we can use in our SharePoint solution, but OpenAI provides a NodeJS library that works in the browser, so we can use it in our SharePoint solution. The “but” here, is that OpenAI library is calling the OpenAI API, and not the Azure one. However, we can tweak a bit the library configuration, to consume the Azure OpenAI API, so we have all the benefits of the library, with all the benefits of Azure OpenAI service (in Microsoft words: “With Azure OpenAI, customers get the security capabilities of Microsoft Azure while running the same models as OpenAI. Azure OpenAI offers private networking, regional availability, and responsible AI content filtering.”).

Let´s see how we can configure the OpenAI Javascript library to use Azure OpenAI service in a SharePoint solution:

First, we must install the package:

$ npm install openai

The image below, shows the code snippet to use the library with OpenAI API:

Code snippet to use the library with OpenAI API

And here is the code to use the library, but consuming the Azure OpenAI Service:

Code to use the library, but consuming the Azure OpenAI Service

First, you need to compose the Base URL of your Azure OpenAI service. You can get it from the Azure portal:

Compose the Base URL of your Azure OpenAI service

The deployment name and Key are also available in the Azure portal.

In the code, you need to create an “OpenAIApi” object, setting the base URL for the Azure OpenAI endpoint. The first parameter in a “Configuration” object, where for the OpenAI API you configure the API Key. However, when working with the Azure OpenAI Service, the Key is passed as a parameter (query string). To be precise, the Azure OpenAI service, also allows Authentication based in Azure AD, using a Bearer token. If you want to go that route, then you can use the “Configuration” object, and set the Bearer token in there.

Once we have the “OpenAIApi” object, we have access to the different methods: createImage, createChatCompletion, createCompletion, etc. In the sample, we are calling the completion endpoint, and the relevant part, is that we need to configure some Headers and Parameters to the call. Internally, the library is using Axios to make the fetch call, so we can configure an AxiosRequestConfig, passing the api-key header, and the api-version query string parameter.

Now we can benefit of the typed response, and find the data in

response.data.choices[0].text

response.data.choices[0] text

And that´s all. Now we have an easier way to call the amazing AI services provided by OpenAI.

Please also have a look at my blog "Calling Azure OpenAI API in stream mode from an SPFx solution". I explain how to call the API and get the response as a stream of data, so you are getting data from the very first second, and you don´t have to wait for the entire response.

Atlas: The best Digital Workplace platform for Microsoft 365

Atlas is the digital workplace platform of choice that powers Microsoft 365. 

  • Platform of choice for organisations using Microsoft 365.
  • Recognised digital workplace platform leader.
  • Redesign and reimagine the creation and use of information to drive business value.
  • Optimises an organisation's investment in Microsoft 365.
  • Enterprise-grade security and smart governance tools, as well as integration capabilities.

Find out what Atlas can do. Book a demo now.

Book a demo

Author bio

Luis Mañez

Luis Mañez

Luis is Atlas Chief Architect. He is also a Microsoft 365 Development MVP and SharePoint and Cloud Solutions architect. "I help find the best technical designs to meet client needs and act as tech lead to build great solutions. I have fun with some R&D tasks, always trying to improve our tools and processes, and I often help the Microsoft community as a blogger and speaker, contributing to open-source projects."

View all articles by this author View all articles by this author

Get our latest posts in your inbox