Tools & Function Calls
Give Agents the ability to interact with your application context and services.
Last updated
Give Agents the ability to interact with your application context and services.
Last updated
Tools enable Agents to go beyond generating text by facilitating interaction with your application services, or external APIs.
Think about Tools as special functions that your AI agent can use when it needs to perform specific tasks. They let you extend your Agent's capabilities by giving it access to specific functions it can call inside your code.
In the example we can define a tool to make the Agent able to retrieve the YouTube video transcription, so it can crteate a short summary:
Let’s break down the code.
We introduced the new method tools()
into the Agent class. This method expects to return an array of Tool objects that the AI will be able to use if needed.
In this example we return an array of just one tool, named get_transcription
.
Notice that the ToolProperty
we define should match with the signature of the function you use as a callable. The callable gets the $video_url
arguments, and the name of the property is exactly "video_url".
The most important thing are the name and description you give to the tool and its properties. All these pieces of information will be passed to the LLM in natural language. The more explicit and clear you are, the more likely the LLM understands when, if, and why, it’s the case to use the tool.
Once the Agent decides to use a tool the callable function is executed. Here we can implement the logic to retrieve the video transcription and return the information back to the LLM.
Neuron provides you with these clear and simple APIs and automates all the underlying interactions with the LLM. Once you get the point it can immediately open to a possibility to connect basically everything you want to the Agent. Being able to execute local functions allows you to invoke any external APIs or application components.
Thanks to the NeuronAI modular architecture, Tools are just a component of the toolkit that rely on the ToolInterface
interface. You are free to create pre-packaged tool classes that implements common functionalities, and release them as external composer packages or submit a PR to our repository to have them integrated into the core framework.
To create a new Tool you can extend the NeuronAI\Tools\Tool
class.
You can attach the tool in the agent class as usual:
Transcriptions are just an example. You can eventually implement other tools to make the Agent able to retrieve other video metadata to enhance its video analysis capabilities.
Finally you can talk to the agent asking the summary of a YouTube video.
When you provide the Agent with tools, Neuron sends their information to the LLM along with the user message.
Once the LLM reads the prompt and the information of the tools attached, it will decide if some tool can help to gather additional information to respond to the user prompt. In that case it will return a special response that contains the tools the LLM wants to call.
Neuron automatically manages this response for you, executing the callable of the tools the LLM decided to call, and return the result back to the LLM to get its final response.
In the image below you can see all the details about the execution of the tool to retrieve the transcription of the video:
Neuron allows you to define the format of the data you want to receive into the tool function. You can nest these objects inside each other to define complex data structures.
Be careful when defining complex data structures. Even the more advanced models can make mistakes with just a couple of properties. We strongly recommend to keep the input properties of your tools as simple as possible to improve the reliability of your agent execution.
This class represent a simple scalar value like string, int, or boolean.
Neuron allows you to instruct the LLM to provide complex data structures as input parameters of the tool function. ArrayProperty
allows you to require a list of items with specific characteristics.
In the example below we require an array of string.
In the same way you can define the object data structure:
Neuron ships with several built-in tools that allows you to quickly equip your agents with many skills. You can use these tools individually or attach entire toolkits with a single line of code.
The MySQL toolkit makes your agent able to interact with your database. If you ask "How many votes did the authors get in the last 14 days?", the agent doesn’t guess or hallucinate an answer. Instead, it recognizes that this question requires database access, identifies the appropriate tables involved and retrieves real data from your system.
The PDO instance is basically a connection to a specific database, so you could aslo think to create dedicated credentials for your agent. It could be helpful to control the level of access your agent has to the database.
Anyway you have separate tools for reading and writing to the database. If you are not confident about your agent behaviour you may not provide the writing tool.
This tool allows agents to understand the structure of your database, enabling them to construct intelligent queries without requiring you to hardcode table structures or relationships into prompts. This tool essentially gives your agent the equivalent of a database administrator’s understanding of your schema, allowing it to craft queries that respect your data model and take advantage of existing indexes and relationships.
This tool also accept a second argument $tables
. You can basically pass a list of tables that you want to include in the schema information passed to the LLM. This is basically a way to limit the scope of the queries the agent will later execute on the database.
By limiting the schema scope, you can create specialized agents that focus on specific areas of your application. A content management agent might only need access to articles, categories, and tags, while a user administration agent requires visibility into users, roles, and permissions tables. This approach not only improves performance but also reduces the cognitive load on the language model, leading to more accurate and focused responses.
Use this tool to make your agent able to run SELECT query against the database.
Use this tool to make your agent able to performs write operations against the database (INSERT, UPDATE, DELETE).
This toolkit enable your agent to performs web search, and read the content of a specific URL.
You can customize the default options to retrieve search results by passing your preference in the withOptions
method:
This toolkit enable your agent to performs web search, and read the content of a specific URL.
The user_id
arguments allows you to separate the long term memory in different silos if you want to serve multiple users. Based on your use case you can use this parameter as a "key" to separate the memory for the various entities the agent interact to (users, companies, etc.).
Notice how the __invoke()
method accepts the same arguments defined by the ToolProperty
. In this example I'm using an external service to retrieve the YouTube video transcription called .
To watch inside this workflow you should to the Inspector monitoring dashboard in order to see the tool call execution flow in real-time.
All the tools in the MySQL toolkit require a instance as a constructor argument. If you are in a framework environment or you are already using an ORM in general, you can gather the underlying PDO instance from the ORM and pass it to the tools. You can learn more about this implementation strategy in this in-depth article:
It makes your Agent able to search the web. It requires access to .
Extract web page content from an URL. It requires access to .
It makes your Agent able to search the web. It requires access to .
Extract web page content from an URL. It requires access to .
This toolkit connects a NeuronAI Agent to knowledge graph. This kind of system allows the agent to store relevant facts that may emerge during interactions with the agent over time. It's a long term memory in the sense that is not limited to the current conversation like the component does. It's an external persistent storage the agent will use to store and retrieve single pieces of information that can allow more personalized answers.
To learn more about the capabilities of these kind of system you can visit the Zep website: