-
Notifications
You must be signed in to change notification settings - Fork 593
Docs: BYO Chat Library #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AbhinRustagi
wants to merge
8
commits into
main
Choose a base branch
from
ar-chat-library-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c585de8
fix: add docs for BYO chat library
AbhinRustagi ad6c941
Merge branch 'main' of github.com:thesysdev/openui into ar-chat-libra…
AbhinRustagi 6dea5e3
fix: update fields
AbhinRustagi 7a84021
fix: change name
AbhinRustagi ac1f8ca
fix: update the content & resolve comments
AbhinRustagi cdfeea8
fix: content fix
AbhinRustagi 22e6df0
Merge branch 'main' of github.com:thesysdev/openui into ar-chat-libra…
AbhinRustagi 8646b50
fix: change content
AbhinRustagi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| --- | ||
| title: "OpenUI Cloud: Chat Library" | ||
| description: Use OpenUI Cloud with its built-in chat library, or bring your own components. | ||
| --- | ||
|
|
||
| ## Using the built-in library | ||
|
|
||
| Out of the box, OpenUI Cloud ships a general-purpose chat library: text, headers, tables, charts, forms, tabs, buttons, and more. | ||
|
|
||
| ```tsx | ||
| import { chatLibrary } from "@openuidev/thesys"; | ||
|
|
||
| <AgentInterface llm={llm} componentLibrary={chatLibrary} />; | ||
| ``` | ||
|
|
||
| And the backend uses it by default when building the request instructions: | ||
|
|
||
| ```ts | ||
| instructions: createResponsesInstructions({ | ||
| instructions: "Optional instructions for the model.", | ||
| }); | ||
| ``` | ||
|
|
||
| OpenUI Cloud takes care of everything in between: the system prompt, output validation, and automatic repair all target the built-in library. | ||
|
|
||
| ## Using your own library | ||
|
|
||
| There can be multiple reasons to use your library instead of the built-in one like if the domain calls for components the generic set can't express or when the app follows its own design system. To lift it, you can provide your own chat library. | ||
|
|
||
| **Define the library.** One [`defineComponent`](/docs/openui-lang/defining-components) per component, in the frontend where your React components live. Prop schemas and descriptions are what the model sees; `id` is an optional free-form revision tag: | ||
|
|
||
| ```tsx title="src/lib/chat-library.tsx" | ||
| import { createLibrary, defineComponent } from "@openuidev/react-lang"; | ||
| import { z } from "zod/v4"; | ||
| import { Metric, Panel } from "@/components"; | ||
|
|
||
| const MetricDef = defineComponent({ | ||
| name: "Metric", | ||
| description: "A single KPI stat with an optional trend arrow.", | ||
| props: z.object({ | ||
| label: z.string(), | ||
| value: z.string(), | ||
| trend: z.enum(["up", "down"]).optional(), | ||
| }), | ||
| component: ({ props }) => <Metric {...props} />, | ||
| }); | ||
|
|
||
| const PanelDef = defineComponent({ | ||
| name: "Panel", | ||
| description: "Top-level container. Children stack vertically.", | ||
| props: z.object({ children: z.array(MetricDef.ref) }), | ||
| component: ({ props, renderNode }) => <Panel>{renderNode(props.children)}</Panel>, | ||
| }); | ||
|
AbhinRustagi marked this conversation as resolved.
|
||
|
|
||
| export const myLibrary = createLibrary({ | ||
| id: "acme-chat@1", | ||
| root: "Panel", | ||
| components: [PanelDef, MetricDef], | ||
| }); | ||
| ``` | ||
|
|
||
| **Generate the spec handover file.** [`openui generate-spec`](/docs/api-reference/cli#openui-generate-spec) turns the library module into a self-contained JSON file for the backend: | ||
|
|
||
| ```bash tab="pnpm" tab-group="pkg" | ||
| pnpx @openuidev/cli@latest generate-spec src/lib/chat-library.tsx --out ./generated/chat-library-spec.json | ||
| ``` | ||
|
|
||
| ```bash tab="bun" tab-group="pkg" | ||
| bunx @openuidev/cli@latest generate-spec src/lib/chat-library.tsx --out ./generated/chat-library-spec.json | ||
| ``` | ||
|
|
||
| ```bash tab="yarn" tab-group="pkg" | ||
| yarn dlx @openuidev/cli@latest generate-spec src/lib/chat-library.tsx --out ./generated/chat-library-spec.json | ||
| ``` | ||
|
|
||
| ```bash tab="npm" tab-group="pkg" | ||
| npx @openuidev/cli@latest generate-spec src/lib/chat-library.tsx --out ./generated/chat-library-spec.json | ||
| ``` | ||
|
|
||
| **Declare it in the backend call.** Import the generated JSON and pass it as `chatLibrary`. | ||
|
|
||
| ```diff | ||
| + import chatLibrarySpec from "./generated/chat-library-spec.json"; | ||
|
|
||
| - instructions: createResponsesInstructions(), | ||
| + instructions: createResponsesInstructions({ | ||
| + chatLibrary: chatLibrarySpec, | ||
| + systemPromptOptions: { preamble: "You build dashboards for Acme operators." }, | ||
| + }), | ||
|
AbhinRustagi marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| `systemPromptOptions` (only valid alongside `chatLibrary`) tunes the generated prompt: `preamble`, `additionalRules`, `examples`. | ||
|
|
||
| **Swap the client library:** | ||
|
|
||
| ```diff | ||
| - import { chatLibrary } from "@openuidev/thesys"; | ||
| + import { myLibrary } from "@/lib/chat-library"; | ||
|
|
||
| - <AgentInterface llm={llm} componentLibrary={chatLibrary} />; | ||
| + <AgentInterface llm={llm} componentLibrary={myLibrary} />; | ||
| ``` | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.