Tool Calling
Give the model access to your application state and enable the model to take action.
Tool calling (or function calling) in Hashbrown provides an intuitive approach to describing the tools that the model has access to.
- Execute a function in your React component scope.
- Return data to the model from state or a service.
Demo
How it Works
When you define a tool using Hashbrown's
- Provide the tool to the model using the
tools
property. - When the model receives a user message, it will analyze the message and determine if it needs to call any of the provided tools.
- If the model decides to call a function, it will invoke the function with the required arguments.
- The function executes within your React component and hook scope.
- Return the result that is sent back to the LLM.
The useTool()
Hook
import { useTool } from '@hashbrownai/react';
useTool({
name: 'getUser',
description: 'Get information about the current user',
handler: async (abortSignal) => {
return await fetchUser({ signal: abortSignal });
},
deps: [fetchUser],
});
- Use the
hook to define a function that the LLM can call. - The
name
property is the name of the function that the LLM will call. - The
description
property is a description of what the function does. This is used by the LLM to determine if it should call the function. - The
handler
property is the function that will be called when the LLM invokes the function. The function is invoked with anAbortSignal
and is expected to return aPromise
. - The
deps
property is an array of dependencies that are used to memoize the handler function. This is similar to how you would useuseCallback
in React.
UseToolOptions
Option | Type | Required | Description |
---|---|---|---|
name |
string |
Yes | The name of the function that the LLM will call |
description |
string |
Yes | Description of what the function does |
schema |
s.HashbrownType |
No | Schema defining the function arguments |
handler |
Function |
Yes | The function to execute when called |
deps |
React.DependencyList |
Yes | Dependencies used to memoize the handler; pass like you would to useCallback |
API Reference
useTool() API
See the hook signature
Handler Signatures
With input
Arguments:
handler: (input: s.Infer<Schema>, abortSignal: AbortSignal) => Promise<Result>;
Without input
Arguments:
handler: (abortSignal: AbortSignal) => Promise<Result>;
Providing the Tools
Provide the tools
when using Hashbrown's hooks-based APIs.
import { useChat, useTool } from '@hashbrownai/react';
import { s } from '@hashbrownai/core';
export function ChatComponent() {
// 1. The getUser() function returns authenticated user information to model
const getUser = useTool({
name: 'getUser',
description: 'Get information about the current user',
handler: async (abortSignal) => fetchUser({ signal: abortSignal }),
deps: [fetchUser],
});
// 2. The getLights() function returns application state to the model
const getLights = useTool({
name: 'getLights',
description: 'Get the current lights',
handler: async () => getLightsFromStore(),
deps: [getLightsFromStore],
});
// 3. The controlLight() function enables the model to mutate state
const controlLight = useTool({
name: 'controlLight',
description: 'Control a light',
schema: s.object('Control light input', {
lightId: s.string('The id of the light'),
brightness: s.number('The brightness of the light'),
}),
handler: async (input, abortSignal) =>
updateLight(input.lightId, { brightness: input.brightness }, abortSignal),
deps: [updateLight],
});
// 4. Specify the `tools` collection
const chat = useChat({
tools: [getUser, getLights, controlLight],
});
return null;
}
Let's review the code above.
- We use the
hook to define each tool. - We provide the collection of
tools
to the model.
Next Steps
Get structured data from models
Use Skillet schema to describe model responses.
Generate user interfaces
Expose React components to the LLM for generative UI.
Execute LLM-generated JS in the browser (safely)
Use Hashbrown's JavaScript runtime for complex and mathematical operations.