PHPackages                             nocika/gpt3-tokenizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nocika/gpt3-tokenizer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nocika/gpt3-tokenizer
=====================

PHP package for Byte Pair Encoding (BPE) used by GPT-3.

015PHP

Since Apr 20Pushed 3y agoCompare

[ Source](https://github.com/MelvinNocika/GPT3Tokenizer)[ Packagist](https://packagist.org/packages/nocika/gpt3-tokenizer)[ RSS](/packages/nocika-gpt3-tokenizer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

GPT3Tokenizer for PHP
=====================

[](#gpt3tokenizer-for-php)

This is a PHP port of the GPT-3 tokenizer. It is based on the [original Python implementation](https://huggingface.co/docs/transformers/model_doc/gpt2#transformers.GPT2Tokenizer) and the [Nodejs implementation](https://github.com/latitudegames/GPT-3-Encoder).

GPT-2 and GPT-3 use a technique called byte pair encoding to convert text into a sequence of integers, which are then used as input for the model. When you interact with the OpenAI API, you may find it useful to calculate the amount of tokens in a given text before sending it to the API.

If you want to learn more, read the [Summary of the tokenizers](https://huggingface.co/docs/transformers/tokenizer_summary) from Hugging Face.

Support ⭐️
----------

[](#support-️)

If you find my work useful, I would be thrilled if you could show your support by giving this project a star ⭐️. It only takes a second and it would mean a lot to me. Your star will not only make me feel warm and fuzzy inside, but it will also help reach more people who can benefit from this project.

Installation
------------

[](#installation)

Install the package from [Packagist](https://packagist.org/packages/gioni06/gpt3-tokenizer) using Composer:

```
composer require gioni06/gpt3-tokenizer
```

Testing
-------

[](#testing)

Loading the vocabulary files consumes a lot of memory. You might need to increase the phpunit memory limit.

```
./vendor/bin/phpunit -d memory_limit=-1 tests/
```

Use the configuration Class
---------------------------

[](#use-the-configuration-class)

```
use Gioni06\Gpt3Tokenizer\Gpt3TokenizerConfig;

// default vocab path
// default merges path
// caching enabled
$defaultConfig = new Gpt3TokenizerConfig();

$customConfig = new Gpt3TokenizerConfig();
$customConfig
    ->vocabPath('custom_vocab.json') // path to a custom vocabulary file
    ->mergesPath('custom_merges.txt') // path to a custom merges file
    ->useCache(false)
```

### A note on caching

[](#a-note-on-caching)

The tokenizer will try to use `apcu` for caching, if that is not available it will use a plain PHP `array`. You will see slightly better performance for long texts when using the cache. The cache is enabled by default.

Encode a text
-------------

[](#encode-a-text)

```
use Gioni06\Gpt3Tokenizer\Gpt3TokenizerConfig;
use Gioni06\Gpt3Tokenizer\Gpt3Tokenizer;

$config = new Gpt3TokenizerConfig();
$tokenizer = new Gpt3Tokenizer($config);
$text = "This is some text";
$tokens = $tokenizer->encode($text);
// [1212,318,617,2420]
```

Decode a text
-------------

[](#decode-a-text)

```
use Gioni06\Gpt3Tokenizer\Gpt3TokenizerConfig;
use Gioni06\Gpt3Tokenizer\Gpt3Tokenizer;

$config = new Gpt3TokenizerConfig();
$tokenizer = new Gpt3Tokenizer($config);
$tokens = [1212,318,617,2420]
$text = $tokenizer->decode($tokens);
// "This is some text"
```

Count the number of tokens in a text
------------------------------------

[](#count-the-number-of-tokens-in-a-text)

```
use Gioni06\Gpt3Tokenizer\Gpt3TokenizerConfig;
use Gioni06\Gpt3Tokenizer\Gpt3Tokenizer;

$config = new Gpt3TokenizerConfig();
$tokenizer = new Gpt3Tokenizer($config);
$text = "This is some text";
$numberOfTokens = $tokenizer->count($text);
// 4
```

Encode a given text into chunks of tokens, with each chunk containing a specified maximum number of tokens.
-----------------------------------------------------------------------------------------------------------

[](#encode-a-given-text-into-chunks-of-tokens-with-each-chunk-containing-a-specified-maximum-number-of-tokens)

This method is useful when handling large texts that need to be divided into smaller chunks for further processing.

```
use Gioni06\Gpt3Tokenizer\Gpt3TokenizerConfig;
use Gioni06\Gpt3Tokenizer\Gpt3Tokenizer;

$config = new Gpt3TokenizerConfig();
$tokenizer = new Gpt3Tokenizer($config);
$text = "1 2 hello，world 3 4";
$tokenizer->encodeInChunks($text, 5)
// [[16, 362, 23748], [171, 120, 234, 6894, 513], [604]]
```

Takes a given text and chunks it into encoded segments, with each segment containing a specified maximum number of tokens.
--------------------------------------------------------------------------------------------------------------------------

[](#takes-a-given-text-and-chunks-it-into-encoded-segments-with-each-segment-containing-a-specified-maximum-number-of-tokens)

This method leverages the encodeInChunks method for encoding the text into Byte-Pair Encoded (BPE) tokens and then decodes these tokens back into text.

```
use Gioni06\Gpt3Tokenizer\Gpt3TokenizerConfig;
use Gioni06\Gpt3Tokenizer\Gpt3Tokenizer;

$config = new Gpt3TokenizerConfig();
$tokenizer = new Gpt3Tokenizer($config);
$text = "1 2 hello，world 3 4";
$tokenizer->chunk($text, 5)
// ['1 2 hello', '，world 3', ' 4']
```

License
-------

[](#license)

This project uses the Apache License 2.0 license. See the [LICENSE](LICENSE) file for more information.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity22

Early-stage or recently created project

 Bus Factor1

Top contributor holds 93.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/aa24b574479a7e6e796b28071f9debf3d7e2321a5e585c5e78414e8a733d5fe7?d=identicon)[MelvinNocika](/maintainers/MelvinNocika)

---

Top Contributors

[![Gioni06](https://avatars.githubusercontent.com/u/536849?v=4)](https://github.com/Gioni06 "Gioni06 (28 commits)")[![adrienbrault](https://avatars.githubusercontent.com/u/611271?v=4)](https://github.com/adrienbrault "adrienbrault (1 commits)")[![MelvinNocika](https://avatars.githubusercontent.com/u/90850670?v=4)](https://github.com/MelvinNocika "MelvinNocika (1 commits)")

### Embed Badge

![Health badge](/badges/nocika-gpt3-tokenizer/health.svg)

```
[![Health](https://phpackages.com/badges/nocika-gpt3-tokenizer/health.svg)](https://phpackages.com/packages/nocika-gpt3-tokenizer)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
