Skip to content

trading_agent.tools.send_image_tool

The send_image tool: shows the user a picture, in the web chat and on Telegram.

The picture is copied into the desk's uploads folder, so it stays viewable after the desk's scratch files are cleared, and is published as a message event that carries it. The web page shows it in the chat, and Telegram sends it as a photo: to the chat that asked when the user wrote from Telegram, otherwise to every paired chat when messages are pushed.

Typical usage example:

tool = SendImageTool(policy, store, event_bus, 'main')

SendImageArguments

Bases: BaseModel

The arguments of send_image.

Attributes:

Name Type Description
path str

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

caption str

A line shown with the picture, or an empty string.

SendImageTool

SendImageTool(policy, store, event_bus, desk_name)

Bases: Tool

Sends the user a picture file.

Creates the tool.

Parameters:

Name Type Description Default
policy PathPolicy

The desk's read rules.

required
store AttachmentStore

Keeps the copy of the picture that is shown.

required
event_bus EventBus

Where the message is published.

required
desk_name str

The desk sending it.

required
Source code in src/trading_agent/tools/send_image_tool.py
def __init__(
    self,
    policy: path_policy_module.PathPolicy,
    store: attachment_store_module.AttachmentStore,
    event_bus: event_bus_module.EventBus,
    desk_name: str,
):
    """Creates the tool.

    Args:
        policy (path_policy_module.PathPolicy): The desk's read rules.
        store (attachment_store_module.AttachmentStore): Keeps the copy of the picture that is shown.
        event_bus (event_bus_module.EventBus): Where the message is published.
        desk_name (str): The desk sending it.
    """
    self._policy = policy
    self._store = store
    self._event_bus = event_bus
    self._desk_name = desk_name

run async

run(arguments)

Copies the picture and publishes it.

Parameters:

Name Type Description Default
arguments SendImageArguments

The picture and its caption.

required

Returns:

Type Description
ToolResult

A confirmation, or an error the model can act on.

Source code in src/trading_agent/tools/send_image_tool.py
async def run(self, arguments: SendImageArguments) -> tool.ToolResult:
    """Copies the picture and publishes it.

    Args:
        arguments (SendImageArguments): The picture and its caption.

    Returns:
        tool.ToolResult: A confirmation, or an error the model can act on.
    """
    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.')
    if path.suffix.lower() not in attachment_store_module.IMAGE_EXTENSIONS:
        return tool.ToolResult.error(
            f'{arguments.path} is not a picture. Send a file ending in {", ".join(attachment_store_module.IMAGE_EXTENSIONS)}; save a chart or table as PNG first.'
        )
    if path.stat().st_size > attachment_store_module.MAXIMUM_BYTES:
        return tool.ToolResult.error(
            f'{arguments.path} is larger than 20 MB; save a smaller picture.'
        )
    try:
        attachment = await asyncio.to_thread(self._save, path)
    except attachment_store_module.AttachmentRefusedError as error:
        return tool.ToolResult.error(str(error))
    self._event_bus.publish(
        event_module.Event.now(
            event_module.EventKind.MESSAGE,
            self._desk_name,
            {
                'text': arguments.caption,
                'urgent': False,
                'attachments': [
                    attachment.to_json_dictionary(),
                ],
            },
        )
    )
    return tool.ToolResult.success(
        f'The user can now see {arguments.path}. Do not describe it again unless they ask.'
    )