Skip to content

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

feat: add tag filter to generations table (#3022)

Richard Krümmel834d0bcc7a2c
diff --git a/packages/shared/src/observationsTable.ts b/packages/shared/src/observationsTable.ts
index 524f0d9f29..2c4b520a98 100644
--- a/packages/shared/src/observationsTable.ts
+++ b/packages/shared/src/observationsTable.ts
@@ -172,6 +172,13 @@ export const observationsTableCols: ColumnDefinition[] = [
internal: "p.version",
nullable: true,
},
+ {
+ name: "Trace Tags",
+ id: "tags",
+ type: "arrayOptions",
+ internal: "t.tags",
+ options: [], // to be added at runtime
+ },
];
// to be used client side, insert options for use in filter-builder
@@ -182,6 +189,7 @@ export type ObservationOptions = {
traceName: Array<OptionsDefinition>;
scores_avg: Array<string>;
promptName: Array<OptionsDefinition>;
+ tags: Array<OptionsDefinition>;
};
export function observationsTableColsWithOptions(
@@ -203,6 +211,9 @@ export function observationsTableColsWithOptions(
if (col.id === "promptName") {
return { ...col, options: options?.promptName ?? [] };
}
+ if (col.id === "tags") {
+ return { ...col, options: options?.tags ?? [] };
+ }
return col;
});
}
diff --git a/packages/shared/src/queries/createSessionsAllQuery.ts b/packages/shared/src/queries/createSessionsAllQuery.ts
index 900993f2d1..46a3c1a9db 100644
--- a/packages/shared/src/queries/createSessionsAllQuery.ts
+++ b/packages/shared/src/queries/createSessionsAllQuery.ts
@@ -50,9 +50,12 @@ export const createSessionsAllQuery = (
MIN(t. "timestamp") AS "min_timestamp",
array_agg(t.id) AS "traceIds",
array_agg(DISTINCT t.user_id) AS "userIds",
- count(t.id)::int AS "countTraces"
+ count(t.id)::int AS "countTraces",
+ array_agg(DISTINCT u.tag) AS "tags"
FROM
traces t
+ LEFT JOIN LATERAL (
+ SELECT DISTINCT UNNEST(t.tags) AS tag) AS u ON TRUE
WHERE
t.project_id = ${projectId}
AND t.session_id = s.id
diff --git a/packages/shared/src/tableDefinitions/sessionsView.ts b/packages/shared/src/tableDefinitions/sessionsView.ts
index 61e46b88b8..04229ebc46 100644
--- a/packages/shared/src/tableDefinitions/sessionsView.ts
+++ b/packages/shared/src/tableDefinitions/sessionsView.ts
@@ -79,10 +79,18 @@ export const sessionsViewCols: ColumnDefinition[] = [
type: "number",
internal: 'o."totalTokens"',
},
+ {
+ name: "Trace Tags",
+ id: "tags",
+ type: "arrayOptions",
+ internal: 't."tags"',
+ options: [], // to be filled in at runtime
+ },
];
export type SessionOptions = {
userIds: Array<OptionsDefinition>;
+ tags: Array<OptionsDefinition>;
};
export function sessionsTableColsWithOptions(
@@ -92,6 +100,9 @@ export function sessionsTableColsWithOptions(
if (col.id === "userIds") {
return { ...col, options: options?.userIds ?? [] };
}
+ if (col.id === "tags") {
+ return { ...col, options: options?.tags ?? [] };
+ }
return col;
});
}
diff --git a/web/src/components/table/use-cases/sessions.tsx b/web/src/components/table/use-cases/sessions.tsx
index ebbbb8393f..cb5f71fe82 100644
--- a/web/src/components/table/use-cases/sessions.tsx
+++ b/web/src/components/table/use-cases/sessions.tsx
@@ -135,6 +135,10 @@ export default function SessionsTable({
const filterOptions = api.sessions.filterOptions.useQuery(
{
projectId,
+ timestampFilter:
+ dateRangeFilter[0]?.type === "datetime"
+ ? dateRangeFilter[0]
+ : undefined,
},
{
trpc: {
diff --git a/web/src/server/api/routers/generations/filterOptionsQuery.ts b/web/src/server/api/routers/generations/filterOptionsQuery.ts
index e772739213..ab2a17160a 100644
--- a/web/src/server/api/routers/generations/filterOptionsQuery.ts
+++ b/web/src/server/api/routers/generations/filterOptionsQuery.ts
@@ -42,40 +42,41 @@ export const filterOptionsQuery = protectedProjectProcedure
: Prisma.empty;
// Score names
- const [scores, model, name, promptNames, traceNames] = await Promise.all([
- // scores
- ctx.prisma.score.groupBy({
- where: {
- projectId: input.projectId,
- timestamp: prismaStartTimeFilter,
- dataType: { in: ["NUMERIC", "BOOLEAN"] },
- },
- take: 1000,
- orderBy: {
- name: "asc",
- },
- by: ["name"],
- }),
- // model
- ctx.prisma.observation.groupBy({
- by: ["model"],
- where: { ...queryFilter, startTime: prismaStartTimeFilter },
- take: 1000,
- orderBy: { model: "asc" },
- }),
- // name
- ctx.prisma.observation.groupBy({
- by: ["name"],
- where: { ...queryFilter, startTime: prismaStartTimeFilter },
- take: 1000,
- orderBy: { name: "asc" },
- }),
- // promptNames
- ctx.prisma.$queryRaw<
- Array<{
- promptName: string | null;
- }>
- >(Prisma.sql`
+ const [scores, model, name, promptNames, traceNames, tags] =
+ await Promise.all([
+ // scores
+ ctx.prisma.score.groupBy({
+ where: {
+ projectId: input.projectId,
+ timestamp: prismaStartTimeFilter,
+ dataType: { in: ["NUMERIC", "BOOLEAN"] },
+ },
+ take: 1000,
+ orderBy: {
+ name: "asc",
+ },
+ by: ["name"],
+ }),
+ // model
+ ctx.prisma.observation.groupBy({
+ by: ["model"],
+ where: { ...queryFilter, startTime: prismaStartTimeFilter },
+ take: 1000,
+ orderBy: { model: "asc" },
+ }),
+ // name
+ ctx.prisma.observation.groupBy({
+ by: ["name"],
+ where: { ...queryFilter, startTime: prismaStartTimeFilter },
+ take: 1000,
+ orderBy: { name: "asc" },
+ }),
+ // promptNames
+ ctx.prisma.$queryRaw<
+ Array<{
+ promptName: string | null;
+ }>
+ >(Prisma.sql`
SELECT
p.name "promptName"
FROM prompts p
@@ -89,12 +90,12 @@ export const filterOptionsQuery = protectedProjectProcedure
ORDER BY p.name ASC
LIMIT 1000;
`),
- // traceNames
- ctx.prisma.$queryRaw<
- Array<{
- traceName: string | null;
- }>
- >(Prisma.sql`
+ // traceNames
+ ctx.prisma.$queryRaw<
+ Array<{
+ traceName: string | null;
+ }>
+ >(Prisma.sql`
SELECT
t.name "traceName"
FROM traces t
@@ -107,7 +108,24 @@ export const filterOptionsQuery = protectedProjectProcedure
ORDER BY t.name ASC
LIMIT 1000;
`),
- ]);
+ // traceTags
+ ctx.prisma.$queryRaw<
+ Array<{
+ tag: string | null;
+ }>
+ >(Prisma.sql`
+ SELECT
+ DISTINCT tag
+ FROM traces t
+ JOIN observations o ON o.trace_id = t.id,
+ UNNEST(t.tags) AS tag
+ WHERE o.type = 'GENERATION'
+ AND o.project_id = ${input.projectId}
+ AND t.project_id = ${input.projectId}
+ ${rawStartTimeFilter}
+ LIMIT 1000;
+ `),
+ ]);
// typecheck filter options, needs to include all columns with options
const res: ObservationOptions = {
@@ -128,6 +146,11 @@ export const filterOptionsQuery = protectedProjectProcedure
.map((i) => ({
value: i.promptName as string,
})),
+ tags: tags
+ .filter((i) => i.tag !== null)
+ .map((i) => ({
+ value: i.tag as string,
+ })),
};
return res;
diff --git a/web/src/server/api/routers/sessions.ts b/web/src/server/api/routers/sessions.ts
index ddbdd3dd86..b92d7b9382 100644
--- a/web/src/server/api/routers/sessions.ts
+++ b/web/src/server/api/routers/sessions.ts
@@ -14,6 +14,8 @@ import {
paginationZod,
type SessionOptions,
singleFilter,
+ timeFilter,
+ datetimeFilterToPrismaSql,
} from "@langfuse/shared";
import { Prisma } from "@langfuse/shared/src/db";
import { TRPCError } from "@trpc/server";
@@ -170,27 +172,52 @@ export const sessionRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(),
+ timestampFilter: timeFilter.optional(),
}),
)
.query(async ({ input, ctx }) => {
try {
- const userIds = await ctx.prisma.$queryRaw<
- Array<{ value: string }>
- >(Prisma.sql`
- SELECT
- traces.user_id AS value
- FROM traces
- WHERE
- traces.session_id IS NOT NULL
- AND traces.user_id IS NOT NULL
- AND traces.project_id = ${input.projectId}
- GROUP BY traces.user_id
- ORDER BY traces.user_id ASC
- LIMIT 1000;
- `);
+ const { timestampFilter } = input;
+ const rawTimestampFilter =
+ timestampFilter && timestampFilter.type === "datetime"
+ ? datetimeFilterToPrismaSql(
+ "timestamp",
+ timestampFilter.operator,
+ timestampFilter.value,
+ )
+ : Prisma.empty;
+ const [userIds, tags] = await Promise.all([
+ ctx.prisma.$queryRaw<Array<{ value: string }>>(Prisma.sql`
+ SELECT
+ traces.user_id AS value
+ FROM traces
+ WHERE
+ traces.session_id IS NOT NULL
+ AND traces.user_id IS NOT NULL
+ AND traces.project_id = ${input.projectId} ${rawTimestampFilter}
+ GROUP BY traces.user_id
+ ORDER BY traces.user_id ASC
+ LIMIT 1000;
+ `),
+ ctx.prisma.$queryRaw<
+ Array<{
+ value: string;
+ }>
+ >(Prisma.sql`
+ SELECT DISTINCT tag AS value
+ FROM traces t
+ JOIN observations o ON o.trace_id = t.id,
+ UNNEST(t.tags) AS tag
+ WHERE o.type = 'GENERATION'
+ AND o.project_id = ${input.projectId}
+ AND t.project_id = ${input.projectId} ${rawTimestampFilter}
+ LIMIT 1000;
+ `),
+ ]);
const res: SessionOptions = {
userIds: userIds,
+ tags: tags,
};
return res;
} catch (e) {