🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fic: use encoding once (#904)
Max Deichmann645f14535bbe
diff --git a/src/features/ingest/lib/usage.ts b/src/features/ingest/lib/usage.tsindex ace1a0b2e6..9a948f999e 100644--- a/src/features/ingest/lib/usage.ts+++ b/src/features/ingest/lib/usage.ts@@ -57,13 +57,6 @@ export function tokenCount(p: {}function openAiChatTokenCount(params: TokenCalculationParams) {- let encoding: Tiktoken;- try {- encoding = encoding_for_model(params.model);- } catch (KeyError) {- console.log("Warning: model not found. Using cl100k_base encoding.");- encoding = get_encoding("cl100k_base");- }let tokens_per_message = 0;let tokens_per_name = 0;@@ -100,7 +93,7 @@ function openAiChatTokenCount(params: TokenCalculationParams) {Object.keys(message).forEach((key) => {const value = message[key as keyof typeof message];if (value) {- num_tokens += encoding.encode(value).length;+ num_tokens += getTokensByModel(params.model, value);}if (key === "name") {num_tokens += tokens_per_name;@@ -108,9 +101,7 @@ function openAiChatTokenCount(params: TokenCalculationParams) {});});num_tokens += 3; // every reply is primed with <| start |> assistant <| message |>- // https://github.com/dqbd/tiktoken/issues/72- // we need to ensure to deallocate memory from the encoder- encoding.free();+return num_tokens;}@@ -141,6 +132,23 @@ const getTokens = (name: TiktokenEncoding, text: string) => {return tokens.length;};+const getTokensByModel = (model: TiktokenModel, text: string) => {+ let encoding: Tiktoken;+ try {+ encoding = encoding_for_model(model);+ } catch (KeyError) {+ console.log("Warning: model not found. Using cl100k_base encoding.");+ encoding = get_encoding("cl100k_base");+ }++ const length = encoding.encode(text).length;++ // https://github.com/dqbd/tiktoken/issues/72+ // we need to ensure to deallocate memory from the encoder+ encoding.free();+ return length;+};+function isString(value: unknown): value is string {return typeof value === "string";}