Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

add token calculation for davinci (#96)

Maximilian Deichmann2b3f9ba73d35
diff --git a/.env.cloud.example b/.env.cloud.example
index a67742db1a..bce8ab6f6e 100644
--- a/.env.cloud.example
+++ b/.env.cloud.example
@@ -30,4 +30,4 @@ NEXTAUTH_SECRET="secret"
# NEXT_PUBLIC_POSTHOG_KEY=
# Id of demo project to automatically assign new users to
-# DEMO_PROJECT_ID=
\ No newline at end of file
+# NEXT_PUBLIC_DEMO_PROJECT_ID=
\ No newline at end of file
diff --git a/src/features/ingest/lib/usage.ts b/src/features/ingest/lib/usage.ts
index 8d50cd07cc..d2395c168f 100644
--- a/src/features/ingest/lib/usage.ts
+++ b/src/features/ingest/lib/usage.ts
@@ -1,4 +1,4 @@
-import { get_encoding } from "tiktoken";
+import { type TiktokenEncoding, get_encoding } from "tiktoken";
import { countTokens } from "@anthropic-ai/tokenizer";
export function tokenCount(p: {
@@ -6,11 +6,12 @@ export function tokenCount(p: {
text: string;
}): number | undefined {
if (!p.model || !p.text) return undefined;
+
if (p.model.toLowerCase().startsWith("gpt")) {
- const encoding = get_encoding("cl100k_base");
- const tokens = encoding.encode(p.text);
- encoding.free();
- return tokens.length;
+ return getTokens("cl100k_base", p.text);
+ }
+ if (p.model.toLowerCase().startsWith("text-davinci")) {
+ return getTokens("p50k_base", p.text);
}
if (p.model.toLowerCase().startsWith("claude")) {
return countTokens(p.text);
@@ -18,3 +19,10 @@ export function tokenCount(p: {
console.log("Unknown model", p.model);
return undefined;
}
+
+const getTokens = (name: TiktokenEncoding, text: string) => {
+ const encoding = get_encoding(name);
+ const tokens = encoding.encode(text);
+ encoding.free();
+ return tokens.length;
+};