base.tools¶
A base interface for using tools (function calling) when calling LLMs.
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
Toolkit
¶
A toolkit for organizing tools under a shared namespace.
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 |
|
tool_fn(fn)
¶
A decorator for adding a function to a tool class.
Adding this decorator will add an fn
property to the tool class that returns the
function that the tool describes. This is convenient for calling the function given
an instance of the tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn |
Callable
|
The function to add to the tool class. |
required |
Returns:
Type | Description |
---|---|
Callable[[type[BaseToolT]], type[BaseToolT]]
|
The decorated tool class. |