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

huangapple go评论63阅读模式
英文:

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

问题

如何从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的完成方式感到烦恼。比如发送“Hello”会给我一封整封信而不是一个问候。

实时示例(通过Github Pages):
https://thedoggybrad.github.io/chat/chatsystem

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

英文:

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

答案1

得分: 0

你正在使用 gpt-3.5-turbo 模型。

Chat Completions API(即 GPT-3.5 API)和 Completions API(即 GPT-3 API)之间有三个主要区别:

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

尝试这个:

const getChatResponse = async (incomingChatDiv) => {
    const API_URL = "https://api.openai.com/v1/chat/completions"; /* 已更改 */
    const pElement = document.createElement("p");

    // 定义 API 请求的属性和数据
    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}`}], /* 已更改 */
            max_tokens: 2048,
            temperature: 0.2,
            n: 1,
            stop: null
        })
    }

    // 发送 POST 请求到 API,获取响应并将响应设置为段落元素文本
    try {
        const response = await (await fetch(API_URL, requestOptions)).json();
        pElement.textContent = response.choices[0].message.content.trim(); /* 已更改 */
    } catch (error) { // 向段落元素添加错误类,并设置错误文本
        pElement.classList.add("error");
        pElement.textContent = "糟糕!在检索响应时出现了问题。请重试。";
    }

    // 移除打字动画,附加段落元素,并将聊天保存到本地存储
    incomingChatDiv.querySelector(".typing-animation").remove();
    incomingChatDiv.querySelector(".chat-details").appendChild(pElement);
    localStorage.setItem("all-chats-thedoggybrad", chatContainer.innerHTML);
    chatContainer.scrollTo(0, chatContainer.scrollHeight);
}
英文:

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

huangapple
  • 本文由 发表于 2023年6月6日 11:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定