Advertisement

OpenAI Chat Completions API error: “openai.createChatCompletion is not a function“

阅读量:

题意 :OpenAI的Chat Completions API存在问题:调用 openai.createChatCompletion 函数时出现错误。

问题背景:

I have this in my MERN stack code file, and it works well.

我在我的 MERN 栈代码文件中有这个代码,它运行良好。

复制代码
 exports.chatbot = async (req, res) => {

    
   console.log("OpenAI Chatbot Post");
    
  
    
   const { textInput } = req.body;
    
  
    
   try {
    
  
    
     const response = await openai.createCompletion({
    
       model: "text-davinci-003",
    
       prompt: `
    
         What is your name?
    
         My name is Chatbot.
    
         How old are you?
    
         I am 900 years old.
    
         ${textInput}`,
    
       max_tokens: 100,
    
       temperature: 0,
    
     });
    
     if (response.data) {
    
       if (response.data.choices[0].text) {
    
     return res.status(200).json(response.data.choices[0].text);
    
       }
    
     }
    
   } catch (err) {
    
     return res.status(404).json({ message: err.message });
    
   }
    
 };

While altering the API request, I will utilize the new Chat Completions API. Such an approach fails.

当我更改 API 请求并使用新的 Chat Completions API 时,这个不工作了。

复制代码
 exports.chatbot = async (req, res) => {

    
   console.log("OpenAI Chatbot Post");
    
  
    
   const { textInput } = req.body;
    
  
    
   try {
    
     const completion = await openai.createChatCompletion({
    
       model: "gpt-3.5-turbo",
    
       messages: [{ role: "user", content: textInput }],
    
     });
    
     console.log(completion.data.choices[0].message);
    
  
    
     if (completion.data) {
    
       if (completion.data.choices[0].message) {
    
     return res.status(200).json(completion.data.choices[0].message);
    
       }
    
     }
    
  
    
   } catch (err) {
    
     return res.status(404).json({ message: err.message });
    
   }
    
 };

Error I'm getting: 我得到的错误:

POST http://localhost:3000/api/openai/chatbot 404 (Not Found)

问题解决:

You must re-install the openai npm package. It has just been updated since it includes a new feature called createChatCompletion within the past two days.

建议你重新配置安装 openai 这个 npm 包。自发布以来,createChatCompletion 功能一直未有更新。

When I reinstalled the package and ran your code it worked successfully.

当我重新安装了这个包并运行你的代码后,它成功运行了。

全部评论 (0)

还没有任何评论哟~