使用 openai 聊天 GTP 时,如何截断我的对话以适应令牌限制?

问题描述 投票:0回答:1

我正在构建一个定制的聊天 GPT,用户可以在其中使用聊天完成功能与 openai 聊天 GPT 模型聊天。

在撰写本文时,我正在使用

gpt-3.5-turbo
,它的令牌限制为
4096
令牌。

我的数据架构遵循以下结构:

 {"role": "system", "content": "You are a helpful assistant."},
 {"role": "user", "content": "Who won the world series in 2020?"},
 {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
 {"role": "user", "content": "Where was it played?"}

在 javascript 中,什么是最好的方法来修剪对话以确保当我发送聊天历史记录时它不会超过令牌限制并为响应留出空间?

此刻,我正在做一个修剪,但一些回应可能是 10 个代币,而另一些则可能是 200 个……或更多,这使得它不可靠。

例子:

const chats = [...]

const sentChats = chats.splice(-5)

// Send sentChats to openai for processing

关于如何做到这一点还有其他想法吗?

javascript openai-api chatgpt-api
1个回答
-1
投票

你可以试试这个解决方案:

  1. 当您接近限制时 - 请 chatGPT 压缩聊天。
  2. 复制压缩版
  3. 开始新聊天并要求解压它
© www.soinside.com 2019 - 2024. All rights reserved.