cohere.tools¶
Classes for using tools with Cohere chat APIs.
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
CohereTool
¶
Bases: BaseTool[ToolCall]
A base class for easy use of tools with the Cohere chat client.
CohereTool
internally handles the logic that allows you to use tools with simple
calls such as CohereCallResponse.tool
or CohereTool.fn
, as seen in the
examples below.
Example:
from mirascope.cohere import CohereCall, CohereCallParams
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(CohereCall):
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 = CohereCallParams(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/cohere/tools.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
from_base_type(base_type)
classmethod
¶
Constructs a CohereTool
type from a BaseType
type.
from_fn(fn)
classmethod
¶
from_model(model)
classmethod
¶
Constructs a CohereTool
type from a BaseModel
type.
from_tool_call(tool_call)
classmethod
¶
Extracts an instance of the tool constructed from a tool call response.
Given ToolCall
from an Cohere chat completion response, takes its function
arguments and creates an CohereTool
instance from it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_call |
ToolCall
|
The |
required |
Returns:
Type | Description |
---|---|
CohereTool
|
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/cohere/tools.py
tool_schema()
classmethod
¶
Constructs a tool schema for use with the Cohere chat client.
A Mirascope CohereTool
is deconstructed into a JSON schema, and relevant keys
are renamed to match the Cohere tool schema used to make function/tool calls in
the Cohere chat API.
Returns:
Type | Description |
---|---|
Tool
|
The constructed tool schema. |
Source code in mirascope/cohere/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 |
|