Skip to content

Token counting for a whole message #31

@Xan-Kun

Description

@Xan-Kun

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions