Functions Runtime
FunctionsRuntime
¶
A runtime for functions.
Parameters:
Source code in src/agentdojo/functions_runtime.py
181 182 |
|
register_function
¶
Registers a function with the runtime.
If function
is a function, it will be converted to a Function
instance and registered,
otherwise it will be registered as is. The conversion is made with make_function
.
Parameters:
-
function
(Callable[P, T] | Function
) –The function to register. It can be a function or a
Function
instance. Seemake_function
for more information.
Returns:
Source code in src/agentdojo/functions_runtime.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
run_function
¶
run_function(
env: TaskEnvironment | None,
function: str,
kwargs: Mapping[str, FunctionCallArgTypes],
raise_on_error: bool = False,
) -> tuple[FunctionReturnType, str | None]
Runs a function with the given arguments.
If raise_on_error
is True
, the function will raise an exception if an error occurs. Otherwise, it will return an error message as the second
argument. The error message has the format 'ErrorType: ErrorMessage'
.
Parameters:
-
env
(TaskEnvironment | None
) –The environment to extract dependencies from. It can be
None
if the function has no dependencies. -
function
(str
) –The name of the function to run.
-
kwargs
(Mapping[str, FunctionCallArgTypes]
) –The arguments to pass to the function.
-
raise_on_error
(bool
, default:False
) –If
True
, the function will raise an exception if an error occurs. Otherwise, it will return an error message.
Returns:
-
tuple[FunctionReturnType, str | None]
–A tuple containing the result of the function and an error message if an error occurred. If no error occurred, the second element will be
None
.
Raises:
-
ValidationError
–If the arguments are invalid and do not match the function's input schema.
-
ToolNotFoundError
–If the requested function is not available.
Source code in src/agentdojo/functions_runtime.py
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
Function
¶
Bases: BaseModel
, Generic[P, T]
A function that can be called by an agent and run by a FunctionsRuntime
.
make_function
¶
Creates a Function
instance from a function.
It does so by extracting the docstrings and type hints from the function. It then creates a Pydantic model for the input schema of the function.
Note
ReStructuredText is the recommended docstring format.
Parameters:
-
function
(Callable[P, T]
) –
Returns:
Raises:
-
ValueError
–If the function has no docstring or short description, or if the arguments docstrings are invalid.
Source code in src/agentdojo/functions_runtime.py
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 |
|
Depends
dataclass
¶
A dependency for a function. It is used to extract information from the environment to pass to the function.
env_dependency
instance-attribute
¶
The name of the attribute to extract from the environment or a function that returns the attribute from the environment.
extract_dep_from_env
¶
Extracts the dependency from the environment.
Source code in src/agentdojo/functions_runtime.py
31 32 33 34 35 |
|
FunctionCall
¶
Bases: BaseModel
An object containing information about a function call requested by an agent.
args
instance-attribute
¶
args: Mapping[str, FunctionCallArgTypes]
The arguments to pass to the function.
id
class-attribute
instance-attribute
¶
id: str | None = None
An optional ID for the function call. E.g., used by OpenAI and Anthropic.
placeholder_args
class-attribute
instance-attribute
¶
placeholder_args: (
Mapping[str, FunctionCallArgTypes] | None
) = None
An optional dictionary of placeholder arguments to use in by ground truth agent in injection tasks.
FunctionCallArgTypes
module-attribute
¶
Valid types for function call arguments.
FunctionReturnType
module-attribute
¶
FunctionReturnType: TypeAlias = (
BaseModel
| Sequence["FunctionReturnType"]
| dict
| str
| int
| float
| bool
| None
)
Union of valid return types for functions. The default FunctionsRuntime is not guaranteed to work with other types.
EmptyEnv
¶
Bases: TaskEnvironment
An empty environment for when a state is not needed.