openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

这个库在chat模型的token计算上有诸多错误

Open CodeInDreams opened this issue 1 year ago • 4 comments

与openai返回的token对比发现,几乎各chat模型都有计算方式错误或结果偏差,于是我自己从零建模和编写了token计算工具

你的部分token计算代码有严重错误,这里列举部分:

  • vision base64读取
  • vision图片缩放
  • function call、functions
  • tool call、tools
  • 0301/0314与0613版本计算差异
  • 多个参数(model、functions)组合使用对token计算的影响
  • 应使用encodeOrdinary来跳过special tokens

由于精力有限我无法在开源代码上提交修改,本issue只是告知绝大部分token计算都有误,请你自己有精力时研究下

CodeInDreams avatar Dec 11 '23 02:12 CodeInDreams

感谢,的确不能保证和官方返回一模一样。

可以看到官方的cookbookHow_to_count_tokens_with_tiktoken中也是提到

image

也是一个预估值,这个计算方法主要是用来预估发送的一些limit,所以有一点误差不会影响逻辑。

forestwanglin avatar Dec 11 '23 05:12 forestwanglin

token计算不支持 gpt-3.5-turbo-0301

forestwanglin avatar Dec 11 '23 09:12 forestwanglin

我按照官方Demo的计算规则,更新了计算方法。 已经测试的:

  • gpt-3.5-turbo(gpt-3.5-turbo-0613/gpt-3.5-turbo-16k-0613)
  • gpt-3.5-turbo-1106
  • gpt-4-1106-preview
  • gpt-4(gpt-4-0613/gpt-4-32k-0613)

更新了funciton的计算规则,具体可参考FunctionFormat

forestwanglin avatar Dec 12 '23 11:12 forestwanglin

More issue about token calculation can be found issue #4.

forestwanglin avatar Jan 30 '24 04:01 forestwanglin