Azure OpenAI Error - Resource Not Found - Error Code - 404 - SOLVED

Shehani Mahalekam 0 Reputation points
2023-07-18T07:41:59.52+00:00

I have a valid subscription, valid Azure OpenAI API key and endpoint. I have added the correct deployment name and the model is gpt-35-turbo. This is a NodeJS project where I get pdfs as inputs and extract the texts and pass the text data to generateSummary function. All other functions are working fine and I have tested those. Only the generateSummary function is failing. Please provide me a solution.

The error I'm getting is,error

const generateSummary = async (data) => {
  try {
    const prompt = `Provide a summary of the text: ${data}`;
    const apiKey = process.env.AZURE_OPENAI_API_KEY;
    const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
    const url = `${endpoint}openai/deployments/SampleDeployment2/completions?api-version=2023-05-15`;
    const response = await axios.get(
      url,
      {
        model: "gpt-35-turbo",
        prompt: prompt,
        temperature: 0.3,
        max_tokens: 250,
        top_p: 1,
        frequency_penalty: 0,
        presence_penalty: 0
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${apiKey}`,
        },
      }
    );
    const summary = response.data.choices[0].text.trim();
    return summary;
  } catch (error) {
    if (error.response && error.response.data && error.response.data.error) {
      const errorMessage = error.response.data.error.message;
      console.error('OpenAI API Error:', errorMessage);
      console.log('Response:', error.response.data);
    } else {
      console.error('An unexpected error occurred:', error.message);
    }
  }
};
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,081 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shehani Mahalekam 0 Reputation points
    2023-07-19T03:32:52.8166667+00:00

    Thanks to you, I tried your suggestions but doesn't work. Then I came to this solution and this worked for me.

    const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
    
    const generateSummary = async (data) => {
      const messages = [
        { role: "user", content: `Provide a summary of the text: ${data}` },
      ];
    
      try {
        const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
        const deploymentId = "<YOUR_DEPLOYMENT_NAME>";
        const result = await client.getChatCompletions(deploymentId, messages);
    
        for (const choice of result.choices) {
          const summary = choice.message.content;
          return summary;
        }
      } catch (err) {
        console.error("The sample encountered an error:", err);
      }
    };
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.