Skip to content

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

Observation view (#28)

Maximilian Deichmanna125337fa754
diff --git a/package-lock.json b/package-lock.json
index 6abc4eea02..68ab3fb07a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -31,6 +31,7 @@
"@sentry/profiling-node": "^1.0.6",
"@t3-oss/env-nextjs": "^0.4.1",
"@tailwindcss/forms": "^0.5.3",
+ "@tailwindcss/line-clamp": "^0.4.4",
"@tanstack/react-query": "^4.29.14",
"@tanstack/react-table": "^8.9.2",
"@tremor/react": "^3.2.2",
@@ -2262,6 +2263,14 @@
"tailwindcss": ">=3.0.0 || >= 3.0.0-alpha.1"
}
},
+ "node_modules/@tailwindcss/line-clamp": {
+ "version": "0.4.4",
+ "resolved": "https://registry.npmjs.org/@tailwindcss/line-clamp/-/line-clamp-0.4.4.tgz",
+ "integrity": "sha512-5U6SY5z8N42VtrCrKlsTAA35gy2VSyYtHWCsg1H87NU1SXnEfekTVlrga9fzUDrrHcGi2Lb5KenUWb4lRQT5/g==",
+ "peerDependencies": {
+ "tailwindcss": ">=2.0.0 || >=3.0.0 || >=3.0.0-alpha.1"
+ }
+ },
"node_modules/@tanstack/query-core": {
"version": "4.29.14",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-4.29.14.tgz",
diff --git a/package.json b/package.json
index d37228f8d6..a5c9c52284 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"@sentry/profiling-node": "^1.0.6",
"@t3-oss/env-nextjs": "^0.4.1",
"@tailwindcss/forms": "^0.5.3",
+ "@tailwindcss/line-clamp": "^0.4.4",
"@tanstack/react-query": "^4.29.14",
"@tanstack/react-table": "^8.9.2",
"@tremor/react": "^3.2.2",
diff --git a/src/components/observation-display.tsx b/src/components/observation-display.tsx
index 5e224dc462..fa96ac7762 100644
--- a/src/components/observation-display.tsx
+++ b/src/components/observation-display.tsx
@@ -1,81 +1,177 @@
-import { Button } from "@/src/components/ui/button";
import {
- Collapsible,
- CollapsibleContent,
- CollapsibleTrigger,
-} from "@/src/components/ui/collapsible";
-import { ChevronsUpDown, ChevronsDownUp, ArrowUpRight } from "lucide-react";
-import { useState } from "react";
-import { type NestedObservation } from "@/src/utils/types";
+ type GenerationUsage,
+ type NestedObservation,
+} from "@/src/utils/types";
+import { JSONView } from "@/src/components/ui/code";
+import { type Prisma } from "@prisma/client";
+import { formatDate } from "@/src/utils/dates";
+import { Button } from "@/src/components/ui/button";
import Link from "next/link";
-import { JSONview } from "@/src/components/ui/code";
+import { ArrowUpRight } from "lucide-react";
+
+type RowData = {
+ id: string;
+ name: string | null;
+ level: number;
+ input: Prisma.JsonValue;
+ output: Prisma.JsonValue;
+ usage: Prisma.JsonValue;
+ startTime: Date;
+ endTime: Date | null;
+ showOutput: boolean;
+ type: string;
+};
+
+function getObservationsAndLevels(
+ observation: NestedObservation,
+ level = 0,
+ showOutput = false
+): RowData[] {
+ const result: RowData[] = [
+ {
+ id: observation.id,
+ name: observation.name,
+ input: observation.input,
+ output: observation.output,
+ startTime: observation.startTime,
+ endTime: observation.endTime,
+ type: observation.type,
+ usage: observation.usage,
+ showOutput,
+ level,
+ },
+ ];
+
+ if (observation.children) {
+ observation.children
+ .map((child) => getObservationsAndLevels(child, level + 1))
+ .map((childResult) => result.push(...childResult));
+ }
+
+ result.push({
+ id: observation.id,
+ name: observation.name,
+ input: observation.input,
+ output: observation.output,
+ startTime: observation.startTime,
+ endTime: observation.endTime,
+ type: observation.type,
+ usage: observation.usage,
+ showOutput: true,
+ level,
+ });
+
+ return result;
+}
export default function ObservationDisplay(props: {
observations: NestedObservation[];
projectId: string;
+ indentationLevel: number;
}) {
+ const flatMap = props.observations.flatMap((o) =>
+ getObservationsAndLevels(o, 0)
+ );
+
return (
<div>
- {props.observations.map((obs) => (
- <SingleObservationDisplay
- key={obs.id}
- observation={obs}
- projectId={props.projectId}
- />
- ))}
+ <div className="">
+ <div className="flex flex-col">
+ {flatMap.map((row) => (
+ <div
+ className="mt-4 flex items-start"
+ key={row.showOutput ? row.id + "output" : row.id}
+ >
+ <div className=" flex w-1/4 overflow-hidden">
+ {!row.showOutput ? (
+ <ObservationInfo
+ observation={row}
+ projectId={props.projectId}
+ />
+ ) : undefined}
+ </div>
+ <div className="ml-4 grid w-3/4 grid-cols-12">
+ <div className={`col-span-${row.level}`}> </div>
+
+ <div className={`col-span-${12 - row.level}`}>
+ {row.showOutput && row.output ? (
+ <JSONView json={row.output} defaultCollapsed />
+ ) : undefined}
+ {row.showOutput && !row.output ? (
+ <h3 className="m-5 text-base font-medium leading-4 text-gray-900">
+ No Output
+ </h3>
+ ) : undefined}
+ {!row.showOutput && row.input ? (
+ <JSONView json={row.input} defaultCollapsed />
+ ) : undefined}
+ {!row.showOutput && !row.input ? (
+ <h3 className="m-5 text-base font-medium leading-4 text-gray-900">
+ No Input
+ </h3>
+ ) : undefined}
+ </div>
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
</div>
);
}
-function SingleObservationDisplay(props: {
- observation: NestedObservation;
+const ObservationInfo = (props: {
+ observation: RowData;
projectId: string;
-}) {
- const [isOpen, setIsOpen] = useState(false);
- const obs = props.observation;
-
+}) => {
+ const usage = props.observation.usage
+ ? (props.observation.usage as unknown as GenerationUsage)
+ : null;
return (
- <div className="space-y-2">
- <Collapsible open={isOpen} onOpenChange={setIsOpen} className="space-y-2">
- <div className="flex items-center space-x-2">
- <span className=" text-xs text-gray-500">{obs.type}:</span>
- <h4 className="text-sm font-semibold">{obs.name}</h4>
- {obs.endTime ? (
- <span className="text-gray-500">{`${
- obs.endTime.getTime() - 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>
- {obs.type === "GENERATION" ? (
- <Button size="sm" variant="ghost" asChild>
- <Link href={`/project/${props.projectId}/generations/${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">Metadata</span>
- <JSONview json={obs.metadata} />
+ <>
+ <div className="flex-auto rounded-md p-3 ring-1 ring-inset ring-gray-200">
+ <div className="flex flex-col justify-between gap-x-4">
+ <div className="flex py-0.5 text-xs leading-5 text-gray-500">
+ <span className="font-medium text-gray-900">
+ {props.observation.type}: {props.observation.name}
+ </span>
+ {props.observation.type === "GENERATION" ? (
+ <Button size="sm" variant="ghost" asChild>
+ <Link
+ href={`/project/${props.projectId}/generations/${props.observation.id}`}
+ >
+ <ArrowUpRight className="h-4 w-4" />
+ </Link>
+ </Button>
+ ) : undefined}
</div>
- </CollapsibleContent>
- </Collapsible>
- <div className="ml-5">
- <ObservationDisplay
- observations={obs.children}
- projectId={props.projectId}
- />
+ {props.observation.startTime ? (
+ <div className="flex">
+ <time
+ dateTime={props.observation.startTime.toString()}
+ className="flex-none py-0.5 text-xs leading-5 text-gray-500"
+ >
+ {formatDate(props.observation.startTime)}
+ </time>
+ {props.observation.endTime ? (
+ <>
+ <p className="whitespace-nowrap py-0.5 text-xs leading-5 text-gray-500">
+ &nbsp;-&nbsp;
+ {props.observation.endTime.getTime() -
+ props.observation.startTime.getTime()}{" "}
+ ms
+ </p>
+ </>
+ ) : undefined}
+ </div>
+ ) : undefined}
+ </div>
+ {usage && usage.promptTokens && usage.completionTokens ? (
+ <p className="text-xs leading-5 text-gray-500">
+ {usage.promptTokens + usage.completionTokens} tokens
+ </p>
+ ) : undefined}
</div>
- </div>
+ </>
);
-}
+};
diff --git a/src/components/prompts.tsx b/src/components/prompts.tsx
deleted file mode 100644
index d06e3180b1..0000000000
--- a/src/components/prompts.tsx
+++ /dev/null
@@ -1,19 +0,0 @@
-import { CodeView } from "@/src/components/ui/code";
-import { type LLMChatMessages } from "@/src/utils/types";
-
-export default function Prompt(props: { messages: LLMChatMessages[] }) {
- return (
- <>
- {props.messages.map((message, index) => (
- <div key={index}>
- <div className="mb-2 mt-5">
- <h3 className="inline-flex items-baseline rounded-full bg-gray-100 px-2.5 py-0.5 text-sm font-medium leading-6 text-gray-800 md:mt-2 lg:mt-0">
- {message.role}
- </h3>
- </div>
- <CodeView>{message.content}</CodeView>
- </div>
- ))}
- </>
- );
-}
diff --git a/src/components/ui/code.tsx b/src/components/ui/code.tsx
index 1b9d0e816d..e958b1bcf7 100644
--- a/src/components/ui/code.tsx
+++ b/src/components/ui/code.tsx
@@ -1,37 +1,59 @@
import { useState } from "react";
import { Button } from "@/src/components/ui/button";
-import { Check, Copy } from "lucide-react";
+import { Check, ChevronsDownUp, ChevronsUpDown, Copy } from "lucide-react";
import { cn } from "@/src/utils/tailwind";
-export function JSONview(props: { json: string | unknown }) {
+export function JSONView(props: {
+ json: string | unknown;
+ defaultCollapsed?: boolean;
+}) {
const text =
typeof props.json === "string"
? props.json
: JSON.stringify(props.json, null, 2);
- return <CodeView>{text}</CodeView>;
+ return <CodeView content={text} defaultCollapsed={props.defaultCollapsed} />;
}
export function CodeView(props: {
- children: string | undefined | null;
+ content: string | undefined | null;
className?: string;
+ defaultCollapsed?: boolean;
}) {
const [isCopied, setIsCopied] = useState(false);
+ const [isCollapsed, setCollapsed] = useState(props.defaultCollapsed);
const handleCopy = () => {
setIsCopied(true);
- void navigator.clipboard.writeText(props.children ?? "");
+ void navigator.clipboard.writeText(props.content ?? "");
setTimeout(() => setIsCopied(false), 1000);
};
+ const handleShowAll = () => setCollapsed(!isCollapsed);
+
return (
<code
className={cn(
- "relative block max-w-full whitespace-pre-wrap break-words rounded-md border px-4 py-3 pr-12 font-mono text-sm",
+ "relative max-w-full whitespace-pre-wrap break-words rounded-md border px-4 py-3 pr-12 font-mono text-xs",
+ isCollapsed ? `line-clamp-4` : "block",
props.className
)}
>
- {props.children}
+ {props.content}
+ {props.defaultCollapsed ? (
+ <Button
+ className="absolute right-12 top-2"
+ variant="secondary"
+ size="xs"
+ onClick={handleShowAll}
+ >
+ {isCollapsed ? (
+ <ChevronsUpDown className="h-3 w-3" />
+ ) : (
+ <ChevronsDownUp className="h-3 w-3" />
+ )}
+ </Button>
+ ) : undefined}
<Button
className="absolute right-2 top-2"
variant="secondary"
diff --git a/src/components/ui/description-lists.tsx b/src/components/ui/description-lists.tsx
index 58cd4e0422..bf33ab405a 100644
--- a/src/components/ui/description-lists.tsx
+++ b/src/components/ui/description-lists.tsx
@@ -9,7 +9,11 @@ export default function DescriptionList(props: {
description: string;
};
items: { label: string; value: string | ReactNode }[];
+ descriptionColumns?: number;
+ valueColumns?: number;
}) {
+ const { descriptionColumns = 1, valueColumns = 2 } = props;
+ const totalColumns = descriptionColumns + valueColumns;
return (
<div>
{props.header ? (
@@ -27,12 +31,14 @@ export default function DescriptionList(props: {
{props.items.map((item) => (
<div
key={item.label}
- className="px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0"
+ className={`sm:grid-cols-${totalColumns} px-4 py-6 sm:grid sm:gap-4 sm:px-0`}
>
<dt className="text-sm font-medium leading-6 text-gray-900">
{item.label}
</dt>
- <dd className="mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0">
+ <dd
+ className={`mt-1 text-sm leading-6 text-gray-700 sm:col-span-${valueColumns} sm:mt-0`}
+ >
{item.value}
</dd>
</div>
diff --git a/src/features/publicApi/components/ApiKeyList.tsx b/src/features/publicApi/components/ApiKeyList.tsx
index 8bbce6b78c..c5bfb84587 100644
--- a/src/features/publicApi/components/ApiKeyList.tsx
+++ b/src/features/publicApi/components/ApiKeyList.tsx
@@ -55,9 +55,10 @@ export function ApiKeyList(props: { projectId: string }) {
</TableCell>
<TableCell>{apiKey.note ?? ""}</TableCell>
<TableCell className="font-mono">
- <CodeView className="inline-block">
- {apiKey.publishableKey}
- </CodeView>
+ <CodeView
+ className="inline-block"
+ content={apiKey.publishableKey}
+ />
</TableCell>
<TableCell className="font-mono">
{apiKey.displaySecretKey}
diff --git a/src/features/publicApi/components/CreateApiKeyButton.tsx b/src/features/publicApi/components/CreateApiKeyButton.tsx
index d40bc179b5..62dc6697a2 100644
--- a/src/features/publicApi/components/CreateApiKeyButton.tsx
+++ b/src/features/publicApi/components/CreateApiKeyButton.tsx
@@ -127,7 +127,7 @@ export function CreateApiKeyButton(props: { projectId: string }) {
<div>
<div>
<div className="mb-2 text-lg font-semibold">Public Key</div>
- <CodeView>{generatedKeys.publishableKey}</CodeView>
+ <CodeView content={generatedKeys.publishableKey} />
</div>
<div className="mt-6">
<div className="text-lg font-semibold">Secret Key</div>
@@ -138,7 +138,7 @@ export function CreateApiKeyButton(props: { projectId: string }) {
</span>
. If you lose it, you will need to generate a new one.
</div>
- <CodeView>{generatedKeys.secretKey}</CodeView>
+ <CodeView content={generatedKeys.secretKey} />
</div>
</div>
</div>
diff --git a/src/pages/project/[projectId]/generations/[generationId].tsx b/src/pages/project/[projectId]/generations/[generationId].tsx
index 457401ac5b..98fd62a398 100644
--- a/src/pages/project/[projectId]/generations/[generationId].tsx
+++ b/src/pages/project/[projectId]/generations/[generationId].tsx
@@ -1,5 +1,5 @@
import { Button } from "@/src/components/ui/button";
-import { CodeView, JSONview } from "@/src/components/ui/code";
+import { CodeView, JSONView } from "@/src/components/ui/code";
import DescriptionList from "@/src/components/ui/description-lists";
import { ArrowUpRight } from "lucide-react";
import Link from "next/link";
@@ -102,22 +102,22 @@ export default function GenerationPage() {
{
label: "Prompt",
value: generation.data.input ? (
- <JSONview json={generation.data.input as string} />
+ <JSONView json={generation.data.input as string} />
) : undefined,
},
{
label: "Completion",
value: jsonOutput ? (
- <CodeView>
- {(jsonOutput["completion"] as string) ?? undefined}
- </CodeView>
+ <CodeView
+ content={(jsonOutput["completion"] as string) ?? undefined}
+ />
) : (
<></>
),
},
{
label: "Metadata",
- value: <JSONview json={generation.data.metadata} />,
+ value: <JSONView json={generation.data.metadata} />,
},
{
label: "Metrics (observation)",
diff --git a/src/pages/project/[projectId]/traces.tsx b/src/pages/project/[projectId]/traces.tsx
index 84500bf874..da5ec55400 100644
--- a/src/pages/project/[projectId]/traces.tsx
+++ b/src/pages/project/[projectId]/traces.tsx
@@ -305,6 +305,7 @@ const Single = (props: {
key={trace.id}
observations={trace.nestedObservation}
projectId={props.projectId}
+ indentationLevel={0}
/>
</>
) : undefined}
diff --git a/src/pages/project/[projectId]/traces/[traceId].tsx b/src/pages/project/[projectId]/traces/[traceId].tsx
index 569a3e5e8b..cb5447041a 100644
--- a/src/pages/project/[projectId]/traces/[traceId].tsx
+++ b/src/pages/project/[projectId]/traces/[traceId].tsx
@@ -2,7 +2,7 @@ import { useRouter } from "next/router";
import Header from "@/src/components/layouts/header";
import { api } from "@/src/utils/api";
import DescriptionList from "@/src/components/ui/description-lists";
-import { JSONview } from "@/src/components/ui/code";
+import { JSONView } from "@/src/components/ui/code";
import ObservationDisplay from "@/src/components/observation-display";
export default function TracePage() {
@@ -23,6 +23,8 @@ export default function TracePage() {
/>
{trace.data ? (
<DescriptionList
+ descriptionColumns={1}
+ valueColumns={5}
items={[
{
label: "Timestamp",
@@ -45,7 +47,7 @@ export default function TracePage() {
},
{
label: "Metadata",
- value: <JSONview json={trace.data.metadata} />,
+ value: <JSONView json={trace.data.metadata} />,
},
{
label: "Detailed trace",
@@ -54,6 +56,7 @@ export default function TracePage() {
key={trace.data.id}
projectId={projectId}
observations={trace.data.nestedObservation}
+ indentationLevel={0}
/>
) : null,
},
diff --git a/src/utils/dates.ts b/src/utils/dates.ts
new file mode 100644
index 0000000000..3051d268b8
--- /dev/null
+++ b/src/utils/dates.ts
@@ -0,0 +1,15 @@
+export function formatDate(date: Date): string {
+ const options: Intl.DateTimeFormatOptions = {
+ year: undefined,
+ month: "numeric",
+ day: "numeric",
+ weekday: undefined,
+ hour: "2-digit",
+ minute: "2-digit",
+ second: "2-digit",
+ timeZoneName: undefined,
+ hour12: false,
+ };
+
+ return new Intl.DateTimeFormat("en-US", options).format(date);
+}
diff --git a/src/utils/types.ts b/src/utils/types.ts
index 6dcadf92c6..48c9202957 100644
--- a/src/utils/types.ts
+++ b/src/utils/types.ts
@@ -19,13 +19,15 @@ export type Span = Observation & {
export type LLMChatMessages = { role: string; content: string };
+export type GenerationUsage = {
+ promptTokens?: number;
+ completionTokens?: number;
+ totalTokens?: number;
+};
+
export type Generation = Observation & {
type: "GENERATION";
- usage: {
- promptTokens?: number;
- completionTokens?: number;
- totalTokens?: number;
- };
+ usage: GenerationUsage;
modelParameters: {
[key: string]: string | number | boolean;
};
diff --git a/tailwind.config.ts b/tailwind.config.ts
index fde2e8e0bd..c32e2ae205 100644
--- a/tailwind.config.ts
+++ b/tailwind.config.ts
@@ -187,5 +187,9 @@ export default {
/^(fill-(?:slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(?:50|100|200|300|400|500|600|700|800|900|950))$/,
},
],
- plugins: [require("@tailwindcss/forms"), require("tailwindcss-animate")],
+ plugins: [
+ require("@tailwindcss/forms"),
+ require("tailwindcss-animate"),
+ require("@tailwindcss/line-clamp"),
+ ],
} satisfies Config;