Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Browser Usage, countTokens convenience method #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Encoder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// This file includes code which was modified from https://github.com/openai/gpt-2
const fs = require('fs')
const path = require('path');

const encoder = JSON.parse(fs.readFileSync(path.join(__dirname, './encoder.json')));
const bpe_file = fs.readFileSync(path.join(__dirname, './vocab.bpe'), 'utf-8');
const encoder = require('./encoder.json');
const bpe_file = require('./vocab');

const range = (x, y) => {
const res = Array.from(Array(y).keys()).slice(x)
Expand Down Expand Up @@ -172,7 +169,12 @@ function decode(tokens) {
return text
}

function countTokens(text) {
return encode(text).length;
}

module.exports = {
countTokens,
encode,
decode
};
9 changes: 7 additions & 2 deletions Encoder.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {encode, decode} = require('./Encoder.js');
const {countTokens, encode, decode} = require('./Encoder.js');

test('empty string', () => {
const str = "";
Expand Down Expand Up @@ -41,4 +41,9 @@ test('properties of Object',()=>{

expect(encode(str)).toEqual([1462, 10100, 23772, 468, 23858, 21746, 1988, 5189]);
expect(decode(encode(str))).toEqual(str);
})
})

test('token count of string', () => {
const str = 'This string should have 7 tokens.';
expect(countTokens(str)).toEqual(7);
});
3 changes: 2 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const {encode, decode} = require('./encoder.js')
const {countTokens, encode, decode} = require('./encoder.js')


const str = 'This is an example sentence to try encoding out on!'
const encoded = encode(str)
console.log('Encoded this string looks like: ', encoded)
console.log('Encoded string contains ' + countTokens(str) + ' tokens');

console.log('We can look at each token and what it represents')
for(let token of encoded){
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
declare module "gpt-3-encoder" {
export function countTokens(text: string): number;

export function encode(text: string): number[];

export function decode(tokens: number[]): string;
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { encode, decode } = require("./Encoder");
const { countTokens, encode, decode } = require("./Encoder");

module.exports = {
countTokens,
encode,
decode,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"files": [
"Encoder.js",
"encoder.json",
"vocab.bpe",
"vocab.js",
"index.d.ts"
],
"scripts": {
Expand Down
17 changes: 9 additions & 8 deletions vocab.bpe → vocab.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#version: 0.2
module.exports = `#version: 0.2
Ġ t
Ġ a
h e
Expand Down Expand Up @@ -4343,7 +4343,7 @@ ack s
ad y
d o
ĠG ood
Ġ `
Ġ \`
Ġw ish
Ġreve aled
Âł Âł
Expand Down Expand Up @@ -7302,7 +7302,7 @@ J ohn
S m
ĠF und
Ġconst antly
Ġ` `
Ġ\` \`
Ġgener ated
ĠA ction
ĠP sych
Expand Down Expand Up @@ -11335,7 +11335,7 @@ P ut
Ġbrief ly
ri ve
Ġstim ul
Ġ`` (
Ġ\`\` (
Ġ __
Ġch ip
Ġha z
Expand Down Expand Up @@ -15249,7 +15249,7 @@ ab a
le tt
Ġfol k
Ġch ase
` `
\` \`
ĠBr us
Ġte ens
c ue
Expand Down Expand Up @@ -32896,7 +32896,7 @@ ath s
n atal
=" "
fl ags
`` ``
\`\` \`\`
Ġs ul
K h
Ġpot assium
Expand Down Expand Up @@ -44389,7 +44389,7 @@ ub en
ĠT ight
ind al
ic as
` .
\` .
C AST
'' ;
ĠF et
Expand Down Expand Up @@ -47414,7 +47414,7 @@ CD C
Ġsal ads
F le
Ġindustrial ized
` ,
\` ,
ĠO WN
Ġbec k
ĠPart icularly
Expand Down Expand Up @@ -49999,3 +49999,4 @@ om inated
ĠColl ider
Ġinform ants
Ġg azed
`;