Skip to content

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

feat: add tags filter to scores table (#3071)

Richard Krümmela330035c403d
diff --git a/packages/shared/src/filterToPrisma.ts b/packages/shared/src/filterToPrisma.ts
index 24eed028e9..7dbf1a6739 100644
--- a/packages/shared/src/filterToPrisma.ts
+++ b/packages/shared/src/filterToPrisma.ts
@@ -1,7 +1,8 @@
import { Prisma } from "@prisma/client";
import { ColumnDefinition, type TableNames } from "./tableDefinitions";
import { FilterState } from "./types";
-import { filterOperators } from "./interfaces/filters";
+import { filterOperators, timeFilter } from "./interfaces/filters";
+import { z } from "zod";
const operatorReplacements = {
"any of": "IN",
@@ -179,3 +180,19 @@ export const datetimeFilterToPrismaSql = (
operator
)} ${value}::timestamp with time zone at time zone 'UTC'`;
};
+
+export const datetimeFilterToPrisma = (
+ timestampFilter: z.infer<typeof timeFilter>
+) => {
+ const prismaTimestampFilter =
+ timestampFilter.operator === ">="
+ ? { gte: timestampFilter.value }
+ : timestampFilter.operator === ">"
+ ? { gt: timestampFilter.value }
+ : timestampFilter.operator === "<="
+ ? { lte: timestampFilter.value }
+ : timestampFilter.operator === "<"
+ ? { lt: timestampFilter.value }
+ : {};
+ return prismaTimestampFilter;
+};
diff --git a/web/src/components/table/use-cases/scores.tsx b/web/src/components/table/use-cases/scores.tsx
index d9ba20dee6..d851a093a5 100644
--- a/web/src/components/table/use-cases/scores.tsx
+++ b/web/src/components/table/use-cases/scores.tsx
@@ -143,6 +143,10 @@ export default function ScoresTable({
const filterOptions = api.scores.filterOptions.useQuery(
{
projectId,
+ timestampFilter:
+ dateRangeFilter[0]?.type === "datetime"
+ ? dateRangeFilter[0]
+ : undefined,
},
{
trpc: {
diff --git a/web/src/server/api/definitions/scoresTable.ts b/web/src/server/api/definitions/scoresTable.ts
index a1d9102017..a221fcb918 100644
--- a/web/src/server/api/definitions/scoresTable.ts
+++ b/web/src/server/api/definitions/scoresTable.ts
@@ -17,6 +17,7 @@ export const scoresTableCols: ColumnDefinition[] = [
id: "traceName",
type: "string",
internal: 't."name"',
+ nullable: true,
},
{
name: "Observation ID",
@@ -52,17 +53,33 @@ export const scoresTableCols: ColumnDefinition[] = [
options: [], // to be added at runtime
},
{ name: "Value", id: "value", type: "number", internal: 's."value"' },
- { name: "User ID", id: "userId", type: "string", internal: 't."user_id"' },
+ {
+ name: "User ID",
+ id: "userId",
+ type: "string",
+ internal: 't."user_id"',
+ nullable: true,
+ },
{
name: "Eval Configuration ID",
id: "jobConfigurationId",
type: "string",
internal: 'je."job_configuration_id"',
+ nullable: true,
+ },
+ {
+ name: "Trace Tags",
+ id: "tags",
+ type: "arrayOptions",
+ internal: 't."tags"',
+ options: [], // to be added at runtime
+ nullable: true,
},
];
export type ScoreOptions = {
name: Array<OptionsDefinition>;
+ tags: Array<OptionsDefinition>;
};
export function scoresTableColsWithOptions(
@@ -72,6 +89,9 @@ export function scoresTableColsWithOptions(
if (col.id === "name") {
return { ...col, options: options?.name ?? [] };
}
+ if (col.id === "tags") {
+ return { ...col, options: options?.tags ?? [] };
+ }
return col;
});
}
diff --git a/web/src/server/api/routers/generations/filterOptionsQuery.ts b/web/src/server/api/routers/generations/filterOptionsQuery.ts
index ab2a17160a..b57d4cbf1c 100644
--- a/web/src/server/api/routers/generations/filterOptionsQuery.ts
+++ b/web/src/server/api/routers/generations/filterOptionsQuery.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import {
+ datetimeFilterToPrisma,
datetimeFilterToPrismaSql,
timeFilter,
type ObservationOptions,
@@ -14,18 +15,9 @@ export const filterOptionsQuery = protectedProjectProcedure
)
.query(async ({ input, ctx }) => {
const { startTimeFilter } = input;
- const prismaStartTimeFilter =
- startTimeFilter?.type === "datetime"
- ? startTimeFilter?.operator === ">="
- ? { gte: startTimeFilter.value }
- : startTimeFilter?.operator === ">"
- ? { gt: startTimeFilter.value }
- : startTimeFilter?.operator === "<="
- ? { lte: startTimeFilter.value }
- : startTimeFilter?.operator === "<"
- ? { lt: startTimeFilter.value }
- : {}
- : {};
+ const prismaStartTimeFilter = startTimeFilter
+ ? datetimeFilterToPrisma(startTimeFilter)
+ : {};
const queryFilter = {
projectId: input.projectId,
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index 672d591b4a..bceb2efc85 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -17,11 +17,14 @@ import {
} from "@/src/server/api/trpc";
import {
CreateAnnotationScoreData,
+ datetimeFilterToPrismaSql,
+ datetimeFilterToPrisma,
orderBy,
orderByToPrismaSql,
paginationZod,
singleFilter,
tableColumnsToSqlFilterAndPrefix,
+ timeFilter,
UpdateAnnotationScoreData,
validateDbScore,
} from "@langfuse/shared";
@@ -115,27 +118,53 @@ export const scoresRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(),
+ timestampFilter: timeFilter.optional(),
}),
)
.query(async ({ input, ctx }) => {
- const names = await ctx.prisma.score.groupBy({
- where: {
- projectId: input.projectId,
- },
- by: ["name"],
- _count: {
- _all: true,
- },
- take: 1000,
- orderBy: {
+ const { timestampFilter } = input;
+ const prismaTimestampFilter = timestampFilter
+ ? datetimeFilterToPrisma(timestampFilter)
+ : {};
+
+ const rawTimestampFilter =
+ timestampFilter && timestampFilter.type === "datetime"
+ ? datetimeFilterToPrismaSql(
+ "timestamp",
+ timestampFilter.operator,
+ timestampFilter.value,
+ )
+ : Prisma.empty;
+ const [names, tags] = await Promise.all([
+ ctx.prisma.score.groupBy({
+ where: {
+ projectId: input.projectId,
+ timestamp: prismaTimestampFilter,
+ },
+ by: ["name"],
_count: {
- id: "desc",
+ _all: true,
},
- },
- });
+ take: 1000,
+ orderBy: {
+ _count: {
+ id: "desc",
+ },
+ },
+ }),
+ ctx.prisma.$queryRaw<{ value: string }[]>`
+ SELECT tags.tag as value
+ FROM traces, UNNEST(traces.tags) AS tags(tag)
+ WHERE traces.project_id = ${input.projectId} ${rawTimestampFilter}
+ GROUP BY tags.tag
+ ORDER BY tags.tag ASC
+ LIMIT 1000
+ `,
+ ]);
const res: ScoreOptions = {
name: names.map((i) => ({ value: i.name, count: i._count._all })),
+ tags: tags,
};
return res;
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index cf3c2ddf83..caba65c069 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -9,6 +9,7 @@ import {
protectedProjectProcedure,
} from "@/src/server/api/trpc";
import {
+ datetimeFilterToPrisma,
datetimeFilterToPrismaSql,
filterAndValidateDbScoreList,
orderBy,
@@ -195,18 +196,9 @@ export const traceRouter = createTRPCRouter({
)
.query(async ({ input, ctx }) => {
const { timestampFilter } = input;
- const prismaTimestampFilter =
- timestampFilter?.type === "datetime"
- ? timestampFilter?.operator === ">="
- ? { gte: timestampFilter.value }
- : timestampFilter?.operator === ">"
- ? { gt: timestampFilter.value }
- : timestampFilter?.operator === "<="
- ? { lte: timestampFilter.value }
- : timestampFilter?.operator === "<"
- ? { lt: timestampFilter.value }
- : {}
- : {};
+ const prismaTimestampFilter = timestampFilter
+ ? datetimeFilterToPrisma(timestampFilter)
+ : {};
const rawTimestampFilter =
timestampFilter && timestampFilter.type === "datetime"