Skip to content

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

feat: add tokenization for gpt4o (#2048)

Marc Klingenc2463cb91ead
diff --git a/packages/shared/prisma/migrations/20240512155021_add_pricing_gpt4o/migration.sql b/packages/shared/prisma/migrations/20240512155021_add_pricing_gpt4o/migration.sql
index 45606ebab9..87d8e1ea0b 100644
--- a/packages/shared/prisma/migrations/20240512155021_add_pricing_gpt4o/migration.sql
+++ b/packages/shared/prisma/migrations/20240512155021_add_pricing_gpt4o/migration.sql
@@ -13,10 +13,8 @@ INSERT INTO models (
)
VALUES
-- gpt-4o
- -- tokenizer_config to be added when js-tiktoken is upgraded, gpt-4o is not supported yet
- ('b9854a5c92dc496b997d99d20', NULL, 'gpt-4o', '(?i)^(gpt-4o)$', NULL, 0.000005, 0.000015, NULL, 'TOKENS', 'openai', NULL),
+ ('b9854a5c92dc496b997d99d20', NULL, 'gpt-4o', '(?i)^(gpt-4o)$', NULL, 0.000005, 0.000015, NULL, 'TOKENS', 'openai', '{ "tokensPerMessage": 3, "tokensPerName": 1, "tokenizerModel": "gpt-4o" }'),
-- gpt-4o-2024-05-13
- -- tokenizer_config to be added when js-tiktoken is upgraded, gpt-4o is not supported yet
- ('b9854a5c92dc496b997d99d21', NULL, 'gpt-4o-2024-05-13', '(?i)^(gpt-4o-2024-05-13)$', NULL, 0.000005, 0.000015, NULL, 'TOKENS', 'openai', NULL)
+ ('b9854a5c92dc496b997d99d21', NULL, 'gpt-4o-2024-05-13', '(?i)^(gpt-4o-2024-05-13)$', NULL, 0.000005, 0.000015, NULL, 'TOKENS', 'openai', '{ "tokensPerMessage": 3, "tokensPerName": 1, "tokenizerModel": "gpt-4o-2024-05-13" }')
\ No newline at end of file
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 57d2dab458..d73e3909ff 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -130,7 +130,7 @@ importers:
version: 0.27.3
langchain:
specifier: ^0.1.36
- version: 0.1.36(@aws-sdk/[email protected])(@aws-sdk/[email protected])([email protected])
+ version: 0.1.36
prisma-extension-kysely:
specifier: ^2.1.0
version: 2.1.0(@prisma/[email protected])
@@ -406,8 +406,8 @@ importers:
specifier: ^3.1.1
version: 3.1.1
js-tiktoken:
- specifier: ^1.0.11
- version: 1.0.11
+ specifier: ^1.0.12
+ version: 1.0.12
kysely:
specifier: ^0.27.3
version: 0.27.3
@@ -3288,7 +3288,7 @@ packages:
engines: {node: '>=18'}
dependencies:
'@langchain/core': 0.1.61
- js-tiktoken: 1.0.11
+ js-tiktoken: 1.0.12
dev: false
@@ -12010,6 +12010,12 @@ packages:
base64-js: 1.5.1
dev: false
+ resolution: {integrity: sha512-L7wURW1fH9Qaext0VzaUDpFGVQgjkdE3Dgsy9/+yXyGEpBKnylTd0mU0bfbNkKDlXRb6TEsZkwuflu1B8uQbJQ==}
+ dependencies:
+ base64-js: 1.5.1
+ dev: false
+
resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}
@@ -12209,6 +12215,256 @@ packages:
resolution: {integrity: sha512-lG03Ru+XyOJFsjH3OMY6R/9U38IjDPfnOfDgO3ynhbDr+Dz8fak+X6L62vqu3iybQnj+lG84OttBuU9KY3L9kA==}
engines: {node: '>=14.0.0'}
+ resolution: {integrity: sha512-NTbnCL/jKWIeEI//Nm1oG8nhW3vkYWvEMr1MPotmTThTfeKfO87eV/OAzAyh6Ruy6GFs/qofRgQZGIe6XvXTNQ==}
+ engines: {node: '>=18'}
+ peerDependencies:
+ '@aws-sdk/client-s3': ^3.310.0
+ '@aws-sdk/client-sagemaker-runtime': ^3.310.0
+ '@aws-sdk/client-sfn': ^3.310.0
+ '@aws-sdk/credential-provider-node': ^3.388.0
+ '@azure/storage-blob': ^12.15.0
+ '@gomomento/sdk': ^1.51.1
+ '@gomomento/sdk-core': ^1.51.1
+ '@gomomento/sdk-web': ^1.51.1
+ '@google-ai/generativelanguage': ^0.2.1
+ '@google-cloud/storage': ^6.10.1 || ^7.7.0
+ '@mendable/firecrawl-js': ^0.0.13
+ '@notionhq/client': ^2.2.10
+ '@pinecone-database/pinecone': '*'
+ '@supabase/supabase-js': ^2.10.0
+ '@vercel/kv': ^0.2.3
+ '@xata.io/client': ^0.28.0
+ apify-client: ^2.7.1
+ assemblyai: ^4.0.0
+ axios: '*'
+ cheerio: ^1.0.0-rc.12
+ chromadb: '*'
+ convex: ^1.3.1
+ couchbase: ^4.3.0
+ d3-dsv: ^2.0.0
+ epub2: ^3.0.1
+ faiss-node: '*'
+ fast-xml-parser: '*'
+ google-auth-library: ^8.9.0
+ handlebars: ^4.7.8
+ html-to-text: ^9.0.5
+ ignore: ^5.2.0
+ ioredis: ^5.3.2
+ jsdom: '*'
+ mammoth: ^1.6.0
+ mongodb: '>=5.2.0'
+ node-llama-cpp: '*'
+ notion-to-md: ^3.1.0
+ officeparser: ^4.0.4
+ pdf-parse: 1.1.1
+ peggy: ^3.0.2
+ playwright: ^1.32.1
+ puppeteer: ^19.7.2
+ pyodide: ^0.24.1
+ redis: ^4.6.4
+ sonix-speech-recognition: ^2.1.1
+ srt-parser-2: ^1.2.3
+ typeorm: ^0.3.12
+ weaviate-ts-client: '*'
+ web-auth-library: ^1.0.3
+ ws: ^8.14.2
+ youtube-transcript: ^1.0.6
+ youtubei.js: ^9.1.0
+ peerDependenciesMeta:
+ '@aws-sdk/client-s3':
+ optional: true
+ '@aws-sdk/client-sagemaker-runtime':
+ optional: true
+ '@aws-sdk/client-sfn':
+ optional: true
+ '@aws-sdk/credential-provider-node':
+ optional: true
+ '@azure/storage-blob':
+ optional: true
+ '@gomomento/sdk':
+ optional: true
+ '@gomomento/sdk-core':
+ optional: true
+ '@gomomento/sdk-web':
+ optional: true
+ '@google-ai/generativelanguage':
+ optional: true
+ '@google-cloud/storage':
+ optional: true
+ '@mendable/firecrawl-js':
+ optional: true
+ '@notionhq/client':
+ optional: true
+ '@pinecone-database/pinecone':
+ optional: true
+ '@supabase/supabase-js':
+ optional: true
+ '@vercel/kv':
+ optional: true
+ '@xata.io/client':
+ optional: true
+ apify-client:
+ optional: true
+ assemblyai:
+ optional: true
+ axios:
+ optional: true
+ cheerio:
+ optional: true
+ chromadb:
+ optional: true
+ convex:
+ optional: true
+ couchbase:
+ optional: true
+ d3-dsv:
+ optional: true
+ epub2:
+ optional: true
+ faiss-node:
+ optional: true
+ fast-xml-parser:
+ optional: true
+ google-auth-library:
+ optional: true
+ handlebars:
+ optional: true
+ html-to-text:
+ optional: true
+ ignore:
+ optional: true
+ ioredis:
+ optional: true
+ jsdom:
+ optional: true
+ mammoth:
+ optional: true
+ mongodb:
+ optional: true
+ node-llama-cpp:
+ optional: true
+ notion-to-md:
+ optional: true
+ officeparser:
+ optional: true
+ pdf-parse:
+ optional: true
+ peggy:
+ optional: true
+ playwright:
+ optional: true
+ puppeteer:
+ optional: true
+ pyodide:
+ optional: true
+ redis:
+ optional: true
+ sonix-speech-recognition:
+ optional: true
+ srt-parser-2:
+ optional: true
+ typeorm:
+ optional: true
+ weaviate-ts-client:
+ optional: true
+ web-auth-library:
+ optional: true
+ ws:
+ optional: true
+ youtube-transcript:
+ optional: true
+ youtubei.js:
+ optional: true
+ dependencies:
+ '@anthropic-ai/sdk': 0.9.1
+ '@langchain/community': 0.0.47(@aws-sdk/[email protected])([email protected])
+ '@langchain/core': 0.1.61
+ '@langchain/openai': 0.0.28
+ '@langchain/textsplitters': 0.0.0
+ binary-extensions: 2.3.0
+ js-tiktoken: 1.0.11
+ js-yaml: 4.1.0
+ jsonpointer: 5.0.1
+ langchainhub: 0.0.8
+ langsmith: 0.1.14
+ ml-distance: 4.0.1
+ openapi-types: 12.1.3
+ p-retry: 4.6.2
+ uuid: 9.0.1
+ yaml: 2.4.1
+ zod: 3.22.4
+ zod-to-json-schema: 3.22.5([email protected])
+ transitivePeerDependencies:
+ - '@aws-crypto/sha256-js'
+ - '@aws-sdk/client-bedrock-agent-runtime'
+ - '@aws-sdk/client-bedrock-runtime'
+ - '@aws-sdk/client-dynamodb'
+ - '@aws-sdk/client-kendra'
+ - '@aws-sdk/client-lambda'
+ - '@azure/search-documents'
+ - '@clickhouse/client'
+ - '@cloudflare/ai'
+ - '@datastax/astra-db-ts'
+ - '@elastic/elasticsearch'
+ - '@getmetal/metal-sdk'
+ - '@getzep/zep-js'
+ - '@gradientai/nodejs-sdk'
+ - '@huggingface/inference'
+ - '@mozilla/readability'
+ - '@opensearch-project/opensearch'
+ - '@planetscale/database'
+ - '@premai/prem-sdk'
+ - '@qdrant/js-client-rest'
+ - '@raycast/api'
+ - '@rockset/client'
+ - '@smithy/eventstream-codec'
+ - '@smithy/protocol-http'
+ - '@smithy/signature-v4'
+ - '@smithy/util-utf8'
+ - '@supabase/postgrest-js'
+ - '@tensorflow-models/universal-sentence-encoder'
+ - '@tensorflow/tfjs-converter'
+ - '@tensorflow/tfjs-core'
+ - '@upstash/redis'
+ - '@upstash/vector'
+ - '@vercel/postgres'
+ - '@writerai/writer-sdk'
+ - '@xenova/transformers'
+ - '@zilliz/milvus2-sdk-node'
+ - better-sqlite3
+ - cassandra-driver
+ - cborg
+ - closevector-common
+ - closevector-node
+ - closevector-web
+ - cohere-ai
+ - discord.js
+ - dria
+ - duck-duck-scrape
+ - encoding
+ - firebase-admin
+ - googleapis
+ - hnswlib-node
+ - interface-datastore
+ - it-all
+ - jsonwebtoken
+ - llmonitor
+ - lodash
+ - lunary
+ - mysql2
+ - neo4j-driver
+ - pg
+ - pg-copy-streams
+ - pickleparser
+ - portkey-ai
+ - replicate
+ - typesense
+ - usearch
+ - vectordb
+ - voy-search
+ dev: false
+
resolution: {integrity: sha512-NTbnCL/jKWIeEI//Nm1oG8nhW3vkYWvEMr1MPotmTThTfeKfO87eV/OAzAyh6Ruy6GFs/qofRgQZGIe6XvXTNQ==}
engines: {node: '>=18'}
@@ -12379,7 +12635,7 @@ packages:
'@langchain/openai': 0.0.28
'@langchain/textsplitters': 0.0.0
binary-extensions: 2.3.0
- js-tiktoken: 1.0.11
+ js-tiktoken: 1.0.12
js-yaml: 4.1.0
jsonpointer: 5.0.1
langchainhub: 0.0.8
diff --git a/web/package.json b/web/package.json
index 71f82eb607..ad4df9b180 100644
--- a/web/package.json
+++ b/web/package.json
@@ -86,7 +86,7 @@
"decimal.js": "^10.4.3",
"dompurify": "^3.1.2",
"exponential-backoff": "^3.1.1",
- "js-tiktoken": "^1.0.11",
+ "js-tiktoken": "^1.0.12",
"kysely": "^0.27.3",
"langchain": "^0.1.36",
"lodash": "^4.17.21",
diff --git a/web/src/__tests__/model-match.servertest.ts b/web/src/__tests__/model-match.servertest.ts
index a781a92323..f017828ee2 100644
--- a/web/src/__tests__/model-match.servertest.ts
+++ b/web/src/__tests__/model-match.servertest.ts
@@ -73,6 +73,15 @@ describe("model match", () => {
input: "I am a prompt",
output: "I am a completion",
},
+ {
+ type: "GENERATION",
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ model: "gpt-4o",
+ startTime: new Date("2024-01-01T00:00:00.000Z"),
+ unit: ModelUsageUnit.Tokens,
+ input: "I am a prompt",
+ output: "I am a completion",
+ },
],
});
@@ -86,7 +95,7 @@ describe("model match", () => {
expect(observation1?.completionTokens).toEqual(3000);
expect(observation1?.totalTokens).toEqual(0);
- expect(observations.length).toEqual(3);
+ expect(observations.length).toEqual(4);
observations.forEach((observation) => {
expect(observation.internalModel).toBeDefined();
expect(observation.promptTokens).toBeGreaterThan(0);
diff --git a/web/src/__tests__/usage.servertest.ts b/web/src/__tests__/usage.servertest.ts
index c1691ca3d3..74e0978d0c 100644
--- a/web/src/__tests__/usage.servertest.ts
+++ b/web/src/__tests__/usage.servertest.ts
@@ -33,6 +33,7 @@ describe("Token Count Functions", () => {
{ model: "claude", tokenizer: "claude", tokens: 118 },
{ model: "claude-instant-1.2", tokenizer: "claude", tokens: 118 },
{ model: "gpt-3.5-turbo-1106", tokenizer: "openai", tokens: 114 },
+ { model: "gpt-4o-2024-05-13", tokenizer: "openai", tokens: 112 },
].forEach(({ model, tokens, tokenizer }) => {
it(`should return token count ${tokens} for ${model}`, () => {
const result = tokenCount({
diff --git a/web/src/utils/types.ts b/web/src/utils/types.ts
index 67faaa2b5d..5ce20ef3ed 100644
--- a/web/src/utils/types.ts
+++ b/web/src/utils/types.ts
@@ -70,6 +70,8 @@ const chatModels = [
"gpt-3.5-turbo-16k-0613",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
+ "gpt-4o-2024-05-13",
+ "gpt-4o",
];
export type ChatModel = (typeof chatModels)[number];
@@ -128,5 +130,7 @@ export const isTiktokenModel = (model: string): model is TiktokenModel => {
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4-turbo-2024-04-09",
+ "gpt-4o-2024-05-13",
+ "gpt-4o",
].includes(model);
};