Skip to content

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

feat: query scores over time (#4149)

Max Deichmann28e43eb74c64
diff --git a/packages/shared/scripts/prepareClickhouse.ts b/packages/shared/scripts/prepareClickhouse.ts
index f25d04fec1..3a5021967e 100644
--- a/packages/shared/scripts/prepareClickhouse.ts
+++ b/packages/shared/scripts/prepareClickhouse.ts
@@ -131,7 +131,7 @@ export const prepareClickhouse = async (
toString(floor(randUniform(0, ${observationsPerProject}))),
NULL
) AS observation_id,
- concat('name_', toString(rand() % 100)) AS name,
+ concat('name_', toString(rand() % 10)) AS name,
randUniform(0, 100) as value,
'API' as source,
'comment' as comment,
diff --git a/packages/shared/src/server/repositories/dashboards.ts b/packages/shared/src/server/repositories/dashboards.ts
index 37db417a29..a209d7e980 100644
--- a/packages/shared/src/server/repositories/dashboards.ts
+++ b/packages/shared/src/server/repositories/dashboards.ts
@@ -245,6 +245,59 @@ export const getDistinctModels = async (
return result;
};
+export const getScoresAggregateOverTime = async (
+ projectId: string,
+ filter: FilterState,
+ groupBy: DateTrunc,
+) => {
+ const chFilter = new FilterList(
+ createFilterFromFilterState(filter, dashboardColumnDefinitions),
+ );
+
+ const appliedFilter = chFilter.apply();
+
+ const query = `
+ SELECT
+ ${selectTimeseriesColumn(groupBy, "timestamp", "timestamp")},
+ name,
+ data_type,
+ source,
+ AVG(value) as avg_value
+ FROM scores FINAL
+ WHERE project_id = {projectId: String}
+ AND ${appliedFilter.query}
+ AND data_type IN ('NUMERIC', 'BOOLEAN')
+ GROUP BY
+ timestamp,
+ name,
+ data_type,
+ source
+ ${orderByTimeSeries(groupBy, "timestamp")};
+`;
+
+ const result = await queryClickhouse<{
+ timestamp: string;
+ name: string;
+ data_type: string;
+ source: string;
+ avg_value: number;
+ }>({
+ query,
+ params: {
+ projectId,
+ ...appliedFilter.params,
+ },
+ });
+
+ return result.map((row) => ({
+ scoreTimestamp: new Date(row.timestamp),
+ scoreName: row.name,
+ scoreDataType: row.data_type,
+ scoreSource: row.source,
+ avgValue: Number(row.avg_value),
+ }));
+};
+
export const getModelUsageByUser = async (
projectId: string,
filter: FilterState,
diff --git a/packages/shared/src/tableDefinitions/mapDashboards.ts b/packages/shared/src/tableDefinitions/mapDashboards.ts
index 6cb531bb2f..a285cc67d8 100644
--- a/packages/shared/src/tableDefinitions/mapDashboards.ts
+++ b/packages/shared/src/tableDefinitions/mapDashboards.ts
@@ -25,6 +25,12 @@ export const dashboardColumnDefinitions: UiColumnMapping[] = [
uiTableId: "scoreTimestamp",
uiTableName: "Score Timestamp",
},
+ {
+ clickhouseTableName: "scores",
+ clickhouseSelect: "data_type",
+ uiTableId: "scoreDataType",
+ uiTableName: "Scores Data Type",
+ },
{
clickhouseTableName: "scores",
clickhouseSelect: "value",
diff --git a/web/src/features/dashboard/components/ChartScores.tsx b/web/src/features/dashboard/components/ChartScores.tsx
index 9ec1a67852..8a6ba1f018 100644
--- a/web/src/features/dashboard/components/ChartScores.tsx
+++ b/web/src/features/dashboard/components/ChartScores.tsx
@@ -8,7 +8,7 @@ import {
fillMissingValuesAndTransform,
isEmptyTimeSeries,
} from "@/src/features/dashboard/components/hooks";
-
+import { useClickhouse } from "@/src/components/layouts/ClickhouseAdminToggle";
import { createTracesTimeFilter } from "@/src/features/dashboard/lib/dashboard-utils";
import {
dashboardDateRangeAggregationSettings,
@@ -56,6 +56,8 @@ export function ChartScores(props: {
{ type: "string", column: "scoreDataType" },
{ type: "string", column: "scoreSource" },
],
+ queryClickhouse: useClickhouse(),
+ queryName: "scores-aggregate-timeseries",
},
{
trpc: {
diff --git a/web/src/features/dashboard/server/dashboard-router.ts b/web/src/features/dashboard/server/dashboard-router.ts
index 12c7b4507a..74b83f0869 100644
--- a/web/src/features/dashboard/server/dashboard-router.ts
+++ b/web/src/features/dashboard/server/dashboard-router.ts
@@ -20,6 +20,7 @@ import {
getObservationUsageByTime,
groupTracesByTime,
getDistinctModels,
+ getScoresAggregateOverTime,
getTracesGroupedByUsers,
getModelUsageByUser,
} from "@langfuse/shared/src/server";
@@ -42,6 +43,7 @@ export const dashboardRouter = createTRPCRouter({
"traces-timeseries",
"observations-usage-timeseries",
"distinct-models",
+ "scores-aggregate-timeseries",
"observations-usage-by-users",
"traces-grouped-by-user",
])
@@ -143,6 +145,19 @@ export const dashboardRouter = createTRPCRouter({
);
return models as DatabaseRow[];
+ case "scores-aggregate-timeseries":
+ const dateTruncScores = extractTimeSeries(input.groupBy);
+ if (!dateTruncScores) {
+ return [];
+ }
+ const aggregatedScores = await getScoresAggregateOverTime(
+ input.projectId,
+ input.filter ?? [],
+ dateTruncScores,
+ );
+
+ return aggregatedScores as DatabaseRow[];
+
case "observations-usage-by-users":
const rowsUsers = await getModelUsageByUser(
input.projectId,