英文:
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)之间有三个主要区别:
- API 端点
- Completions API: 
https://api.openai.com/v1/completions - Chat Completions API: 
https://api.openai.com/v1/chat/completions 
 - Completions API: 
 prompt参数(Completions API)被messages参数(Chat Completions API)替代- 响应访问
- Completions API: 
response.choices[0].text.trim() - Chat Completions API: 
response.choices[0].message.content.trim() 
 - Completions API: 
 
尝试这个:
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).
- API endpoint
- Completions API: 
https://api.openai.com/v1/completions - Chat Completions API: 
https://api.openai.com/v1/chat/completions 
 - Completions API: 
 - The 
promptparameter (Completions API) is replaced by themessagesparameter (Chat Completions API) - Response access
- Completions API: 
response.choices[0].text.trim() - Chat Completions API: 
response.choices[0].message.content.trim() 
 - Completions API: 
 
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论