Generative UI
The tool result in the UI doesn't look great and isn't something we would ever want to show to users. Wouldn't it be great if we could represent that information in a more engaging way? This is where generative user interfaces come in.
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.
To do this, we map over the toolInvocations we were already showing in the UI. If the toolName is equal to "getWeather", we pipe the results into the Weather component as props.
Update your page.tsx
with the following code:
Head back to the browser and try it out. Ask for the weather in a specific location and see how the UI dynamically generates a more engaging representation of the weather data.
You should now see a visually appealing weather component displayed instead of raw JSON data when requesting weather information.
You may also want to allow the component to interact with the chat and trigger subsequent generations! For example, you could add a button to get the weather for a random city. To do this, set an id
on your useChat
hook which will allow you to use the hook in any other component within your application.
Update your page.tsx
with the following code:
Now update the weather component to import and use the useChat
hook to trigger a new weather request when the button is clicked.
Now head back to the browser and try clicking the button o nthe weather component. You should see a new message in the chat window which will trigger a subsequent generation!