In accordance with the design characteristics of agentUniverse domain components, like other components, creating a Tool definition consists of two parts:
- tool_xx.yaml
- tool_xx.py
The tool_xx.yaml
contains important attribute information about the Tool component such as its name, description, inputs, outputs, etc.; tool_xx.py
includes the specific definition of the tool. Having understood this principle, let's take a closer look at how to create these two parts.
We will provide a detailed introduction to each component within the configuration.
name
: The name of the tool, you can set any name you desire.description
: A description of the tool, fill in according to your actual requirements.input_keys
: Alist
type, list of input parameters.
metadata
- metadata of component
type
: The component type, 'TOOL'module
: Path to the Tool entity packageclass
: Name of the Tool entity class
name: 'google_search_tool'
description: 'demo google search tool'
input_keys: ['input']
metadata:
type: 'TOOL'
module: 'xxx.tool.google_search_tool'
class: 'GoogleSearchTool'
The above is an actual example of a Tool configuration sample.
In addition to the standard configuration items introduced above, you can find more examples of tool configuration YAML files in the sample_standard_app.app.core.tool
directory of our sample project.
Moreover, agentUniverse does not restrict users from extending the Tool YAML configuration content. You can create any custom configuration keys according to your own requirements, but please be careful not to duplicate the names of the default configuration keywords mentioned above.
Create the corresponding Tool class object and inherit from the agentUniverse framework base class Tool
.
Write the actual logic snippet of the tool in the execute
method.
@abstractmethod
def execute(self, tool_input: ToolInput):
raise NotImplementedError
from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper
from agentuniverse.agent.action.tool.tool import Tool, ToolInput
class GoogleSearchTool(Tool):
"""The demo google search tool.
Implement the execute method of demo google search tool, using the `GoogleSerperAPIWrapper` to implement a simple Google search.
Note:
You need to sign up for a free account at https://serper.dev and get the serpher api key (2500 free queries).
"""
def execute(self, tool_input: ToolInput):
query = tool_input.get_data("input")
# get top3 results from Google search.
search = GoogleSerperAPIWrapper(serper_api_key='', k=3, type="search")
return search.run(query=query)
In this example, we integrated Google's search tool, which retrieves and returns the three most relevant pieces of content related to the search query.
With the Tool configuration and definition covered above, you have mastered all the steps of creating tools. Next, we will use these Tools, but before using them, please ensure that the created Tool is within the correct package scan path.
In the config.toml of the agentUniverse project, you need to configure the package corresponding to the Tool configuration. Please confirm again if the package path where your created file is located is under the CORE_PACKAGE
in the tool
path or its subpaths.
Taking the configuration in the example project as an example, it is as follows.
[CORE_PACKAGE]
# Scan and register tool components for all paths under this list, with priority over the default.
tool = ['sample_standard_app.app.core.tool']
You can set up any tool you have created in the tool of your agent according to the contents of Agent Creation and Usage section.
Refer to the example: demo_rag_agent
, with the specific file path being sample_standard_app/app/core/agent/rag_agent_case/demo_rag_agent.yaml
.
You can obtain the instance of the tool with the corresponding name through the .get_instance_obj(xx_tool_name)
method in the Tool manager, and call it using the run
method.
from agentuniverse.agent.action.tool.tool_manager import ToolManager
tool = ToolManager().get_instance_obj('your_tool_name')
tool_input = {'your_input_key': 'input_values'}
tool.run(**tool_input)
More examples of tools provided by the framework can be found in the sample_standard_app.app.core.tool
package path, where you can further explore the corresponding tools.
By now, you have mastered the definition and usage of Tool components. Go ahead and try creating and using tools.