🪢 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(ingestionService): add trace correctness script (#2889)
Hassieb Pakzad02266d420edf
diff --git a/worker/src/scripts/verifyClickhouseRecords.ts b/worker/src/scripts/verifyClickhouseRecords.tsindex 632c0c2aa3..9503198f26 100644--- a/worker/src/scripts/verifyClickhouseRecords.ts+++ b/worker/src/scripts/verifyClickhouseRecords.ts@@ -8,20 +8,26 @@ import {// Constantsconst LIMIT = 50;-const ITERATIONS = 100;-const DATE_CUTOFF = new Date("2024-08-09T08:20:00.000Z");-const TABLE_SAMPLE_RATE = 10;+const ITERATIONS = 10;+const DATE_START = new Date("2024-08-09T08:20:00.000Z");+const DATE_END = new Date(Date.now() - 1000 * 60 * 60); // 1 hour ago++const TABLE_SAMPLE_RATE = 0.003;async function main() {- const failedList: string[] = [];- const checkedSet = new Set<string>();+ const failedObservationList: string[] = [];+ const failedTraceList: string[] = [];+ const checkedObservationSet = new Set<string>();+ const checkedTraceSet = new Set<string>();- let totalCount = 0;+ let totalObservationCount = 0;+ let totalTraceCount = 0;let currentIteration = 0;while (currentIteration < ITERATIONS) {console.log(`Iteration ${currentIteration + 1}/${ITERATIONS}`);+ // Check observationsconst randomObservations = await prisma.$queryRaw<unknown[]>(Prisma.sql`SELECT@@ -29,7 +35,8 @@ async function main() {FROMobservations TABLESAMPLE SYSTEM(${TABLE_SAMPLE_RATE})WHERE- start_time > ${DATE_CUTOFF}::TIMESTAMP WITH time zone at time zone 'UTC'+ start_time > ${DATE_START}::TIMESTAMP WITH time zone at time zone 'UTC'+ AND start_time < ${DATE_END}::TIMESTAMP WITH time zone at time zone 'UTC'LIMIT ${LIMIT}`);@@ -40,11 +47,11 @@ async function main() {const results = await Promise.allSettled(randomObservations.map(async (obs) => {const id = (obs as any).id;- if (checkedSet.has(id)) {- return Promise.resolve();+ if (checkedObservationSet.has(id)) {+ return;}- totalCount += 1;- checkedSet.add(id);+ totalObservationCount += 1;+ checkedObservationSet.add(id);await verifyClickhouseObservation(obs).catch((e) => {throw new Error(@@ -57,7 +64,51 @@ async function main() {results.forEach((result, i) => {if (result.status === "rejected") {const message = `[${i}] ` + result.reason;- failedList.push(message);+ failedObservationList.push(message);++ console.error(message);+ }+ });+ } catch (e) {+ console.error(e);+ }++ // Check traces+ const randomTraces = await prisma.$queryRaw<unknown[]>(+ Prisma.sql`+ SELECT *+ FROM traces TABLESAMPLE SYSTEM(${TABLE_SAMPLE_RATE})+ WHERE+ timestamp > ${DATE_START}::TIMESTAMP WITH time zone at time zone 'UTC'+ AND timestamp < ${DATE_END}::TIMESTAMP WITH time zone at time zone 'UTC'+ LIMIT ${LIMIT}+ `+ );++ console.log(`Verifying ${randomTraces.length} traces...`);++ try {+ const results = await Promise.allSettled(+ randomTraces.map(async (trace) => {+ const id = (trace as any).id;+ if (checkedTraceSet.has(id)) {+ return;+ }+ totalTraceCount += 1;+ checkedTraceSet.add(id);++ await verifyClickhouseTrace(trace).catch((e) => {+ throw new Error(+ `Failed to verify trace ${id}: ${e.message}, ${e.stack}`+ );+ });+ })+ );++ results.forEach((result, i) => {+ if (result.status === "rejected") {+ const message = `[${i}] ` + result.reason;+ failedTraceList.push(message);console.error(message);}@@ -69,18 +120,35 @@ async function main() {currentIteration++;}- console.log(`Total observations verified: ${totalCount}`);-- if (failedList.length > 0) {- await writeFile(- `src/scripts/output/${new Date().toISOString()}_failedObservations.txt`,- failedList.join("\n")- );+ console.log(`Total observations verified: ${totalObservationCount}`);+ if (failedObservationList.length > 0) {+ await Promise.all([+ writeFile(+ `src/scripts/output/${new Date().toISOString()}_failedObservations.txt`,+ failedObservationList.join("\n")+ ),+ ]);console.error(- `Failed to verify ${failedList.length} out of ${totalCount} observations`+ `Failed to verify ${failedObservationList.length} out of ${totalObservationCount} observations`+ );+ } else {+ console.log(+ `All ${totalObservationCount} observations verified successfully`);}++ if (failedTraceList.length > 0) {+ writeFile(+ `src/scripts/output/${new Date().toISOString()}_failedTraces.txt`,+ failedTraceList.join("\n")+ ),+ console.error(+ `Failed to verify ${failedTraceList.length} out of ${totalTraceCount} traces`+ );+ } else {+ console.log(`All ${totalTraceCount} traces verified successfully`);+ }}main().then(() => {@@ -255,13 +323,13 @@ async function verifyClickhouseObservation(postgresObservation: any) {case "completion_tokens": {if (pgValue !== 0 &&- pgValue !== (clickhouseRecord as any)["provided_output_usage_units"]+ pgValue !== (clickhouseRecord as any)["output_usage_units"]) {throw new Error(getErrorMessage({key,pgValue,- chValue: (clickhouseRecord as any)["provided_output_usage_units"],+ chValue: (clickhouseRecord as any)["output_usage_units"],}));}@@ -272,13 +340,13 @@ async function verifyClickhouseObservation(postgresObservation: any) {case "prompt_tokens": {if (pgValue !== 0 &&- pgValue !== (clickhouseRecord as any)["provided_input_usage_units"]+ pgValue !== (clickhouseRecord as any)["input_usage_units"]) {throw new Error(getErrorMessage({key,pgValue,- chValue: (clickhouseRecord as any)["provided_input_usage_units"],+ chValue: (clickhouseRecord as any)["input_usage_units"],}));}@@ -289,13 +357,13 @@ async function verifyClickhouseObservation(postgresObservation: any) {case "total_tokens": {if (pgValue !== 0 &&- pgValue !== (clickhouseRecord as any)["provided_total_usage_units"]+ pgValue !== (clickhouseRecord as any)["total_usage_units"]) {throw new Error(getErrorMessage({key,pgValue,- chValue: (clickhouseRecord as any)["provided_total_usage_units"],+ chValue: (clickhouseRecord as any)["total_usage_units"],}));}@@ -419,3 +487,138 @@ async function verifyClickhouseObservation(postgresObservation: any) {}}}++async function verifyClickhouseTrace(postgresTrace: any) {+ const { id: traceId, project_id: projectId } = postgresTrace;++ const clickhouseResult = await clickhouseClient.query({+ query: `SELECT * FROM traces WHERE project_id = '${projectId}' AND id = '${traceId}' ORDER BY updated_at DESC LIMIT 1`,+ format: "JSONEachRow",+ });++ const clickhouseTrace = (await clickhouseResult.json())[0];+ if (!clickhouseTrace) {+ throw new Error(+ `Trace ${traceId} not found in Clickhouse for project ${projectId}`+ );+ }++ const getErrorMessage = (params: {+ key: string;+ pgValue: any;+ chValue: any;+ }) =>+ `[${projectId}-trace-${traceId}] Field ${params.key} does not match between Postgres and Clickhouse: \n Postgres: ${JSON.stringify(params.pgValue)} \n Clickhouse: ${JSON.stringify(params.chValue)}`;++ for (const key of Object.keys(postgresTrace)) {+ const pgValue = postgresTrace[key];+ const chValue = (clickhouseTrace as any)[key];++ switch (key) {+ // Different by nature+ case "created_at":+ case "updated_at":+ continue;++ // Dropped in CH+ case "external_id":+ continue;++ // Same in PG as in CH+ case "id":+ case "name":+ case "project_id":+ case "user_id":+ case "release":+ case "public":+ case "bookmarked":+ case "session_id":+ case "version": {+ if (pgValue && pgValue !== chValue) {+ throw new Error(getErrorMessage({ key, pgValue, chValue }));+ }++ break;+ }++ // timestamp cannot be overwritten in CH, so they are allowed to be different+ case "timestamp": {+ if (+ pgValue &&+ chValue &&+ typeof pgValue.toISOString() !==+ typeof clickhouseStringDateSchema.parse(chValue)+ ) {+ throw new Error(getErrorMessage({ key, pgValue, chValue }));+ }++ break;+ }++ case "metadata": {+ if (!pgValue && JSON.stringify(chValue) === "{}") continue;++ const parsedChMetadata =+ "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),+ ])+ );++ if (+ JSON.stringify(pgValue, Object.keys(pgValue).sort()) !==+ JSON.stringify(+ parsedChMetadata,+ typeof parsedChMetadata === "string"+ ? undefined+ : Object.keys(parsedChMetadata as any).sort()+ )+ ) {+ throw new Error(+ getErrorMessage({ key, pgValue, chValue: parsedChMetadata })+ );+ }++ break;+ }++ case "tags":+ if (JSON.stringify(pgValue) !== JSON.stringify(chValue)) {+ throw new Error(getErrorMessage({ key, pgValue, chValue }));+ }++ break;++ case "input":+ case "output": {+ const parsedPgValue = parseJsonPrioritised(pgValue);+ const parsedChValue = parseJsonPrioritised(chValue);++ if (+ parsedPgValue &&+ JSON.stringify(parsedPgValue, Object.keys(parsedPgValue).sort()) !==+ JSON.stringify(+ parsedChValue,+ Object.keys(parsedChValue as any).sort()+ )+ ) {+ throw new Error(+ getErrorMessage({+ key,+ pgValue: JSON.stringify(parsedPgValue),+ chValue: JSON.stringify(parsedChValue),+ })+ );+ }++ break;+ }++ default:+ throw Error("Unhandled observation key: " + key);+ }+ }+}