anthropic.tools¶
Classes for using tools with Anthropic's Claude API.
AnthropicTool
¶
Bases: BaseTool[ToolUseBlock]
A base class for easy use of tools with the Anthropic Claude client.
AnthropicTool
internally handles the logic that allows you to use tools with
simple calls such as AnthropicCallResponse.tool
or AnthropicTool.fn
, as seen in
the example below.
Example:
from mirascope import AnthropicCall, AnthropicCallParams
def animal_matcher(fav_food: str, fav_color: str) -> str:
"""Tells you your most likely favorite animal from personality traits.
Args:
fav_food: your favorite food.
fav_color: your favorite color.
Returns:
The animal most likely to be your favorite based on traits.
"""
return "Your favorite animal is the best one, a frog."
class AnimalMatcher(AnthropicCall):
prompt_template = """
Tell me my favorite animal if my favorite food is {food} and my
favorite color is {color}.
"""
food: str
color: str
call_params = AnthropicCallParams(tools=[animal_matcher])
response = AnimalMatcher(food="pizza", color="red").call
tool = response.tool
print(tool.fn(**tool.args))
#> Your favorite animal is the best one, a frog.
Source code in mirascope/anthropic/tools.py
from_base_type(base_type)
classmethod
¶
Constructs a AnthropicTool
type from a BaseType
type.
from_fn(fn)
classmethod
¶
from_model(model)
classmethod
¶
Constructs a AnthropicTool
type from a BaseModel
type.
from_tool_call(tool_call)
classmethod
¶
Extracts an instance of the tool constructed from a tool call response.
Given the tool call contents in a Message
from an Anthropic call response,
this method parses out the arguments of the tool call and creates an
AnthropicTool
instance from them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_call |
ToolUseBlock
|
The list of |
required |
Returns:
Type | Description |
---|---|
AnthropicTool
|
An instance of the tool constructed from the tool call. |
Raises:
Type | Description |
---|---|
ValidationError
|
if the tool call doesn't match the tool schema. |
Source code in mirascope/anthropic/tools.py
tool_schema()
classmethod
¶
Constructs JSON tool schema for use with Anthropic's Claude API.
Source code in mirascope/anthropic/tools.py
BaseTool
¶
Bases: BaseModel
, Generic[ToolCallT]
, ABC
A base class for easy use of tools with prompts.
BaseTool
is an abstract class interface and should not be used directly. When
implementing a class that extends BaseTool
, you must include the original
tool_call
from which this till was instantiated. Make sure to skip tool_call
when generating the schema by annotating it with SkipJsonSchema
.
Source code in mirascope/base/tools.py
args: dict[str, Any]
property
¶
The arguments of the tool as a dictionary.
fn: Callable[..., str]
property
¶
Returns the function that the tool describes.
call()
¶
description()
classmethod
¶
from_base_type(base_type)
abstractmethod
classmethod
¶
from_fn(fn)
abstractmethod
classmethod
¶
from_model(model)
abstractmethod
classmethod
¶
from_tool_call(tool_call)
abstractmethod
classmethod
¶
Extracts an instance of the tool constructed from a tool call response.
name()
classmethod
¶
tool_schema()
classmethod
¶
Constructs a JSON Schema tool schema from the BaseModel
schema defined.
Source code in mirascope/base/tools.py
convert_base_model_to_tool(schema, base)
¶
Converts a BaseModel
schema to a BaseToolT
type.
By adding a docstring (if needed) and passing on fields and field information in
dictionary format, a Pydantic BaseModel
can be converted into an BaseToolT
for
performing extraction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
type[BaseModel]
|
The |
required |
Returns:
Type | Description |
---|---|
type[BaseToolT]
|
The constructed |
Source code in mirascope/base/tools.py
convert_base_type_to_tool(schema, base)
¶
Converts a BaseType
to a BaseToolT
type.
Source code in mirascope/base/tools.py
convert_function_to_tool(fn, base)
¶
Constructs a BaseToolT
type from the given function.
This method expects all function parameters to be properly documented in identical order with identical variable names, as well as descriptions of each parameter. Errors will be raised if any of these conditions are not met.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn |
Callable
|
The function to convert. |
required |
Returns:
Type | Description |
---|---|
type[BaseToolT]
|
The constructed |
Raises:
Type | Description |
---|---|
ValueError
|
if the given function doesn't have a docstring. |
ValueError
|
if the given function's parameters don't have type annotations. |
ValueError
|
if a given function's parameter is in the docstring args section but the name doesn't match the docstring's parameter name. |
ValueError
|
if a given function's parameter is in the docstring args section but doesn't have a dosctring description. |
Source code in mirascope/base/tools.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|