|
| 1 | +import { stringify } from "yaml" |
| 2 | + |
1 | 3 | import Group from "../Group" |
2 | 4 | import Result from "../../Result" |
3 | 5 |
|
| 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 | + |
4 | 15 | export default function ModelItems({ |
5 | 16 | block: { platform, model, input, result, parser }, |
6 | 17 | }: { |
7 | 18 | block: import("../../../pdl_ast").LitellmModelBlock |
8 | 19 | }) { |
| 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 | + |
9 | 46 | return ( |
10 | 47 | <> |
11 | 48 | {typeof platform === "string" && ( |
12 | 49 | <Group term="Platform" description={platform} /> |
13 | 50 | )} |
14 | 51 | {typeof model === "string" && <Group term="Model" description={model} />} |
15 | 52 | {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} |
26 | 55 | </> |
27 | 56 | ) |
28 | 57 | } |
0 commit comments