javascript - OpenAI GPT-3 API 错误 : "This model' s maximum context length is 2049 tokens"

标签 javascript php json openai-api gpt-3

我有 2 个问题与 OpenAI 完成的响应结果有关。

当我给出一个500字的内容并提示“修复语法错误”时,如下结果没有返回全文(是tokens问题吗?)

enter image description here

第二个问题是当文本有时有一些双引号或单引号时它会混淆 JSON 格式所以我从内容中删除任何类型的引号(不确定这是否是最好的解决方案但我可能更喜欢在 JS 上完成不是 PHP)

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n  \"model\": \"text-davinci-001\",\n  \"prompt\": \"" . $open_ai_prompt  . ":nn" . $content_text  . "\",\n  \"temperature\": 0,\n  \"top_p\": 1.0,\n  \"frequency_penalty\": 0.0,\n  \"presence_penalty\": 0.0\n}");

"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON.

最佳答案

关于 token 限制

首先,我认为您不了解 token 的工作原理:500 个单词超过 500 个 token 。使用 Tokenizer计算 token 的数量。

如官方所述OpenAI article :

Depending on the model used, requests can use up to 4097 tokens shared between prompt and completion. If your prompt is 4000 tokens, your completion can be 97 tokens at most.

The limit is currently a technical limitation, but there are often creative ways to solve problems within the limit, e.g. condensing your prompt, breaking the text into smaller pieces, etc.

text-davinci-001 切换为 GPT-3 模型,因为 token 限制更高。

GPT-3 models :

Table


关于JSON中的双引号

您可以在双引号前使用 \ 转义 JSON 中的双引号,如下所示:

"This is how you can escape \"double quotes\" in JSON."

但是……这更像是一个快速修复。有关正确的解决方案,请参阅上面@ADyson 的评论:

Don't build your JSON by hand like that. Make a PHP object / array with the correct structure, and then use json_encode() to turn it into valid JSON, it will automatically handle any escaping etc which is needed, and you can also use the options to tweak certain things about the output - check the PHP documentation.


编辑 1

您需要设置 max_tokens参数更高。否则,输出将比您的输入短。您不会得到整个固定文本,而只会得到其中的一部分。


编辑 2

现在您将 max_tokens 参数设置得太高了!如果您设置 max_tokens = 5000,即使对于功能最强大的 GPT-3 模型(即 text-davinci-003)来说,这也太多了。提示和完成一起可以是4097 标记。

如果你看一下你得到的错误,你可以弄清楚这一点:

"error": {"message": "This model's maximum context length is 4097 tokens, however you requested 6450 tokens (1450 in your prompt; 5000 for the completion). Please reduce your prompt; or completion length."}

关于javascript - OpenAI GPT-3 API 错误 : "This model' s maximum context length is 2049 tokens",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75373129/

相关文章:

php - 为什么我会收到这条 Strict Standards 消息?

php - 保护 MySQL 密码和管理多个帐户的最佳做法是什么?

javascript - 将数组格式化为 JSON 以输入到 Google Charts API?

javascript - 点击Selenium on Sauce 发送后,Firefox 未完成页面加载

javascript - 在模态中创建数据表

javascript - MomentJS 重置/重用 DOM 元素来显示新时间

php - Twig - 访问用户对象 ID

arrays - 如何在 groovy 中使用 JsonBuilder 创建数组

json - 使用 Dapper ORM (.NET Core) 将 JSON 插入 PostgreSQL 数据库

javascript - 无论年份如何,如何找出日和月之间的天数差异