scraipt
Version:
Scrape away inefficient code during compile-time using AI
53 lines (52 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.limitInputTokens = exports.countTokens = exports.getMaxModelTokens = void 0;
const gpt_tokenizer_1 = require("gpt-tokenizer");
const maxModelTokens = {
'gpt-4': 8192,
'gpt-4-0613': 8192,
'gpt-4-32k': 32768,
'gpt-4-32k-0613': 32768,
'gpt-4-0125-preview': 128000,
'gpt-4-turbo-preview': 128000,
'gpt-4-1106-preview': 128000,
'gpt-4-vision-preview': 128000,
'gpt-3.5-turbo-0125': 16385,
'gpt-3.5-turbo-1106': 16385,
'gpt-3.5-turbo': 4096,
'gpt-3.5-turbo-instruct': 4096,
};
/**
* Get the maximum number of tokens for the model.
* @param model The model to get the maximum number of tokens for.
* @returns The maximum number of tokens for the model.
*/
const getMaxModelTokens = (model) => {
var _a;
return (_a = maxModelTokens[model]) !== null && _a !== void 0 ? _a : 4096;
};
exports.getMaxModelTokens = getMaxModelTokens;
/**
* Count the number of tokens in the messages.
* @param messages The messages to count.
* @param model The model to use for the messages.
* @returns The number of tokens in the messages.
*/
const countTokens = (messages, model) => {
return (0, gpt_tokenizer_1.encodeChat)(messages, model).length;
};
exports.countTokens = countTokens;
/**
* Limit the number of tokens in the input.
* @param input The input to limit.
* @param model The model to use for the input.
* @returns The input with a limited number of tokens.
*/
const limitInputTokens = (input, model) => {
const maxTokens = (0, exports.getMaxModelTokens)(model);
if (input.length > maxTokens) {
return input.substring(0, maxTokens);
}
return input;
};
exports.limitInputTokens = limitInputTokens;