Generative User Interfaces
Generative user interfaces (generative UI) is the process of allowing a large language model (LLM) to go beyond text and “generate UI”. This creates a more engaging and AI-native experience for users. At the core of generative UI are tools , which are functions you provide to the model to perform specialized tasks like getting the weather in a location. The model can decide when and how to use these tools based on the context of the conversation. Generative UI is the process of connecting the results of a tool call to a React component. Here’s how it works:- You provide the model with a prompt or conversation history, along with a set of tools.
- Based on the context, the model may decide to call a tool.
- If a tool is called, it will execute and return data.
- This data can then be passed to a React component for rendering.
Build a Generative UI Chat Interface
Let’s create a chat interface that handles text-based conversations and incorporates dynamic UI elements based on model responses.Basic Chat Implementation
Start with a basic chat implementation using theuseChat hook:
streamText function to process chat messages and stream the model’s responses back to the client.
Create a Tool
Before enhancing your chat interface with dynamic UI elements, you need to create a tool and corresponding React component. A tool will allow the model to perform a specific action, such as fetching weather information. Create a new file calledai/tools.ts with the following content:
weatherTool. This tool simulates fetching weather information for a given location. This tool will return simulated data after a 2-second delay. In a real-world application, you would replace this simulation with an actual API call to a weather service.
Update the API Route
Update the API route to include the tool you’ve defined:streamText call, let’s build a React component to display the weather information it returns.
Create UI Components
Create a new file calledcomponents/weather.tsx:
temperature, weather, and location (exactly what the weatherTool returns).
Render the Weather Component
Now that you have your tool and corresponding React component, let’s integrate them into your chat interface. You’ll render the Weather component when the model calls the weather tool. To check if the model has called a tool, you can check theparts array of the UIMessage object for tool-specific parts. In AI SDK 5.0, tool parts use typed naming: tool-${toolName} instead of generic types.
Update your page.tsx file:
- Use manual input state management with
useStateinstead of the built-ininputandhandleInputChange. - Use
sendMessageinstead ofhandleSubmitto send messages. - Check the
partsarray of each message for different content types. - Handle tool parts with type
tool-displayWeatherand their different states (input-available,output-available,output-error).
Expanding Your Generative UI Application
You can enhance your chat application by adding more tools and components, creating a richer and more versatile user experience. Here’s how you can expand your application:Adding More Tools
To add more tools, simply define them in yourai/tools.ts file:
components/stock.tsx:
page.tsx file to include the new Stock component: