Openai API via azure错误:NotFoundError:404未找到资源

guykilcj  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(510)

感谢大学账户,我和我的团队能够通过微软Azure获得openai学分。问题是,现在,试图使用openai库的JavaScript,正确指定Azure给我们的密钥和端点,我们无法连接,并出现404错误:

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

字符串
我们正在测试的代码是这样的:

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();

3qpi33ja

3qpi33ja1#

正如@NickShieh提到的,你需要像下面这样使用Azure客户端库。
安装下面的包。

npm install @azure/openai

字符串
并使用以下代码。

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);
});


在这里,您需要提供在Azure OpenAI中部署的正确部署名称。
它看起来应该如下所示。
x1c 0d1x的数据
输出量:



有关详细信息,请参阅此文档

3pmvbmvn

3pmvbmvn2#

您可以按照以下资源通过OpenAI客户端调用Azure OpenAI模型。
资源链接- Azure OpenAI with node-openai
例如-

const openai = new OpenAI({
    apiKey,
    baseURL: `https://${resource}.openai.azure.com/openai/deployments/${model}`,
    defaultQuery: { "api-version": apiVersion },
    defaultHeaders: { "api-key": apiKey },
});

字符串
但是,建议使用@azure/openai作为Microsoft提供的特定于Azure的SDK。

相关问题