Skip to content

Commit

Permalink
Fix invalidate cache
Browse files Browse the repository at this point in the history
Related to def176a
  • Loading branch information
yethee committed Jul 17, 2024
1 parent 4e2794f commit 4317a32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/EncoderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ private function getVocab(string $encodingName): Vocab
$loader = $this->vocabLoader = new DefaultVocabLoader($this->vocabCacheDir);
}

return $this->vocabs[$encodingName] = $loader->load(self::ENCODINGS[$encodingName]['vocab']);
return $this->vocabs[$encodingName] = $loader->load(
self::ENCODINGS[$encodingName]['vocab'],
self::ENCODINGS[$encodingName]['hash'] ?? null,
);
}
}
20 changes: 20 additions & 0 deletions tests/EncoderProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Yethee\Tiktoken\Tests;

use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yethee\Tiktoken\EncoderProvider;

use function dirname;
use function hash;

final class EncoderProviderTest extends TestCase
{
Expand Down Expand Up @@ -39,6 +41,24 @@ public function testEncode(): void
self::assertSame([15339, 1917], $encoder->encode('hello world'));
}

public function testUseHashWhenLoadVocab(): void
{
$cache = vfsStream::setup('cache');
$vocabCacheFilename = hash('sha1', EncoderProvider::ENCODINGS['p50k_base']['vocab']);

$cacheFile = vfsStream::newFile($vocabCacheFilename)
->withContent('broken cache')
->at($cache);

$provider = new EncoderProvider();
/** @psalm-suppress ArgumentTypeCoercion */
$provider->setVocabCache($cache->url());

$provider->get('p50k_base');

self::assertNotEquals('broken cache', $cacheFile->getContent());
}

/**
* @return iterable<array{non-empty-string, non-empty-string}>
*
Expand Down

0 comments on commit 4317a32

Please sign in to comment.