Tools
[!TIP]
Location within the framework
Hive-agent-framework/tools.
Tools in the context of an agent refer to additional functionalities or capabilities integrated with the agent to perform specific tasks beyond text processing.
These tools extend the agent's abilities, allowing it to interact with external systems, access information, and execute actions.
Built-in tools
PythonTool
Run arbitrary Python code in the remote environment.
WikipediaTool
Search for data on Wikipedia.
GoogleSearchTool
Search for data on Google using Custom Search Engine.
DuckDuckGoTool
Search for data on DuckDuckGo.
SQLTool
Execute SQL queries against relational databases.
ElasticSearchTool
Perform search or aggregation queries against an ElasticSearch database.
CustomTool
Run your own Python function in the remote environment.
LLMTool
Use an LLM to process input data.
DynamicTool
Construct to create dynamic tools.
ArXivTool
Retrieve research articles published on arXiv.
WebCrawlerTool
Retrieve content of an arbitrary website.
OpenMeteoTool
Retrieve current, previous, or upcoming weather for a given destination.
MilvusDatabaseTool
Perform retrieval queries (search, insert, delete, manage collections) against a MilvusDatabaseTool database.
OpenAPITool
Send requests to and receive responses from API server.
➕ Request
All examples can be found here.
[!TIP]
Would you like to use a tool from LangChain? See the example.
Usage
Basic
Source: examples/tools/base.ts
Advanced
Source: examples/tools/advanced.ts
[!TIP]
To learn more about caching, refer to the Cache documentation page.
Usage with agents
Source: examples/tools/agent.ts
Writing a new tool
To create a new tool, you have the following options on how to do that:
Implement the base
Toolclass.Initiate the
DynamicToolby passing your own handler (function) with thename,descriptionandinput schema.Initiate the
CustomToolby passing your own Python function (code interpreter needed).
Implementing the Tool class
Tool classThe recommended and most sustainable way to create a tool is by implementing the base Tool class.
Basic
Source: examples/tools/custom/base.ts
[!TIP]
inputSchemacan be asynchronous.
[!TIP]
If you want to return an array or a plain object, use
JSONToolOutputor implement your own.
Advanced
If your tool is more complex, you may want to use the full power of the tool abstraction, as the following example shows.
Source: examples/tools/custom/openLibrary.ts
Implementation Notes
Implement the
Toolclass:MyNewToolOutputis required, must be an implementation ofToolOutputsuch asStringToolOutputorJSONToolOutput.ToolOptionsis optional (default BaseToolOptions), constructor parameters that are passed during tool creationToolRunOptionsis optional (default BaseToolRunOptions), optional parameters that are passed to the run method
Be given a unique name:
Note: Convention and best practice is to set the tool's name to the name of its class
Provide a natural language description of what the tool does:
❗Important: the agent uses this description to determine when the tool should be used. It's probably the most important aspect of your tool and you should experiment with different natural language descriptions to ensure the tool is used in the correct circumstances. You can also include usage tips and guidance for the agent in the description, but its advisable to keep the description succinct in order to reduce the probability of conflicting with other tools, or adversely affecting agent behavior.
Declare an input schema:
This is used to define the format of the input to your tool. The agent will formalise the natural language input(s) it has received and structure them into the fields described in the tool's input. The input schema can be specified using Zod (recommended) or JSONSchema. It must be a function (either sync or async). Zod effects (e.g.
z.object().transform(...)) are not supported. The return value ofinputSchemamust always be an object and pass validation by thevalidateSchema()function defined in schema.ts. Keep your tool input schema simple and provide schema descriptions to help the agent to interpret fields.Implement initialisation:
The unnamed static block is executed when your tool is called for the first time. It is used to register your tool as
serializable(you can then use theserialize()method).Implement the
_run()method:
Using the DynamicTool class
DynamicTool classThe DynamicTool allows you to create a tool without extending the base tool class.
Source: examples/tools/custom/dynamic.ts
The name of the tool is required and must only contain characters between a-z, A-Z, 0-9, or one of - or _. The inputSchema and description are also both required.
Using the CustomTool (Python functions)
CustomTool (Python functions)If you want to use the Python function, use the CustomTool.
Source: examples/tools/custom/python.ts
[!IMPORTANT]
Custom tools are executed within the code interpreter, but they cannot access any files. Only
PythonTooldoes.
General Tips
Data Minimization
If your tool is providing data to the agent, try to ensure that the data is relevant and free of extraneous metatdata. Preprocessing data to improve relevance and minimize unnecessary data conserves agent memory, improving overall performance.
Provide Hints
If your tool encounters an error that is fixable, you can return a hint to the agent; the agent will try to reuse the tool in the context of the hint. This can improve the agent's ability to recover from errors.
Security & Stability
When building tools, consider that the tool is being invoked by a somewhat unpredictable third party (the agent). You should ensure that sufficient guardrails are in place to prevent adverse outcomes.