Connect Azure Openapi with Nodejs (Via Python)

·

3 min read

Background

Before starting this blog I just wanted to give a small background on why am i writing this. Recently while I was attending a hackathon, I was supposed to connect my server with Azure's openapi (paid service). However, even after following the entire documentation thoroughly, all the teams including mine using Nodejs as the main framework failed to connect to their deployment. Therefore I am writing this blog for all those NodeJs developers as a possible alternative to the standard documentation.

Python Script

We will write the connection as a Python script and then try to run the file in the NodeJ environment. The Python script is very simple. Over here P is a message array that is passed as an argument from your server. The code includes the scope of chaining and hence we store all the conversations and pass the entire array object.

# filename : gpt.py
import sys
import openai
import json
p = []
for data in sys.argv[1:]:
    p.append(json.loads(data))
openai.api_type= "azure"
openai.api_base ='your-openapi-base-url'
openai.api_version = "your-openapi-preview-version"
openai.api_key = 'your-openapi-azure-key'
response = openai.ChatCompletion.create(
    engine = "your-azure-deployment-name",
    messages = p,
    temperature = 0.7,
    max_tokens = 800,
    top_p = 0.95,
    frequency_penalty = 0,
    presence_penalty = 0,
    stop= None
)
print(response["choices"][0]["message"]["content"])

NodeJs Scripting

The final part is to write the code that runs the above Python file. The prompt is passed as an argument. (In this case, you can pass your api_key and api_url as well to ensure protection). I am not covering the explanation of the format in which the prompts are passed because it is widely available everywhere. We are going to use

python-shell library which is a modification of child-process and provides an excellent execution environment for Python files. It catches and displays all the print line statements from the script and returns a stringified array.

Note: It is important to pass all prompts in the form of JSON strings so that Python can interpret the objects

One more thing to remember is to pass the relative path of the script file. In my case, they were in the same directory.

const path = require("path");
require("dotenv").config();
const { PythonShell } = require("python-shell");
const data = [];

async function Query(prompt) {
  data.push({ role: "user", content: prompt });
  try {
// you can pass other args in the options object like api-key as well.
    let options = {
      args: [...data.map((x) => JSON.stringify(x))],
    };
    let answer;
    await PythonShell.run("gpt.py", options).then((res) => {
      answer = res;
    });
    return answer;
  } catch (err) {
    return err;
  }
}


module.exports = Query;

I hope this blog helps you to connect to Azure Openapi rapidly without searching for external documentation.

THANK YOU