Skip to content

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

fix(scores): add categorical score filtering (#10335)

Nimar3bf4c2387069
diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.ts
index e081708bf2..39b075c338 100644
--- a/packages/shared/src/server/repositories/scores.ts
+++ b/packages/shared/src/server/repositories/scores.ts
@@ -1042,6 +1042,55 @@ export const getScoreNames = async (
}));
};
+export const getScoreStringValues = async (
+ projectId: string,
+ timestampFilter: FilterState,
+) => {
+ const chFilter = new FilterList(
+ createFilterFromFilterState(
+ timestampFilter,
+ scoresTableUiColumnDefinitions,
+ ),
+ );
+ const timestampFilterRes = chFilter.apply();
+
+ const query = `
+ select
+ string_value,
+ count(*) as count
+ from scores s
+ WHERE s.project_id = {projectId: String}
+ AND string_value IS NOT NULL
+ AND string_value != ''
+ ${timestampFilterRes?.query ? `AND ${timestampFilterRes.query}` : ""}
+ GROUP BY string_value
+ ORDER BY count() desc
+ LIMIT 1000;
+ `;
+
+ const rows = await queryClickhouse<{
+ string_value: string;
+ count: string;
+ }>({
+ query: query,
+ params: {
+ projectId: projectId,
+ ...(timestampFilterRes ? timestampFilterRes.params : {}),
+ },
+ tags: {
+ feature: "tracing",
+ type: "score",
+ kind: "list",
+ projectId,
+ },
+ });
+
+ return rows.map((row) => ({
+ value: row.string_value,
+ count: Number(row.count),
+ }));
+};
+
export const deleteScores = async (projectId: string, scoreIds: string[]) => {
const query = `
DELETE FROM scores
diff --git a/web/src/components/table/use-cases/scores.tsx b/web/src/components/table/use-cases/scores.tsx
index 0e0eb739e6..821b8e8ecb 100644
--- a/web/src/components/table/use-cases/scores.tsx
+++ b/web/src/components/table/use-cases/scores.tsx
@@ -233,6 +233,11 @@ export default function ScoresTable({
source: ["ANNOTATION", "API", "EVAL"],
dataType: ["NUMERIC", "CATEGORICAL", "BOOLEAN"],
value: [],
+ stringValue:
+ filterOptions.data?.stringValue?.map((sv) => ({
+ value: sv.value,
+ count: sv.count !== undefined ? Number(sv.count) : undefined,
+ })) || [],
traceName:
filterOptions.data?.traceName?.map((tn) => ({
value: tn.value,
diff --git a/web/src/features/filters/config/scores-config.ts b/web/src/features/filters/config/scores-config.ts
index 638cf236f7..c07fad9761 100644
--- a/web/src/features/filters/config/scores-config.ts
+++ b/web/src/features/filters/config/scores-config.ts
@@ -45,6 +45,11 @@ export const scoreFilterConfig: FilterConfig = {
min: -100,
max: 100,
},
+ {
+ type: "categorical" as const,
+ column: "stringValue",
+ label: "String Value",
+ },
{
type: "string" as const,
column: "traceId",
diff --git a/web/src/server/api/definitions/scoresTable.ts b/web/src/server/api/definitions/scoresTable.ts
index 410eea20d5..3f648a223f 100644
--- a/web/src/server/api/definitions/scoresTable.ts
+++ b/web/src/server/api/definitions/scoresTable.ts
@@ -62,6 +62,14 @@ export const scoresTableCols: ColumnDefinition[] = [
options: [], // to be added at runtime
},
{ name: "Value", id: "value", type: "number", internal: 's."value"' },
+ {
+ name: "String Value",
+ id: "stringValue",
+ type: "stringOptions",
+ internal: 's."string_value"',
+ options: [], // to be added at runtime
+ nullable: true,
+ },
{
name: "User ID",
id: "userId",
@@ -85,6 +93,7 @@ export type ScoreOptions = {
tags: Array<SingleValueOption>;
traceName: Array<SingleValueOption>;
userId: Array<SingleValueOption>;
+ stringValue: Array<SingleValueOption>;
};
export function scoresTableColsWithOptions(
@@ -103,6 +112,9 @@ export function scoresTableColsWithOptions(
if (col.id === "userId") {
return formatColumnOptions(col, options?.userId ?? []);
}
+ if (col.id === "stringValue") {
+ return formatColumnOptions(col, options?.stringValue ?? []);
+ }
return col;
});
}
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index 4907b3fe21..e6d76fe64e 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -37,6 +37,7 @@ import {
getScoresUiCount,
getScoresUiTable,
getScoreNames,
+ getScoreStringValues,
getTracesGroupedByTags,
getTracesGroupedByName,
getTracesGroupedByUsers,
@@ -321,25 +322,27 @@ export const scoresRouter = createTRPCRouter({
)
.query(async ({ input }) => {
const { timestampFilter } = input;
- const [names, tags, traceNames, userIds] = await Promise.all([
- getScoreNames(input.projectId, timestampFilter ?? []),
- getTracesGroupedByTags({
- projectId: input.projectId,
- filter: timestampFilter ?? [],
- }),
- getTracesGroupedByName(
- input.projectId,
- tracesTableUiColumnDefinitions,
- timestampFilter ?? [],
- ),
- getTracesGroupedByUsers(
- input.projectId,
- timestampFilter ?? [],
- undefined,
- 100, // limit to top 100 users
- 0,
- ),
- ]);
+ const [names, tags, traceNames, userIds, stringValues] =
+ await Promise.all([
+ getScoreNames(input.projectId, timestampFilter ?? []),
+ getTracesGroupedByTags({
+ projectId: input.projectId,
+ filter: timestampFilter ?? [],
+ }),
+ getTracesGroupedByName(
+ input.projectId,
+ tracesTableUiColumnDefinitions,
+ timestampFilter ?? [],
+ ),
+ getTracesGroupedByUsers(
+ input.projectId,
+ timestampFilter ?? [],
+ undefined,
+ 100, // limit to top 100 users
+ 0,
+ ),
+ getScoreStringValues(input.projectId, timestampFilter ?? []),
+ ]);
return {
name: names.map((i) => ({ value: i.name, count: i.count })),
@@ -349,6 +352,7 @@ export const scoresRouter = createTRPCRouter({
count: tn.count,
})),
userId: userIds.map((u) => ({ value: u.user, count: u.count })),
+ stringValue: stringValues,
};
}),
deleteMany: protectedProjectProcedure