🪢 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
perf: use js-tiktoken (#957)
Max Deichmannef6e1e0bf4a7
diff --git a/package-lock.json b/package-lock.jsonindex 765339068d..30f26a81ae 100644--- a/package-lock.json+++ b/package-lock.json@@ -61,6 +61,7 @@"date-fns": "^3.3.1","decimal.js": "^10.4.3","exponential-backoff": "^3.1.1",+ "js-tiktoken": "^1.0.8","lodash": "^4.17.21","lucide-react": "^0.314.0","next": "^14.1.0",@@ -79,7 +80,6 @@"superjson": "2.2.1","tailwind-merge": "^2.2.0","tailwindcss-animate": "^1.0.7",- "tiktoken": "^1.0.11","use-query-params": "^2.2.1","uuid": "^9.0.1","zod": "^3.22.4"@@ -7649,7 +7649,6 @@"version": "1.5.1","resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz","integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",- "dev": true,"funding": [{"type": "github",@@ -13471,6 +13470,14 @@"node": "^14.17.0 || ^16.13.0 || >=18.0.0"}},+ "node_modules/js-tiktoken": {+ "version": "1.0.8",+ "resolved": "https://registry.npmjs.org/js-tiktoken/-/js-tiktoken-1.0.8.tgz",+ "integrity": "sha512-r7XK3E9/I+SOrbAGqb39pyO/rHAS1diAOSRAvaaLfHgXjkUSK9AiSd+r84Vn2f/GvXJYRAxKj8NHrUvqlaH5qg==",+ "dependencies": {+ "base64-js": "^1.5.1"+ }+ },"node_modules/js-tokens": {"version": "4.0.0","resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",diff --git a/package.json b/package.jsonindex 460c69243b..1dc1511781 100644--- a/package.json+++ b/package.json@@ -79,6 +79,7 @@"date-fns": "^3.3.1","decimal.js": "^10.4.3","exponential-backoff": "^3.1.1",+ "js-tiktoken": "^1.0.8","lodash": "^4.17.21","lucide-react": "^0.314.0","next": "^14.1.0",@@ -97,7 +98,6 @@"superjson": "2.2.1","tailwind-merge": "^2.2.0","tailwindcss-animate": "^1.0.7",- "tiktoken": "^1.0.11","use-query-params": "^2.2.1","uuid": "^9.0.1","zod": "^3.22.4"diff --git a/src/features/ingest/lib/usage.ts b/src/features/ingest/lib/usage.tsindex 5d65f2df54..0d0cc65278 100644--- a/src/features/ingest/lib/usage.ts+++ b/src/features/ingest/lib/usage.ts@@ -4,9 +4,9 @@ import { type Model } from "@prisma/client";import {type TiktokenModel,type Tiktoken,- get_encoding,- encoding_for_model,-} from "tiktoken";+ getEncoding,+ encodingForModel,+} from "js-tiktoken";import { z } from "zod";const OpenAiTokenConfig = z.object({@@ -131,7 +131,8 @@ function openAiChatTokenCount(params: {"functionCall",].some((k) => k === key)) {- numTokens += getTokensByModel(model, value);+ const tokens = getTokensByModel(model, value);+ if (tokens) numTokens += tokens;}if (key === "name") {numTokens += params.config.tokensPerName;@@ -144,17 +145,27 @@ function openAiChatTokenCount(params: {}const getTokensByModel = (model: TiktokenModel, text: string) => {- let encoding: Tiktoken;+ // encoiding should be kept in memory to avoid re-creating it+ let encoding: Tiktoken | undefined;try {- encoding = encoding_for_model(model);+ cachedTokenizerByModel[model] =+ cachedTokenizerByModel[model] || encodingForModel(model);++ encoding = cachedTokenizerByModel[model];} catch (KeyError) {console.log("Warning: model not found. Using cl100k_base encoding.");- encoding = get_encoding("cl100k_base");++ encoding = getEncoding("cl100k_base");}const cleandedText = unicodeToBytesInString(text);- return encoding.encode(cleandedText).length;+ return encoding?.encode(cleandedText).length;};+interface Tokenizer {+ [model: string]: Tiktoken;+}+const cachedTokenizerByModel: Tokenizer = {};+function isString(value: unknown): value is string {return typeof value === "string";}