Search code examples
openai-apigpt-3chatgpt-apitext-davinci-003

OpenAI API: How do I migrate from text-davinci-003 to gpt-3.5-turbo in NodeJS?


How do I migrate from text-davinci-003 to gpt-3.5-turbo?

What I tried to do is the following:

Changing this...

model: "text-davinci-003"

...to this.

model: "gpt-3.5-turbo"

Also, changing this...

const API_URL = "https://api.openai.com/v1/completions";

...to this.

const API_URL = "https://api.openai.com/v1/chat/completions";

The Problem is that it does not work. The code I will be giving is the unmodified code, so that anyone can help me what to change.

Why I wanted this upgrade? I was irritated by text-davinci-003's completion. Like sending "Hello" gives me an entire letter not a greeting.

Live Sample (Via Github Pages): https://thedoggybrad.github.io/chat/chatsystem

Github Repository: https://github.com/thedoggybrad/chat/tree/main/chatsystem


Solution

  • You're using the gpt-3.5-turbo model.

    There are three main differences between the Chat Completions API (i.e., the GPT-3.5 API) and the Completions API (i.e., the GPT-3 API).

    1. API endpoint
      • Completions API: https://api.openai.com/v1/completions
      • Chat Completions API: https://api.openai.com/v1/chat/completions
    2. The prompt parameter (Completions API) is replaced by the messages parameter (Chat Completions API)
    3. Response access
      • Completions API: response.choices[0].text.trim()
      • Chat Completions API: response.choices[0].message.content.trim()

    Try this:

    const getChatResponse = async (incomingChatDiv) => {
        const API_URL = "https://api.openai.com/v1/chat/completions"; /* Changed */
        const pElement = document.createElement("p");
    
        // Define the properties and data for the API request
        const requestOptions = {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Authorization": `Bearer ${API_KEY}`
            },
            body: JSON.stringify({
                model: "gpt-3.5-turbo",
                messages: [{role: "user", content: `${userText}`}], /* Changed */
                max_tokens: 2048,
                temperature: 0.2,
                n: 1,
                stop: null
            })
        }
    
        // Send POST request to API, get response and set the reponse as paragraph element text
        try {
            const response = await (await fetch(API_URL, requestOptions)).json();
            pElement.textContent = response.choices[0].message.content.trim(); /* Changed */
        } catch (error) { // Add error class to the paragraph element and set error text
            pElement.classList.add("error");
            pElement.textContent = "Oops! Something went wrong while retrieving the response. Please try again.";
        }
    
        // Remove the typing animation, append the paragraph element and save the chats to local storage
        incomingChatDiv.querySelector(".typing-animation").remove();
        incomingChatDiv.querySelector(".chat-details").appendChild(pElement);
        localStorage.setItem("all-chats-thedoggybrad", chatContainer.innerHTML);
        chatContainer.scrollTo(0, chatContainer.scrollHeight);
    }