🪢 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
feat(ui): show/hide metrics and scores on observation tree (#809)
Marc Klingen40fcede09502
diff --git a/src/components/trace/ObservationTree.tsx b/src/components/trace/ObservationTree.tsxindex bb7d6ed00a..03b74bffab 100644--- a/src/components/trace/ObservationTree.tsx+++ b/src/components/trace/ObservationTree.tsx@@ -13,15 +13,20 @@ export const ObservationTree = (props: {scores: Score[];currentObservationId: string | undefined;setCurrentObservationId: (id: string | undefined) => void;+ showMetrics: boolean;+ showScores: boolean;+ className?: string;}) => {const nestedObservations = nestObservations(props.observations);return (- <div className="flex flex-col">+ <div className={props.className}><ObservationTreeTraceNodetrace={props.trace}scores={props.scores}currentObservationId={props.currentObservationId}setCurrentObservationId={props.setCurrentObservationId}+ showMetrics={props.showMetrics}+ showScores={props.showScores}/><ObservationTreeNodeobservations={nestedObservations}@@ -29,6 +34,8 @@ export const ObservationTree = (props: {indentationLevel={1}currentObservationId={props.currentObservationId}setCurrentObservationId={props.setCurrentObservationId}+ showMetrics={props.showMetrics}+ showScores={props.showScores}/></div>);@@ -38,10 +45,12 @@ const ObservationTreeTraceNode = (props: {scores: Score[];currentObservationId: string | undefined;setCurrentObservationId: (id: string | undefined) => void;+ showMetrics?: boolean;+ showScores?: boolean;}) => (<divclassName={cn(- "group my-1 flex cursor-pointer flex-col gap-1 rounded-sm p-2",+ "group my-0.5 flex cursor-pointer flex-col gap-1 rounded-sm p-1.5",props.currentObservationId === undefined ||props.currentObservationId === ""? "bg-gray-100"@@ -51,23 +60,23 @@ const ObservationTreeTraceNode = (props: {><div className="flex gap-2"><span className={cn("rounded-sm bg-gray-200 p-1 text-xs")}>TRACE</span>- <span>{props.trace.name}</span>+ <span className="text-sm">{props.trace.name}</span></div>- {props.trace.latency ? (+ {props.showMetrics && props.trace.latency ? (<div className="flex gap-2"><span className="text-xs text-gray-500">{formatInterval(props.trace.latency)}</span></div>) : null}- <div className="flex flex-wrap gap-1">- {props.scores.find((s) => s.observationId === null) ? (+ {props.showScores && props.scores.find((s) => s.observationId === null) ? (+ <div className="flex flex-wrap gap-1"><GroupedScoreBadgesscores={props.scores.filter((s) => s.observationId === null)}/>- ) : null}- </div>+ </div>+ ) : null}</div>);const ObservationTreeNode = (props: {@@ -76,6 +85,8 @@ const ObservationTreeNode = (props: {indentationLevel: number;currentObservationId: string | undefined;setCurrentObservationId: (id: string | undefined) => void;+ showMetrics?: boolean;+ showScores?: boolean;}) => (<>{props.observations@@ -84,11 +95,11 @@ const ObservationTreeNode = (props: {<Fragment key={observation.id}><div className="flex">{Array.from({ length: props.indentationLevel }, (_, i) => (- <div className="mx-2 border-r lg:mr-4" key={i} />+ <div className="mx-2 border-r" key={i} />))}<divclassName={cn(- "group my-1 flex flex-1 cursor-pointer flex-col gap-1 rounded-sm p-2 ",+ "group my-0.5 flex flex-1 cursor-pointer flex-col gap-1 rounded-sm p-1.5",props.currentObservationId === observation.id? "bg-gray-100": "hover:bg-gray-50",@@ -103,27 +114,34 @@ const ObservationTreeNode = (props: {>{observation.type}</span>- <span className="line-clamp-1">{observation.name}</span>- </div>- <div className="flex gap-2">- {observation.endTime ? (- <span className="text-xs text-gray-500">- {formatInterval(- (observation.endTime.getTime() -- observation.startTime.getTime()) /- 1000,- )}- </span>- ) : null}- {observation.promptTokens ||- observation.completionTokens ||- observation.totalTokens ? (- <span className="text-xs text-gray-500">- {observation.promptTokens} → {observation.completionTokens}{" "}- (∑ {observation.totalTokens})- </span>- ) : null}+ <span className="line-clamp-1 text-sm">{observation.name}</span></div>+ {props.showMetrics &&+ (observation.promptTokens ||+ observation.completionTokens ||+ observation.totalTokens ||+ observation.endTime) && (+ <div className="flex gap-2">+ {observation.endTime ? (+ <span className="text-xs text-gray-500">+ {formatInterval(+ (observation.endTime.getTime() -+ observation.startTime.getTime()) /+ 1000,+ )}+ </span>+ ) : null}+ {observation.promptTokens ||+ observation.completionTokens ||+ observation.totalTokens ? (+ <span className="text-xs text-gray-500">+ {observation.promptTokens} →{" "}+ {observation.completionTokens} (∑{" "}+ {observation.totalTokens})+ </span>+ ) : null}+ </div>+ )}{observation.level !== "DEFAULT" ? (<div className="flex"><span@@ -137,7 +155,8 @@ const ObservationTreeNode = (props: {</span></div>) : null}- {props.scores.find((s) => s.observationId === observation.id) ? (+ {props.showScores &&+ props.scores.find((s) => s.observationId === observation.id) ? (<div className="flex flex-wrap gap-1"><GroupedScoreBadgesscores={props.scores.filter(@@ -154,6 +173,8 @@ const ObservationTreeNode = (props: {indentationLevel={props.indentationLevel + 1}currentObservationId={props.currentObservationId}setCurrentObservationId={props.setCurrentObservationId}+ showMetrics={props.showMetrics}+ showScores={props.showScores}/></Fragment>))}diff --git a/src/components/trace/index.tsx b/src/components/trace/index.tsxindex b49f9cbc48..d1824026b6 100644--- a/src/components/trace/index.tsx+++ b/src/components/trace/index.tsx@@ -17,6 +17,9 @@ import { DeleteTrace } from "@/src/components/delete-trace";import { StarTraceToggle } from "@/src/components/star-toggle";import Link from "next/link";import { NoAccessError } from "@/src/components/no-access";+import useLocalStorage from "@/src/components/useLocalStorage";+import { Toggle } from "@/src/components/ui/toggle";+import { ChevronsDownUp, ChevronsUpDown, StarHalf } from "lucide-react";export function Trace(props: {observations: Array<ObservationReturnType>;@@ -28,6 +31,12 @@ export function Trace(props: {"observation",StringParam,);+ const [metricsOnObservationTree, setMetricsOnObservationTree] =+ useLocalStorage("metricsOnObservationTree", true);+ const [scoresOnObservationTree, setScoresOnObservationTree] = useLocalStorage(+ "scoresOnObservationTree",+ true,+ );return (<div className="grid h-full gap-4 md:grid-cols-3">@@ -50,13 +59,42 @@ export function Trace(props: {/>)}</div>- <div className="md:h-full md:overflow-y-auto">+ <div className="md:h-full md:overflow-hidden">+ <div className="flex flex-row justify-end gap-2">+ <Toggle+ pressed={scoresOnObservationTree}+ onPressedChange={(e) => {+ setScoresOnObservationTree(e);+ }}+ size="sm"+ title="Show scores"+ >+ <StarHalf className="h-4 w-4" />+ </Toggle>+ <Toggle+ pressed={metricsOnObservationTree}+ onPressedChange={(e) => {+ setMetricsOnObservationTree(e);+ }}+ size="sm"+ title="Show metrics"+ >+ {metricsOnObservationTree ? (+ <ChevronsDownUp className="h-4 w-4" />+ ) : (+ <ChevronsUpDown className="h-4 w-4" />+ )}+ </Toggle>+ </div><ObservationTreeobservations={props.observations}trace={props.trace}scores={props.scores}currentObservationId={currentObservationId ?? undefined}setCurrentObservationId={setCurrentObservationId}+ showMetrics={metricsOnObservationTree}+ showScores={scoresOnObservationTree}+ className="md:h-full md:overflow-y-auto"/></div></div>diff --git a/src/components/ui/toggle.tsx b/src/components/ui/toggle.tsxnew file mode 100644index 0000000000..e3293e8241--- /dev/null+++ b/src/components/ui/toggle.tsx@@ -0,0 +1,45 @@+"use client";++import * as React from "react";+import * as TogglePrimitive from "@radix-ui/react-toggle";+import { cva, type VariantProps } from "class-variance-authority";++import { cn } from "@/src/utils/tailwind";++const toggleVariants = cva(+ "inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors hover:bg-muted hover:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground",+ {+ variants: {+ variant: {+ default: "bg-transparent",+ outline:+ "border border-input bg-transparent hover:bg-accent hover:text-accent-foreground",+ },+ size: {+ default: "h-10 px-3",+ sm: "h-9 px-2.5",+ lg: "h-11 px-5",+ },+ },+ defaultVariants: {+ variant: "default",+ size: "default",+ },+ },+);++const Toggle = React.forwardRef<+ React.ElementRef<typeof TogglePrimitive.Root>,+ React.ComponentPropsWithoutRef<typeof TogglePrimitive.Root> &+ VariantProps<typeof toggleVariants>+>(({ className, variant, size, ...props }, ref) => (+ <TogglePrimitive.Root+ ref={ref}+ className={cn(toggleVariants({ variant, size, className }))}+ {...props}+ />+));++Toggle.displayName = TogglePrimitive.Root.displayName;++export { Toggle, toggleVariants };diff --git a/src/components/useLocalStorage.tsx b/src/components/useLocalStorage.tsxnew file mode 100644index 0000000000..f227f7301a--- /dev/null+++ b/src/components/useLocalStorage.tsx@@ -0,0 +1,56 @@+import { useState, useEffect } from "react";++/**+ * useLocalStorage is a hook for managing data with the localStorage API.+ *+ * @param {string} localStorageKey - The key under which the value is stored in localStorage.+ * @param {T} initialValue - The initial value of the data to be stored.+ *+ * Note: The object T should be able to be stringified, as it will be stored in localStorage as a string.+ *+ * @return An array with three elements:+ * value: Current value+ * setValue: Function to update the value+ * clearValue: Function to remove value from the local storage.+ * This function will also reset the value to initial value+ *+ * @template T - The type of the data to be stored in localStorage. It should be a type that can be stringified.+ *+ * @throws Will throw an error if the stringifying the value or accessing local storage fails.+ */+function useLocalStorage<T>(localStorageKey: string, initialValue: T) {+ const [value, setValue] = useState<T>(() => {+ try {+ const storedValue = localStorage.getItem(localStorageKey);+ return storedValue ? (JSON.parse(storedValue) as T) : initialValue;+ } catch (error) {+ console.error("Error reading from local storage", error);+ return initialValue;+ }+ });++ const clearValue = () => {+ try {+ localStorage.removeItem(localStorageKey);+ setValue(initialValue);+ } catch (error) {+ console.error("Error clearing local storage", error);+ }+ };++ useEffect(() => {+ try {+ localStorage.setItem(localStorageKey, JSON.stringify(value));+ } catch (error) {+ console.error("Error writing to local storage", error);+ }+ }, [localStorageKey, value]);++ return [value, setValue, clearValue] as [+ T,+ React.Dispatch<React.SetStateAction<T>>,+ () => void,+ ];+}++export default useLocalStorage;