Tool module
Tool
Bases: Module
A module that wraps an async function as a callable tool.
The Tool module allows you to wrap any async function and use it as a
module within Synalinks programs. It automatically extracts the function's
schema from its type hints and docstring.
Example:
import synalinks
@synalinks.saving.register_synalinks_serializable()
async def calculate(expression: str):
"""Calculate the result of a mathematical expression.
Args:
expression (str): The mathematical expression to calculate.
"""
if not all(char in "0123456789+-*/(). " for char in expression):
return {"result": None, "log": "Error: invalid characters"}
result = eval(expression, {"__builtins__": None}, {})
return {"result": result}
tool = synalinks.Tool(calculate)
result = await tool(expression="2 + 2")
Important
Optional Parameters: Parameters with default values are
supported. A defaulted parameter is left out of the schema's
required list and its default is emitted in the schema, so the
language model may omit it and the function's default applies.
(Optional[T] is treated as T; the None member is dropped.)
The "every property must be required" rule that some providers
enforce only applies to strict structured output, which is a
separate code path from tool calling.
Complete Docstring Required: The wrapped function must have a
complete docstring with an Args: section that documents every
parameter. The Tool extracts parameter descriptions from the docstring
to build the JSON schema sent to the language model. Missing
descriptions will raise a ValueError.
Example of a properly documented tool function:
async def search(query: str, limit: int):
"""Search the database for matching records.
Args:
query (str): The search query string.
limit (int): Maximum number of results to return.
"""
# Implementation here
return {"results": [...]}
Images and audio: A tool can show images to the language model,
or play it audio, by returning synalinks.Image / synalinks.Audio
values in its dict (alone, or in a list). The agents attach them to
the tool result so a model that takes them gets them, and they stay
in the returned trajectory for the user:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The async function to wrap as a tool. |
required |
name
|
str
|
Optional. The name of the module. Defaults to the function name. |
None
|
description
|
str
|
Optional. The description of the module. Defaults to the function's docstring description (its summary and the paragraphs before the Args section). |
None
|
trainable
|
bool
|
Whether the module's variables should be trainable. Defaults to False since tools typically don't have trainable state. |
False
|
Source code in synalinks/src/modules/core/tool.py
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
call(training=False, **kwargs)
async
Execute the wrapped function with the provided arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
training
|
bool
|
Whether in training mode. Not used by Tool but included for consistency with other modules. |
False
|
**kwargs
|
Any
|
The arguments to pass to the wrapped function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
JsonDataModel |
Optional[JsonDataModel]
|
The result wrapped in a JsonDataModel with the output schema. |
Source code in synalinks/src/modules/core/tool.py
compute_output_spec(training=False, **kwargs)
async
Compute the output specification for the tool.
Uses the function's schema to define the output structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
training
|
bool
|
Whether in training mode. |
False
|
**kwargs
|
Any
|
The input arguments. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
SymbolicDataModel |
SymbolicDataModel
|
A SymbolicDataModel with the tool's output schema. |
Source code in synalinks/src/modules/core/tool.py
get_input_schema()
Get the JSON schema for this tool's input parameters.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The JSON schema describing the tool's input parameters. |
Source code in synalinks/src/modules/core/tool.py
get_tool_schema()
Get the JSON schema for this tool's parameters.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The JSON schema describing the tool's input parameters. |