-
Notifications
You must be signed in to change notification settings - Fork 192
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
Latpull docs, demo, browserify, countTokens, tokenStats #30
base: master
Are you sure you want to change the base?
Conversation
I don't actually want to encode into tokens for my use case, quickly count to check my request won't exceed the limit. This should be faster since we don't initialize the memory for the output array. ``` const crypto = require('crypto'); // Generate a random string of a given length function generateRandomString(length) { return crypto.randomBytes(length).toString('hex'); } const {encode, decode, countTokens} = require('gpt-3-encoder') let str = 'This is an example sentence to try encoding out on!' // let now = Date.now(); let encoded = encode(str) console.log('Encoded this string looks like: ', encoded) console.log('We can look at each token and what it represents) let tokencount = 0; for(let token of encoded){ tokencount ++; console.log({token, string: decode([token])}) } console.log("there are n tokens: ", tokencount); let decoded = decode(encoded) console.log('We can decode it back into:\n', decoded) let now = Date.now(); // todo: write an benchmark for the above method vs int countTokens(str) str = generateRandomString(10000); console.time('fencode'); encoded = encode(str); console.log(`First encode to cache string n stuff in mem`); console.timeEnd('fencode'); console.log(`Original string length: ${str.length}`); // Benchmark the encode function console.time('encode'); encoded = encode(str); console.log(`Encoded string length: ${encoded.length}`); console.timeEnd('encode'); // Benchmark the countTokens function console.time('countTokens'); let tokenCount = countTokens(str); console.log(`Number of tokens: ${tokenCount}`); console.timeEnd('countTokens'); console.log(`Original string length: ${str.length}`); console.log(`Encoded string length: ${encoded.length}`); console.log(`Number of tokens: ${tokenCount}`); ``` ``` We can decode it back into: This is an example sentence to try encoding out on! First encode to cache string n stuff in mem fencode: 163.57ms Original string length: 20000 Encoded string length: 11993 encode: 124.265ms Number of tokens: 11993 countTokens: 29.2ms Original string length: 20000 Encoded string length: 11993 Number of tokens: 11993 ```
Co-authored-by: Andrew Healey <[email protected]>
I don't actually want to encode into tokens for my use case, quickly count to check my request won't exceed the limit. This should be faster since we don't initialize the memory for the output array. ``` const crypto = require('crypto'); // Generate a random string of a given length function generateRandomString(length) { return crypto.randomBytes(length).toString('hex'); } const {encode, decode, countTokens} = require('gpt-3-encoder') let str = 'This is an example sentence to try encoding out on!' // let now = Date.now(); let encoded = encode(str) console.log('Encoded this string looks like: ', encoded) console.log('We can look at each token and what it represents) let tokencount = 0; for(let token of encoded){ tokencount ++; console.log({token, string: decode([token])}) } console.log("there are n tokens: ", tokencount); let decoded = decode(encoded) console.log('We can decode it back into:\n', decoded) let now = Date.now(); // todo: write an benchmark for the above method vs int countTokens(str) str = generateRandomString(10000); console.time('fencode'); encoded = encode(str); console.log(`First encode to cache string n stuff in mem`); console.timeEnd('fencode'); console.log(`Original string length: ${str.length}`); // Benchmark the encode function console.time('encode'); encoded = encode(str); console.log(`Encoded string length: ${encoded.length}`); console.timeEnd('encode'); // Benchmark the countTokens function console.time('countTokens'); let tokenCount = countTokens(str); console.log(`Number of tokens: ${tokenCount}`); console.timeEnd('countTokens'); console.log(`Original string length: ${str.length}`); console.log(`Encoded string length: ${encoded.length}`); console.log(`Number of tokens: ${tokenCount}`); ``` ``` We can decode it back into: This is an example sentence to try encoding out on! First encode to cache string n stuff in mem fencode: 163.57ms Original string length: 20000 Encoded string length: 11993 encode: 124.265ms Number of tokens: 11993 countTokens: 29.2ms Original string length: 20000 Encoded string length: 11993 Number of tokens: 11993 ``` Co-authored-by: Kier <[email protected]>
# Conflicts: # Encoder.js
merge back the one other change and follow nick with moving to version 1.2 for feature add.
…e updates maybe we should host jsdoc on github pages also performed npm audit and updates for security vulnerabilities.
It seems to work fine
By using the Buffer class in this way, you can encode and decode strings as UTF-8 without using the TextEncoder and TextDecoder classes. This can be useful if you need to support earlier versions of Node.js that do not have these classes in the util module.
…eaking change but i think its actually mor widely compatible and should be fine.
…by webpaking... todo need to test browser version This precomputes bpe_ranks so that it is it the from needed make sure to rebuild if you change the bpe dump file
…ll need to deploy docs to github pages for demo to work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Started to look over the changes once more and added a few comments on what I did
@@ -16,7 +16,7 @@ jobs: | |||
|
|||
strategy: | |||
matrix: | |||
node-version: [12.x, 14.x] | |||
node-version: [12.x, 14.x, 16.x, 18.x] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test all the versions
|
||
export function decode(tokens: number[]): string; | ||
|
||
export function countTokens(text: string): number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
functions added to type def
@@ -1,6 +1,8 @@ | |||
const { encode, decode } = require("./Encoder"); | |||
const { encode, decode, countTokens, tokenStats } = require("./Encoder"); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also added functions to node lib index
} | ||
"browserify": "^17.0.0", | ||
"jest": "^26.4.2", | ||
"jsdoc": "^4.0.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only dev dependencies added for building docs and browserifying a version for client side execution (2.5M including data)
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/AIDungeon/GPT-3-Encoder.git" | ||
"url": "git+https://github.com/latitudegames/GPT-3-Encoder.git" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were you guys renaming the pakage to aidungon
Encoder.js
Outdated
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 = loader('./encoder.json'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could erase these comments but in order to support loading into a browser module i have loaded the data into encoder.js
and bpe_ranks.js
These are pre-computed a build time and this removes some of the work the client has to do other then load the data into memory. Todo maybe we should clean up these comments. but theoretical you could switch it back if you only wanted nodejs.
I don't know if your fork is stable or not, but I can't open issues to there... Simply adding this to a next repo (rc3), and calling the tokenStats seems to be broken... Also, it would be nice to type out the tokenstats return not just an any but something like an (didn't checked if this is actually correct or not): interface TokenStats {
count: number,
unique: number,
frequency: Record<number, number>,
positions: Record<number, number[]>,
tokens: number[],
} |
The pre 1.4rcX should be stable but doesn't have it all. I made some more extreme changes namely browser support. It does pass the tests and seems to work for a few people. needs a bit of elbow grease to clean up some stuff. I can look into the type def. I also enabled issues in my fork. |
…460069823 Add types let me know if this is the correct way to implement it. And here are some additional useful stats that could be added to the stats object: averageWordLength: The average length of the words in the tokens array. mostFrequentWords: An array of the most frequently occurring words in the tokens array, ordered by frequency. leastFrequentWords: An array of the least frequently occurring words in the tokens array, ordered by frequency. wordPositions: An object that maps each word in the tokens array to its positions (indices) in the array. wordLengths: An array of the lengths of the words in the tokens array.
# Conflicts: # docs/Encoder.js.html # docs/global.html # docs/index.html # package-lock.json
I have reverted npm branching and changed syonfox to latitudegames for a possible pull. you can check it out and test in the syonfox fork