Skip to content

mirascope.core.bedrock.tool

The BedrockTool class for easy tool usage with Bedrock LLM calls.

Usage Documentation

Tools

BedrockToolConfig

Bases: ToolConfig

A tool configuration for Bedrock-specific features.

BedrockTool

Bases: BaseTool[ToolTypeDef]

A class for defining tools for Bedrock LLM calls.

Example:

from mirascope.core import prompt_template
from mirascope.core.bedrock import bedrock_call


def format_book(title: str, author: str) -> str:
    return f"{title} by {author}"


@bedrock_call("anthropic.claude-3-haiku-20240307-v1:0", tools=[format_book])
@prompt_template("Recommend a {genre} book")
def recommend_book(genre: str):
    ...


response = recommend_book("fantasy")
if tool := response.tool:  # returns an `BedrockTool` instance
    print(tool.call())

tool_schema classmethod

tool_schema() -> ToolTypeDef

Constructs a JSON Schema tool schema from the BaseModel schema defined.

Example:

from mirascope.core.bedrock import BedrockTool


def format_book(title: str, author: str) -> str:
    return f"{title} by {author}"


tool_type = BedrockTool.type_from_fn(format_book)
print(tool_type.tool_schema())  # prints the Bedrock-specific tool schema

Source code in mirascope/core/bedrock/tool.py
@classmethod
def tool_schema(cls) -> ToolTypeDef:
    """Constructs a JSON Schema tool schema from the `BaseModel` schema defined.

    Example:
    ```python
    from mirascope.core.bedrock import BedrockTool


    def format_book(title: str, author: str) -> str:
        return f"{title} by {author}"


    tool_type = BedrockTool.type_from_fn(format_book)
    print(tool_type.tool_schema())  # prints the Bedrock-specific tool schema
    ```
    """
    schema_generator = GenerateJsonSchemaNoTitles
    return ToolTypeDef(
        toolSpec=ToolSpecificationTypeDef(
            name=cls._name(),
            description=cls._description(),
            inputSchema={
                "json": cls.model_json_schema(schema_generator=schema_generator)
            },
        )
    )

from_tool_call classmethod

from_tool_call(
    tool_call: ToolUseBlockContentTypeDef,
) -> BedrockTool

Constructs an BedrockTool instance from a tool_call.

Parameters:

Name Type Description Default
tool_call ToolUseBlockContentTypeDef

The Bedrock tool call from which to construct this tool instance.

required
Source code in mirascope/core/bedrock/tool.py
@classmethod
def from_tool_call(cls, tool_call: ToolUseBlockContentTypeDef) -> BedrockTool:
    """Constructs an `BedrockTool` instance from a `tool_call`.

    Args:
        tool_call: The Bedrock tool call from which to construct this tool instance.
    """
    return cls.model_validate(
        {"tool_call": tool_call, **tool_call["toolUse"]["input"]}
    )