🪢 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
chore: validate clickhouse data quality (#3809)
Steffen Schmitz1ac6e512718a
diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 20a84299a5..655c1f373a 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -939,6 +939,9 @@ importers:prettier:specifier: ^3.3.3version: 3.3.3+ rus-diff:+ specifier: ^1.1.0+ version: 1.1.0ts-node:specifier: ^10.9.2version: 10.9.2(@types/[email protected])([email protected])@@ -8058,6 +8061,9 @@ 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==}@@ -9996,6 +10002,9 @@ packages:resolution: {integrity: sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==}+ resolution: {integrity: sha512-h2SBIrpYQ0eevfvghEA67QQbc3fD2KJl/aERQuvi8AF8g0FSvZpSEwIeJgcoBDteFB5TmXtGkw6aLn5uhMYxYw==}+resolution: {integrity: sha512-cLgakCUf6PedEu15t8kbsjnwIFFR2D4RfL+W3iWFJ4iac7z4B0ZI8fxy4R3J956kAI68HclCFGL8MPoUVC3qVA==}@@ -20765,6 +20774,8 @@ snapshots:+ [email protected]: {}+@@ -23002,6 +23013,10 @@ snapshots:dependencies:queue-microtask: 1.2.3+ dependencies:+ json-hash: 1.2.0+diff --git a/worker/package.json b/worker/package.jsonindex 7ec0728def..5b0a0b42d4 100644--- a/worker/package.json+++ b/worker/package.json@@ -73,6 +73,7 @@"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/README.md b/worker/src/scripts/verifyClickhouseRecords/README.mdnew file mode 100644index 0000000000..eca51ad697--- /dev/null+++ b/worker/src/scripts/verifyClickhouseRecords/README.md@@ -0,0 +1,36 @@+# Verify Clickhouse Records++This script is used to compare Clickhouse records to our Postgres records.+Per default, it draws a sample of observations, traces, and scores from the Postgres+tables and executes single queries on Clickhouse to compare the individual fields.+It is possible to overwrite the selection process using the overwriteIds per type.++## Usage++To compare the records configure the database environment variables and afterward+execute the script.+Modify the `DATE_START` setting in the script to a date after the clickhouse sync was in place.+Set `ITERATIONS` based on the number of records that you want to validate. We sample 100 records per kind+in each iteration.++```bash+# Insert postgres database URL+export DATABASE_URL=+export CLICKHOUSE_URL=+export CLICKHOUSE_USER=+export CLICKHOUSE_PASSWORD=++cd worker/src/scripts/verifyClickhouseRecords++npx ts-node index.ts+```++The script writes the objects and deltas into the `output` folder within the current working directory.+Use it to validate whether Clickhouse and Postgres produce equal results.++## Known Issues++### Numeric values++Postgres seems to retain a higher precision for floating point numeric numbers.+In the object comparisons this frequently leads to false positives in the 10^-8 range.\ No newline at end of filediff --git a/worker/src/scripts/verifyClickhouseRecords.ts b/worker/src/scripts/verifyClickhouseRecords/index.tssimilarity index 77%rename from worker/src/scripts/verifyClickhouseRecords.tsrename to worker/src/scripts/verifyClickhouseRecords/index.tsindex ef7e66d1e5..6ef96f4acf 100644--- a/worker/src/scripts/verifyClickhouseRecords.ts+++ b/worker/src/scripts/verifyClickhouseRecords/index.ts@@ -1,4 +1,5 @@import { writeFile } from "node:fs/promises";+import path from "node:path";import { parseJsonPrioritised } from "@langfuse/shared";import { prisma, Prisma } from "@langfuse/shared/src/db";import {@@ -6,11 +7,25 @@ import {clickhouseStringDateSchema,logger,} from "@langfuse/shared/src/server";+// @ts-ignore+import { diff } from "rus-diff";++const getErrorMessage = (params: {+ type: "observation" | "trace" | "score";+ projectId: string;+ id: string;+ key: string;+ 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)}`;+ return delta;+};// Constantsconst LIMIT = 100;const ITERATIONS = 10;-const DATE_START = new Date("2024-08-15T00:00:00.000Z");+const DATE_START = new Date("2024-10-17T00:00:00.000Z");const DATE_END = new Date(Date.now() - 1000 * 60 * 15); // 15 minutes agoconst TABLE_SAMPLE_RATE = 0.05;@@ -118,7 +133,7 @@ async function main(params: MainParams) {// Check traceslet traces: unknown[] = [];if (overwriteTraceIds.length > 0) {- const traces = await prisma.$queryRaw<unknown[]>(+ traces = await prisma.$queryRaw<unknown[]>(Prisma.sql`SELECT *FROM traces@@ -234,13 +249,14 @@ async function main(params: MainParams) {logger.info(`Total scores verified: ${totalScoreCount}`);if (failedObservationList.length > 0) {- await writeFile(- `src/scripts/output/${new Date().toISOString()}_failedObservations.txt`,- failedObservationList.join("\n"),- ),- logger.error(- `Failed to verify ${failedObservationList.length} out of ${totalObservationCount} observations`,- );+ const outPath = path.join(+ process.cwd(),+ `/output/${new Date().toISOString()}_failedObservations.txt`,+ );+ await writeFile(outPath, failedObservationList.join("\n"));+ logger.error(+ `Failed to verify ${failedObservationList.length} out of ${totalObservationCount} observations`,+ );} else {logger.info(`All ${totalObservationCount} observations verified successfully`,@@ -248,11 +264,11 @@ async function main(params: MainParams) {}if (failedTraceList.length > 0) {- await writeFile(- `src/scripts/output/${new Date().toISOString()}_failedTraces.txt`,- failedTraceList.join("\n"),+ const outPath = path.join(+ process.cwd(),+ `/output/${new Date().toISOString()}_failedTraces.txt`,);-+ await writeFile(outPath, failedTraceList.join("\n"));logger.error(`Failed to verify ${failedTraceList.length} out of ${totalTraceCount} traces`,);@@ -261,11 +277,11 @@ async function main(params: MainParams) {}if (failedScoreList.length > 0) {- await writeFile(- `src/scripts/output/${new Date().toISOString()}_failedScores.txt`,- failedScoreList.join("\n"),+ const outPath = path.join(+ process.cwd(),+ `/output/${new Date().toISOString()}_failedScores.txt`,);-+ await writeFile(outPath, failedScoreList.join("\n"));logger.error(`Failed to verify ${failedScoreList.length} out of ${totalScoreCount} scores`,);@@ -281,19 +297,16 @@ async function verifyClickhouseObservation(postgresObservation: any) {format: "JSONEachRow",});- const clickhouseRecord = (await clickhouseResult.json())[0];+ const clickhouseRecord = (await clickhouseResult.json()).shift();if (!clickhouseRecord) {throw new Error(`Observation ${observationId} not found in Clickhouse for project ${projectId}`,);}- const getErrorMessage = (params: {- key: string;- pgValue: any;- chValue: any;- }) =>- `[${projectId}-observation-${observationId}] Mismatch between Postgres and Clickhouse:\n${JSON.stringify({ key: params.key, pg: params.pgValue, ch: params.chValue }, null, 2)}`;+ logger.info(+ `Comparing delta for observation ${projectId}-${observationId}, ${JSON.stringify(diff(postgresObservation, clickhouseRecord), null, 2)}`,+ );for (const key in postgresObservation) {const pgValue = postgresObservation[key];@@ -323,7 +336,16 @@ async function verifyClickhouseObservation(postgresObservation: any) {case "internal_model_id":case "unit": {if (pgValue && pgValue !== chValue) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -337,7 +359,16 @@ async function verifyClickhouseObservation(postgresObservation: any) {typeof pgValue.toISOString() !==typeof clickhouseStringDateSchema.parse(chValue)) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -351,7 +382,16 @@ async function verifyClickhouseObservation(postgresObservation: any) {chValue &&pgValue.toISOString() !== clickhouseStringDateSchema.parse(chValue)) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -366,7 +406,7 @@ async function verifyClickhouseObservation(postgresObservation: any) {: Object.fromEntries(Object.entries(chValue).map(([k, v]) => [k,- JSON.parse(v as any),+ v ? JSON.parse(v as any) : null,]),);@@ -380,7 +420,14 @@ async function verifyClickhouseObservation(postgresObservation: any) {)) {throw new Error(- getErrorMessage({ key, pgValue, chValue: parsedChMetadata }),+ getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",+ key,+ pgValue,+ chValue: parsedChMetadata,+ }),);}@@ -389,7 +436,16 @@ async function verifyClickhouseObservation(postgresObservation: any) {case "model":if (pgValue !== (clickhouseRecord as any)["provided_model_name"]) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -409,6 +465,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue: JSON.stringify(parsedPgValue),chValue: JSON.stringify(parsedChValue),@@ -431,7 +490,14 @@ async function verifyClickhouseObservation(postgresObservation: any) {JSON.stringify(parsedChValue, Object.keys(parsedChValue).sort())) {throw new Error(- getErrorMessage({ key, pgValue, chValue: parsedChValue }),+ getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",+ key,+ pgValue,+ chValue: parsedChValue,+ }),);}@@ -445,6 +511,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["output_usage_units"],@@ -462,6 +531,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["input_usage_units"],@@ -479,6 +551,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["total_usage_units"],@@ -497,6 +572,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["input_cost"],@@ -515,6 +593,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["output_cost"],@@ -533,6 +614,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["total_cost"],@@ -552,6 +636,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["provided_input_cost"],@@ -571,6 +658,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue,chValue: (clickhouseRecord as any)["provided_output_cost"],@@ -590,6 +680,9 @@ async function verifyClickhouseObservation(postgresObservation: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: observationId,+ type: "observation",key,pgValue: Number(pgValue),chValue: (clickhouseRecord as any)["provided_total_cost"],@@ -621,13 +714,6 @@ async function verifyClickhouseTrace(postgresTrace: any) {);}- const getErrorMessage = (params: {- key: string;- pgValue: any;- chValue: any;- }) =>- `[${projectId}-trace-${traceId}] Mismatch between Postgres and Clickhouse:\n${JSON.stringify({ key: params.key, pg: params.pgValue, ch: params.chValue }, null, 2)}`;-for (const key of Object.keys(postgresTrace)) {const pgValue = postgresTrace[key];const chValue = (clickhouseTrace as any)[key];@@ -653,7 +739,16 @@ async function verifyClickhouseTrace(postgresTrace: any) {case "session_id":case "version": {if (pgValue && pgValue !== chValue) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: traceId,+ type: "trace",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -667,7 +762,16 @@ async function verifyClickhouseTrace(postgresTrace: any) {typeof pgValue.toISOString() !==typeof clickhouseStringDateSchema.parse(chValue)) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: traceId,+ type: "trace",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -696,7 +800,14 @@ async function verifyClickhouseTrace(postgresTrace: any) {)) {throw new Error(- getErrorMessage({ key, pgValue, chValue: parsedChMetadata }),+ getErrorMessage({+ projectId,+ id: traceId,+ type: "trace",+ key,+ pgValue,+ chValue: parsedChMetadata,+ }),);}@@ -705,7 +816,16 @@ async function verifyClickhouseTrace(postgresTrace: any) {case "tags":if (JSON.stringify(pgValue) !== JSON.stringify(chValue)) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: traceId,+ type: "trace",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -727,6 +847,9 @@ async function verifyClickhouseTrace(postgresTrace: any) {) {throw new Error(getErrorMessage({+ projectId,+ id: traceId,+ type: "trace",key,pgValue: JSON.stringify(parsedPgValue),chValue: JSON.stringify(parsedChValue),@@ -738,7 +861,7 @@ async function verifyClickhouseTrace(postgresTrace: any) {}default:- throw Error("Unhandled observation key: " + key);+ throw Error("Unhandled trace key: " + key);}}}@@ -758,13 +881,6 @@ async function verifyClickhouseScore(postgresScore: any) {);}- const getErrorMessage = (params: {- key: string;- pgValue: any;- chValue: any;- }) =>- `[${projectId}-score-${scoreId}] Mismatch between Postgres and Clickhouse:\n${JSON.stringify({ key: params.key, pg: params.pgValue, ch: params.chValue }, null, 2)}`;-for (const key of Object.keys(postgresScore)) {const pgValue = postgresScore[key];const chValue = (clickhouseScore as any)[key];@@ -778,6 +894,7 @@ async function verifyClickhouseScore(postgresScore: any) {// Same in PG as in CHcase "id":case "name":+ case "queue_id":case "project_id":case "value":case "observation_id":@@ -788,8 +905,23 @@ async function verifyClickhouseScore(postgresScore: any) {case "config_id":case "string_value":case "data_type": {- if (pgValue && pgValue !== chValue) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ // If types are numeric, we just check that they are very close to each other+ if (+ pgValue &&+ ((typeof pgValue === "number" &&+ Math.abs(pgValue - chValue) >= 0.00001) ||+ pgValue !== chValue)+ ) {+ throw new Error(+ getErrorMessage({+ projectId,+ id: scoreId,+ type: "score",+ key,+ pgValue,+ chValue,+ }),+ );}break;@@ -803,14 +935,23 @@ async function verifyClickhouseScore(postgresScore: any) {typeof pgValue.toISOString() !==typeof clickhouseStringDateSchema.parse(chValue)) {- throw new Error(getErrorMessage({ key, pgValue, chValue }));+ throw new Error(+ getErrorMessage({+ projectId,+ id: scoreId,+ type: "score",+ key,+ pgValue,+ chValue,+ }),+ );}break;}default:- throw Error("Unhandled observation key: " + key);+ throw Error("Unhandled score key: " + key);}}}