Repository navigation
Expand file tree
/
Copy pathencodeCache.js
More file actions
55 lines (43 loc) · 1.87 KB
/
Copy pathencodeCache.js
File metadata and controls
55 lines (43 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { encoding_for_model } from '@dqbd/tiktoken';
import { FastLRUCache } from './fastLruCache.js';
class EncodeCache {
constructor(model = "gpt-4o") {
this.encodeCache = new FastLRUCache(1000);
this.decodeCache = new FastLRUCache(100); // we don't use decode nearly as much
this.encoder = encoding_for_model(model);
}
encode(value) {
// Handle CortexResponse objects by extracting the text content
if (value && typeof value === 'object' && value.constructor && value.constructor.name === 'CortexResponse') {
value = value.output_text || '';
}
if (this.encodeCache.get(value) !== -1) {
return this.encodeCache.get(value);
}
const encoded = this.encoder.encode(value);
this.encodeCache.put(value, encoded);
return encoded;
}
decode(value) {
// Create a cache key based on array values
const key = Array.from(value).toString();
if (this.decodeCache.get(key) !== -1) {
return this.decodeCache.get(key);
}
// The tiktoken decoder returns Uint8Array, we need to convert it to a string
const decoded = this.encoder.decode(value);
// Convert the decoded tokens to a string
const decodedString = typeof decoded === 'string' ? decoded : new TextDecoder().decode(decoded);
this.decodeCache.put(key, decodedString);
if (this.encodeCache.get(decodedString) === -1) {
this.encodeCache.put(decodedString, value);
}
return decodedString;
}
}
// Create one instance of the cache
const cache = new EncodeCache();
// Make sure the instance is bound to the methods, so
// references to 'this' are correct
export const encode = cache.encode.bind(cache);
export const decode = cache.decode.bind(cache);