Skip to content

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

JSON viewer (#260)

Marc Klingen26d4f7c9e865
diff --git a/package-lock.json b/package-lock.json
index b76afd5abb..2f902f4615 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -66,6 +66,7 @@
"react-dom": "18.2.0",
"react-hook-form": "^7.46.2",
"react-icons": "^4.11.0",
+ "react18-json-view": "^0.2.5",
"sharp": "^0.32.6",
"superagent": "^8.1.2",
"superjson": "1.13.1",
@@ -11913,6 +11914,14 @@
"react-dom": ">=16.6.0"
}
},
+ "node_modules/react18-json-view": {
+ "version": "0.2.5",
+ "resolved": "https://registry.npmjs.org/react18-json-view/-/react18-json-view-0.2.5.tgz",
+ "integrity": "sha512-BiCWyRUCVbnaK4kfNay8crOXZnWsZ6XsnY3fwOf5C+ZaY9w9FSTawo2p+h2UG/KcDP8meZuGlkP95klfFG9GfQ==",
+ "peerDependencies": {
+ "react": ">=16.8.0"
+ }
+ },
"node_modules/read-cache": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz",
diff --git a/package.json b/package.json
index 63821d1dd1..0218e09bf2 100644
--- a/package.json
+++ b/package.json
@@ -79,6 +79,7 @@
"react-dom": "18.2.0",
"react-hook-form": "^7.46.2",
"react-icons": "^4.11.0",
+ "react18-json-view": "^0.2.5",
"sharp": "^0.32.6",
"superagent": "^8.1.2",
"superjson": "1.13.1",
diff --git a/src/components/trace/ObservationPreview.tsx b/src/components/trace/ObservationPreview.tsx
index 6a1ea2407e..0af92f73a6 100644
--- a/src/components/trace/ObservationPreview.tsx
+++ b/src/components/trace/ObservationPreview.tsx
@@ -97,17 +97,25 @@ export const ObservationPreview = (props: {
</CardHeader>
<CardContent className="flex flex-col gap-4">
<JSONView
+ key={observation.id + "-input"}
title={observation.type === "GENERATION" ? "Prompt" : "Input"}
json={observation.input}
- defaultCollapsed
/>
<JSONView
+ key={observation.id + "-output"}
title={observation.type === "GENERATION" ? "Completion" : "Output"}
json={observation.output}
- defaultCollapsed
/>
- <JSONView title="Status Message" json={observation.statusMessage} />
- <JSONView title="Metadata" json={observation.metadata} />
+ <JSONView
+ key={observation.id + "-status"}
+ title="Status Message"
+ json={observation.statusMessage}
+ />
+ <JSONView
+ key={observation.id + "-metadata"}
+ title="Metadata"
+ json={observation.metadata}
+ />
{props.scores.find((s) => s.observationId === observation.id) ? (
<div className="flex flex-col gap-2">
<h3>Scores</h3>
diff --git a/src/components/trace/TracePreview.tsx b/src/components/trace/TracePreview.tsx
index dd9c00a9c6..20ca028e4c 100644
--- a/src/components/trace/TracePreview.tsx
+++ b/src/components/trace/TracePreview.tsx
@@ -57,7 +57,11 @@ export const TracePreview = ({
/>
</CardHeader>
<CardContent>
- <JSONView title="Metadata" json={trace.metadata} scrollable />
+ <JSONView
+ key={trace.id + "-metadata"}
+ title="Metadata"
+ json={trace.metadata}
+ />
{scores.find((s) => s.observationId === null) ? (
<div className="mt-5 flex flex-col gap-2">
<h3>Scores</h3>
diff --git a/src/components/ui/code.tsx b/src/components/ui/code.tsx
index 16b7d90215..48c6dc3c44 100644
--- a/src/components/ui/code.tsx
+++ b/src/components/ui/code.tsx
@@ -2,85 +2,90 @@ import { useState } from "react";
import { Button } from "@/src/components/ui/button";
import { Check, ChevronsDownUp, ChevronsUpDown, Copy } from "lucide-react";
import { cn } from "@/src/utils/tailwind";
+import { default as React18JsonView } from "react18-json-view";
export function JSONView(props: {
json: unknown;
- defaultCollapsed?: boolean;
- scrollable?: boolean;
title?: string;
className?: string;
}) {
- const text = parseJsonInput(props.json);
-
return (
- <CodeView
- content={text}
- defaultCollapsed={props.defaultCollapsed}
- scrollable={props.scrollable}
- title={props.title}
- className={props.className}
- />
+ <div className={cn("max-w-full rounded-md border ", props.className)}>
+ {props.title ? (
+ <div className="border-b px-4 py-1 text-xs font-medium">
+ {props.title}
+ </div>
+ ) : undefined}
+ <div className="flex gap-2 p-3 text-xs">
+ <React18JsonView
+ src={props.json}
+ theme="github"
+ collapseObjectsAfterLength={20}
+ collapseStringsAfterLength={500}
+ />
+ </div>
+ </div>
);
}
-const parseJsonInput = (jsonIn: unknown): string => {
- if (typeof jsonIn === "string") return jsonIn;
+// const parseJsonInput = (jsonIn: unknown): string => {
+// if (typeof jsonIn === "string") return jsonIn;
- try {
- if (jsonIn && typeof jsonIn === "object") {
- // For completions of generations, display the generation as text
- // { completion: "<completion>" } -> "<completion>"
- if (
- Object.keys(jsonIn).length === 1 &&
- "completion" in jsonIn &&
- typeof jsonIn.completion === "string"
- ) {
- return jsonIn.completion;
- }
+// try {
+// if (jsonIn && typeof jsonIn === "object") {
+// // For completions of generations, display the generation as text
+// // { completion: "<completion>" } -> "<completion>"
+// if (
+// Object.keys(jsonIn).length === 1 &&
+// "completion" in jsonIn &&
+// typeof jsonIn.completion === "string"
+// ) {
+// return jsonIn.completion;
+// }
- // For OpenAI ChatCompletion Prompts, concat the messages
- // [ { "role": "<role>", "content": "<content>" } ] -> "<role>\n<content>\n\n"
- if (
- Array.isArray(jsonIn) &&
- jsonIn.length > 0 &&
- typeof jsonIn[0] === "object" &&
- "role" in jsonIn[0] &&
- "content" in jsonIn[0]
- ) {
- return (jsonIn as { role: string; content: string }[])
- .map(
- (message) => `${message.role.toUpperCase()}\n\n${message.content}`,
- )
- .join("\n\n------\n\n");
- }
+// // For OpenAI ChatCompletion Prompts, concat the messages
+// // [ { "role": "<role>", "content": "<content>" } ] -> "<role>\n<content>\n\n"
+// if (
+// Array.isArray(jsonIn) &&
+// jsonIn.length > 0 &&
+// typeof jsonIn[0] === "object" &&
+// "role" in jsonIn[0] &&
+// "content" in jsonIn[0]
+// ) {
+// return (jsonIn as { role: string; content: string }[])
+// .map(
+// (message) => `${message.role.toUpperCase()}\n\n${message.content}`,
+// )
+// .join("\n\n------\n\n");
+// }
- // If it is an array with a single string, return the string
- // [ "<string>" ] -> "<string>"
- if (
- Array.isArray(jsonIn) &&
- jsonIn.length === 1 &&
- typeof jsonIn[0] === "string"
- ) {
- return jsonIn[0];
- }
+// // If it is an array with a single string, return the string
+// // [ "<string>" ] -> "<string>"
+// if (
+// Array.isArray(jsonIn) &&
+// jsonIn.length === 1 &&
+// typeof jsonIn[0] === "string"
+// ) {
+// return jsonIn[0];
+// }
- // If it is an Object with a single key, listed in the list of keys, return the value if it is a string
- // { "<key>": "<string>" } -> "<string>"
- const keys = ["input", "output", "text", "prompt"];
- if (
- Object.keys(jsonIn).length === 1 &&
- keys.includes(Object.keys(jsonIn)[0] as string) &&
- typeof Object.values(jsonIn)[0] === "string"
- ) {
- return Object.values(jsonIn)[0] as string;
- }
- }
- } catch (e) {
- console.error("Error while trying to parse the string", e, jsonIn);
- }
+// // If it is an Object with a single key, listed in the list of keys, return the value if it is a string
+// // { "<key>": "<string>" } -> "<string>"
+// const keys = ["input", "output", "text", "prompt"];
+// if (
+// Object.keys(jsonIn).length === 1 &&
+// keys.includes(Object.keys(jsonIn)[0] as string) &&
+// typeof Object.values(jsonIn)[0] === "string"
+// ) {
+// return Object.values(jsonIn)[0] as string;
+// }
+// }
+// } catch (e) {
+// console.error("Error while trying to parse the string", e, jsonIn);
+// }
- return JSON.stringify(jsonIn, null, 2);
-};
+// return JSON.stringify(jsonIn, null, 2);
+// };
export function CodeView(props: {
content: string | undefined | null;
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx
index 0ac14c9adb..c807c047fc 100644
--- a/src/pages/_app.tsx
+++ b/src/pages/_app.tsx
@@ -18,6 +18,9 @@ import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
import { CrispWidget, chatSetUser } from "@/src/features/support-chat";
+// Other CSS
+import "react18-json-view/src/style.css";
+
const setProjectInPosthog = () => {
// project
const url = window.location.href;