trading_agent.tools.tool
¶
The base class for every tool the agent can use, and the result a tool returns.
A tool describes its arguments as a pydantic model. The model gives the JSON schema the AI model sees, and checks the arguments the AI model sends before the tool runs.
Typical usage example:
class GetTimeTool(Tool): name = 'get_time' description = 'Returns the current date and time in India.' arguments_model = NoArguments
async def run(self, arguments: NoArguments) -> ToolResult:
return ToolResult.success('10:15')
ToolResult
dataclass
¶
What a tool returns to the model.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The output the model reads. |
is_error |
bool
|
Whether the tool failed, so the model knows to change course. |
image |
MessageImage | None
|
A picture for the model to look at, such as a screenshot, or None. |
picture_desk_path |
str | None
|
Where that picture is saved, relative to the desk folder, so the web page can show it in the tool's card; None when it is not saved in the desk folder. |
success
classmethod
¶
Creates a successful result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The output. |
required |
Returns:
| Type | Description |
|---|---|
ToolResult
|
The result. |
error
classmethod
¶
Creates a failed result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
What went wrong, written so the model can act on it. |
required |
Returns:
| Type | Description |
|---|---|
ToolResult
|
The result. |
NoArguments
¶
Bases: BaseModel
The arguments of a tool that takes none.
Tool
¶
One tool. Subclasses set the class attributes and override run.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
The name the model calls the tool by. |
|
description |
What the tool does, written for the model. |
|
arguments_model |
type[BaseModel]
|
The pydantic model that describes and checks the arguments. |
timeout_seconds |
float | None
|
How long one call may run, or None for the registry's default. |
input_schema
¶
The JSON schema of the tool's arguments, as the model sees it.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A JSON schema object. |
Source code in src/trading_agent/tools/tool.py
run
async
¶
Runs the tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments
|
BaseModel
|
The checked arguments, an instance of arguments_model. |
required |
Returns:
| Type | Description |
|---|---|
ToolResult
|
The output. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always, because subclasses must override this method. |
Source code in src/trading_agent/tools/tool.py
close
async
¶
Releases anything the tool started, such as a bash process.
Tools that start nothing keep this empty default.