ITokenizerService

Wisej.AI.Services.ITokenizerService

Namespace: Wisej.AI.Services

Assembly: Wisej.AI (3.5.0.0)

Represents a service for tokenizing text, counting tokens, and truncating content based on token limits.

public interface ITokenizerService

The ITokenizerService interface provides methods to handle text tokenization operations such as:

  • Counting the number of tokens in a given text.

  • Converting text into tokens.

  • Truncating text to ensure it does not exceed a specified number of tokens. These operations can be customized using a different encoder if specified.

Methods

CountTokens(text, encoder)

Counts the number of tokens in the specified text, optionally using a specified encoder.

Parameter
Type
Description

text

The text to be tokenized and counted.

encoder

The optional name of the encoder used for tokenization. If not specified, a default encoder is used.

Returns: Int32. The total count of tokens in the specified text.

This method provides a way to determine the length of a text in terms of tokens, which can be useful for operations that have token limits. The method can utilize different models to tokenize the text, which may affect the token count. Example usage:


  ITokenizerService tokenizerService = GetTokenizerService();
  int tokenCount = tokenizerService.CountTokens("This is a sample text.");

Throws:

Tokenize(text, encoder)

Tokenizes the specified text into an array of tokens, optionally using a specified encoder.

Parameter
Type
Description

text

The text to be tokenized.

encoder

The optional name of the encoder used for tokenization. If not specified, a default encoder is used.

Returns: String[]. An array of tokens derived from the specified text.

This method splits the text into discrete tokens, which can be useful for various text processing tasks. Different tokenization models can produce different token arrays from the same text. Example usage:


  ITokenizerService tokenizerService = GetTokenizerService();
  string[] tokens = tokenizerService.Tokenize("This is a sample text.");

Throws:

TruncateContent(text, maxTokens, encoder)

Truncates the specified text to ensure it does not exceed a given number of tokens, optionally using a specified encoder.

Parameter
Type
Description

text

The text to be truncated based on token count.

maxTokens

The maximum number of tokens allowed for the text.

encoder

The optional name of the encoder used for tokenization. If not specified, a default encoder is used.

Returns: String. The truncated text that is within the specified token limit.

This method is useful for ensuring that the text does not exceed a certain token limit, which can be important in contexts where token usage is limited or costly. The truncation respects token boundaries. Example usage:


  ITokenizerService tokenizerService = GetTokenizerService();
  string truncatedText = tokenizerService.TruncateContent("This is a sample text that may need truncation.", 5);

Throws:

Implemented By

Name
Description

Provides services for tokenizing text, including counting tokens, tokenizing, and truncating content based on a token limit.

Last updated