**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
11小时前关闭
Improve this question
不久前我发现GPT-3模型非常适合语言学习。我让它编写了一个HTML,用于发送和接收OpenAI API的提示的简单方法。
验证码:
<!DOCTYPE html>
<html>
<head>
<title>Language Translator</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Language Translator</h1>
<p>Translate text between languages using ChatGPT.</p>
<label for="input-lang">Input Language:</label>
<select id="input-lang">
<option value="en">English</option>
<option value="ko">Korean (casual)</option>
<option value="ko-KR">Korean (formal)</option>
<option value="zh">Mandarin Chinese</option>
<option value="ja">Japanese</option>
</select>
<br><br>
<label for="output-lang">Output Language:</label>
<select id="output-lang">
<option value="en">English</option>
<option value="ko">Korean (casual)</option>
<option value="ko-KR">Korean (formal)</option>
<option value="zh">Mandarin Chinese</option>
<option value="ja">Japanese</option>
</select>
<br><br>
<textarea id="input-text" rows="10" cols="50"></textarea>
<br><br>
<button id="translate-btn">Translate</button>
<br><br>
<textarea id="output-text" rows="10" cols="50"></textarea>
<script>
$(document).ready(function () {
$("#translate-btn").click(function () {
var inputLang = $("#input-lang").val();
var outputLang = $("#output-lang").val();
var inputText = $("#input-text").val();
var prompt = "Translate this " + inputLang + " to " + outputLang;
// Add your API key here
var apiKey = "sk-xxxxxxxxxxxxxxxxxxxx";
var apiUrl = "https://api.openai.com/v1/engines/davinci-codex/completions";
// Send request to API
fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + apiKey
},
body: JSON.stringify({
prompt: prompt,
max_tokens: 60,
temperature: 0.5,
n: 1,
stop: "\n"
})
})
.then(response => response.json())
.then(data => {
var outputText = data.choices[0].text;
$("#output-text").val(outputText);
})
.catch(error => console.error(error));
// Add error message if output is undefined
if ($("#output-text").val() === undefined || $("#output-text").val() === "") {
alert("Error: output is undefined");
}
});
});
</script>
</body>
</html>
截图:
Visual Studio Code出现以下错误:
TypeError:Cannot read properties of undefined(阅读'0')at file:///C:/Users/16469/Desktop/website%20translator.html:63:50 {stack:'类型错误:无法读取未定义的属性...16469/Desktop/website%20translator.html:63:50 ',消息:'无法读取未定义的属性(阅读' 0 ')'}
网站翻译.html:66
arg0:
TypeError:无法读取在file:///C:/Users/16469/Desktop/website%20translator.html:63:50 {stack:'类型错误:无法读取未定义的属性...16469/Desktop/website%20translator.html:63:50 ',消息:'无法读取未定义的属性(阅读' 0 ')'}
@ c:\Users\16469\Desktop\website translator.html:66:41
1条答案
按热度按时间gajydyqb1#
问题
你可以从屏幕截图中看到,你得到了一个错误
404
。这是因为所有Engines endpoints都已弃用。
因此,您没有从OpenAI API中获得完成,这就是为什么您得到另一个错误(即
Cannot read properties of undefined
)。解决方案
从此更改URL ...
...这个
另外,像这样添加
model
参数: