Not sure how to configure my nodeJS API through Azure

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_version_azure",
});
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;
    } 

The new code isnā€™t working and I get an ā€œTypeError: Cannot read properties of undefined (reading ā€˜createā€™)ā€ error on the "await openai.ChatCompletion.create({ā€¦})
Did I made an syntax error? Hope you guys can help me.

Iā€™m sure my API keys and path are OK, the code works perfectly on python, but I get these errors using NodeJS.
I tried using createChatCompletion() but I get a 401 error (Authentication error) that makes no sense, since I already used the same Keys/Path on a python script and it worked. Does azure have security locks for diferent programming languages?

Are you still using v3 of the node library or did you upgrade to v4?

1 Like

Iā€™m using openai@3.3.0
not sure if thereā€™s a node lib, but my node version is v18.12.1

do a console log of both the configuration object and the openai before you even try the call to see which of them is wacky. You know for sure you arenā€™t getting a good openai object from that constructor. That ENV var name looks pretty suspicious to me.

1 Like

to change your api means that the place you stored it, and its content must change.

In the linux context - .bashrc is a file that contains a mess of precursors, including aliases.

However to [Export][a][Something] is also written here without understanding.

The simplest way of going about it: Is to go throught the process

  1. Is your key in your code?
    No?
    keep goingā€¦
  2. has your key stopped working when you made a new code
    (well then its your code.

Nano / gedit .bashrc

replace the key (probly at the bottom) close terminal, re open terminal.

1 Like

Both apear as:
[class Configuration]
[class OpenAIApi extends BaseAPI]

The env var is set right, iā€™m using the same process to store API Keys for AWS and SQL databases, and both work. Iā€™m really confused, because using createChatCompletion(), iā€™m getting the 401 (wrong API Key used) error, but itā€™s the same one that is doing ok on my python script.

you should print the configuration like this, to check it:

const jsonString = JSON.stringify(configuration, null, 2);
console.log(jsonString);

1 Like

Hi @gabrielsavonitti,

Welcome to the OpenAI community.

If you want to use the Azure OpenAI service, then hereā€™s a guide on using the Azure OpenAI Node.js client.

Hereā€™s how you can use the latest OpenAI Node.js library with Azure OpenAI service.

The OpenAI Node.js GitHub page says:

An example of using this library with Azure OpenAI can be found here.

Please note there are subtle differences in API shape & behavior between the Azure OpenAI API and the OpenAI API, so using this library with Azure OpenAI may result in incorrect types, which can lead to bugs.

See @azure/openai for an Azure-specific SDK provided by Microsoft.

EDIT: To run your current code, just use the basePath param and set it to your endpoint deployment url in the Configuration initialization. Then pass the API key within the createChatCompletion method as axios request configuration:

{
	headers: {
		'api-key': "AzureOpenAIAPIKey",
	},
	params: {
		"api-version": "2022-12-01"
	}
}

ā€“Source

2 Likes

oh that helped!
now I can check it better

{
ā€œapiKeyā€: ā€œ{my_key}ā€,
ā€œbasePathā€: ā€œhttps://{my_user}.openai.azure.com/deployments/{my_model}ā€,
ā€œbaseOptionsā€:
{
ā€œheadersā€: {
ā€œUser-Agentā€: ā€œOpenAI/NodeJS/3.3.0ā€,
ā€œAuthorizationā€: ā€œBearer {my_key}ā€
}
}
}

iā€™m not sure if I need it, but both
my ā€œapi_typeā€ ā†’ ā€œazureā€
and ā€œapi_versionā€ ā†’ ā€œ2023-07-01-previewā€
are not showing, am I using the syntax wrong?

Hello sps, thanks for the greetings and help, I think iā€™m having some progress!
thatā€™s how my code is now:

try{
    const client = new OpenAIClient(my_azure_path, AzureApiKey);
    const {choices} = await client.getCompletions(my_model, prompt_with_question, {maxTokens: 64, });
    const resposta = choices[0].text;
    console.log(answer);
    return answer;
  }
  catch(error){
    console.log(error);
  }

now instead of getting an invalid ID (authentication error) iā€™m getting an:
ā€˜Invalid URL (POST /v1/openai/deployments/{my_model}/completions)ā€™,
type: ā€˜invalid_request_errorā€™

Iā€™m still now sure why, i tried passing the path as:
1)https://{my_user}.openai.azure.com/deployments/{my_model}
and
2)https://{my_user}.openai.azure.com/

both give the same error mentioned above

Thatā€™s because youā€™re probably querying a Chat Completion model on the getCompletions().

Follow this tutorial for chat completions

Also, the endpoint URL will have to in this format:

"https://<resource name>.openai.azure.com/"
2 Likes

Thanks Sukhman!!Tthat was exactly it, I wanted to do completions, but gpt 3.5 16k works only with chat completions, so for now thatā€™s what Iā€™ll use.
Sorry for the newbie troubles hahaha and thanks for the great help everybody!!!

4 Likes