Class TokenizerUtils

java.lang.Object
uno.anahata.asi.internal.TokenizerUtils

public final class TokenizerUtils extends Object
A centralized utility class for token counting, powered by the jtokkit library. This provides a single, efficient, and reusable tokenizer instance for the entire application.
Author:
anahata
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final com.knuddels.jtokkit.api.EncodingRegistry
    Tokkit default registry.
    private static final com.knuddels.jtokkit.api.Encoding
    A robust, general-purpose tokenizer.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    Counts the number of tokens in the given text using a specific tokenizer strategy.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • REGISTRY

      private static final com.knuddels.jtokkit.api.EncodingRegistry REGISTRY
      Tokkit default registry.
    • TOKENIZER

      private static final com.knuddels.jtokkit.api.Encoding TOKENIZER
      A robust, general-purpose tokenizer. CL100K_BASE is the encoding used by gpt-4, gpt-3.5-turbo, and text-embedding-ada-002. It serves as a high-quality default for token counting.
  • Constructor Details

    • TokenizerUtils

      public TokenizerUtils()
  • Method Details

    • countTokens

      public static int countTokens(String text, TokenizerType type)
      Counts the number of tokens in the given text using a specific tokenizer strategy.
      Parameters:
      text - The text to count tokens for.
      type - The tokenizer strategy to use.
      Returns:
      The number of tokens.