forked from latitudegames/GPT-3-Encoder
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tokenStats some tests, and some error checks, also docs and readm…
…e updates maybe we should host jsdoc on github pages also performed npm audit and updates for security vulnerabilities.
- Loading branch information
syonfox
committed
Dec 25, 2022
1 parent
63260ae
commit 65a3181
Showing
7 changed files
with
4,233 additions
and
3,408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,109 @@ | ||
const {encode, decode} = require('./Encoder.js'); | ||
const {encode, decode, countTokens, tokenStats} = require('./Encoder.js'); | ||
const crypto = require('crypto'); | ||
|
||
// Generate a random string of a given length | ||
function generateRandomString(length) { | ||
return crypto.randomBytes(length).toString('hex'); | ||
} | ||
|
||
|
||
test('empty string', () => { | ||
const str = ""; | ||
expect(encode(str)).toEqual([]) | ||
expect(decode(encode(str))).toEqual(str) | ||
const str = ""; | ||
expect(encode(str)).toEqual([]) | ||
expect(decode(encode(str))).toEqual(str) | ||
}); | ||
|
||
test('space', () => { | ||
const str = " "; | ||
expect(encode(str)).toEqual([220]) | ||
expect(decode(encode(str))).toEqual(str) | ||
const str = " "; | ||
expect(encode(str)).toEqual([220]) | ||
expect(decode(encode(str))).toEqual(str) | ||
}); | ||
|
||
test('tab', () => { | ||
const str = "\t"; | ||
expect(encode(str)).toEqual([197]) | ||
expect(decode(encode(str))).toEqual(str) | ||
const str = "\t"; | ||
expect(encode(str)).toEqual([197]) | ||
expect(decode(encode(str))).toEqual(str) | ||
}); | ||
|
||
test('simple text', () => { | ||
const str = "This is some text"; | ||
expect(encode(str)).toEqual([1212, 318, 617, 2420]) | ||
expect(decode(encode(str))).toEqual(str) | ||
const str = "This is some text"; | ||
expect(encode(str)).toEqual([1212, 318, 617, 2420]) | ||
expect(decode(encode(str))).toEqual(str) | ||
}); | ||
|
||
test('multi-token word', () => { | ||
const str = "indivisible"; | ||
expect(encode(str)).toEqual([521, 452, 12843]) | ||
expect(decode(encode(str))).toEqual(str) | ||
const str = "indivisible"; | ||
expect(encode(str)).toEqual([521, 452, 12843]) | ||
expect(decode(encode(str))).toEqual(str) | ||
}); | ||
|
||
test('emojis', () => { | ||
const str = "hello 👋 world 🌍"; | ||
expect(encode(str)).toEqual([31373, 50169, 233, 995, 12520, 234, 235]) | ||
expect(decode(encode(str))).toEqual(str) | ||
const str = "hello 👋 world 🌍"; | ||
expect(encode(str)).toEqual([31373, 50169, 233, 995, 12520, 234, 235]) | ||
expect(decode(encode(str))).toEqual(str) | ||
}); | ||
|
||
test('properties of Object',()=>{ | ||
const str = "toString constructor hasOwnProperty valueOf"; | ||
test('properties of Object', () => { | ||
const str = "toString constructor hasOwnProperty valueOf"; | ||
|
||
expect(encode(str)).toEqual([1462, 10100, 23772, 468, 23858, 21746, 1988, 5189]); | ||
expect(decode(encode(str))).toEqual(str); | ||
}) | ||
|
||
|
||
test('Random encode=decode count', () => { | ||
let n = 200 | ||
let str | ||
|
||
let t = { | ||
c:0,e:0,d:0,l: 0,f:0 | ||
} | ||
for (let i = 0; i < n; i++) { | ||
const randomNumber = Math.floor(Math.random() * (2 * n + 1)) + n; | ||
str = generateRandomString(randomNumber); | ||
t.l+= randomNumber; | ||
let now = Date.now() | ||
let _fe = encode(str); | ||
t.f += Date.now()-now; now = Date.now(); | ||
let count = countTokens(str); | ||
t.c += Date.now()-now; now = Date.now(); | ||
let e = encode(str); | ||
t.e += Date.now()-now; now = Date.now(); | ||
let d = decode(e); | ||
t.d += Date.now()-now; now = Date.now(); | ||
expect(d).toEqual(str); | ||
expect(e.length).toEqual(count); | ||
|
||
} | ||
|
||
console.log(`Timings for chars(${t.l}): fencode: ${t.f}, counting: ${t.c}, encoding: ${t.e}, decoding:${t.d}`) | ||
|
||
|
||
// const str = "toString constructor hasOwnProperty valueOf"; | ||
// expect(encode(str).length).toEqual(countTokens(str)); | ||
}) | ||
|
||
test('empty encode', () => { | ||
expect(encode()).toEqual([]); | ||
|
||
}) | ||
test('null encode', () => { | ||
expect(encode(null)).toEqual(encode("null")); | ||
|
||
}) | ||
test('empty decode', () => { | ||
expect(decode()).toEqual(""); | ||
|
||
}) | ||
|
||
test('stats test', () => { | ||
const str = "hello 👋 world 🌍, im a foo your a foo, everwer where a foo foo"; | ||
|
||
expect(encode(str)).toEqual([1462, 10100, 23772, 468, 23858, 21746, 1988, 5189]); | ||
expect(decode(encode(str))).toEqual(str); | ||
}) | ||
let e = encode(str); | ||
let stats = tokenStats(e); | ||
console.log("example stats: ", stats); | ||
expect(tokenStats(e)).toEqual(tokenStats(str)) | ||
expect(decode(encode(str))).toEqual(str) | ||
// const str = "toString constructor hasOwnProperty valueOf"; | ||
// expect(encode(str).length).toEqual(countTokens(str)); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
declare module "gpt-3-encoder" { | ||
export function encode(text: string): number[]; | ||
export function encode(text: string): number[]; | ||
|
||
export function decode(tokens: number[]): string; | ||
|
||
export function countTokens(text: string): number; | ||
|
||
export function tokenStats(input: string | number[]): any; | ||
|
||
export function decode(tokens: number[]): string; | ||
|
||
export function countTokens(text: string): number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
const { encode, decode, countTokens } = require("./Encoder"); | ||
const { encode, decode, countTokens, tokenStats } = require("./Encoder"); | ||
|
||
module.exports = { | ||
encode, | ||
decode, | ||
countTokens, | ||
tokenStats | ||
}; |
Oops, something went wrong.