Skip to content

Commit

Permalink
fix: Patch tiktoken method missing
Browse files Browse the repository at this point in the history
resolves #541
  • Loading branch information
timothycarambat committed Jan 5, 2024
1 parent d95d1a9 commit 3e088f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion server/utils/helpers/chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function cannonball({
// if the delta is the token difference between where our prompt is in size
// and where we ideally need to land.
const delta = initialInputSize - targetTokenSize;
const tokenChunks = tokenManager.countFromString(input);
const tokenChunks = tokenManager.tokensFromString(input);
const middleIdx = Math.floor(tokenChunks.length / 2);

// middle truncate the text going left and right of midpoint
Expand Down
13 changes: 10 additions & 3 deletions server/utils/helpers/tiktoken.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ class TokenManager {
}
}

// Pass in an empty array of disallowedSpecials to handle all tokens as text and to be tokenized.
// https://github.com/openai/tiktoken/blob/9e79899bc248d5313c7dd73562b5e211d728723d/tiktoken/core.py#L91C20-L91C38
// Returns number[]
tokensFromString(input = "") {
const tokens = this.encoder.encode(input, undefined, []);
return tokens;
}

bytesFromTokens(tokens = []) {
const bytes = this.encoder.decode(tokens);
return bytes;
}

// Pass in an empty array of disallowedSpecials to handle all tokens as text and to be tokenized.
// https://github.com/openai/tiktoken/blob/9e79899bc248d5313c7dd73562b5e211d728723d/tiktoken/core.py#L91C20-L91C38
// Returns number
countFromString(input = "") {
const tokens = this.encoder.encode(input, undefined, []);
const tokens = this.tokensFromString(input);
return tokens.length;
}

Expand Down

0 comments on commit 3e088f2

Please sign in to comment.