AI functions
AI functions enhance and extend AI agents with custom functionality
When configuring your AI agents or AI assistants, you might find that certain questions require specific responses that are difficult to configure through instructions. For example, you might have industry-specific formulas that the model isn't familiar with. Or you may want the agent to take an action in response to a specific prompt.
In these cases (and many others), you can use Squid's AI functions. By extending a function in a Squid Service with the @aiFunction
decorator, you can connect your agent to these functions, allowing you to customize each agent with the functionality it requires.
Because these functions are defined in a Squid Service in Typescript, they can access the full power of the Squid platform. This means customization of these functions is limitless.
How to create AI functions
AI functions take a description
parameter, which is a string describing the function. This description is how the AI agent determines when to use the function, so it is important that it is clear and informative.
The following example uses the @aiFunction
decorator to define a function that an AI agent can use:
import { SquidService, aiFunction } from '@squidcloud/backend';
class AiService extends SquidService {
@aiFunction<{ shipName: string }>(
'This function returns the list of pirates in a ship. ' +
'Call this function whenever the user asks about the crew of a ship.',
[
{
name: 'shipName',
description: 'The name of the ship',
type: 'string',
required: true,
}
]
)
async listPiratesInAShip(params: { shipName: string }): Promise<string> {
const { shipName } = params;
if (shipName === 'Black Pearl') {
return 'Jack Sparrow';
}
else if (shipName === "Queen Anne's Revenge") {
return 'Black Beard';
}
return 'No ship found';
}
}
To add the function to an AI agent, pass the function name with a call to ask
as part of the options parameter:
const response = await squid
.ai()
.agent('AGENT_ID')
.ask('Which pirate was captain of the Black Pearl?', {
functions: ['listPiratesInAShip'],
});
const assistantId = await createAssistant('YOUR_ASSISTANT_NAME', "YOUR ASSISTANT'S INSTRUCTIONS", ['listPiratesInAShip']);
Next Steps
Want to see a more complex example of an AI function in action? Take a look at our AI home maintenance tutorial.