Skip to content
Discussion options

You must be logged in to vote

I finally found the answer. Since there's no built-in function to get the token count, we need to calculate it manually. Fortunately, I came across a package called yethee/tiktoken-php that helps with the token calculation.

I’m not sure about its performance yet, but for now, it solves my problem.

Here’s a helper function I created to get the token count:

public static function countTokens(string $model, string $text): int
    {
        // Inisialisasi provider dan encoder sesuai model
        $provider = new EncoderProvider();
        $encoder = $provider->getForModel($model);

        // Hitung token prompt
        $totalToken = count($encoder->encode($text));
        return $totalToken;…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@b-barry
Comment options

@hellodit
Comment options

Answer selected by hellodit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants