这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
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.tokensFromString(input);
const tokenChunks = tokenManager.countFromString(input);
const middleIdx = Math.floor(tokenChunks.length / 2);

// middle truncate the text going left and right of midpoint
Expand Down
14 changes: 5 additions & 9 deletions server/utils/helpers/tiktoken.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,27 @@ const { getEncodingNameForModel, getEncoding } = require("js-tiktoken");
class TokenManager {
constructor(model = "gpt-3.5-turbo") {
this.model = model;
this.encoderName = this.getEncodingFromModel(model);
this.encoderName = this.#getEncodingFromModel(model);
this.encoder = getEncoding(this.encoderName);
this.buffer = 50;
}

getEncodingFromModel(model) {
#getEncodingFromModel(model) {
try {
return getEncodingNameForModel(model);
} catch {
return "cl100k_base";
}
}

tokensFromString(input = "") {
const tokens = this.encoder.encode(input);
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
countFromString(input = "") {
const tokens = this.encoder.encode(input);
const tokens = this.encoder.encode(input, undefined, []);
return tokens.length;
}

Expand Down