Skip to content

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

feat(prompts): extend prompts table (#1346)

Richard Krümmel8155749b2be0
diff --git a/web/src/__tests__/generation-exports.servertest.ts b/web/src/__tests__/generation-exports.servertest.ts
index 62813c280f..17d9b97bbb 100644
--- a/web/src/__tests__/generation-exports.servertest.ts
+++ b/web/src/__tests__/generation-exports.servertest.ts
@@ -95,7 +95,7 @@ describe("observations.export RPC", () => {
orderBy: { column: "id", order: "ASC" },
filter: [
{
- column: "start_time",
+ column: "Start Time",
type: "datetime",
operator: ">",
value: new Date("1990-01-01"),
@@ -123,7 +123,7 @@ describe("observations.export RPC", () => {
orderBy: { column: "id", order: "ASC" },
filter: [
{
- column: "start_time",
+ column: "Start Time",
type: "datetime",
operator: ">",
value: new Date("1990-01-01"),
@@ -150,7 +150,7 @@ describe("observations.export RPC", () => {
orderBy: { column: "id", order: "ASC" },
filter: [
{
- column: "start_time",
+ column: "Start Time",
type: "datetime",
operator: ">",
value: new Date("1990-01-01"),
@@ -179,7 +179,7 @@ describe("observations.export RPC", () => {
orderBy: { column: "id", order: "ASC" },
filter: [
{
- column: "start_time",
+ column: "Start Time",
type: "datetime",
operator: ">",
value: new Date("1990-01-01"),
diff --git a/web/src/components/table/use-cases/generations.tsx b/web/src/components/table/use-cases/generations.tsx
index f6fcc56d6d..80a0f0bbd8 100644
--- a/web/src/components/table/use-cases/generations.tsx
+++ b/web/src/components/table/use-cases/generations.tsx
@@ -93,7 +93,7 @@ export default function GenerationsTable({ projectId }: GenerationsTableProps) {
const [filterState, setFilterState] = useQueryFilterState([
{
- column: "start_time",
+ column: "Start Time",
type: "datetime",
operator: ">",
value: utcDateOffsetByDays(-14),
diff --git a/web/src/features/prompts/components/prompts-table.tsx b/web/src/features/prompts/components/prompts-table.tsx
index 8cb322636d..b737fa0f29 100644
--- a/web/src/features/prompts/components/prompts-table.tsx
+++ b/web/src/features/prompts/components/prompts-table.tsx
@@ -18,6 +18,7 @@ type RowData = {
id: string;
createdAt: Date;
isActive: boolean;
+ numberOfObservations: number;
};
export function PromptTable(props: { projectId: string }) {
@@ -72,6 +73,25 @@ export function PromptTable(props: { projectId: string }) {
return createdAt.toLocaleString();
},
},
+ {
+ accessorKey: "numberOfObservations",
+ header: "Number of Generations",
+ cell: ({ row }) => {
+ const numberOfObservations: number = row.getValue(
+ "numberOfObservations",
+ );
+ const name: string = row.getValue("name");
+ const filter = encodeURIComponent(
+ `Prompt Name;stringOptions;;any of;${name}`,
+ );
+ return (
+ <TableLink
+ path={`/project/${props.projectId}/generations?filter=${numberOfObservations ? filter : ""}`}
+ value={numberOfObservations.toLocaleString()}
+ />
+ );
+ },
+ },
{
accessorKey: "actions",
header: "Actions",
@@ -95,6 +115,7 @@ export function PromptTable(props: { projectId: string }) {
version: item.version,
createdAt: item.createdAt,
isActive: item.isActive,
+ numberOfObservations: Number(item.observationCount),
};
};
diff --git a/web/src/features/prompts/server/prompt-router.ts b/web/src/features/prompts/server/prompt-router.ts
index 9c094bc2e3..6738a70aab 100644
--- a/web/src/features/prompts/server/prompt-router.ts
+++ b/web/src/features/prompts/server/prompt-router.ts
@@ -8,6 +8,7 @@ import { throwIfNoAccess } from "@/src/features/rbac/utils/checkAccess";
import { type Prompt, type PrismaClient } from "@prisma/client";
import { jsonSchema } from "@/src/utils/zod";
import { auditLog } from "@/src/features/audit-logs/auditLog";
+import { DB } from "@/src/server/db";
export const CreatePrompt = z.object({
projectId: z.string(),
@@ -30,8 +31,17 @@ export const promptRouter = createTRPCRouter({
projectId: input.projectId,
scope: "prompts:read",
});
+
const prompts = await ctx.prisma.$queryRaw<Array<Prompt>>`
- SELECT id, name, version, project_id as "projectId", prompt, updated_at as "updatedAt", created_at AS "createdAt", is_active AS "isActive"
+ SELECT
+ id,
+ name,
+ version,
+ project_id AS "projectId",
+ prompt,
+ updated_at AS "updatedAt",
+ created_at AS "createdAt",
+ is_active AS "isActive"
FROM prompts
WHERE (name, version) IN (
SELECT name, MAX(version)
@@ -41,7 +51,34 @@ export const promptRouter = createTRPCRouter({
)
AND "project_id" = ${input.projectId}
ORDER BY name ASC`;
- return prompts;
+
+ const promptCountQuery = DB.selectFrom("observations")
+ .fullJoin("prompts", "prompts.id", "observations.prompt_id")
+ .select(({ fn }) => [
+ "prompts.name",
+ fn.count("observations.id").as("count"),
+ ])
+ .where("prompts.project_id", "=", input.projectId)
+ .groupBy("prompts.name");
+
+ const compiledQuery = promptCountQuery.compile();
+
+ const promptCounts = await ctx.prisma.$queryRawUnsafe<
+ Array<{
+ name: string;
+ count: bigint;
+ }>
+ >(compiledQuery.sql, ...compiledQuery.parameters);
+
+ const joinedPromptsAndCounts = prompts.map((p) => {
+ const marchedCount = promptCounts.find((c) => c.name === p.name);
+ return {
+ ...p,
+ observationCount: marchedCount?.count ?? 0,
+ };
+ });
+
+ return joinedPromptsAndCounts;
}),
byId: protectedProjectProcedure
.input(
diff --git a/web/src/server/api/definitions/observationsTable.ts b/web/src/server/api/definitions/observationsTable.ts
index a70ffe1556..63934e5da6 100644
--- a/web/src/server/api/definitions/observationsTable.ts
+++ b/web/src/server/api/definitions/observationsTable.ts
@@ -7,13 +7,13 @@ import { ObservationLevel } from "@prisma/client";
// to be used server side
export const observationsTableCols: ColumnDefinition[] = [
{
- name: "id",
+ name: "Id",
id: "id",
type: "string",
internal: 'o."id"',
},
{
- name: "name",
+ name: "Name",
id: "name",
type: "stringOptions",
internal: 'o."name"',
@@ -21,7 +21,7 @@ export const observationsTableCols: ColumnDefinition[] = [
},
{ name: "traceId", id: "traceId", type: "string", internal: 't."id"' },
{
- name: "traceName",
+ name: "Trace Name",
id: "traceName",
type: "stringOptions",
internal: 't."name"',
@@ -29,30 +29,30 @@ export const observationsTableCols: ColumnDefinition[] = [
},
{ name: "userId", id: "userId", type: "string", internal: 't."user_id"' },
{
- name: "start_time",
+ name: "Start Time",
id: "startTime",
type: "datetime",
internal: 'o."start_time"',
},
{
- name: "end_time",
+ name: "End Time",
id: "endTime",
type: "datetime",
internal: 'o."end_time"',
},
{
- name: "latency (s)",
+ name: "Latency (s)",
id: "latency",
type: "number",
internal: '"latency"',
},
{
- name: "Cost ($)",
+ name: "Total Cost ($)",
type: "number",
internal: 'o."calculated_total_cost"',
},
{
- name: "level",
+ name: "Level",
id: "level",
type: "stringOptions",
internal: 'o."level"::text',
@@ -65,30 +65,37 @@ export const observationsTableCols: ColumnDefinition[] = [
internal: 'o."status_message"',
},
{
- name: "model",
+ name: "Model",
id: "model",
type: "stringOptions",
internal: 'o."model"',
options: [], // to be added at runtime
},
{
- name: "metadata",
+ name: "Metadata",
id: "metadata",
type: "stringObject",
internal: 'o."metadata"',
},
{
- name: "scores_avg",
+ name: "Scores",
id: "scores_avg",
type: "numberObject",
internal: "scores_avg",
},
{
- name: "version",
+ name: "Version",
id: "version",
type: "string",
internal: 'o."version"',
},
+ {
+ name: "Prompt Name",
+ id: "promptName",
+ type: "stringOptions",
+ internal: "p.name",
+ options: [], // to be added at runtime
+ },
];
// to be used client side, insert options for use in filter-builder
@@ -98,24 +105,28 @@ export type ObservationOptions = {
name: Array<OptionsDefinition>;
traceName: Array<OptionsDefinition>;
scores_avg: Array<string>;
+ promptName: Array<OptionsDefinition>;
};
export function observationsTableColsWithOptions(
options?: ObservationOptions,
): ColumnDefinition[] {
return observationsTableCols.map((col) => {
- if (col.name === "model") {
+ if (col.id === "model") {
return { ...col, options: options?.model ?? [] };
}
- if (col.name === "name") {
+ if (col.id === "name") {
return { ...col, options: options?.name ?? [] };
}
- if (col.name === "traceName") {
+ if (col.id === "traceName") {
return { ...col, options: options?.traceName ?? [] };
}
- if (col.name === "scores_avg") {
+ if (col.id === "scores_avg") {
return { ...col, keyOptions: options?.scores_avg ?? [] };
}
+ if (col.id === "promptName") {
+ return { ...col, options: options?.promptName ?? [] };
+ }
return col;
});
}
diff --git a/web/src/server/api/routers/generations/exportQuery/index.ts b/web/src/server/api/routers/generations/exportQuery/index.ts
index 05196859ff..aedf784482 100644
--- a/web/src/server/api/routers/generations/exportQuery/index.ts
+++ b/web/src/server/api/routers/generations/exportQuery/index.ts
@@ -39,7 +39,7 @@ export const generationsExportQuery = protectedProjectProcedure
const queryPageSize = env.DB_EXPORT_PAGE_SIZE ?? 1000;
const dateCutoffFilter = {
- column: "start_time",
+ column: "Start Time",
operator: "<" as const,
value: new Date(),
type: "datetime" as const,
diff --git a/web/src/server/api/routers/generations/filterOptionsQuery.ts b/web/src/server/api/routers/generations/filterOptionsQuery.ts
index 616ed129ac..7ee4376bc5 100644
--- a/web/src/server/api/routers/generations/filterOptionsQuery.ts
+++ b/web/src/server/api/routers/generations/filterOptionsQuery.ts
@@ -31,6 +31,23 @@ export const filterOptionsQuery = protectedProjectProcedure
where: queryFilter,
_count: { _all: true },
});
+ const promptNames = await ctx.prisma.$queryRaw<
+ Array<{
+ promptName: string | null;
+ count: number;
+ }>
+ >(Prisma.sql`
+ SELECT
+ p.name "promptName",
+ count(*)::int AS count
+ FROM prompts p
+ JOIN observations o ON o.prompt_id = p.id
+ WHERE o.type = 'GENERATION'
+ AND o.project_id = ${input.projectId}
+ AND o.prompt_id IS NOT NULL
+ AND p.project_id = ${input.projectId}
+ GROUP BY 1
+ `);
const traceName = await ctx.prisma.$queryRaw<
Array<{
traceName: string | null;
@@ -70,6 +87,12 @@ export const filterOptionsQuery = protectedProjectProcedure
count: i.count,
})),
scores_avg: scores.map((score) => score.name),
+ promptName: promptNames
+ .filter((i) => i.promptName !== null)
+ .map((i) => ({
+ value: i.promptName as string,
+ count: i.count,
+ })),
};
return res;
diff --git a/web/src/server/api/routers/generations/getAllQuery.ts b/web/src/server/api/routers/generations/getAllQuery.ts
index b5d727b32a..97e215321d 100644
--- a/web/src/server/api/routers/generations/getAllQuery.ts
+++ b/web/src/server/api/routers/generations/getAllQuery.ts
@@ -41,6 +41,7 @@ export const getAllQuery = protectedProjectProcedure
count(*)
FROM observations_view o
JOIN traces t ON t.id = o.trace_id AND t.project_id = o.project_id
+ LEFT JOIN prompts p ON p.id = o.prompt_id
LEFT JOIN LATERAL (
SELECT
jsonb_object_agg(name::text, avg_value::double precision) AS "scores_avg"