gpt4 book ai didi

openai-api - 将我的代码从 text-davinci-003 更新到 gpt-3.5-turbo 时出现问题

转载 作者:行者123 更新时间:2023-12-02 22:47:36 26 4
gpt4 key购买 nike

我正在学习编码,并试图弄清楚如何在我的网站上复制我自己的小聊天 GPT。我让它在达芬奇三号上工作,但是当我尝试升级到 3.5 时它就坏了。这是工作链接和代码。有什么建议吗?

https://wellinformedluminouspublishers.benmiller14.repl.co/

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GPT-3 API Example</title>
</head>
<body>
<h1>GPT-3 API Example</h1>
<div>
<label for="user-message">Enter a message:</label>
<input type="text" id="user-message">
<button onclick="generateResponse()">Generate Response</button>
</div>
<div id="response-container"></div>

<script>
function generateResponse() {
const url = "https://api.openai.com/v1/completions";
const apiKey = "API-KEY-HERE";
const model = "text-davinci-003";
const userMessage = document.getElementById("user-message").value;
const payload = {
prompt: userMessage,
temperature: 0.7,
max_tokens: 50,
model: model
};
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + apiKey
},
body: JSON.stringify(payload)
})
.then(response => response.json())
.then(data => {
const responseContainer = document.getElementById("response-container");
responseContainer.innerText = data.choices[0].text;
})
.catch(error => {
console.error("Error generating response:", error);
});
}
</script>
</body>
</html>

我尝试将第 20 行的“text-davinci-003”替换为“gpt-3.5-turbo”,但当我这样做时它会中断。我想是因为它可能是不同的 API 端点?但我对 API 的经验还不够,还不知道如何修复它。

以下是 API 更新的页面:

https://help.openai.com/en/articles/6283125-what-happened-to-engines

我想我需要将“提示”更改为“消息”,也许还需要更改端点 url。但不确定...

最佳答案

这段代码有效!

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OpenAI API Test</title>
</head>
<body>
<form id="input-form">
<label for="input-text">Topic: </label>
<input type="text" id="input-text" name="input-text"><br><br>
<input type="hidden" id="hidden-field-1" name="hidden-field-1" value="List 10 clickbait titles for a blog article about this topic with the following tone: ">
<input type="hidden" id="hidden-field-2" name="hidden-field-2" value="Maximum title length should be 60 characters. "><br><br>
<label for="dropdown-menu">Choose an option:</label>
<select id="dropdown-menu" name="dropdown-menu">
<option value="funny">Funny</option>
<option value="scary">Scary</option>
<option value="option-3">Option 3</option>
</select><br><br>

<input type="submit" value="Submit">
</form>
<div id="output-div"></div>

<script>
const form = document.getElementById('input-form');
const input = document.getElementById('input-text');
const output = document.getElementById('output-div');
const hiddenField1 = document.getElementById('hidden-field-1');
const hiddenField2 = document.getElementById('hidden-field-2');
const dropdownMenu = document.getElementById('dropdown-menu');

form.addEventListener('submit', async (event) => {
event.preventDefault();

// Replace with your own OpenAI API key
const apiKey = 'MY-API_KEY';

const message = `${input.value}. ${hiddenField1.value} ${dropdownMenu.value}. ${hiddenField2.value}.`;

const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: message }],
temperature: 0.7
})
});

const result = await response.json();

output.innerHTML = `<p>${result.choices[0].message.content}</p>`;
});
</script>
</body>
</html>

关于openai-api - 将我的代码从 text-davinci-003 更新到 gpt-3.5-turbo 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75774552/

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