Search code examples
azureopenai-apichatgpt-apiazure-openai

Openai api via azure error: NotFoundError: 404 Resource not found


thanks to the university account my team and I were able to get openai credits through microsoft azure. The problem is that now, trying to use the openai library for javascript, rightly specifying the key and endpoint that azure gave us, we can't connect and a 404 error comes up:

NotFoundError: 404 Resource not found
    at APIError.generate (file:///home/teo/social_stories_creator/node_modules/openai/error.mjs:48:20)
    at OpenAI.makeStatusError (file:///home/teo/social_stories_creator/node_modules/openai/core.mjs:244:25)
    at OpenAI.makeRequest (file:///home/teo/social_stories_creator/node_modules/openai/core.mjs:283:30)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async main (file:///home/teo/social_stories_creator/test.mjs:9:22) {
  status: 404,
  headers: {
    'apim-request-id': 'e04bf750-6d8c-478e-b6d5-967bbbc44b62',
    'content-length': '56',
    'content-type': 'application/json',
    date: 'Sat, 18 Nov 2023 10:14:24 GMT',
    'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
    'x-content-type-options': 'nosniff'
  },
  error: { code: '404', message: 'Resource not found' },
  code: '404',
  param: undefined,
  type: undefined
}

Node.js v21.1.0

The code we are testing, is this:

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'OUR_KEY',
  baseURL: 'OUR_ENDPOINT',
});

async function main() {
  const completion = await openai.chat.completions.create({
    messages: [{ role: 'system', content: 'You are a helpful assistant.' }],
    model: 'gpt-3.5-turbo',
  });

  console.log(completion.choices[0]);
}

main();

Solution

  • You need to use the Azure client library like below.

    Install the package below.

    npm install @azure/openai
    

    And use the following code.

    
    const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
    
    async function main(){
      // Replace with your Azure OpenAI key
      const key = "YOUR_AZURE_OPENAI_KEY";
      const endpoint = "https://myaccount.openai.azure.com/";
      const client = new OpenAIClient(endpoint, new AzureKeyCredential(key));
    
      const examplePrompts = [
        "How are you today?",
        "What is Azure OpenAI?",
        "Why do children love dinosaurs?",
        "Generate a proof of Euler's identity",
        "Describe in single words only the good things that come into your mind about your mother.",
      ];
    
      const deploymentName  =  "gpt35turbo"; //Your deployment name
    
      let promptIndex = 0;
      const { choices } = await client.getCompletions(deploymentName, examplePrompts);
      for (const choice of choices) {
        const completion = choice.text;
        console.log(`Input: ${examplePrompts[promptIndex++]}`);
        console.log(`Chatbot: ${completion}`);
      }
    }
    
    main().catch((err) => {
      console.error("The sample encountered an error:", err);
    });
    

    Here, you need to provide the correct deployment name you deployed in Azure OpenAI.

    It should look like the following.

    enter image description here

    Output:

    enter image description here

    For more information refer this documentation