🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
add metrics
Marc Klingen91edf09c773c
diff --git a/src/components/layouts/layout.tsx b/src/components/layouts/layout.tsxindex 110d14160f..8219fe4d0c 100644--- a/src/components/layouts/layout.tsx+++ b/src/components/layouts/layout.tsx@@ -16,13 +16,14 @@ import {import Link from "next/link";import { useRouter } from "next/router";import clsx from "clsx";-import { Joystick } from "lucide-react";+import { Joystick, LineChart } from "lucide-react";const navigationPaths = [{ name: "Dashboard", href: "/", icon: HomeIcon },{ name: "Traces", href: "/traces", icon: UsersIcon },{ name: "LLM Calls", href: "/llm-calls", icon: DocumentDuplicateIcon },- { name: "Playground", href: "/playground", icon: Joystick },+ { name: "Metrics", href: "/metrics", icon: LineChart },+ { name: "Playground (soon)", href: "#", icon: Joystick },];const teams = [{ id: 1, name: "Heroicons", href: "#", initial: "H", current: false },diff --git a/src/components/ui/descriptionLists.tsx b/src/components/ui/descriptionLists.tsxindex e5149b95fa..58cd4e0422 100644--- a/src/components/ui/descriptionLists.tsx+++ b/src/components/ui/descriptionLists.tsx@@ -22,7 +22,7 @@ export default function DescriptionList(props: {</p></div>) : null}- <div className={clsx("mt-6", props.header && "border-t border-gray-100")}>+ <div className={clsx(props.header && "mt-6 border-t border-gray-100")}><dl className="divide-y divide-gray-100">{props.items.map((item) => (<divdiff --git a/src/pages/llm-calls/[llmCallId].tsx b/src/pages/llm-calls/[llmCallId].tsxindex 09455708ac..eed74478d8 100644--- a/src/pages/llm-calls/[llmCallId].tsx+++ b/src/pages/llm-calls/[llmCallId].tsx@@ -16,6 +16,14 @@ export default function LlmCallPage() {enabled: llmCallId !== undefined,});+ const obsMetrics =+ llmCall.data?.metrics.filter(+ (metric) => metric.observationId === llmCall.data.id+ ) ?? [];+ const traceMetrics =+ llmCall.data?.metrics.filter((metric) => !obsMetrics?.includes(metric)) ??+ [];+return (<><Header@@ -81,6 +89,28 @@ export default function LlmCallPage() {label: "Attributes",value: <JSONview json={llmCall.data.attributes} />,},+ {+ label: "Metrics (observation)",+ value: (+ <DescriptionList+ items={obsMetrics.map((metric) => ({+ label: metric.name,+ value: metric.value,+ }))}+ />+ ),+ },+ {+ label: "Metrics (trace)",+ value: (+ <DescriptionList+ items={traceMetrics.map((metric) => ({+ label: metric.name,+ value: metric.value,+ }))}+ />+ ),+ },]}/>) : null}diff --git a/src/pages/metrics.tsx b/src/pages/metrics.tsxnew file mode 100644index 0000000000..a9b9e5dae8--- /dev/null+++ b/src/pages/metrics.tsx@@ -0,0 +1,103 @@+import Header from "~/components/layouts/header";++import {+ DataGrid,+ type GridRowsProp,+ type GridColDef,+ type GridRowParams,+ GridToolbar,+} from "@mui/x-data-grid";+import { api } from "~/utils/api";+import { useRouter } from "next/router";++export default function MetricsPage() {+ const llmCalls = api.llmCalls.all.useQuery();+ const router = useRouter();++ const columns: GridColDef[] = [+ {+ field: "id",+ type: "actions",+ headerName: "ID",+ width: 100,+ getActions: (params: GridRowParams) => [+ <button+ key="openLlmCall"+ className="rounded bg-indigo-50 px-2 py-1 text-xs font-semibold text-indigo-600 shadow-sm hover:bg-indigo-100"+ onClick={() =>+ void router.push(`/llm-calls/${params.row.id as string}`)+ }+ >+ ...{lastCharacters(params.row.id as string, 7)}+ </button>,+ ],+ },+ {+ field: "traceId",+ type: "actions",+ headerName: "Trace",+ width: 100,+ getActions: (params: GridRowParams) => [+ <button+ key="openTrace"+ className="rounded bg-indigo-50 px-2 py-1 text-xs font-semibold text-indigo-600 shadow-sm hover:bg-indigo-100"+ onClick={() =>+ void router.push(`/traces/${params.row.traceId as string}`)+ }+ >+ ...{lastCharacters(params.row.traceId as string, 7)}+ </button>,+ ],+ },+ {+ field: "startTime",+ type: "dateTime",+ headerName: "Start time",+ width: 170,+ },+ { field: "name", headerName: "Name", minWidth: 200 },+ {+ field: "prompt",+ headerName: "Prompt",+ flex: 1,+ },+ {+ field: "completion",+ headerName: "Completion",+ flex: 1,+ },+ {+ field: "model",+ headerName: "Model",+ flex: 1,+ },+ ];++ const rows: GridRowsProp = llmCalls.isSuccess+ ? llmCalls.data.map((llmCall) => ({+ id: llmCall.id,+ traceId: llmCall.traceId,+ startTime: llmCall.startTime,+ name: llmCall.name,+ prompt: llmCall.attributes.prompt,+ completion: llmCall.attributes.completion,+ model: JSON.stringify(llmCall.attributes.model),+ }))+ : [];++ return (+ <>+ <Header title="LLM Calls" />+ <DataGrid+ rows={rows}+ columns={columns}+ slots={{ toolbar: GridToolbar }}+ loading={llmCalls.isLoading}+ />+ </>+ );+}++function lastCharacters(str: string, n: number) {+ return str.substring(str.length - n);+}diff --git a/src/pages/traces.tsx b/src/pages/traces.tsxindex ff5d80d27c..870e59fe7d 100644--- a/src/pages/traces.tsx+++ b/src/pages/traces.tsx@@ -54,6 +54,11 @@ export default function Traces() {headerName: "Attributes",flex: 1,},+ {+ field: "metrics",+ headerName: "Metrics",+ flex: 1,+ },];const rows: GridRowsProp = traces.isSuccess@@ -64,6 +69,9 @@ export default function Traces() {status: trace.status,statusMessage: trace.statusMessage,attributes: JSON.stringify(trace.attributes),+ metrics: trace.metrics+ .map((metric) => `${metric.name}: ${metric.value}`)+ .join("; "),})): [];diff --git a/src/pages/traces/[traceId].tsx b/src/pages/traces/[traceId].tsxindex b50eaee6d5..5124a973ba 100644--- a/src/pages/traces/[traceId].tsx+++ b/src/pages/traces/[traceId].tsx@@ -50,6 +50,17 @@ export default function TracePage() {? ` (${trace.data.statusMessage})`: ""),},+ {+ label: "Metrics",+ value: (+ <DescriptionList+ items={trace.data.metrics.map((metric) => ({+ label: metric.name,+ value: metric.value.toString(),+ }))}+ />+ ),+ },{label: "Attributes",value: <JSONview json={trace.data.attributes} />,diff --git a/src/server/api/routers/llmCalls.ts b/src/server/api/routers/llmCalls.tsindex f6ff48d6a2..b9b0f4a97c 100644--- a/src/server/api/routers/llmCalls.ts+++ b/src/server/api/routers/llmCalls.ts@@ -1,4 +1,3 @@-import { ObservationType } from "@prisma/client";import { z } from "zod";import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";@@ -29,6 +28,12 @@ export const llmCallRouter = createTRPCRouter({},})) as LlmCall;- return llmCall;+ const metrics = await prisma.metric.findMany({+ where: {+ traceId: llmCall.traceId,+ },+ });++ return { ...llmCall, metrics };}),});diff --git a/src/server/api/routers/traces.ts b/src/server/api/routers/traces.tsindex 1c4a3e8a46..0c0edaef82 100644--- a/src/server/api/routers/traces.ts+++ b/src/server/api/routers/traces.ts@@ -6,11 +6,14 @@ import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";import { prisma } from "~/server/db";export const traceRouter = createTRPCRouter({- all: publicProcedure.query(() => {- const traces = prisma.trace.findMany({+ all: publicProcedure.query(async () => {+ const traces = await prisma.trace.findMany({orderBy: {timestamp: "desc",},+ include: {+ metrics: true,+ },});return traces;@@ -22,6 +25,9 @@ export const traceRouter = createTRPCRouter({where: {id: input,},+ include: {+ metrics: true,+ },}),prisma.observation.findMany({where: {