You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

调用OpenAI API生成语句解释时返回内容混乱无意义的问题排查求助

调用OpenAI API生成语句解释时返回内容混乱无意义的问题排查求助

最近我在开发一个小功能:点击页面上的句子,调用OpenAI API生成对应的解释,但遇到了一个棘手的问题——API返回的内容完全逻辑混乱,和我在GPT4里用相同prompt得到的清晰解释差得十万八千里。跟大家梳理下我的折腾过程和当前的状态:

之前的尝试

  • 最初我做了两个页面,index.html点击句子后通过URL参数把内容传给explanation.html,结果页面一直卡在“Loading...”状态,根本拿不到有效响应
  • 后来改成用localStorage传递句子内容,终于能收到API的返回了,但输出的解释依旧语无伦次,完全不是想要的结果
  • 为了简化调试,我把逻辑合并到了index.html里,直接用alert弹窗显示解释,但问题还是没解决

当前的代码

现在的核心代码是这样的,点击句子后直接调用API:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example</title>
<script type="text/javascript">
function showExplanation(element) {
const sentence = element.textContent.trim();
fetch('https://api.openai.com/v1/engines/davinci/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
prompt: "What does this sentence mean? " + sentence,
max_tokens: 128,
temperature: 0.5
})
})
.then(response => response.json())
.then(output => {
const explanation = output.choices[0].text.trim();
alert(explanation);
})
.catch(error => console.log(error));
}
</script>
</head>
<body>
<a href="#" onclick="showExplanation(this)">When push comes to shove in a situation defined by opposition deemed as evil according to the way the opponent dismisses any honor you show you must not expect that what corners you will let up for any premature retreat you make becomes bound to come back as a weakened ability to fight off attacks made on you.</a>
</body>
</html>

具体问题

我测试的句子是上面那段长句,用同样的prompt(What does this sentence mean? + 目标句子)在GPT4里能得到清晰的解释,但API返回的内容却完全不通顺,逻辑混乱。

我自己琢磨了下可能的原因:

  • 是不是我用的davinci引擎太老旧了?毕竟GPT4是新模型,旧引擎的理解能力跟不上?
  • 或者prompt的表述不够明确?是不是需要调整提问方式?
  • 参数设置有没有问题?比如max_tokens设得太小,导致解释没说完就被截断?

有没有大佬能帮我看看问题出在哪?或者给点优化的建议,让API返回的解释能和GPT4的结果接近一些?

备注:内容来源于stack exchange,提问作者Caesarion

火山引擎 最新活动