Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Estimate token usage + dry run option #318

Open
1 task done
taylorjdawson opened this issue Sep 17, 2023 · 4 comments
Open
1 task done

Estimate token usage + dry run option #318

taylorjdawson opened this issue Sep 17, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@taylorjdawson
Copy link

Confirm this is a feature request for the Node library and not the underlying OpenAI API.

  • This is a feature request for the Node library

Describe the feature or improvement you're requesting

There a couple nodejs tokenizers laying around npm land:

However, they have to be kept maintained and up to date with the official tiktoken. It feels very natural that such a tool be apart of this repository and updated regularly by the openai team. I feel that pre-request token estimation is vital to ensuring hobbyist developers don't unknowingly succumb to financial demise.

Why apart of this library?
Take function calling for example:

    const response = await openai.chat.completions.create({
        model: "gpt-3.5-turbo",
        messages: messages,
        functions: functions,
        function_call: "auto",  // auto is default, but we'll be explicit
    });

I could intercept this function call above to view what data is sent to chatGPT so I can figure out how many tokens this call takes with the message + the functions, pipe them into gpt-tokenizer and see how many tokens are used. But if it were to be built into the API itself then you can have a dry run option on your nodejs api functions that estimate cost and then the developer could determine in the logic if they want to proceed with the actual run.

What else could be very helpful would be to include some parameter whether in the configuration or when calling api functions such that if the tokens and/or usd cost exceeds a certain limit the call auto fails and doesn't actually get executed.

Additional context

No response

@rattrayalex rattrayalex added the enhancement New feature or request label Sep 18, 2023
@rattrayalex
Copy link
Collaborator

Thanks for the thoughtful feature request, the function-calling example is a compelling one. We'll think about this.

Can you share an example of how you'd want to code the logic to proceed or not proceed based on the token count?

@eugene-kim
Copy link

I use function calling extensively. I'd love a simple function that returned a token count given completion input, e.g. ChatCompletionCreateParamsNonStreaming.

I want to ensure that I respect rate limits before I ever hit them. Rate limiting libraries such as Bottleneck can be configured to respect both the request and token limits, but I need the token counts before I make the request

@rattrayalex
Copy link
Collaborator

I don't expect to get to this anytime soon, but I would be interested in PR's implementing this that add the functionality to the lib/ folder!

@NatoBoram
Copy link

If anyone is stuck at this problem...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants