Skip to content

Commit

Permalink
add register
Browse files Browse the repository at this point in the history
  • Loading branch information
danny50610 committed Aug 20, 2023
1 parent a162176 commit ed6f622
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ $enc = EncodingFactory::createByModelName('gpt-3.5-turbo');

var_dump($enc->decode($enc->encode("hello world")));
// output: string(11) "hello world"
```
```

For available encodings, see `src/EncodingFactory.php`
34 changes: 32 additions & 2 deletions src/EncodingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Danny50610\BpeTokeniser;

use Closure;
use Exception;
use SplFileObject;

Expand Down Expand Up @@ -63,10 +64,39 @@ class EncodingFactory
"gpt-35-turbo" => "cl100k_base", # Azure deployment name
];

protected static $encodingInstance = [];

protected static $encodingConstructors = null;

public static function registerModelToEncoding(string $modelName, string $encodingName)
{
if (array_key_exists($modelName, self::$modelToEncoding)) {
throw new Exception("{$modelName} already exists");
}

self::$modelToEncoding[$modelName] = $encodingName;
}

public static function registerModelPrefixToEncoding(string $modelPrefix, string $encodingName)
{
if (array_key_exists($modelPrefix, self::$modelPrefixToEncoding)) {
throw new Exception("{$modelPrefix} already exists");
}

self::$modelPrefixToEncoding[$modelPrefix] = $encodingName;
}

public static function registerEncoding(string $encodingName, Closure $constructor)
{
static::initConstructor();

if (array_key_exists($encodingName, self::$encodingConstructors)) {
throw new Exception("{$encodingName} already exists");
}

self::$encodingConstructors[$encodingName] = $constructor;
}

protected static $encodingInstance = [];

public static function createByModelName(string $modelName): Encoding
{
$encodingName = null;
Expand Down

0 comments on commit ed6f622

Please sign in to comment.