Skip to content

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

Fix nested observations (#20)

Marc Klingen9822383d9b42
diff --git a/prisma/seed.ts b/prisma/seed.ts
index 251c2a124e..ed3203207b 100755
--- a/prisma/seed.ts
+++ b/prisma/seed.ts
@@ -122,8 +122,8 @@ async function main() {
id: trace.id,
},
},
- // if this is the first span, add no parent; if not, then randomly select parent from existing spans or the first span
- ...(existingSpanIds.length === 0
+ // if this is the first span or in 50% of cases, add no parent; otherwise randomly select parent from existing spans
+ ...(existingSpanIds.length === 0 || Math.random() > 0.5
? {}
: {
parent: {
diff --git a/src/components/observation-display.tsx b/src/components/observation-display.tsx
index 5b6138c425..2cf4741493 100644
--- a/src/components/observation-display.tsx
+++ b/src/components/observation-display.tsx
@@ -11,58 +11,63 @@ import Link from "next/link";
import { JSONview } from "@/src/components/ui/code";
export default function ObservationDisplay(props: {
- obs: NestedObservation;
+ observations: NestedObservation[];
projectId: string;
}) {
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 === "GENERATION" ? (
- <Button size="sm" variant="ghost" asChild>
- <Link
- href={`/project/${props.projectId}/generations/${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">Metadata</span>
- <JSONview json={props.obs.metadata} />
+ {props.observations.map((obs) => (
+ <div key={obs.id} 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>
+ </CollapsibleContent>
+ </Collapsible>
+ <div className="ml-5">
+ <ObservationDisplay
+ observations={obs.children}
+ projectId={props.projectId}
+ />
</div>
- </CollapsibleContent>
- </Collapsible>
- <div className="ml-5">
- {props.obs.children.map((obs) => (
- <ObservationDisplay
- key={obs.name}
- obs={obs}
- projectId={props.projectId}
- />
- ))}
- </div>
+ </div>
+ ))}
</div>
);
}
diff --git a/src/pages/project/[projectId]/traces.tsx b/src/pages/project/[projectId]/traces.tsx
index a0c1fbb268..84500bf874 100644
--- a/src/pages/project/[projectId]/traces.tsx
+++ b/src/pages/project/[projectId]/traces.tsx
@@ -303,12 +303,7 @@ const Single = (props: {
<div className="mt-4 text-sm font-bold">Observations:</div>
<ObservationDisplay
key={trace.id}
- obs={trace.nestedObservation}
- projectId={props.projectId}
- />
- <ObservationDisplay
- key={trace.id}
- obs={trace.nestedObservation}
+ observations={trace.nestedObservation}
projectId={props.projectId}
/>
</>
diff --git a/src/pages/project/[projectId]/traces/[traceId].tsx b/src/pages/project/[projectId]/traces/[traceId].tsx
index 67562a1371..569a3e5e8b 100644
--- a/src/pages/project/[projectId]/traces/[traceId].tsx
+++ b/src/pages/project/[projectId]/traces/[traceId].tsx
@@ -53,7 +53,7 @@ export default function TracePage() {
<ObservationDisplay
key={trace.data.id}
projectId={projectId}
- obs={trace.data.nestedObservation}
+ observations={trace.data.nestedObservation}
/>
) : null,
},
diff --git a/src/server/api/routers/traces.ts b/src/server/api/routers/traces.ts
index 2539066c1f..9a5491b7d6 100644
--- a/src/server/api/routers/traces.ts
+++ b/src/server/api/routers/traces.ts
@@ -193,7 +193,7 @@ export const traceRouter = createTRPCRouter({
}),
});
-function nestObservations(list: Observation[]): NestedObservation | null {
+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>();
@@ -217,12 +217,10 @@ function nestObservations(list: Observation[]): NestedObservation | null {
}
// 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;
+ return Array.from(roots.values());
}
function createScoreCondition(score: ScoreFilter) {