Skip to content

Commit 37cbe52

Browse files
committed
feat: initial support for showing structured model responses in detail ui
Signed-off-by: Nick Mitchell <[email protected]>
1 parent 778e9f8 commit 37cbe52

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed
Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
1+
import { stringify } from "yaml"
2+
13
import Group from "../Group"
24
import Result from "../../Result"
35

6+
function tryJson(s: unknown) {
7+
if (typeof s === "string") {
8+
try {
9+
return JSON.parse(s)
10+
} catch (_err) {}
11+
}
12+
return s
13+
}
14+
415
export default function ModelItems({
516
block: { platform, model, input, result, parser },
617
}: {
718
block: import("../../../pdl_ast").LitellmModelBlock
819
}) {
20+
const json = tryJson(result)
21+
const resultForDisplay = Array.isArray(json)
22+
? json.map(({ sentence }) => sentence).join("\n")
23+
: result
24+
25+
const lang = Array.isArray(json)
26+
? undefined
27+
: parser === "jsonl" || parser === "json"
28+
? "json"
29+
: parser === "yaml"
30+
? "yaml"
31+
: "plaintext"
32+
33+
const meta = Array.isArray(json)
34+
? json.flatMap(({ meta }, idx) =>
35+
Object.entries(meta).map(([k, v]) => (
36+
<Result
37+
key={k + "." + idx}
38+
term={k}
39+
result={typeof v === "object" ? stringify(v) : v}
40+
lang={typeof v === "object" ? "yaml" : undefined}
41+
/>
42+
)),
43+
)
44+
: undefined
45+
946
return (
1047
<>
1148
{typeof platform === "string" && (
1249
<Group term="Platform" description={platform} />
1350
)}
1451
{typeof model === "string" && <Group term="Model" description={model} />}
1552
{typeof input === "string" && <Group term="Input" description={input} />}
16-
<Result
17-
result={result}
18-
lang={
19-
parser === "jsonl" || parser === "json"
20-
? "json"
21-
: parser === "yaml"
22-
? "yaml"
23-
: "plaintext"
24-
}
25-
/>
53+
<Result result={resultForDisplay} lang={lang} />
54+
{meta}
2655
</>
2756
)
2857
}

0 commit comments

Comments
 (0)