Skip to content

trading_agent.tools.view_image_tool

The view_image tool: shows the model a picture file, such as a chart it made or one the user sent earlier.

Typical usage example:

tool = ViewImageTool(policy)

ViewImageArguments

Bases: BaseModel

The arguments of view_image.

Attributes:

Name Type Description
path str

The picture; a relative path starts at your desk folder.

ViewImageTool

ViewImageTool(policy, image_preparer=None)

Bases: Tool

Shows the model a PNG, JPEG, WebP or GIF file.

Creates the tool.

Parameters:

Name Type Description Default
policy PathPolicy

The desk's read rules.

required
image_preparer ImagePreparer | None

Checks and resizes pictures; None for a new one.

None
Source code in src/trading_agent/tools/view_image_tool.py
def __init__(
    self,
    policy: path_policy_module.PathPolicy,
    image_preparer: image_preparer_module.ImagePreparer | None = None,
):
    """Creates the tool.

    Args:
        policy (path_policy_module.PathPolicy): The desk's read rules.
        image_preparer (image_preparer_module.ImagePreparer | None): Checks and resizes pictures; None for a new one.
    """
    self._policy = policy
    self._image_preparer = (
        image_preparer or image_preparer_module.ImagePreparer()
    )

run async

run(arguments)

Loads the picture.

Parameters:

Name Type Description Default
arguments ViewImageArguments

The picture's path.

required

Returns:

Type Description
ToolResult

A line naming the picture, with the picture attached, or an error.

Source code in src/trading_agent/tools/view_image_tool.py
async def run(self, arguments: ViewImageArguments) -> tool.ToolResult:
    """Loads the picture.

    Args:
        arguments (ViewImageArguments): The picture's path.

    Returns:
        tool.ToolResult: A line naming the picture, with the picture attached, or an error.
    """
    try:
        path = self._policy.readable(arguments.path)
    except path_policy_module.PathRefusedError as error:
        return tool.ToolResult.error(str(error))
    if not path.is_file():
        return tool.ToolResult.error(f'{arguments.path} is not a file.')
    try:
        await asyncio.to_thread(self._image_preparer.check, path)
    except image_preparer_module.ImageError as error:
        return tool.ToolResult.error(f'{arguments.path}: {error}.')
    image = await asyncio.to_thread(self._image_preparer.prepare, path)
    if image is None:
        return tool.ToolResult.error(f'{arguments.path} could not be read.')
    return tool.ToolResult(
        text=f'This is the picture {arguments.path}.',
        image=image,
        picture_desk_path=self._desk_path(path),
    )