我正在构建一个定制的聊天 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
关于如何做到这一点还有其他想法吗?
你可以试试这个解决方案: