Search code examples
next.jsopenai-apigpt-3

OpenAI GPT-3 API errors: 'text' does not exist TS(2339) & 'prompt' does not exist on type 'CreateChatCompletion' TS(2345)


import openai from "./zggpt";

const query = async (prompt:string,  chatId:string, model:string) => {
    const res= await openai
    .createChatCompletion({
        model,
        prompt,
        temperature: 0.9,
        
        top_p:1,
       
        max_tokens:1000,
        frequency_penalty:0,
        presence_penalty:0,
    })
    .then((res) => res.data.choices[0].text)
    .catch((err)=>
    `ZG was unable to find an answer for that!
     (Error: ${err.message})`
     );
     return res;
};

export default query;

Property 'text' does not exist on type 'CreateChatCompletionResponseChoicesInner'.ts(2339)

Argument of type '{ model: string; prompt: string; temperature: number; top_p: number; max_tokens: number; frequency_penalty: number; presence_penalty: number; }' is not assignable to parameter of type 'CreateChatCompletionRequest'. Object literal may only specify known properties, and 'prompt' does not exist in type 'CreateChatCompletionRequest'.ts(2345)

even though I do everything as in the video, I get these errors.

i'm a beginner in coding, so I'm trying to make applications based on videos to learn.

Thanks

the application responds without returning an error. enter image description here

https://www.youtube.com/watch?v=V6Hq_EX2LLM&t=6293s


Solution

  • Problem

    You watched a tutorial which used the Completions API endpoint where you need to provide the prompt and parameters to get a completion). In this case, this is the function which generates a completion:

    openai.createCompletion()
    

    Whereas, you used the code from the tutorial, but used the Chat Completions API endpoint (you need to provide the chat message to get a completion). In this case, this is the function which generates a completion:

    openai.createChatCompletion()
    

    Note: OpenAI NodeJS SDK v4 was released on August 16, 2023, and is a complete rewrite of the SDK. Among other things, there are changes in method names. See the v3 to v4 migration guide.

    API endpoint NodeJS function (SDK v3) NodeJS function (SDK v4)
    Chat Completions API openai.createChatCompletion openai.chat.completions.create
    Completions API openai.createCompletion openai.completions.create

    Solution

    • If you have the OpenAI NodeJS SDK v3:

    Change this...

    openai.createChatCompletion()
    

    ...to this.

    openai.createCompletion()
    

    • If you have the OpenAI NodeJS SDK v4:

    Change this...

    openai.chat.completions.create
    

    ...to this.

    openai.completions.create
    

    Both errors will disappear.


    My advice

    However, you want to achieve a chat-like bot using a GPT-3 model. At the time the tutorial was recorded, this was the only way to do it. Since 1 March 2023, the gpt-3.5-turbo model is available. I strongly suggest you to use it. See the official OpenAI documentation.