| tags |
|
||
|---|---|---|---|
| title | Generative UI | ||
| description | Generative UI refers to a dynamic and adaptive user interface that is generated and adjusted in real-time by artificial intelligence (AI) to better meet user needs and preferences. Unlike static interfaces, where the layout and elements are predetermined, generative UIs leverage AI algorithms to create and modify interface components based on user interactions and contextual information. | ||
| authors |
|
||
| date | 2024-08-08 |
- A generative UI (genUI) is a user interface that responds to the user with AI-generated elements instead of just text messages.
- It offers a personalized experience based on the user's needs and context. Users can interact with this AI-generated interface and sometimes make requests through it.
- Enhances UI/UX when using chatbots.
- Generative UI allows for highly personalized, tailor-made interfaces that suit the needs of each individual.
Goal
- The chatbot can respond to the user with both text and UI in the correct order.
- The chatbot should understand the responded UI.
- The approach should be implementable on any web technology.
Idea
-
Langchain-supported tool that allows LLM to detect which agent to take action (given the description, defined parameters) ⇒ On each tool, define the corresponding UI component to render when it triggers
-
LLM supports streaming responses. There are two approaches:
- Construct messages based on all events received
// Example final message [ { type: "text", data: "......", }, { type: "movie-search-tool", data: { title: ".....", description: "....", }, }, ];
- Directly forward stream events to the frontend to handle using (HTTP Streaming, Server-Sent Events, WebSocket, etc.)
- After a tool is done, include the tool result data in the chat history to help the chatbot understand the context.
Currently, the most used solution is the Vercel AI SDK, which follows Approach 2.
- Utilizes the
server componentto handle event streaming on the Next.js server instead of on the browser. - Uses the
createStreamableUImethod to run on the Next.js server, creating aSuspend-wrapped Componentthat can respond to the browser immediately and trigger UI updates without client-side code. - Here is the pseudo code that simply explains what it does under the hood.
// use server
const askGPT = async () => {
const ui = createStreamableUI()(
// invoke some task
async () => {
workflow = createAgentExecutor();
// handle stream events from LLM
for await (const streamEvent of (
runnable as Runnable<RunInput, RunOutput>
).streamEvents(inputs, {
version: "v2",
})) {
// handle event stream from LLM
ui.update(<UI props={data} />);
}
}
)();
return ui;
};const Chat = () => {
const [elements, setElements] = useState([]);
const handleSubmit = (message: string) => {
const ui = askGPT({
message: message,
});
setElements([...elements, ui]);
};
return (
<form
onSubmit={() => {
handleSubmit(inputValue);
}}
>
{elements}
<input />
</form>
);
};- Easy to use; everything is provided, so you only need to import the function or copy the code to use it.
- Library is usable for Next.js with server component support.
- Poorly documented for Next.js’s Page Router; it is recommended for the App Router.
- Handle event flow





