Skip to content

Commit 3c11f87

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 3c11f87

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed
Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
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+
// intentional fall-through
12+
}
13+
}
14+
return s
15+
}
16+
417
export default function ModelItems({
518
block: { platform, model, input, result, parser },
619
}: {
720
block: import("../../../pdl_ast").LitellmModelBlock
821
}) {
22+
const json = tryJson(result)
23+
const resultForDisplay = Array.isArray(json)
24+
? json.map(({ sentence }) => sentence).join("\n")
25+
: result
26+
27+
const lang = Array.isArray(json)
28+
? undefined
29+
: parser === "jsonl" || parser === "json"
30+
? "json"
31+
: parser === "yaml"
32+
? "yaml"
33+
: "plaintext"
34+
35+
const meta = Array.isArray(json)
36+
? json.flatMap(({ meta }, idx) =>
37+
Object.entries(meta).map(([k, v]) => (
38+
<Result
39+
key={k + "." + idx}
40+
term={k}
41+
result={typeof v === "object" ? stringify(v) : v}
42+
lang={typeof v === "object" ? "yaml" : undefined}
43+
/>
44+
)),
45+
)
46+
: undefined
47+
948
return (
1049
<>
1150
{typeof platform === "string" && (
1251
<Group term="Platform" description={platform} />
1352
)}
1453
{typeof model === "string" && <Group term="Model" description={model} />}
1554
{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-
/>
55+
<Result result={resultForDisplay} lang={lang} />
56+
{meta}
2657
</>
2758
)
2859
}

0 commit comments

Comments
 (0)