Tool UIs
You can show a custom UI when a tool is called to let the user know what is happening.
Tool UI Components
You can put this component anywhere in your app inside the <AssistantRuntimeProvider />
component.
Tool UI Hooks
You can use this hook anywhere in your app inside the <AssistantRuntimeProvider />
component.
Inline Tool UI Hooks
If you need access to component props, you can use the useAssistantToolUI
hook. If you are passing a component inline, you should use the useInlineRender
hook to prevent the component from being re-mounted on every render.
Tool Execution Context
When implementing a tool's execute function, you have access to a context object that includes:
toolCallId
: A unique identifier for the current tool executionabortSignal
: An AbortSignal for handling cancellation
Schema Validation Error Handling
Tools can now handle schema validation errors through the experimental_onSchemaValidationError
property. This allows you to provide custom behavior when the tool's parameters fail validation:
Field-Level Validation Status
You can use the useToolArgsFieldStatus
hook to check the validation status of individual tool argument fields. This is useful for providing real-time feedback about the validity of specific input fields in your tool UI:
Function Calling for User Input
The following example shows a date_picker
tool that the AI can call to collect a date from the user.
Implementing Tool UIs: A Step-by-Step Guide to Creating Interactive AI Tools
In this tutorial, we'll walk through the process of implementing Tool UIs in assistant-ui. Tool UIs allow you to create custom interfaces for AI tools, enhancing the user experience and providing visual feedback when a tool is called.
Table of Contents
- Introduction to Tool UIs
- Creating a Basic Tool UI Component
- Using Tool UI Hooks
- Implementing Inline Tool UI Hooks
- Function Calling for User Input
1. Introduction to Tool UIs
Tool UIs in assistant-ui provide a way to display custom interfaces when an AI tool is called. This can help users understand what's happening behind the scenes and provide interactive elements when needed.
2. Creating a Basic Tool UI Component
Let's start by creating a simple Tool UI component for a web search tool.
To use this component, place it inside the <AssistantRuntimeProvider />
:
3. Using Tool UI Hooks
For more flexibility, you can create a Tool UI hook:
Use the hook in a component within the <AssistantRuntimeProvider />
:
4. Implementing Inline Tool UI Hooks
For cases where you need access to component props, use the useAssistantToolUI
hook with useInlineRender
:
5. Function Calling for User Input
Finally, let's create a Tool UI that allows user input, such as a date picker:
This Tool UI displays a date picker when called and allows the user to select a date. Once a date is chosen, it's added to the result and displayed.
By following these steps, you can create interactive and informative Tool UIs that enhance the AI chat experience in your application. These UIs provide visual feedback and allow for user interaction when AI tools are called, making your assistant more user-friendly and engaging."