Created
August 6, 2024 20:33
-
-
Save metaskills/2bf51e5f4ed89a5d5e0d3dfe78be0d1f to your computer and use it in GitHub Desktop.
Bespoke UI Example with OpenAI Structured Outputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from "zod"; | |
import { zodResponseFormat } from "openai/helpers/zod"; | |
const uiSchema = z | |
.lazy(() => | |
z.object({ | |
type: z | |
.enum(["div", "button", "header", "section", "field", "form"]) | |
.describe("The type of the UI component"), | |
label: z | |
.string() | |
.describe( | |
"The label of the UI component, used for buttons or form fields" | |
), | |
children: z.array(uiSchema).describe("Nested UI components"), | |
attributes: z | |
.array( | |
z.object({ | |
name: z | |
.string() | |
.describe( | |
"The name of the attribute, for example onClick or className" | |
), | |
value: z.string().describe("The value of the attribute"), | |
}) | |
) | |
.describe( | |
"Arbitrary attributes for the UI component, suitable for any element" | |
), | |
}) | |
) | |
.describe("Dynamically generated UI"); | |
const responseFormat = zodResponseFormat(uiSchema, "ui"); | |
console.log(JSON.stringify(responseFormat, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment