Tiktoken icon indicating copy to clipboard operation
Tiktoken copied to clipboard

Token counting for a whole message

Open Xan-Kun opened this issue 9 months ago • 3 comments

Not sure if this would fit into the scope of this project, but could be a real killer feature, since none of the others do it. If not, please feel free to delete :-)

What would you like to be added:

Be able to pass a whole OpenAI Message object into a function, and get the complete token count back.

Why is this needed:

So far, counting of a complete OpenAI message is quite tricky, as the message can include multiple parts now, functions, tools etc. As far as I know, there is no C# lib that supports this, doesn't seem like MS is adding any value here (in contrary :-) ) and it seems everyone wants to count tokens for messages, not just text.

Anything else we need to know?

I tried to implement it following this https://stackoverflow.com/a/77175648/4821032 There is also a typescript library that seems to come very close: https://github.com/hmarr/openai-chat-tokens

P.S.: I think it only is really needed for outgoing (prompt) messages, since the incoming chat objects have the actual token count in them.

Xan-Kun avatar May 19 '24 04:05 Xan-Kun