# 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.

{% tabs %}
{% tab title="C#" %}

```csharp
public interface ITokenizerService
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Interface ITokenizerService
```

{% endtab %}
{% endtabs %}

The [ITokenizerService](https://docs.wisej.com/ai/components/api/services/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

### ![](https://1941161015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFjp24h5m0lgli5swqzhO%2Fuploads%2FdLVsGEp1mKmjdHvG0Itk%2Finstance.png?alt=media\&token=0061ee96-c913-420d-aa2f-daf782ca6a1d) CountTokens(text, encoder)

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

| Parameter                                                                                                                                                                                                                                | Type                                                          | Description                                                                                          |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **text**                                                                                                                                                                                                                                 | [String](https://docs.microsoft.com/dotnet/api/system.string) | The text to be tokenized and counted.                                                                |
| **encoder** ![](https://1941161015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFjp24h5m0lgli5swqzhO%2Fuploads%2FBPFc5AMUqwkzAJdlTYv8%2Fbadge-optional.svg?alt=media\&token=38bdb20d-14b6-4aba-816f-833f010523df) | [String](https://docs.microsoft.com/dotnet/api/system.string) | The optional name of the encoder used for tokenization. If not specified, a default encoder is used. |

**Returns:** [Int32](https://docs.microsoft.com/dotnet/api/system.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:

```csharp

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

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when *text* is null.

### ![](https://1941161015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFjp24h5m0lgli5swqzhO%2Fuploads%2FdLVsGEp1mKmjdHvG0Itk%2Finstance.png?alt=media\&token=0061ee96-c913-420d-aa2f-daf782ca6a1d) Tokenize(text, encoder)

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

| Parameter                                                                                                                                                                                                                                | Type                                                          | Description                                                                                          |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **text**                                                                                                                                                                                                                                 | [String](https://docs.microsoft.com/dotnet/api/system.string) | The text to be tokenized.                                                                            |
| **encoder** ![](https://1941161015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFjp24h5m0lgli5swqzhO%2Fuploads%2FBPFc5AMUqwkzAJdlTYv8%2Fbadge-optional.svg?alt=media\&token=38bdb20d-14b6-4aba-816f-833f010523df) | [String](https://docs.microsoft.com/dotnet/api/system.string) | The optional name of the encoder used for tokenization. If not specified, a default encoder is used. |

**Returns:** [String\[\]](https://docs.microsoft.com/dotnet/api/system.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:

```csharp

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

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when *text* is null.

### ![](https://1941161015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFjp24h5m0lgli5swqzhO%2Fuploads%2FdLVsGEp1mKmjdHvG0Itk%2Finstance.png?alt=media\&token=0061ee96-c913-420d-aa2f-daf782ca6a1d) 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**                                                                                                                                                                                                                                 | [String](https://docs.microsoft.com/dotnet/api/system.string) | The text to be truncated based on token count.                                                       |
| **maxTokens**                                                                                                                                                                                                                            | [Int32](https://docs.microsoft.com/dotnet/api/system.int32)   | The maximum number of tokens allowed for the text.                                                   |
| **encoder** ![](https://1941161015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFjp24h5m0lgli5swqzhO%2Fuploads%2FBPFc5AMUqwkzAJdlTYv8%2Fbadge-optional.svg?alt=media\&token=38bdb20d-14b6-4aba-816f-833f010523df) | [String](https://docs.microsoft.com/dotnet/api/system.string) | The optional name of the encoder used for tokenization. If not specified, a default encoder is used. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.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:

```csharp

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

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when *text* is null.
* [ArgumentOutOfRangeException](https://docs.microsoft.com/dotnet/api/system.argumentoutofrangeexception)\
  Thrown when *maxTokens* is less than zero.

## Implemented By

| Name                                                                                              | Description                                                                                                                  |
| ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| [DefaultTokenizerService](https://docs.wisej.com/api?q=wisej.ai.services.defaulttokenizerservice) | Provides services for tokenizing text, including counting tokens, tokenizing, and truncating content based on a token limit. |
