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 Angular's injection context.
- Return data to the model from state or an Angular 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 is executed within Angular's injection context
- Return the result that is sent back to the LLM.
The createTool()
Function
import { createTool } from '@hashbrownai/angular';
createTool({
name: 'getUser',
description: 'Get information about the current user',
handler: (): Promise<User> => {
const authService = inject(AuthService);
return authService.getUser();
},
});
- Use the
function 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. This is where you can perform any logic you need, such as fetching data from a service or performing a task. The function is invoked with anAbortSignal
and is expected to return aPromise
.
CreateToolOptions
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 | object |
No | Schema defining the function arguments |
handler |
Function |
Yes | The function to execute when called |
API Reference
createTool() API
See the function 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 resource-based APIs.
@Component({
selector: 'app-chat',
providers: [LightsStore],
template: ` <!-- omitted for brevity - full code in stackblitz example --> `,
})
export class ChatComponent {
lightsStore = inject(LightsStore);
chat = chatResource({
// 1. Specify the tools collection using createTool() function
tools: [
//2. The getUser() function returns authenticated user information to model
createTool({
name: 'getUser',
description: 'Get information about the current user',
handler: () => {
const authService = inject(AuthService);
return authService.getUser();
},
}),
// 3. The getLights() function returns application state to the model
createTool({
name: 'getLights',
description: 'Get the current lights',
handler: async () => this.lightsStore.entities(),
}),
// 4. The controlLight() function enables the model to mutate state
createTool({
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) =>
this.lightsStore.updateLight(input.lightId, {
brightness: input.brightness,
}),
}),
],
});
sendMessage(message: string) {
this.chat.sendMessage({ role: 'user', content: message });
}
}
Let's review the code above.
- First, we define the
tools
array in thechatResource
configuration. - We use the
function to define each tool. - The
getUser()
function returns the authenticated user information to the model. - The
getLights()
function returns application state to the model. - The
controlLight()
function enables the model to mutate application state.
Next Steps
Get structured data from models
Use Skillet schema to describe model responses.
Generate user interfaces
Expose Angular 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.