gpt4 book ai didi

openai-api - NodeJS 中的 OpenAI API : How do I migrate from text-davinci-003 to gpt-3. 5-turbo?

转载 作者:行者123 更新时间:2023-12-02 22:48:00 33 4
gpt4 key购买 nike

如何从 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 页面): https://thedoggybrad.github.io/chat/chatsystem

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

最佳答案

您使用的是 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
    • 聊天完成 API:https://api.openai.com/v1/chat/completions
  2. prompt 参数(Completions API)已替换为 messages 参数(Chat Completions API)
  3. 响应访问
    • 完成 API:response.choices[0].text.trim()
    • 聊天完成 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);
}

关于openai-api - NodeJS 中的 OpenAI API : How do I migrate from text-davinci-003 to gpt-3. 5-turbo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76411359/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com