OpenAI API:如何在NodeJS中从text-davinci-003迁移到gpt-3.5-turbo?

umuewwlo  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(189)

如何从text-davinci-003迁移到gpt-3.5-turbo
我试图做的是以下几点:
改变这个。。

model: "text-davinci-003"

...这个

model: "gpt-3.5-turbo"

所以,改变这个…

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

...这个

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

问题是它不起作用。我将给出的代码是未修改的代码,以便任何人都可以帮助我修改什么。
为什么我想要这个升级?text-davinci-003的完成让我很恼火。就像发送“你好”给我一整封信,而不是一个问候。
Live Sample(通过Github Pages):https://thedoggybrad.github.io/chat/chatsystem
Github Repository:https://github.com/thedoggybrad/chat/tree/main/chatsystem

mwg9r5ms

mwg9r5ms1#

您希望使用gpt-3.5-turbo模型。
ChatGPT API(即GPT-3.5 API)和GPT-3 API之间有三个主要区别:

  • API端点
  • GPT-3 API:https://api.openai.com/v1/completions
  • ChatGPT API:https://api.openai.com/v1/chat/completions
  • prompt参数(GPT-3 API)被messages参数(ChatGPT API)替换
  • 响应访问
  • GPT-3 API:response.choices[0].text.trim()
  • ChatGPT API:response.choices[0].message.content.trim()

试试这个:

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

相关问题