NodeJS 404错误提示404 API 404“请求失败,状态代码为404”

66bbxpm5  于 2023-03-17  发布在  Node.js
关注(0)|答案(2)|浏览(683)

我正在尝试为我的Telegram机器人使用ChatGPT,我曾经使用“text-davinci-003”模型,它工作得很好(即使现在也工作得很好),但我对它的响应不满意。
现在,我尝试将模型更改为“gpt-3.5-turbo”,它会抛出一个404响应代码,并显示文本“Error:请求失败,状态代码为404”,没有其他内容。以下是我的代码:

import { Configuration, OpenAIApi } from "openai";
import { env } from "../utils/env.js";

const model = "gpt-3.5-turbo"; // works fine when it's "text-davinci-003"
const configuration = new Configuration({
  apiKey: env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

export async function getChatGptResponse(request) {
  try {
    const response = await openai.createCompletion({
      model,
      prompt: request, // request comes as a string
      max_tokens: 2000,
      temperature: 1,
      stream: false
    });

    console.log("Full response: ", response, `Choices: `, ...response.data.choices)
    return response.data.choices[0].text;
  } catch (err) {
    console.log(`ChatGPT error: ` + err);
    return err;
  }
}
fdbelqdn

fdbelqdn1#

尝试使用createChatCompletion而不是createCompletion

const response = async (message) => {
  const response = await openai.createChatCompletion({
    model: "gpt-3.5-turbo",
    messages: [{role: "user", content: "Hello world"}],
  });

  return response.data.choices[0].message.content;
};
slmsl1lt

slmsl1lt2#

有3件事你需要改变:

  • createCompletion(GPT-3 API)更改为createChatCompletion(聊天GPT应用程序接口)。
  • prompt参数(GPT-3 API)更改为messages参数(ChatGPT API)。
  • return response.data.choices[0].text;(GPT-3 API)更改为return response.data.choices[0].message.content;(聊天GPT应用程序接口)。

这是正确的代码:

import { Configuration, OpenAIApi } from 'openai';
import { env } from '../utils/env.js';

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

const openai = new OpenAIApi(configuration);

const model = 'gpt-3.5-turbo';

async function getChatGptResponse(request) {
  try {
    const response = await openai.createChatCompletion({
      model: model,
      messages: [{ role: 'user', content: 'Hello!' }],
      max_tokens: 2000,
      temperature: 1,
      stream: false,
    });

    console.log(
      'Full response: ', response,
      'Choices: ', ...response.data.choices
    );
    return response.data.choices[0].message.content;
  } catch (err) {
    console.log('ChatGPT error: ' + err);
    return err;
  }
}

getChatGptResponse();

相关问题