Skip to content

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

v1

Marc Klingenaf5f855f076a
diff --git a/prisma/seed.ts b/prisma/seed.ts
index 02074c5376..836da664c1 100755
--- a/prisma/seed.ts
+++ b/prisma/seed.ts
@@ -106,6 +106,48 @@ async function main() {
},
});
+ const trace2 = await prisma.trace.create({
+ data: {
+ id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53c",
+ timestamp: new Date(),
+ name: "sales-pilot",
+ attributes: {
+ user: "Nima",
+ },
+ status: "success",
+ },
+ });
+
+ const span2 = await prisma.observation.create({
+ data: {
+ id: "57a266de-df34-4cea-b4e4-087bb6a3eac1",
+ trace: { connect: { id: trace2.id } },
+ type: "SPAN",
+ name: "sales-pilot-retrieval",
+ attributes: { user: "Nima" },
+ startTime: new Date(new Date().setMinutes(new Date().getMinutes() - 1)),
+ endTime: new Date(),
+ },
+ });
+
+ const event2 = await prisma.observation.create({
+ data: {
+ id: "57a266de-df34-4cea-b4e4-78vcdhuv7",
+ type: "EVENT",
+ startTime: new Date(),
+ trace: { connect: { id: trace2.id } },
+ parent: {
+ connect: {
+ id: span2.id,
+ },
+ },
+ name: "sales-pilot-vector-db-response",
+ attributes: {
+ docs: ["Doc 1", "Doc 2", "Doc 3"],
+ },
+ },
+ });
+
console.log({
trace,
span,
diff --git a/src/components/layouts/header.tsx b/src/components/layouts/header.tsx
index e1e35f53bc..1b56be17c9 100644
--- a/src/components/layouts/header.tsx
+++ b/src/components/layouts/header.tsx
@@ -76,7 +76,7 @@ export default function Header(props: {
) : null}
</div>
<div className="md:flex-1" />
- {/* <div className="mt-4 flex flex-shrink-0 md:ml-4 md:mt-0">
+ <div className="mt-4 flex flex-shrink-0 md:ml-4 md:mt-0">
<button
type="button"
className="inline-flex items-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
@@ -89,7 +89,7 @@ export default function Header(props: {
>
Publish
</button>
- </div> */}
+ </div>
</div>
</div>
);
diff --git a/src/components/observationDisplay.tsx b/src/components/observationDisplay.tsx
new file mode 100644
index 0000000000..396619b26d
--- /dev/null
+++ b/src/components/observationDisplay.tsx
@@ -0,0 +1,59 @@
+import { Button } from "~/components/ui/button";
+import {
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
+} from "~/components/ui/collapsible";
+import { ChevronsUpDown, ChevronsDownUp, ArrowUpRight } from "lucide-react";
+import { useState } from "react";
+import { type NestedObservation } from "@/src/utils/types";
+import Link from "next/link";
+import { JSONview } from "@/src/components/ui/code";
+
+export default function ObservationDisplay(props: { obs: NestedObservation }) {
+ const [isOpen, setIsOpen] = useState(false);
+
+ return (
+ <div>
+ <Collapsible open={isOpen} onOpenChange={setIsOpen} className="space-y-2">
+ <div className="flex items-center space-x-2">
+ <span className=" text-xs text-gray-500">{props.obs.type}:</span>
+ <h4 className="text-sm font-semibold">{props.obs.name}</h4>
+ {props.obs.endTime ? (
+ <span className="text-gray-500">{`${
+ props.obs.endTime.getTime() - props.obs.startTime.getTime()
+ } ms`}</span>
+ ) : null}
+ <CollapsibleTrigger asChild>
+ <Button variant="ghost" size="sm" className="w-9 p-0">
+ {isOpen ? (
+ <ChevronsDownUp className="h-4 w-4" />
+ ) : (
+ <ChevronsUpDown className="h-4 w-4" />
+ )}
+ <span className="sr-only">Toggle</span>
+ </Button>
+ </CollapsibleTrigger>
+ {props.obs.type === "LLMCALL" ? (
+ <Button size="sm" variant="ghost" asChild>
+ <Link href={`/llm-calls/${props.obs.id}`}>
+ <ArrowUpRight className="h-4 w-4" />
+ </Link>
+ </Button>
+ ) : null}
+ </div>
+ <CollapsibleContent>
+ <div className="mb-4 space-y-2">
+ <span className="text-sm font-semibold">Attributes</span>
+ <JSONview json={props.obs.attributes} />
+ </div>
+ </CollapsibleContent>
+ </Collapsible>
+ <div className="ml-5">
+ {props.obs.children.map((obs) => (
+ <ObservationDisplay key={obs.name} obs={obs} />
+ ))}
+ </div>
+ </div>
+ );
+}
diff --git a/src/pages/traces.tsx b/src/pages/traces.tsx
index 7222f77b04..868d5779a7 100644
--- a/src/pages/traces.tsx
+++ b/src/pages/traces.tsx
@@ -14,7 +14,6 @@ interface TraceRowData {
id: string;
}
-
export default function Traces() {
const traces = api.traces.all.useQuery(undefined, { refetchInterval: 1000 });
const router = useRouter();
diff --git a/src/pages/traces/[traceId].tsx b/src/pages/traces/[traceId].tsx
index f6d29652e6..c744d373c0 100644
--- a/src/pages/traces/[traceId].tsx
+++ b/src/pages/traces/[traceId].tsx
@@ -1,18 +1,9 @@
import { useRouter } from "next/router";
import Header from "~/components/layouts/header";
-import { Button } from "~/components/ui/button";
-import {
- Collapsible,
- CollapsibleContent,
- CollapsibleTrigger,
-} from "~/components/ui/collapsible";
import { api } from "~/utils/api";
-import { ChevronsUpDown, ChevronsDownUp, ArrowUpRight } from "lucide-react";
-import { useState } from "react";
-import { type NestedObservation } from "@/src/utils/types";
-import Link from "next/link";
import DescriptionList from "@/src/components/ui/descriptionLists";
import { JSONview } from "@/src/components/ui/code";
+import ObservationDisplay from "@/src/components/observationDisplay";
export default function TracePage() {
const router = useRouter();
@@ -67,13 +58,12 @@ export default function TracePage() {
},
{
label: "Detailed trace",
- value: (
- <div className="space-y-2">
- {trace.data?.nestedObservations.map((obs) => (
- <ObservationDisplay key={obs.id} obs={obs} />
- )) ?? null}
- </div>
- ),
+ value: trace.data.nestedObservation ? (
+ <ObservationDisplay
+ key={trace.data.id}
+ obs={trace.data.nestedObservation}
+ />
+ ) : null,
},
]}
/>
@@ -81,49 +71,3 @@ export default function TracePage() {
</>
);
}
-
-function ObservationDisplay(props: { obs: NestedObservation }) {
- const [isOpen, setIsOpen] = useState(false);
-
- return (
- <div>
- <Collapsible open={isOpen} onOpenChange={setIsOpen} className="space-y-2">
- <div className="flex items-center space-x-2 px-4">
- <span className="p-2 text-xs text-gray-500">{props.obs.type}:</span>
- <h4 className="text-sm font-semibold">{props.obs.name}</h4>
- {props.obs.endTime ? (
- <span className="text-gray-500">{`${
- props.obs.endTime.getTime() - props.obs.startTime.getTime()
- } ms`}</span>
- ) : null}
- <CollapsibleTrigger asChild>
- <Button variant="ghost" size="sm" className="w-9 p-0">
- {isOpen ? (
- <ChevronsDownUp className="h-4 w-4" />
- ) : (
- <ChevronsUpDown className="h-4 w-4" />
- )}
- <span className="sr-only">Toggle</span>
- </Button>
- </CollapsibleTrigger>
- {props.obs.type === "LLMCALL" ? (
- <Button size="sm" variant="ghost" asChild>
- <Link href={`/llm-calls/${props.obs.id}`}>
- <ArrowUpRight className="h-4 w-4" />
- </Link>
- </Button>
- ) : null}
- </div>
- <CollapsibleContent className="ml-6 space-y-2">
- <span className="text-sm font-semibold">Attributes</span>
- <JSONview json={props.obs.attributes} />
- </CollapsibleContent>
- </Collapsible>
- <div className="ml-5">
- {props.obs.children.map((obs) => (
- <ObservationDisplay key={obs.name} obs={obs} />
- ))}
- </div>
- </div>
- );
-}
diff --git a/src/pages/traces/side-by-side.tsx b/src/pages/traces/side-by-side.tsx
new file mode 100644
index 0000000000..447f18bae5
--- /dev/null
+++ b/src/pages/traces/side-by-side.tsx
@@ -0,0 +1,57 @@
+import Header from "@/src/components/layouts/header";
+import ObservationDisplay from "@/src/components/observationDisplay";
+import { Button } from "@/src/components/ui/button";
+import DescriptionList from "@/src/components/ui/descriptionLists";
+import { api } from "@/src/utils/api";
+import { type RouterOutput } from "@/src/utils/types";
+import { ArrowUpRight } from "lucide-react";
+import Link from "next/link";
+
+export default function SideBySide() {
+ const traces = api.traces.all.useQuery();
+
+ return (
+ <>
+ <Header title="Side-by-side" />
+ <span className="text-2xl font-bold">Traces</span>
+ <div className="relative flex max-w-full flex-row gap-2 overflow-x-scroll py-5">
+ {traces.data?.map((trace) => (
+ <Single key={trace.id} trace={trace} />
+ ))}
+ {traces.data?.map((trace) => (
+ <Single key={trace.id} trace={trace} />
+ ))}
+ {traces.data?.map((trace) => (
+ <Single key={trace.id} trace={trace} />
+ ))}
+ {traces.data?.map((trace) => (
+ <Single key={trace.id} trace={trace} />
+ ))}
+ </div>
+ </>
+ );
+}
+
+const Single = (props: { trace: RouterOutput["traces"]["all"][number] }) => {
+ const { trace } = props;
+
+ if (trace.nestedObservation)
+ return (
+ <div className="w-[550px] flex-none rounded-md border px-3">
+ <div className="mt-4 font-bold">Trace</div>
+ <Button variant="ghost" size="sm" asChild>
+ <Link href={`/traces/${trace.id}`}>
+ {trace.id}
+ <ArrowUpRight className="ml-2 h-4 w-4" />
+ </Link>
+ </Button>
+ <div className="mt-4 text-sm font-bold">Timestamp</div>
+ <div>{trace.timestamp.toLocaleString()}</div>
+ <div className="mt-4 text-sm font-bold">Name</div>
+ <div>{trace.name}</div>
+ <div className="mt-4 text-sm font-bold">Observations:</div>
+ <ObservationDisplay key={trace.id} obs={trace.nestedObservation} />
+ </div>
+ );
+ else return null;
+};
diff --git a/src/server/api/routers/traces.ts b/src/server/api/routers/traces.ts
index 31ce44c8e2..68cd2601f4 100644
--- a/src/server/api/routers/traces.ts
+++ b/src/server/api/routers/traces.ts
@@ -13,10 +13,14 @@ export const traceRouter = createTRPCRouter({
},
include: {
scores: true,
+ observations: true,
},
});
- return traces;
+ return traces.map((trace) => ({
+ ...trace,
+ nestedObservation: nestObservations(trace.observations),
+ }));
}),
byId: publicProcedure.input(z.string()).query(async ({ input }) => {
@@ -36,16 +40,14 @@ export const traceRouter = createTRPCRouter({
}),
]);
- const nestedObservations = nestObservations(observations);
-
return {
...trace,
- nestedObservations,
+ nestedObservation: nestObservations(observations),
};
}),
});
-function nestObservations(list: Observation[]): NestedObservation[] {
+function nestObservations(list: Observation[]): NestedObservation | null {
// Step 1: Create a map where the keys are object IDs, and the values are
// the corresponding objects with an added 'children' property.
const map = new Map<string, NestedObservation>();
@@ -68,6 +70,11 @@ function nestObservations(list: Observation[]): NestedObservation[] {
}
}
- // Step 4: Return the roots as an array.
- return Array.from(roots.values());
+ // Step 4: Assert that there is only one root.
+ if (roots.size > 1)
+ console.error("Expected exactly one root, but got:", roots.size);
+ if (roots.size === 0) return null;
+
+ // Step 5: Return the root.
+ return Array.from(roots.values())[0] as NestedObservation;
}
diff --git a/src/utils/types.ts b/src/utils/types.ts
index 834bedfc45..7ccf9b4179 100644
--- a/src/utils/types.ts
+++ b/src/utils/types.ts
@@ -1,11 +1,11 @@
import { type Observation, type Prisma } from "@prisma/client";
+import { type inferRouterInputs, type inferRouterOutputs } from "@trpc/server";
+import { type AppRouter } from "../server/api/root";
export type NestedObservation = Observation & {
children: NestedObservation[];
};
-// TODO: Marc add types for span and event
-
export type TypedObservation = Event | Span | LlmCall;
export type Event = Observation & {
@@ -29,3 +29,6 @@ export type LlmCall = Observation & {
model: Prisma.JsonValue;
};
};
+
+export type RouterInput = inferRouterInputs<AppRouter>;
+export type RouterOutput = inferRouterOutputs<AppRouter>;