Skip to content

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

chore: update cost calculation to handle null values from DB (#4057)

Steffen Schmitz20ed40510a1b
diff --git a/packages/shared/src/server/repositories/definitions.ts b/packages/shared/src/server/repositories/definitions.ts
index 31a7e69aae..c63ddbf4ab 100644
--- a/packages/shared/src/server/repositories/definitions.ts
+++ b/packages/shared/src/server/repositories/definitions.ts
@@ -9,10 +9,26 @@ export const clickhouseStringDateSchema = z
//https://clickhouse.com/docs/en/integrations/javascript#integral-types-int64-int128-int256-uint64-uint128-uint256
// clickhouse returns int64 as string
-export const UsageCostStringSchema = z.record(z.string(), z.string());
-export const UsageCostNumberSchema = z.record(z.string(), z.coerce.number());
-export type UsageCostStringType = z.infer<typeof UsageCostStringSchema>;
-export type UsageCostNumberType = z.infer<typeof UsageCostNumberSchema>;
+export const UsageCostSchema = z
+ .record(z.string(), z.coerce.string().nullable())
+ .transform((val, ctx) => {
+ const result: Record<string, number> = {};
+ for (const key in val) {
+ if (val[key] !== null && val[key] !== undefined) {
+ const parsed = Number(val[key]);
+ if (isNaN(parsed)) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ message: `Key ${key} is not a number`,
+ });
+ } else {
+ result[key] = parsed;
+ }
+ }
+ }
+ return result;
+ });
+export type UsageCostType = z.infer<typeof UsageCostSchema>;
export const observationRecordBaseSchema = z.object({
id: z.string(),
@@ -47,10 +63,10 @@ export const observationRecordReadSchema = observationRecordBaseSchema.extend({
end_time: clickhouseStringDateSchema.nullish(),
completion_start_time: clickhouseStringDateSchema.nullish(),
event_ts: clickhouseStringDateSchema,
- provided_usage_details: UsageCostStringSchema,
- provided_cost_details: UsageCostNumberSchema,
- usage_details: UsageCostStringSchema,
- cost_details: UsageCostNumberSchema,
+ provided_usage_details: UsageCostSchema,
+ provided_cost_details: UsageCostSchema,
+ usage_details: UsageCostSchema,
+ cost_details: UsageCostSchema,
});
export type ObservationRecordReadType = z.infer<
typeof observationRecordReadSchema
@@ -64,10 +80,10 @@ export const observationRecordInsertSchema = observationRecordBaseSchema.extend(
end_time: z.number().nullish(),
completion_start_time: z.number().nullish(),
event_ts: z.number(),
- provided_usage_details: UsageCostNumberSchema,
- provided_cost_details: UsageCostNumberSchema,
- usage_details: UsageCostNumberSchema,
- cost_details: UsageCostNumberSchema,
+ provided_usage_details: UsageCostSchema,
+ provided_cost_details: UsageCostSchema,
+ usage_details: UsageCostSchema,
+ cost_details: UsageCostSchema,
},
);
export type ObservationRecordInsertType = z.infer<
@@ -159,11 +175,6 @@ export const convertObservationReadToInsert = (
): ObservationRecordInsertType => {
const convertDate = (date: string) => new Date(date).getTime();
- const convertDetails = (details: Record<string, string>) =>
- Object.fromEntries(
- Object.entries(details).map(([key, value]) => [key, Number(value)]),
- );
-
return {
...record,
created_at: convertDate(record.created_at),
@@ -174,9 +185,9 @@ export const convertObservationReadToInsert = (
? convertDate(record.completion_start_time)
: undefined,
event_ts: convertDate(record.event_ts),
- provided_usage_details: convertDetails(record.provided_usage_details),
+ provided_usage_details: record.provided_usage_details,
provided_cost_details: record.provided_cost_details,
- usage_details: convertDetails(record.usage_details),
+ usage_details: record.usage_details,
cost_details: record.cost_details,
};
};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 5047631f57..abf6dc3d84 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -101,7 +101,7 @@ importers:
'@vercel/style-guide':
specifier: ^6.0.0
eslint-config-next:
specifier: ^14.2.15
@@ -572,7 +572,7 @@ importers:
version: 0.27.4
langchain:
specifier: ^0.3.2
lodash:
specifier: ^4.17.21
version: 4.17.21
@@ -688,7 +688,7 @@ importers:
'@testing-library/jest-dom':
specifier: ^6.4.6
'@testing-library/react':
specifier: ^15.0.7
@@ -945,9 +945,6 @@ importers:
prettier:
specifier: ^3.3.3
version: 3.3.3
- rus-diff:
- specifier: ^1.1.0
- version: 1.1.0
ts-node:
specifier: ^10.9.2
version: 10.9.2(@types/[email protected])([email protected])
@@ -8100,9 +8097,6 @@ packages:
resolution: {integrity: sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==}
- resolution: {integrity: sha512-lL0pLS/jguRLPZZZUEoNNoOxXtBHiM+iNsaTbPG0W8s/HpGuRNOw+LztiYaSo6Q8YWShEyIM++jM1jvvNcl0tw==}
-
resolution: {integrity: sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==}
@@ -10041,9 +10035,6 @@ packages:
resolution: {integrity: sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==}
- resolution: {integrity: sha512-h2SBIrpYQ0eevfvghEA67QQbc3fD2KJl/aERQuvi8AF8g0FSvZpSEwIeJgcoBDteFB5TmXtGkw6aLn5uhMYxYw==}
-
resolution: {integrity: sha512-cLgakCUf6PedEu15t8kbsjnwIFFR2D4RfL+W3iWFJ4iac7z4B0ZI8fxy4R3J956kAI68HclCFGL8MPoUVC3qVA==}
@@ -11620,7 +11611,7 @@ snapshots:
'@aws-sdk/client-sso-oidc': 3.675.0(@aws-sdk/[email protected])
'@aws-sdk/client-sts': 3.675.0
'@aws-sdk/core': 3.667.0
- '@aws-sdk/credential-provider-node': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
+ '@aws-sdk/credential-provider-node': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
'@aws-sdk/middleware-host-header': 3.667.0
'@aws-sdk/middleware-logger': 3.667.0
'@aws-sdk/middleware-recursion-detection': 3.667.0
@@ -11716,7 +11707,7 @@ snapshots:
'@aws-sdk/client-sso-oidc': 3.675.0(@aws-sdk/[email protected])
'@aws-sdk/client-sts': 3.675.0
'@aws-sdk/core': 3.667.0
- '@aws-sdk/credential-provider-node': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
+ '@aws-sdk/credential-provider-node': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
'@aws-sdk/middleware-bucket-endpoint': 3.667.0
'@aws-sdk/middleware-expect-continue': 3.667.0
'@aws-sdk/middleware-flexible-checksums': 3.669.0
@@ -12112,7 +12103,7 @@ snapshots:
'@aws-sdk/credential-provider-env': 3.667.0
'@aws-sdk/credential-provider-http': 3.667.0
'@aws-sdk/credential-provider-process': 3.667.0
- '@aws-sdk/credential-provider-sso': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/credential-provider-sso': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
'@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
'@aws-sdk/types': 3.667.0
'@smithy/credential-provider-imds': 3.2.4
@@ -12124,14 +12115,14 @@ snapshots:
- '@aws-sdk/client-sso-oidc'
- aws-crt
dependencies:
'@aws-sdk/client-sts': 3.675.0
'@aws-sdk/core': 3.667.0
'@aws-sdk/credential-provider-env': 3.667.0
'@aws-sdk/credential-provider-http': 3.667.0
'@aws-sdk/credential-provider-process': 3.667.0
- '@aws-sdk/credential-provider-sso': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/credential-provider-sso': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
'@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
'@aws-sdk/types': 3.667.0
'@smithy/credential-provider-imds': 3.2.4
@@ -12142,15 +12133,17 @@ snapshots:
transitivePeerDependencies:
- '@aws-sdk/client-sso-oidc'
- aws-crt
+ optional: true
- '@aws-sdk/[email protected]':
dependencies:
+ '@aws-sdk/client-sts': 3.675.0
+ '@aws-sdk/core': 3.667.0
'@aws-sdk/credential-provider-env': 3.667.0
'@aws-sdk/credential-provider-http': 3.667.0
- '@aws-sdk/credential-provider-ini': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
'@aws-sdk/credential-provider-process': 3.667.0
- '@aws-sdk/credential-provider-sso': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
- '@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
+ '@aws-sdk/credential-provider-sso': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
'@aws-sdk/types': 3.667.0
'@smithy/credential-provider-imds': 3.2.4
'@smithy/property-provider': 3.1.7
@@ -12159,9 +12152,7 @@ snapshots:
tslib: 2.6.3
transitivePeerDependencies:
- '@aws-sdk/client-sso-oidc'
- - '@aws-sdk/client-sts'
- aws-crt
- optional: true
dependencies:
@@ -12188,7 +12179,7 @@ snapshots:
'@aws-sdk/credential-provider-http': 3.667.0
'@aws-sdk/credential-provider-ini': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
'@aws-sdk/credential-provider-process': 3.667.0
- '@aws-sdk/credential-provider-sso': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/credential-provider-sso': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
'@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
'@aws-sdk/types': 3.667.0
'@smithy/credential-provider-imds': 3.2.4
@@ -12201,13 +12192,52 @@ snapshots:
- '@aws-sdk/client-sts'
- aws-crt
+ dependencies:
+ '@aws-sdk/credential-provider-env': 3.667.0
+ '@aws-sdk/credential-provider-http': 3.667.0
+ '@aws-sdk/credential-provider-ini': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
+ '@aws-sdk/credential-provider-process': 3.667.0
+ '@aws-sdk/credential-provider-sso': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
+ '@aws-sdk/types': 3.667.0
+ '@smithy/credential-provider-imds': 3.2.4
+ '@smithy/property-provider': 3.1.7
+ '@smithy/shared-ini-file-loader': 3.1.8
+ '@smithy/types': 3.5.0
+ tslib: 2.6.3
+ transitivePeerDependencies:
+ - '@aws-sdk/client-sso-oidc'
+ - '@aws-sdk/client-sts'
+ - aws-crt
+ optional: true
+
+ dependencies:
+ '@aws-sdk/credential-provider-env': 3.667.0
+ '@aws-sdk/credential-provider-http': 3.667.0
+ '@aws-sdk/credential-provider-ini': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
+ '@aws-sdk/credential-provider-process': 3.667.0
+ '@aws-sdk/credential-provider-sso': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
+ '@aws-sdk/types': 3.667.0
+ '@smithy/credential-provider-imds': 3.2.4
+ '@smithy/property-provider': 3.1.7
+ '@smithy/shared-ini-file-loader': 3.1.8
+ '@smithy/types': 3.5.0
+ tslib: 2.6.3
+ transitivePeerDependencies:
+ - '@aws-sdk/client-sso-oidc'
+ - '@aws-sdk/client-sts'
+ - aws-crt
+
dependencies:
'@aws-sdk/credential-provider-env': 3.667.0
'@aws-sdk/credential-provider-http': 3.667.0
'@aws-sdk/credential-provider-ini': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
'@aws-sdk/credential-provider-process': 3.667.0
- '@aws-sdk/credential-provider-sso': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/credential-provider-sso': 3.675.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
'@aws-sdk/credential-provider-web-identity': 3.667.0(@aws-sdk/[email protected])
'@aws-sdk/types': 3.667.0
'@smithy/credential-provider-imds': 3.2.4
@@ -12243,11 +12273,11 @@ snapshots:
- '@aws-sdk/client-sso-oidc'
- aws-crt
- '@aws-sdk/[email protected](@aws-sdk/[email protected](@aws-sdk/[email protected]))':
+ '@aws-sdk/[email protected](@aws-sdk/[email protected](@aws-sdk/[email protected]))':
dependencies:
'@aws-sdk/client-sso': 3.668.0
'@aws-sdk/core': 3.667.0
- '@aws-sdk/token-providers': 3.667.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/token-providers': 3.667.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
'@aws-sdk/types': 3.667.0
'@smithy/property-provider': 3.1.7
'@smithy/shared-ini-file-loader': 3.1.8
@@ -12257,11 +12287,11 @@ snapshots:
- '@aws-sdk/client-sso-oidc'
- aws-crt
- '@aws-sdk/[email protected](@aws-sdk/[email protected](@aws-sdk/[email protected]))':
+ '@aws-sdk/[email protected](@aws-sdk/[email protected](@aws-sdk/[email protected]))':
dependencies:
'@aws-sdk/client-sso': 3.675.0
'@aws-sdk/core': 3.667.0
- '@aws-sdk/token-providers': 3.667.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
+ '@aws-sdk/token-providers': 3.667.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))
'@aws-sdk/types': 3.667.0
'@smithy/property-provider': 3.1.7
'@smithy/shared-ini-file-loader': 3.1.8
@@ -12464,7 +12494,7 @@ snapshots:
'@smithy/types': 3.5.0
tslib: 2.6.3
- '@aws-sdk/[email protected](@aws-sdk/[email protected](@aws-sdk/[email protected]))':
+ '@aws-sdk/[email protected](@aws-sdk/[email protected](@aws-sdk/[email protected]))':
dependencies:
'@aws-sdk/client-sso-oidc': 3.675.0(@aws-sdk/[email protected])
'@aws-sdk/types': 3.667.0
@@ -13481,42 +13511,6 @@ snapshots:
jest-util: 29.7.0
slash: 3.0.0
- dependencies:
- '@jest/console': 29.7.0
- '@jest/reporters': 29.7.0
- '@jest/test-result': 29.7.0
- '@jest/transform': 29.7.0
- '@jest/types': 29.6.3
- '@types/node': 20.14.8
- ansi-escapes: 4.3.2
- chalk: 4.1.2
- ci-info: 3.9.0
- exit: 0.1.2
- graceful-fs: 4.2.11
- jest-changed-files: 29.7.0
- jest-config: 29.7.0(@types/[email protected])
- jest-haste-map: 29.7.0
- jest-message-util: 29.7.0
- jest-regex-util: 29.6.3
- jest-resolve: 29.7.0
- jest-resolve-dependencies: 29.7.0
- jest-runner: 29.7.0
- jest-runtime: 29.7.0
- jest-snapshot: 29.7.0
- jest-util: 29.7.0
- jest-validate: 29.7.0
- jest-watcher: 29.7.0
- micromatch: 4.0.8
- pretty-format: 29.7.0
- slash: 3.0.0
- strip-ansi: 6.0.1
- transitivePeerDependencies:
- - babel-plugin-macros
- - supports-color
- - ts-node
- optional: true
-
dependencies:
'@jest/console': 29.7.0
@@ -13731,12 +13725,12 @@ snapshots:
- '@aws-sdk/client-sts'
- aws-crt
dependencies:
'@aws-sdk/client-bedrock-agent-runtime': 3.668.0
'@aws-sdk/client-bedrock-runtime': 3.668.0
'@aws-sdk/client-kendra': 3.668.0
- '@aws-sdk/credential-provider-node': 3.668.0
+ '@aws-sdk/credential-provider-node': 3.668.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])
'@langchain/core': 0.3.9([email protected]([email protected]))
zod: 3.23.8
zod-to-json-schema: 3.23.5([email protected])
@@ -16388,7 +16382,7 @@ snapshots:
lz-string: 1.5.0
pretty-format: 27.5.1
dependencies:
'@adobe/css-tools': 4.4.0
'@babel/runtime': 7.24.7
@@ -16402,7 +16396,7 @@ snapshots:
'@jest/globals': 29.7.0
'@types/jest': 29.5.12
- vitest: 2.1.2(@types/[email protected])
dependencies:
@@ -17054,7 +17048,7 @@ snapshots:
'@ungap/[email protected]': {}
dependencies:
'@babel/core': 7.24.3
'@babel/eslint-parser': 7.24.1(@babel/[email protected])([email protected])
@@ -17063,18 +17057,18 @@ snapshots:
'@typescript-eslint/parser': 7.12.0([email protected])([email protected])
eslint-config-prettier: 9.1.0([email protected])
- eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])
eslint-plugin-eslint-comments: 3.2.0([email protected])
eslint-plugin-jsx-a11y: 6.8.0([email protected])
eslint-plugin-react: 7.34.1([email protected])
eslint-plugin-react-hooks: 4.6.0([email protected])
eslint-plugin-testing-library: 6.2.0([email protected])([email protected])
eslint-plugin-tsdoc: 0.2.17
eslint-plugin-unicorn: 51.0.1([email protected])
prettier-plugin-packagejson: 2.4.12([email protected])
optionalDependencies:
'@next/eslint-plugin-next': 14.2.15
@@ -17095,6 +17089,16 @@ snapshots:
chai: 5.1.1
tinyrainbow: 1.2.0
+ dependencies:
+ '@vitest/spy': 2.1.2
+ estree-walker: 3.0.3
+ magic-string: 0.30.11
+ optionalDependencies:
+ msw: 2.4.9([email protected])
+ vite: 5.2.14(@types/[email protected])([email protected])
+ optional: true
+
dependencies:
'@vitest/spy': 2.1.2
@@ -17104,13 +17108,14 @@ snapshots:
msw: 2.4.9([email protected])
vite: 5.2.14(@types/[email protected])([email protected])
dependencies:
'@vitest/spy': 2.1.2
estree-walker: 3.0.3
magic-string: 0.30.11
optionalDependencies:
- vite: 5.2.14(@types/[email protected])
+ msw: 2.4.9([email protected])
+ vite: 5.2.14(@types/[email protected])([email protected])
optional: true
'@vitest/[email protected]':
@@ -18141,13 +18146,13 @@ snapshots:
crc-32: 1.2.2
readable-stream: 3.6.2
dependencies:
'@jest/types': 29.6.3
chalk: 4.1.2
exit: 0.1.2
graceful-fs: 4.2.11
- jest-config: 29.7.0(@types/[email protected])
jest-util: 29.7.0
prompts: 2.4.2
transitivePeerDependencies:
@@ -18155,15 +18160,14 @@ snapshots:
- babel-plugin-macros
- supports-color
- ts-node
- optional: true
dependencies:
'@jest/types': 29.6.3
chalk: 4.1.2
exit: 0.1.2
graceful-fs: 4.2.11
jest-util: 29.7.0
prompts: 2.4.2
transitivePeerDependencies:
@@ -18171,6 +18175,7 @@ snapshots:
- babel-plugin-macros
- supports-color
- ts-node
+ optional: true
@@ -18934,7 +18939,7 @@ snapshots:
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])([email protected])
eslint-plugin-jsx-a11y: 6.8.0([email protected])
eslint-plugin-react: 7.34.1([email protected])
eslint-plugin-react-hooks: 4.6.0([email protected])
@@ -18951,7 +18956,7 @@ snapshots:
dependencies:
eslint: 8.57.0
- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])
eslint-plugin-n: 16.6.2([email protected])
eslint-plugin-promise: 6.4.0([email protected])
@@ -18962,7 +18967,7 @@ snapshots:
dependencies:
dependencies:
@@ -18978,7 +18983,7 @@ snapshots:
enhanced-resolve: 5.17.1
eslint: 8.57.0
fast-glob: 3.3.2
get-tsconfig: 4.8.1
is-core-module: 2.15.1
@@ -18989,13 +18994,13 @@ snapshots:
- eslint-import-resolver-webpack
- supports-color
dependencies:
debug: 4.3.7([email protected])
enhanced-resolve: 5.17.1
eslint: 8.57.0
fast-glob: 3.3.2
get-tsconfig: 4.8.1
is-core-module: 2.15.1
@@ -19017,14 +19022,14 @@ snapshots:
transitivePeerDependencies:
- supports-color
dependencies:
debug: 3.2.7
optionalDependencies:
'@typescript-eslint/parser': 7.12.0([email protected])([email protected])
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
- eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])
transitivePeerDependencies:
- supports-color
@@ -19041,7 +19046,7 @@ snapshots:
eslint: 8.57.0
ignore: 5.3.1
dependencies:
array-includes: 3.1.7
array.prototype.findlastindex: 1.2.4
@@ -19051,7 +19056,7 @@ snapshots:
doctrine: 2.1.0
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
hasown: 2.0.2
is-core-module: 2.15.1
is-glob: 4.0.3
@@ -19068,40 +19073,13 @@ snapshots:
- eslint-import-resolver-webpack
- supports-color
- dependencies:
- array-includes: 3.1.7
- array.prototype.findlastindex: 1.2.4
- array.prototype.flat: 1.3.2
- array.prototype.flatmap: 1.3.2
- debug: 3.2.7
- doctrine: 2.1.0
- eslint: 8.57.0
- eslint-import-resolver-node: 0.3.9
- hasown: 2.0.2
- is-core-module: 2.15.1
- is-glob: 4.0.3
- minimatch: 3.1.2
- object.fromentries: 2.0.8
- object.groupby: 1.0.3
- object.values: 1.2.0
- semver: 6.3.1
- tsconfig-paths: 3.15.0
- optionalDependencies:
- '@typescript-eslint/parser': 7.12.0([email protected])([email protected])
- transitivePeerDependencies:
- - eslint-import-resolver-typescript
- - eslint-import-resolver-webpack
- - supports-color
-
dependencies:
'@typescript-eslint/utils': 5.62.0([email protected])([email protected])
eslint: 8.57.0
optionalDependencies:
'@typescript-eslint/eslint-plugin': 7.3.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])
- jest: 29.7.0
+ jest: 29.7.0(@types/[email protected])([email protected])
transitivePeerDependencies:
- supports-color
- typescript
@@ -19143,12 +19121,12 @@ snapshots:
dependencies:
eslint: 8.57.0
globals: 13.24.0
optionalDependencies:
dependencies:
@@ -19230,13 +19208,13 @@ snapshots:
transitivePeerDependencies:
- supports-color
dependencies:
'@typescript-eslint/utils': 7.3.1([email protected])([email protected])
eslint: 8.57.0
optionalDependencies:
'@typescript-eslint/eslint-plugin': 7.3.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])
transitivePeerDependencies:
- supports-color
- typescript
@@ -20456,16 +20434,16 @@ snapshots:
- babel-plugin-macros
- supports-color
dependencies:
- '@jest/core': 29.7.0
'@jest/test-result': 29.7.0
'@jest/types': 29.6.3
chalk: 4.1.2
- create-jest: 29.7.0
exit: 0.1.2
import-local: 3.1.0
- jest-config: 29.7.0(@types/[email protected])
jest-util: 29.7.0
jest-validate: 29.7.0
yargs: 17.7.2
@@ -20474,18 +20452,17 @@ snapshots:
- babel-plugin-macros
- supports-color
- ts-node
- optional: true
dependencies:
'@jest/test-result': 29.7.0
'@jest/types': 29.6.3
chalk: 4.1.2
+ create-jest: 29.7.0(@types/[email protected])([email protected])
exit: 0.1.2
import-local: 3.1.0
jest-util: 29.7.0
jest-validate: 29.7.0
yargs: 17.7.2
@@ -20494,6 +20471,7 @@ snapshots:
- babel-plugin-macros
- supports-color
- ts-node
+ optional: true
dependencies:
@@ -20526,37 +20504,6 @@ snapshots:
- babel-plugin-macros
- supports-color
- dependencies:
- '@babel/core': 7.24.3
- '@jest/test-sequencer': 29.7.0
- '@jest/types': 29.6.3
- babel-jest: 29.7.0(@babel/[email protected])
- chalk: 4.1.2
- ci-info: 3.9.0
- deepmerge: 4.3.1
- glob: 7.2.3
- graceful-fs: 4.2.11
- jest-circus: 29.7.0([email protected])
- jest-environment-node: 29.7.0
- jest-get-type: 29.6.3
- jest-regex-util: 29.6.3
- jest-resolve: 29.7.0
- jest-runner: 29.7.0
- jest-util: 29.7.0
- jest-validate: 29.7.0
- micromatch: 4.0.8
- parse-json: 5.2.0
- pretty-format: 29.7.0
- slash: 3.0.0
- strip-json-comments: 3.1.1
- optionalDependencies:
- '@types/node': 20.14.8
- transitivePeerDependencies:
- - babel-plugin-macros
- - supports-color
- optional: true
-
dependencies:
'@babel/core': 7.24.3
@@ -20824,30 +20771,30 @@ snapshots:
merge-stream: 2.0.0
supports-color: 8.1.1
dependencies:
- '@jest/core': 29.7.0
'@jest/types': 29.6.3
import-local: 3.1.0
- jest-cli: 29.7.0
transitivePeerDependencies:
- '@types/node'
- babel-plugin-macros
- supports-color
- ts-node
- optional: true
dependencies:
'@jest/types': 29.6.3
import-local: 3.1.0
+ jest-cli: 29.7.0(@types/[email protected])([email protected])
transitivePeerDependencies:
- '@types/node'
- babel-plugin-macros
- supports-color
- ts-node
+ optional: true
@@ -20929,8 +20876,6 @@ snapshots:
-
@@ -21031,7 +20976,7 @@ snapshots:
- encoding
- openai
dependencies:
'@langchain/core': 0.3.9([email protected]([email protected]))
'@langchain/openai': 0.3.0(@langchain/[email protected]([email protected]([email protected])))
@@ -21048,7 +20993,7 @@ snapshots:
zod-to-json-schema: 3.23.5([email protected])
optionalDependencies:
'@langchain/anthropic': 0.3.1(@langchain/[email protected]([email protected]([email protected])))
- '@langchain/aws': 0.1.0(@langchain/[email protected]([email protected]([email protected])))
+ '@langchain/aws': 0.1.0(@aws-sdk/[email protected](@aws-sdk/[email protected]))(@aws-sdk/[email protected])(@langchain/[email protected]([email protected]([email protected])))
axios: 1.7.7
handlebars: 4.7.8
transitivePeerDependencies:
@@ -23169,10 +23114,6 @@ snapshots:
dependencies:
queue-microtask: 1.2.3
- dependencies:
- json-hash: 1.2.0
-
@@ -24222,12 +24163,12 @@ snapshots:
d3-time: 3.1.0
d3-timer: 3.0.1
dependencies:
cac: 6.7.14
debug: 4.3.7([email protected])
pathe: 1.1.2
- vite: 5.2.14(@types/[email protected])
+ vite: 5.2.14(@types/[email protected])([email protected])
transitivePeerDependencies:
- '@types/node'
- less
@@ -24255,7 +24196,24 @@ snapshots:
- supports-color
- terser
+ dependencies:
+ cac: 6.7.14
+ debug: 4.3.7([email protected])
+ pathe: 1.1.2
+ vite: 5.2.14(@types/[email protected])([email protected])
+ transitivePeerDependencies:
+ - '@types/node'
+ - less
+ - lightningcss
+ - sass
+ - stylus
+ - sugarss
+ - supports-color
+ - terser
+ optional: true
+
dependencies:
esbuild: 0.20.2
postcss: 8.4.47
@@ -24263,6 +24221,7 @@ snapshots:
optionalDependencies:
'@types/node': 20.10.5
fsevents: 2.3.3
+ terser: 5.34.1
optional: true
@@ -24275,10 +24234,21 @@ snapshots:
fsevents: 2.3.3
terser: 5.34.1
+ dependencies:
+ esbuild: 0.20.2
+ postcss: 8.4.47
+ rollup: 4.13.0
+ optionalDependencies:
+ '@types/node': 20.14.8
+ fsevents: 2.3.3
+ terser: 5.34.1
+ optional: true
+
dependencies:
'@vitest/expect': 2.1.2
- '@vitest/mocker': 2.1.2(@vitest/[email protected])([email protected](@types/[email protected]))
'@vitest/pretty-format': 2.1.2
'@vitest/runner': 2.1.2
'@vitest/snapshot': 2.1.2
@@ -24293,11 +24263,12 @@ snapshots:
tinyexec: 0.3.0
tinypool: 1.0.1
tinyrainbow: 1.2.0
- vite: 5.2.14(@types/[email protected])
- vite-node: 2.1.2(@types/[email protected])
+ vite: 5.2.14(@types/[email protected])([email protected])
+ vite-node: 2.1.2(@types/[email protected])([email protected])
why-is-node-running: 2.3.0
optionalDependencies:
'@types/node': 20.10.5
+ jsdom: 20.0.3
transitivePeerDependencies:
- less
- lightningcss
@@ -24343,6 +24314,41 @@ snapshots:
- supports-color
- terser
+ dependencies:
+ '@vitest/expect': 2.1.2
+ '@vitest/pretty-format': 2.1.2
+ '@vitest/runner': 2.1.2
+ '@vitest/snapshot': 2.1.2
+ '@vitest/spy': 2.1.2
+ '@vitest/utils': 2.1.2
+ chai: 5.1.1
+ debug: 4.3.7([email protected])
+ magic-string: 0.30.11
+ pathe: 1.1.2
+ std-env: 3.7.0
+ tinybench: 2.9.0
+ tinyexec: 0.3.0
+ tinypool: 1.0.1
+ tinyrainbow: 1.2.0
+ vite: 5.2.14(@types/[email protected])([email protected])
+ vite-node: 2.1.2(@types/[email protected])([email protected])
+ why-is-node-running: 2.3.0
+ optionalDependencies:
+ '@types/node': 20.14.8
+ jsdom: 20.0.3
+ transitivePeerDependencies:
+ - less
+ - lightningcss
+ - msw
+ - sass
+ - stylus
+ - sugarss
+ - supports-color
+ - terser
+ optional: true
+
diff --git a/worker/package.json b/worker/package.json
index d5a83e342b..8e033dca23 100644
--- a/worker/package.json
+++ b/worker/package.json
@@ -74,7 +74,6 @@
"msw": "^2.4.9",
"nodemon": "^3.1.7",
"prettier": "^3.3.3",
- "rus-diff": "^1.1.0",
"ts-node": "^10.9.2",
"tsc-watch": "^6.2.0",
"tsx": "^4.19.1",
diff --git a/worker/src/scripts/verifyClickhouseRecords/index.ts b/worker/src/scripts/verifyClickhouseRecords/index.ts
index 6ef96f4acf..4d4cd67596 100644
--- a/worker/src/scripts/verifyClickhouseRecords/index.ts
+++ b/worker/src/scripts/verifyClickhouseRecords/index.ts
@@ -7,8 +7,6 @@ import {
clickhouseStringDateSchema,
logger,
} from "@langfuse/shared/src/server";
-// @ts-ignore
-import { diff } from "rus-diff";
const getErrorMessage = (params: {
type: "observation" | "trace" | "score";
@@ -18,14 +16,14 @@ const getErrorMessage = (params: {
pgValue: any;
chValue: any;
}) => {
- const delta = `[${params.projectId}-${params.type}-${params.id}] Mismatch between Postgres and Clickhouse:\n${JSON.stringify(params, null, 2)}\n${JSON.stringify(diff(params.pgValue, params.chValue), null, 2)}`;
+ const delta = `[${params.projectId}-${params.type}-${params.id}] Mismatch between Postgres and Clickhouse:\n${JSON.stringify(params, null, 2)}`;
return delta;
};
// Constants
const LIMIT = 100;
const ITERATIONS = 10;
-const DATE_START = new Date("2024-10-17T00:00:00.000Z");
+const DATE_START = new Date("2024-11-02T00:00:00.000Z");
const DATE_END = new Date(Date.now() - 1000 * 60 * 15); // 15 minutes ago
const TABLE_SAMPLE_RATE = 0.05;
@@ -304,9 +302,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
);
}
- logger.info(
- `Comparing delta for observation ${projectId}-${observationId}, ${JSON.stringify(diff(postgresObservation, clickhouseRecord), null, 2)}`,
- );
+ logger.info(`Comparing delta for observation ${projectId}-${observationId}`);
for (const key in postgresObservation) {
const pgValue = postgresObservation[key];
@@ -333,8 +329,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
case "level":
case "status_message":
case "version":
- case "internal_model_id":
- case "unit": {
+ case "internal_model_id": {
if (pgValue && pgValue !== chValue) {
throw new Error(
getErrorMessage({
@@ -351,6 +346,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {
break;
}
+ case "unit":
+ break;
+
// Start_time cannot be overwritten in CH, so they are allowed to be different
case "start_time": {
if (
@@ -404,10 +402,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
"metadata" in chValue && Object.keys(chValue).length === 1
? parseJsonPrioritised(chValue.metadata)
: Object.fromEntries(
- Object.entries(chValue).map(([k, v]) => [
- k,
- v ? JSON.parse(v as any) : null,
- ]),
+ Object.entries(chValue).map(([k, v]) => [k, v]),
);
if (
@@ -507,7 +502,8 @@ async function verifyClickhouseObservation(postgresObservation: any) {
case "completion_tokens": {
if (
pgValue !== 0 &&
- pgValue !== (clickhouseRecord as any)["output_usage_units"]
+ pgValue.toString() !==
+ (clickhouseRecord as any)["usage_details"]["output"]
) {
throw new Error(
getErrorMessage({
@@ -516,7 +512,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["output_usage_units"],
+ chValue: (clickhouseRecord as any)["usage_details"]["output"],
}),
);
}
@@ -527,7 +523,8 @@ async function verifyClickhouseObservation(postgresObservation: any) {
case "prompt_tokens": {
if (
pgValue !== 0 &&
- pgValue !== (clickhouseRecord as any)["input_usage_units"]
+ pgValue.toString() !==
+ (clickhouseRecord as any)["usage_details"]["input"]
) {
throw new Error(
getErrorMessage({
@@ -536,7 +533,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["input_usage_units"],
+ chValue: (clickhouseRecord as any)["usage_details"]["input"],
}),
);
}
@@ -547,7 +544,8 @@ async function verifyClickhouseObservation(postgresObservation: any) {
case "total_tokens": {
if (
pgValue !== 0 &&
- pgValue !== (clickhouseRecord as any)["total_usage_units"]
+ pgValue.toString() !==
+ (clickhouseRecord as any)["usage_details"]["total"]
) {
throw new Error(
getErrorMessage({
@@ -556,7 +554,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["total_usage_units"],
+ chValue: (clickhouseRecord as any)["usage_details"]["total"],
}),
);
}
@@ -567,8 +565,10 @@ async function verifyClickhouseObservation(postgresObservation: any) {
case "calculated_input_cost": {
if (
pgValue !== null &&
- Math.abs(Number(pgValue) - (clickhouseRecord as any)["input_cost"]) >
- 1e-9
+ Math.abs(
+ Number(pgValue) -
+ (clickhouseRecord as any)["cost_details"]["input"],
+ ) > 1e-9
) {
throw new Error(
getErrorMessage({
@@ -577,7 +577,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["input_cost"],
+ chValue: (clickhouseRecord as any)["cost_details"]["input"],
}),
);
}
@@ -588,8 +588,10 @@ async function verifyClickhouseObservation(postgresObservation: any) {
case "calculated_output_cost": {
if (
pgValue !== null &&
- Math.abs(Number(pgValue) - (clickhouseRecord as any)["output_cost"]) >
- 1e-9
+ Math.abs(
+ Number(pgValue) -
+ (clickhouseRecord as any)["cost_details"]["output"],
+ ) > 1e-9
) {
throw new Error(
getErrorMessage({
@@ -598,7 +600,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["output_cost"],
+ chValue: (clickhouseRecord as any)["cost_details"]["output"],
}),
);
}
@@ -609,8 +611,10 @@ async function verifyClickhouseObservation(postgresObservation: any) {
case "calculated_total_cost": {
if (
pgValue !== null &&
- Math.abs(Number(pgValue) - (clickhouseRecord as any)["total_cost"]) >
- 1e-9
+ Math.abs(
+ Number(pgValue) -
+ (clickhouseRecord as any)["cost_details"]["total"],
+ ) > 1e-9
) {
throw new Error(
getErrorMessage({
@@ -619,7 +623,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["total_cost"],
+ chValue: (clickhouseRecord as any)["cost_details"]["total"],
}),
);
}
@@ -631,7 +635,8 @@ async function verifyClickhouseObservation(postgresObservation: any) {
if (
pgValue !== null &&
Math.abs(
- Number(pgValue) - (clickhouseRecord as any)["provided_input_cost"],
+ Number(pgValue) -
+ (clickhouseRecord as any)["provided_cost_details"]["input"],
) > 1e-9
) {
throw new Error(
@@ -641,7 +646,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["provided_input_cost"],
+ chValue: (clickhouseRecord as any)["provided_cost_details"][
+ "input"
+ ],
}),
);
}
@@ -653,7 +660,8 @@ async function verifyClickhouseObservation(postgresObservation: any) {
if (
pgValue !== null &&
Math.abs(
- Number(pgValue) - (clickhouseRecord as any)["provided_output_cost"],
+ Number(pgValue) -
+ (clickhouseRecord as any)["provided_cost_details"]["output"],
) > 1e-9
) {
throw new Error(
@@ -663,7 +671,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue,
- chValue: (clickhouseRecord as any)["provided_output_cost"],
+ chValue: (clickhouseRecord as any)["provided_cost_details"][
+ "output"
+ ],
}),
);
}
@@ -675,7 +685,8 @@ async function verifyClickhouseObservation(postgresObservation: any) {
if (
pgValue !== null &&
Math.abs(
- Number(pgValue) - (clickhouseRecord as any)["provided_total_cost"],
+ Number(pgValue) -
+ (clickhouseRecord as any)["provided_cost_details"]["total"],
) > 1e-9
) {
throw new Error(
@@ -685,7 +696,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {
type: "observation",
key,
pgValue: Number(pgValue),
- chValue: (clickhouseRecord as any)["provided_total_cost"],
+ chValue: (clickhouseRecord as any)["provided_cost_details"][
+ "total"
+ ],
}),
);
}
@@ -784,10 +797,7 @@ async function verifyClickhouseTrace(postgresTrace: any) {
"metadata" in chValue && Object.keys(chValue).length === 1
? parseJsonPrioritised(chValue.metadata)
: Object.fromEntries(
- Object.entries(chValue).map(([k, v]) => [
- k,
- JSON.parse(v as any),
- ]),
+ Object.entries(chValue).map(([k, v]) => [k, v]),
);
if (
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index b291103436..42987ee87e 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -32,7 +32,7 @@ import {
TraceRecordInsertType,
traceRecordReadSchema,
validateAndInflateScore,
- UsageCostNumberType,
+ UsageCostType,
} from "@langfuse/shared/src/server";
import { tokenCount } from "../../features/tokenisation/usage";
@@ -539,7 +539,7 @@ export class IngestionService {
static calculateUsageCosts(
modelPrices: Price[] | null | undefined,
observationRecord: ObservationRecordInsertType,
- usageUnits: UsageCostNumberType,
+ usageUnits: UsageCostType,
): Pick<ObservationRecordInsertType, "cost_details" | "total_cost"> {
const { provided_cost_details } = observationRecord;
diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
index 1835d46d2d..88fe585576 100644
--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
@@ -342,13 +342,13 @@ describe("Ingestion end-to-end tests", () => {
expect(generation.version).toBe("2.0.0");
expect(generation.internal_model_id).toBeNull();
expect(generation.usage_details.input).toEqual(
- testConfig.expectedInputUnits?.toString(),
+ testConfig.expectedInputUnits,
);
expect(generation.usage_details.output).toEqual(
- testConfig.expectedOutputUnits?.toString(),
+ testConfig.expectedOutputUnits,
);
expect(generation.usage_details.total).toEqual(
- testConfig.expectedTotalUnits?.toString(),
+ testConfig.expectedTotalUnits,
);
expect(generation.output).toEqual(
JSON.stringify({
@@ -652,10 +652,10 @@ describe("Ingestion end-to-end tests", () => {
testConfig.observationExternalModel,
);
expect(generation.usage_details.input).toBe(
- testConfig.expectedInputUnits?.toString(),
+ testConfig.expectedInputUnits,
);
expect(generation.usage_details.output).toBe(
- testConfig.expectedOutputUnits?.toString(),
+ testConfig.expectedOutputUnits,
);
expect(generation.internal_model_id).toBe(
testConfig.expectedInternalModelId,
@@ -1107,10 +1107,153 @@ describe("Ingestion end-to-end tests", () => {
expect(observation.input).toBe(JSON.stringify({ key: "value" }));
expect(observation.output).toBe("overwritten");
expect(observation.model_parameters).toBe('{"hello":"world"}');
- expect(observation.usage_details.output).toBe("5");
+ expect(observation.usage_details.output).toBe(5);
expect(observation.project_id).toBe("7a88fb47-b4e2-43b8-a06c-a5ce950dc53a");
});
+ it("should merge observations and calculate cost", async () => {
+ await prisma.model.create({
+ data: {
+ id: "clyrjpbe20000t0mzcbwc42rg",
+ modelName: "gpt-4o-mini-2024-07-18",
+ matchPattern: "(?i)^(gpt-4o-mini-2024-07-18)$",
+ startDate: new Date("2021-01-01T00:00:00.000Z"),
+ unit: ModelUsageUnit.Tokens,
+ tokenizerId: "openai",
+ inputPrice: 0.00000015,
+ outputPrice: 0.0000006,
+ tokenizerConfig: {
+ tokensPerName: 1,
+ tokenizerModel: "gpt-4o",
+ tokensPerMessage: 3,
+ },
+ },
+ });
+
+ await prisma.price.create({
+ data: {
+ id: "cm2uio8ef006mh6qlzc2mqa0e",
+ modelId: "clyrjpbe20000t0mzcbwc42rg",
+ price: 0.00000015,
+ usageType: "input",
+ },
+ });
+
+ await prisma.price.create({
+ data: {
+ id: "cm2uio8ef006oh6qlldn36376",
+ modelId: "clyrjpbe20000t0mzcbwc42rg",
+ price: 0.0000006,
+ usageType: "output",
+ },
+ });
+
+ await prisma.observation.create({
+ data: {
+ id: "c8d30f61-4097-407f-a337-5fb1e0c100f2",
+ name: "extract_location",
+ startTime: "2024-11-04T16:13:51.495868Z",
+ endTime: "2024-11-04T16:13:52.156248Z",
+ type: "GENERATION",
+ traceId: "82c480bc-1c4e-4ba8-a153-0bd9f9e1a28e",
+ internalModel: "gpt-4o-mini-2024-07-18",
+ internalModelId: "clyrjpbe20000t0mzcbwc42rg",
+ modelParameters: {
+ temperature: "0.4",
+ max_tokens: 1000,
+ },
+ input: "Sample input",
+ output: "Sample output",
+ projectId,
+ completionTokens: 18,
+ promptTokens: 1295,
+ totalTokens: 1313,
+ calculatedInputCost: 0.00019425,
+ calculatedOutputCost: 0.0000108,
+ calculatedTotalCost: 0.00020505,
+ },
+ });
+
+ const observationId = "c8d30f61-4097-407f-a337-5fb1e0c100f2";
+ const observationEventList: ObservationEvent[] = [
+ {
+ id: "084274e5-f15e-4f66-8419-a171808d8180",
+ timestamp: "2024-11-04T16:13:51.496457Z",
+ type: "generation-create",
+ body: {
+ traceId: "82c480bc-1c4e-4ba8-a153-0bd9f9e1a28e",
+ name: "extract_location",
+ startTime: "2024-11-04T16:13:51.495868Z",
+ metadata: {
+ ls_provider: "openai",
+ ls_model_name: "gpt-4o-mini-2024-07-18",
+ ls_model_type: "chat",
+ ls_temperature: 0.4,
+ ls_max_tokens: 1000,
+ },
+ input: "Sample input",
+ id: "c8d30f61-4097-407f-a337-5fb1e0c100f2",
+ model: "gpt-4o-mini-2024-07-18",
+ modelParameters: {
+ temperature: "0.4",
+ max_tokens: 1000,
+ },
+ usage: null,
+ },
+ },
+ {
+ id: "ef654262-b1d0-4b0b-9e4a-2a410e0577a6",
+ timestamp: "2024-11-04T16:13:52.156691Z",
+ type: "generation-update",
+ body: {
+ traceId: "82c480bc-1c4e-4ba8-a153-0bd9f9e1a28e",
+ output: "Sample output",
+ id: "c8d30f61-4097-407f-a337-5fb1e0c100f2",
+ endTime: "2024-11-04T16:13:52.156248Z",
+ model: "gpt-4o-mini-2024-07-18",
+ usage: {
+ input: 1295,
+ output: 18,
+ total: 1313,
+ unit: "TOKENS",
+ },
+ },
+ },
+ ];
+
+ await ingestionService.processObservationEventList({
+ projectId,
+ entityId: observationId,
+ observationEventList,
+ });
+
+ await clickhouseWriter.flushAll(true);
+
+ const observation = await getClickhouseRecord(
+ TableName.Observations,
+ observationId,
+ );
+
+ expect(observation.name).toBe("extract_location");
+ expect(observation.provided_usage_details).toStrictEqual({
+ input: 1295,
+ output: 18,
+ total: 1313,
+ });
+ expect(observation.usage_details).toStrictEqual({
+ input: 1295,
+ output: 18,
+ total: 1313,
+ });
+ expect(observation.provided_cost_details).toStrictEqual({});
+ expect(observation.cost_details).toStrictEqual({
+ input: 0.00019425,
+ output: 0.0000108,
+ total: 0.00020505,
+ });
+ expect(observation.total_cost).toBe(0.00020505);
+ });
+
it("should put observation updates after creates if timestamp is same", async () => {
const generationId = randomUUID();
@@ -1263,13 +1406,13 @@ describe("Ingestion end-to-end tests", () => {
generationId,
);
- expect(generation.usage_details.input).toEqual("1285");
- expect(generation.usage_details.output).toEqual("513");
- expect(generation.usage_details.total).toEqual("1798");
+ expect(generation.usage_details.input).toEqual(1285);
+ expect(generation.usage_details.output).toEqual(513);
+ expect(generation.usage_details.total).toEqual(1798);
- expect(generation.provided_usage_details.input).toEqual("1285");
- expect(generation.provided_usage_details.output).toEqual("513");
- expect(generation.provided_usage_details.total).toEqual("1798");
+ expect(generation.provided_usage_details.input).toEqual(1285);
+ expect(generation.provided_usage_details.output).toEqual(513);
+ expect(generation.provided_usage_details.total).toEqual(1798);
expect(generation.cost_details.input).toEqual(0.0006425);
expect(generation.cost_details.output).toEqual(0.0007695);
@@ -1376,8 +1519,8 @@ describe("Ingestion end-to-end tests", () => {
expect(generation?.output).toEqual(
JSON.stringify({ key: "this is a great gpt output" }),
);
- expect(generation?.usage_details.input).toEqual("5");
- expect(generation?.usage_details.output).toEqual("11");
+ expect(generation?.usage_details.input).toEqual(5);
+ expect(generation?.usage_details.output).toEqual(11);
});
it("should update all token counts if update does not contain model name and events come in wrong order", async () => {
@@ -1471,8 +1614,8 @@ describe("Ingestion end-to-end tests", () => {
expect(observation?.output).toEqual(
JSON.stringify({ key: "this is a great gpt output" }),
);
- expect(observation?.usage_details.input).toEqual("5");
- expect(observation?.usage_details.output).toEqual("11");
+ expect(observation?.usage_details.input).toEqual(5);
+ expect(observation?.usage_details.output).toEqual(11);
});
it("null does not override set values", async () => {