Skip to content

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

fix: fix filters for csv exports (#9887)

Max Deichmann420d7c504cee
diff --git a/packages/shared/src/server/repositories/clickhouse.ts b/packages/shared/src/server/repositories/clickhouse.ts
index f04b725d5f..a0f3adcb2f 100644
--- a/packages/shared/src/server/repositories/clickhouse.ts
+++ b/packages/shared/src/server/repositories/clickhouse.ts
@@ -239,6 +239,7 @@ export async function* queryClickhouseStream<T>(opts: {
log_comment: JSON.stringify(opts.tags ?? {}),
},
});
+
// same logic as for prisma. we want to see queries in development
if (env.NODE_ENV === "development") {
logger.info(`clickhouse:query ${res.query_id} ${opts.query}`);
diff --git a/worker/src/__tests__/batchExport.test.ts b/worker/src/__tests__/batchExport.test.ts
index c7bb1f1e12..b22cbe17d9 100644
--- a/worker/src/__tests__/batchExport.test.ts
+++ b/worker/src/__tests__/batchExport.test.ts
@@ -234,6 +234,111 @@ describe("batch export test suite", () => {
});
});
+ it("should export observations filtered by scores", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+
+ const trace = createTrace({
+ project_id: projectId,
+ id: randomUUID(),
+ });
+
+ await createTracesCh([trace]);
+
+ // Create observations with different score values
+ const observations = [
+ createObservation({
+ project_id: projectId,
+ trace_id: trace.id,
+ id: randomUUID(),
+ type: "GENERATION",
+ name: "high-accuracy",
+ start_time: new Date("2024-01-01").getTime(),
+ }),
+ createObservation({
+ project_id: projectId,
+ trace_id: trace.id,
+ id: randomUUID(),
+ type: "GENERATION",
+ name: "medium-accuracy",
+ start_time: new Date("2024-01-02").getTime(),
+ }),
+ createObservation({
+ project_id: projectId,
+ trace_id: trace.id,
+ id: randomUUID(),
+ type: "GENERATION",
+ name: "low-accuracy",
+ start_time: new Date("2024-01-03").getTime(),
+ }),
+ ];
+
+ await createObservationsCh(observations);
+
+ // Create scores with different values
+ const scores = [
+ createTraceScore({
+ project_id: projectId,
+ trace_id: trace.id,
+ observation_id: observations[0].id,
+ name: "accuracy",
+ value: 0.95,
+ data_type: "NUMERIC",
+ }),
+ createTraceScore({
+ project_id: projectId,
+ trace_id: trace.id,
+ observation_id: observations[1].id,
+ name: "accuracy",
+ value: 0.75,
+ data_type: "NUMERIC",
+ }),
+ createTraceScore({
+ project_id: projectId,
+ trace_id: trace.id,
+ observation_id: observations[2].id,
+ name: "accuracy",
+ value: 0.45,
+ data_type: "NUMERIC",
+ }),
+ ];
+
+ await createScoresCh(scores);
+
+ // Filter observations with accuracy >= 0.7
+ const stream = await getObservationStream({
+ projectId: projectId,
+ cutoffCreatedAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
+ filter: [
+ {
+ type: "numberObject",
+ column: "Scores",
+ key: "accuracy",
+ operator: ">=",
+ value: 0.7,
+ },
+ ],
+ });
+
+ const rows: any[] = [];
+
+ for await (const chunk of stream) {
+ rows.push(chunk);
+ }
+
+ // Should only include observations with accuracy >= 0.7
+ expect(rows).toHaveLength(2);
+
+ const exportedNames = rows.map((row) => row.name).sort();
+ expect(exportedNames).toEqual(["high-accuracy", "medium-accuracy"]);
+
+ // Verify scores are included in the export
+ const highAccuracyRow = rows.find((r) => r.name === "high-accuracy");
+ expect(highAccuracyRow?.accuracy).toEqual([0.95]);
+
+ const mediumAccuracyRow = rows.find((r) => r.name === "medium-accuracy");
+ expect(mediumAccuracyRow?.accuracy).toEqual([0.75]);
+ });
+
it("should export sessions", async () => {
const { projectId } = await createOrgProjectAndApiKey();
diff --git a/worker/src/features/database-read-stream/observation-stream.ts b/worker/src/features/database-read-stream/observation-stream.ts
index 7e7f3e2b7f..7b407fd9e2 100644
--- a/worker/src/features/database-read-stream/observation-stream.ts
+++ b/worker/src/features/database-read-stream/observation-stream.ts
@@ -219,11 +219,11 @@ export const getObservationStream = async (props: {
t.tags as traceTags,
t.timestamp as traceTimestamp,
t.user_id as userId,
- sa.scores_avg as scores_avg,
- sa.score_categories as score_categories
+ s.scores_avg as scores_avg,
+ s.score_categories as score_categories
FROM observations o
LEFT JOIN traces t ON t.id = o.trace_id AND t.project_id = o.project_id
- LEFT JOIN scores_agg sa ON sa.trace_id = o.trace_id AND sa.observation_id = o.id
+ LEFT JOIN scores_agg s ON s.trace_id = o.trace_id AND s.observation_id = o.id
WHERE ${appliedObservationsFilter.query}
${search.query}
LIMIT 1 BY o.id, o.project_id
@@ -254,6 +254,7 @@ export const getObservationStream = async (props: {
rowLimit,
...appliedScoresFilter.params,
...appliedObservationsFilter.params,
+ ...search.params,
},
clickhouseConfigs,
tags: {