Azure OpenAI on nodeJS - Problems configuring Azure Openai

415 Views Asked by At

My code was working properly untill I changed my API key to use openai through Azure, this was my old code:

const {Configuration, OpenAIApi } = require('openai');


const apiKey = process.env.OPENAI_API_KEY;
const configuration = new Configuration({
  apiKey: apiKey,
});
const openai = new OpenAIApi(configuration);

async function chatGPT(prompt,question,model_gpt) {
  try 
    { 
          const response = await openai.createChatCompletion({
          model: model_gpt,
          messages: [
            {
              role: 'system',
              content: prompt
            },
            {
              role: 'user',
              content: question
            }
          ]
        });
        const completion = response.data.choices[0].message.content;
        return completion;
    } 

now using Azure I had to use some new parameters:

const {Configuration, OpenAIApi } = require('openai');

const configuration = new Configuration({
  apiKey: process.env.placeholder_azure_APIKEY,
  api_type: "azure",
  api_base: "placeholder_azure_path",
  api_version: "placeholder_azure_version",
});
const openai = new OpenAIApi(configuration);

async function chatGPT(prompt,question,model_gpt) {
    try 
    { 
          const response = await openai.ChatCompletion.create({
          engine: "placeHolder",
          messages: [
            {
              role: 'system',
              content: prompt
            },
            {
              role: 'user',
              content: question
            }
          ],
        });
        console.log(response);
        const completion = response.data.choices[0].message;
        console.log(completion);
        return completion;
    } 

I tried using the python function but it doesn't work, but when I try to use the Node one: openai.createChatCompletion() I get an 401 error - wrong API Key used.

I'm sure my API keys and path are OK, the code works perfectly on python (using the same API Key), but I get these errors using NodeJS.

0

There are 0 best solutions below