Parsing callables
conatus.utils.callable_parsing.actions
¶
Utilities for parsing actions.
add_termination_action
¶
add_termination_action(
existing_actions: list[Action],
expected_outputs: OrderedDict[
str, tuple[TypeOfType, str]
],
) -> list[Action]
Add the termination action to the list of actions.
| PARAMETER | DESCRIPTION |
|---|---|
existing_actions
|
The list of actions to which we will add the termination action. |
expected_outputs
|
The expected outputs of the task. The keys are the names of the outputs, and the values are a tuple containing the type of the output and its description.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Action]
|
The list of actions with the termination action added. |
Source code in conatus/utils/callable_parsing/actions.py
process_user_provided_actions
¶
process_user_provided_actions(
user_provided_actions: (
Collection[RawAction]
| Sequence[RawAction]
| ActionStarterPack
),
*,
accepts_empty_actions_parameter: bool
) -> list[Action]
Normalize the values of the 'actions' parameter to a list of actions.
Note that this function does not add the default actions to the list of actions.
| PARAMETER | DESCRIPTION |
|---|---|
user_provided_actions
|
The list of actions, or action-like objects that need to be cast to actions.
TYPE:
|
accepts_empty_actions_parameter
|
Whether the task accepts an empty list of actions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Action]
|
The list of actions instances. |
| RAISES | DESCRIPTION |
|---|---|
EmptyActionListError
|
If the list of actions is empty and the task does not accept empty lists of actions. |
Source code in conatus/utils/callable_parsing/actions.py
add_additional_and_default_actions
¶
add_additional_and_default_actions(
user_provided_actions: list[Action],
additional_actions: (
list[RawAction] | ActionStarterPack | None
) = None,
*extra_actions: RawAction
) -> list[Action]
Add the additional and default actions to the list of actions.
| PARAMETER | DESCRIPTION |
|---|---|
user_provided_actions
|
The list of actions provided by the user. |
additional_actions
|
The additional actions to add to the list of
actions. We expect this to be the product of the
TYPE:
|
extra_actions
|
The extra actions to add to the list of actions. This is really a last resort, and should be used sparingly.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Action]
|
The list of actions. |
Source code in conatus/utils/callable_parsing/actions.py
conatus.utils.callable_parsing.callable_parsing
¶
Utilities for parsing callables.
parse_callable_for_task
¶
parse_callable_for_task(
fn: Callable[Ps, R],
) -> tuple[
str,
str,
OrderedDict[
str, tuple[TypeOfType, ParamKind, bool, str]
],
OrderedDict[str, tuple[TypeOfType, str]],
]
Parse the callable for the task.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The name of the task. |
str
|
The description of the task. |
dict[str, tuple[TypeOfType, ParamKind, bool, str]]
|
The inputs of the task with their type, kind (positional, keyword, or positional-only), whether they are required, and the description of the parameter. |
dict[str, TypeOfType]
|
The outputs of the task. |
Source code in conatus/utils/callable_parsing/callable_parsing.py
conatus.utils.callable_parsing.definition
¶
Assemble the definition of a task.
TypeCollection
module-attribute
¶
TypeCollection = dict[str, TypeOfType] | list[TypeOfType]
Type alias representing a dictionary or list of type hints.
init_definition_not_decorated
¶
init_definition_not_decorated(
*,
name: str,
description: str,
all_actions: list[Action],
inputs: TypeCollection | TypeOfType | None,
outputs: TypeCollection | TypeOfType | None,
starting_variables: (
list[ParamType] | dict[str, ParamType] | None
),
user_provided_actions_keys: list[str],
max_var_repr_len: int | None,
flatten_output_to_string: bool = False
) -> TaskDefinition
Initialize the task definition if not processed via a decorator.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the task.
TYPE:
|
description
|
The description of the task.
TYPE:
|
all_actions
|
The list of actions to add to the task. |
inputs
|
The inputs of the task.
TYPE:
|
outputs
|
The outputs of the task.
TYPE:
|
starting_variables
|
The starting variables of the task. |
user_provided_actions_keys
|
The keys of the user-provided actions. |
max_var_repr_len
|
The maximum length of the variable representation.
TYPE:
|
flatten_output_to_string
|
Whether to flatten the output to a string.
Will only be respected if
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TaskDefinition
|
The task definition. |
Source code in conatus/utils/callable_parsing/definition.py
init_definition_decorated
¶
init_definition_decorated(
*,
name: str,
description: str,
all_actions: list[Action],
inputs: OrderedDict[
str, tuple[TypeOfType, ParamKind, bool, str]
],
outputs: OrderedDict[str, tuple[TypeOfType, str]],
user_provided_actions_keys: list[str]
) -> TaskDefinition
Initialize the task definition if processed via a decorator.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the task.
TYPE:
|
description
|
The description of the task.
TYPE:
|
all_actions
|
The list of actions to add to the task. |
inputs
|
The inputs of the task.
TYPE:
|
outputs
|
The outputs of the task.
TYPE:
|
user_provided_actions_keys
|
The keys of the user-provided actions. |
| RETURNS | DESCRIPTION |
|---|---|
TaskDefinition
|
The task definition. |
Source code in conatus/utils/callable_parsing/definition.py
conatus.utils.callable_parsing.parse_inputs
¶
Utilities for parsing inputs.
ProcessedInputsOutputs
¶
Bases: TypedDict
The processed inputs and outputs of a task.
Convenience TypedDict to simplify the handling of
inputs and outputs in the constructor of BaseTask
.
inputs_with_positions
instance-attribute
¶
inputs_with_positions: OrderedDict[
str, tuple[TypeOfType, ParamKind, bool, str]
]
The normalized inputs of the task.
The structure of the dictionary is as follows:
Whereinput_type is the type of the input, ParamKind is the kind of
the input (positional, keyword, or positional-only), is_required is
a boolean indicating whether the input is required, and description is
the description of the input.
inputs_are_predefined
instance-attribute
¶
inputs_are_predefined: bool
Whether the inputs are predefined.
inputs_form
instance-attribute
¶
inputs_form: ExpectedInputForm
The form of the inputs of the task (singleton, list, or dictionary).
outputs_with_positions
instance-attribute
¶
outputs_with_positions: OrderedDict[
str, tuple[TypeOfType, str]
]
outputs_form
instance-attribute
¶
outputs_form: ExpectedOutputForm
The form of the outputs of the task (singleton, list, or dictionary).
predefined_inputs
instance-attribute
¶
predefined_inputs: OrderedDict[str, RuntimeVariable] | None
The predefined inputs of the task.
get_inputs_with_positions
¶
get_inputs_with_positions(
inputs: OrderedDict[str, TypeOfType],
inputs_form: ExpectedInputForm,
) -> OrderedDict[
str, tuple[TypeOfType, ParamKind, bool, str]
]
Get the inputs with their positions.
| PARAMETER | DESCRIPTION |
|---|---|
inputs
|
The inputs to the task.
TYPE:
|
inputs_form
|
The form of the inputs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OrderedDict[str, tuple[TypeOfType, ParamKind, bool, str]]
|
The inputs with their positions. |
Source code in conatus/utils/callable_parsing/parse_inputs.py
process_inputs_outputs
¶
process_inputs_outputs(
inputs: (
dict[str, TypeOfType]
| list[TypeOfType]
| TypeOfType
| None
),
outputs: (
dict[str, TypeOfType]
| list[TypeOfType]
| TypeOfType
| None
),
starting_variables: (
list[ParamType] | dict[str, ParamType] | None
),
*,
max_var_repr_len: int
) -> ProcessedInputsOutputs
Process the inputs and outputs of the task.
| PARAMETER | DESCRIPTION |
|---|---|
inputs
|
The inputs to the task.
TYPE:
|
outputs
|
The outputs of the task.
TYPE:
|
starting_variables
|
The starting variables of the task. |
max_var_repr_len
|
The maximum length of the variable representation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ProcessedInputsOutputs
|
The normalized inputs and outputs of the task. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If both |
Source code in conatus/utils/callable_parsing/parse_inputs.py
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 | |
extract_starting_variables
¶
extract_starting_variables(
definition: TaskDefinition,
config: ConsolidatedTaskConfig,
*args: ParamType,
**kwargs: ParamType
) -> OrderedDict[str, RuntimeVariable]
Extract the starting variables from the arguments passed Agent.run.
We perform some checks to make sure that the arguments are correct:
- No inputs: * If the agent expects no inputs, we check that the user did not provide any inputs.
- Singleton inputs: * If the agent expects a single input, we check that the user provided only one input. * If the user provided a keyword argument, we check that the keyword argument is the same as the auto-generated name of the input.
- List inputs: * If the agent expects a list of inputs, we check that the user provided the correct number of inputs. * If the user provided keyword arguments, we check that the keyword arguments are the same as the auto-generated names of the inputs.
- Dictionary inputs: * If the agent expects a dictionary of inputs, we check that the user provided the correct number of inputs. * If the user provided keyword arguments, we check that the keyword arguments are the same as the auto-generated names of the inputs.
No type checking
At this time, we do not perform any type checking on the inputs.
| PARAMETER | DESCRIPTION |
|---|---|
definition
|
The definition of the task.
TYPE:
|
config
|
The configuration of the task.
TYPE:
|
args
|
The arguments.
TYPE:
|
kwargs
|
The keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OrderedDict[str, RuntimeVariable]
|
The starting variables. |
| RAISES | DESCRIPTION |
|---|---|
WrongArgsToAgentRunError
|
If the arguments are not correct. |
Source code in conatus/utils/callable_parsing/parse_inputs.py
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 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 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
extract_singleton_inputs
¶
extract_singleton_inputs(
definition: TaskDefinition,
config: ConsolidatedTaskConfig,
*args: ParamType,
**kwargs: ParamType
) -> OrderedDict[str, RuntimeVariable]
Extract the singleton inputs from the arguments passed Agent.run.
See the documentation of extract_starting_variables
for more information.
| PARAMETER | DESCRIPTION |
|---|---|
definition
|
The definition of the task.
TYPE:
|
config
|
The configuration of the task.
TYPE:
|
args
|
The arguments.
TYPE:
|
kwargs
|
The keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OrderedDict[str, RuntimeVariable]
|
The singleton inputs. |
| RAISES | DESCRIPTION |
|---|---|
WrongArgsToAgentRunError
|
If the arguments are not correct. |
Source code in conatus/utils/callable_parsing/parse_inputs.py
conatus.utils.callable_parsing.parse_outputs
¶
Module for parsing the outputs of a callable.
convert_to_output_form
¶
convert_to_output_form(
definition: TaskDefinition,
results: dict[str, RuntimeVariable],
) -> object
Convert the results to the output form.
This method can be overridden by the user in the task class.
| PARAMETER | DESCRIPTION |
|---|---|
definition
|
The definition of the task.
TYPE:
|
results
|
The results of the task.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
The results of the task in the output form. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the output is not present in the results. |
Source code in conatus/utils/callable_parsing/parse_outputs.py
conatus.utils.callable_parsing.runtime
¶
Assemble the runtime of a task.
generate_state_and_runtime
¶
generate_state_and_runtime(
*,
task: BaseTask[ParamType, ...],
config: None = None,
definition: None = None,
args: list[ParamType] | None = None,
**kwargs: ParamType
) -> tuple[RuntimeState, Runtime]
generate_state_and_runtime(
*,
config: ConsolidatedTaskConfig,
definition: TaskDefinition,
task: None = None,
args: list[ParamType] | None = None,
**kwargs: ParamType
) -> tuple[RuntimeState, Runtime]
generate_state_and_runtime(
*,
task: BaseTask[ParamType, ...] | None = None,
config: ConsolidatedTaskConfig | None = None,
definition: TaskDefinition | None = None,
args: list[ParamType] | None = None,
**kwargs: ParamType
) -> tuple[RuntimeState, Runtime]
Generate the state and runtime for the task.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
The task to run. |
definition
|
The definition of the task.
TYPE:
|
config
|
The configuration for the task.
TYPE:
|
args
|
The arguments to pass to the task. |
**kwargs
|
The keyword arguments to pass to the task.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[RuntimeState, Runtime]
|
The state and runtime for the task. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If task is None and both definition and config are None. |