Tasks¶
conatus.tasks.decorator.task
¶
task(
fn: Callable[Ps, R] | None = None,
*,
using: type[BaseTask[ParamType, ...]] = Task,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
agent_cls: (
type[BaseAgent[ParamType, ...]] | None
) = None,
config: TaskConfig | None = None
) -> (
BaseTask[R, Ps]
| Callable[[Callable[Ps, R]], BaseTask[R, Ps]]
)
Create a Task from a function.
| PARAMETER | DESCRIPTION |
|---|---|
fn
|
The function to wrap in a task. |
actions
|
The actions to add to the task.
TYPE:
|
using
|
If you want to mention a specific class of |
agent_cls
|
The class of the agent to use for the task. |
config
|
The configuration for the task.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseTask[R, Ps] | Callable[[Callable[Ps, R]], BaseTask[R, Ps]]
|
Source code in conatus/tasks/decorator.py
Default task¶
Task
¶
Task(
func_or_description: Callable[Ps, R] | None = None,
/,
*,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None,
agent_cls: type[BaseAgent[R, Ps]] | None = None,
starting_variables: None = None,
mock_responses: None = None,
description: None = None,
name: None = None,
inputs: None = None,
outputs: None = None,
)
Task(
func_or_description: str,
/,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
*,
name: str,
description: None = None,
inputs: TypeCollection | TypeOfType | None = None,
outputs: TypeCollection | type[R] | None = None,
starting_variables: (
list[ParamType] | dict[str, ParamType] | None
) = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None,
agent_cls: type[BaseAgent[R, Ps]] | None = None,
mock_responses: list[AIResponse] | None = None,
)
Task(
func_or_description: None = None,
/,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
*,
name: str,
description: str,
inputs: TypeCollection | TypeOfType | None = None,
outputs: (
TypeCollection | TypeOfType | type[R] | None
) = None,
starting_variables: (
list[ParamType] | dict[str, ParamType] | None
) = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None,
agent_cls: type[BaseAgent[R, Ps]] | None = None,
mock_responses: list[AIResponse] | None = None,
)
Task(
func_or_description: (
str | Callable[Ps, R] | None
) = None,
/,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
*,
description: str | None = None,
name: str | None = None,
inputs: TypeCollection | TypeOfType | None = None,
outputs: (
TypeCollection | TypeOfType | type[R] | None
) = None,
starting_variables: (
list[ParamType] | dict[str, ParamType] | None
) = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None,
agent_cls: type[BaseAgent[R, Ps]] | None = None,
mock_responses: list[AIResponse] | None = None,
)
Default Task implementation.
Using generic parameters¶
If you want to benefit from the generic parameters, you just need to
specific a specific type in the outputs parameter.
from conatus import Task
write_generics_poem = Task(
description="Write me a poem about generics",
name="write_generics_poem",
outputs=str
)
# Type-checkers will infer 'result' to be of type 'str'
# result = write_generics_poem()
| PARAMETER | DESCRIPTION |
|---|---|
func_or_description
|
The function to use as the task, or the
description of the task. |
actions
|
The actions that are available to the task.
TYPE:
|
description
|
The description of the task.
TYPE:
|
name
|
The name of the task. Even though it is optional in type, it is required in practice and we will throw an error if you don't provide it.
TYPE:
|
inputs
|
The inputs to the task. Can be a list of type hints, a
dictionary of type hints, a single type hint,
or None. If you don't provide any inputs, or pass None,
you will not be able to pass any variables to the task,
or the recipe that you will generate from it.
If you want to specify specific variables instead of type
hints, you can use the
TYPE:
|
outputs
|
The expected output of the task. It should either be a
dictionary of format {output_name: output_type}, a list of
output types, a single output type, or None. If None,
it will be up to the LLM to determine the expected output.
If you want the task to return
TYPE:
|
starting_variables
|
The variables to start with. Can be a list of
variables, or a dictionary of variables, or None. This
parameter is mutually exclusive with the
TYPE:
|
config
|
The task configuration, as a dictionary. Many other
values can also be set in the keyword arguments, and will
override the values set in the
TYPE:
|
model_config
|
The model configuration, as a dictionary. This will override any model configuration set in the classes that will be used to generate the task.
TYPE:
|
agent_cls
|
The class of the agent to use for the task. |
mock_responses
|
The mock responses to use for the task. Useful for testing.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the |
ValueError
|
If the |
ValueError
|
If the |
Source code in conatus/tasks/base.py
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
definition
instance-attribute
¶
definition: TaskDefinition
The task definition.
It contains information about the task, such as the user prompt,
the actions that can be used to perform the task, and the expected
inputs and outputs of the task. It should be set by the constructor
of the BaseTask subclass.
TaskDefinition can be
subclassed to add additional information to the task; the downside, of
course, is that lower classes such as BaseAgent
will have to be updated to support the new
information.
config
instance-attribute
¶
config: ConsolidatedTaskConfig = ConsolidatedTaskConfig(
user_provided_config
)
The task configuration.
The user can provide a TaskConfig
dictionary when instantiating a Task.
The config attribute is a
ConsolidatedTaskConfig
instance, which contains the user-provided values, as well as the default
values.
For more information, see ConsolidatedTaskConfig
.
accepts_empty_actions_parameter
class-attribute
instance-attribute
¶
accepts_empty_actions_parameter: bool = True
Whether the task accepts an empty list of actions.
Set this to True if you're already providing some pre-defined
actions in the constructor.
running
class-attribute
instance-attribute
¶
running: bool = False
Whether the task is currently running.
actions
property
writable
¶
The actions to perform the task.
Note that you cannot modify this attribute while the task is running.
inputs
property
writable
¶
inputs: OrderedDict[str, TypeOfType]
The inputs of the task.
Note that you cannot modify this attribute.
inputs_form
property
writable
¶
inputs_form: ExpectedInputForm | None
The form of the inputs of the task.
Note that you cannot modify this attribute.
outputs
property
writable
¶
outputs: OrderedDict[str, TypeOfType]
The outputs of the task.
Note that you cannot modify this attribute while the task is running.
outputs_form
property
writable
¶
outputs_form: ExpectedOutputForm | None
The form of the outputs of the task.
Note that you cannot modify this attribute.
starting_variables
property
¶
starting_variables: dict[str, RuntimeVariable] | None
The starting variables of the task.
If the inputs are not predefined, this is set to None.
Note that you cannot modify this attribute.
__call__
¶
__call__(
func: Callable[Ps2, R2] | None = None,
*args: args,
**kwargs: kwargs
) -> Task[R2, Ps2] | R
Run the task.
This is a convenience method to run the task.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The function to use as the task. |
*args
|
The arguments to pass to the task.
TYPE:
|
**kwargs
|
The keyword arguments to pass to the task.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Task[R2, Ps2] | R
|
The result of the task. |
Source code in conatus/tasks/default.py
run
¶
run(*args: args, **kwargs: kwargs) -> R
Run the task.
Just treat this is a normal function call.
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
The arguments to pass to the task.
TYPE:
|
**kwargs
|
The keyword arguments to pass to the task.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
R
|
The result of the task. |
Source code in conatus/tasks/base.py
additional_actions
¶
additional_actions() -> list[RawAction] | ActionStarterPack
The additional actions to add to the task.
This method can be overridden by the user in the task class.
| RETURNS | DESCRIPTION |
|---|---|
list[RawAction] | ActionStarterPack
|
The additional actions to add to the task. |
Source code in conatus/tasks/base.py
prepare_state_and_runtime
¶
prepare_state_and_runtime(
state: RuntimeState, runtime: Runtime
) -> tuple[RuntimeState, Runtime]
Prepare the state and runtime for the task.
This method can be overridden by the user in the task class.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
The state of the agent.
TYPE:
|
runtime
|
The runtime of the agent.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[RuntimeState, Runtime]
|
The state and runtime of the agent. |
Source code in conatus/tasks/base.py
postprocess_run_data
¶
Postprocess the run data.
This method can be overridden by the user in the task class.
| PARAMETER | DESCRIPTION |
|---|---|
run_data
|
The run data to postprocess.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CompleteRunData
|
The postprocessed run data. |
Source code in conatus/tasks/base.py
write_recipe
¶
write_recipe(
run_data: CompleteRunData, run_writer: FileWriter
) -> None
Write the recipe to the run writer.
This method can be overridden by the user in the task class.
| PARAMETER | DESCRIPTION |
|---|---|
run_data
|
The run data to write the recipe to.
TYPE:
|
run_writer
|
The run writer to write the recipe to.
TYPE:
|
Source code in conatus/tasks/base.py
from_function
classmethod
¶
from_function(
*,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
agent_cls: type[BaseAgent[R2, Ps2]] | None = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None
) -> Callable[[Callable[Ps2, R2]], BaseTask[R2, Ps2]]
from_function(
fn: Callable[Ps2, R2],
/,
*,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
agent_cls: type[BaseAgent[R2, Ps2]] | None = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None,
) -> BaseTask[R2, Ps2]
from_function(
fn: Callable[Ps2, R2] | None = None,
/,
*,
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
agent_cls: type[BaseAgent[R2, Ps2]] | None = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None,
) -> (
BaseTask[R2, Ps2]
| Callable[[Callable[Ps2, R2]], BaseTask[R2, Ps2]]
)
Create a Task from a function.
| PARAMETER | DESCRIPTION |
|---|---|
fn
|
The function to wrap in a task. |
actions
|
The actions to add to the task.
TYPE:
|
agent_cls
|
The class of the agent to use for the task. |
config
|
The configuration for the task.
TYPE:
|
model_config
|
The model configuration for the task.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseTask[R2, Ps2] | Callable[[Callable[Ps2, R2]], BaseTask[R2, Ps2]]
|
Source code in conatus/tasks/base.py
Configuration¶
conatus.tasks.config.TaskConfig
¶
Bases: TypedDict
The configuration for a task, as passed by the user.
This is a dictionary that can be passed to the Task
constructor:
from conatus import Task
task = Task(
description="Actually, I don't want to do a task today.",
name="no_task_for_you",
config={
"preferred_model": "openai:gpt-o1",
}
)
Internally, this dictionary is consolidated with the default values,
and the result is stored in the ConsolidatedTaskConfig
class. If you want to write
your own Task subclass, it is with that
class that you will be dealing instead.
max_var_repr_len
instance-attribute
¶
max_var_repr_len: NotRequired[int | None]
The maximum length of the string representation of a variable.
This is used to limit the length of the string representation of a variable in the agent's output. Think of this as a way to prevent your AI bill from exploding. This is particularly useful when you're dealing with variables with verbose string representations, like lists or dictionaries.
Possible values are:
None: No limit (dangerous!).0: None, the variable will be represented as....> 0: Length of the string representation of the variable.
If not passed, defaults to ConsolidatedTaskConfig.max_var_repr_len
, which
is 300.
max_result_repr_len
instance-attribute
¶
max_result_repr_len: NotRequired[int | None]
The maximum length of the string representation of a result.
It differs from TaskConfig.max_var_repr_len
in a subtle but
important way:
max_var_repr_lenis all about ensuring that the quick repr of each variable in the state does not amount to a very lengthy text. In other words, it is designed for repetitive use in the conversation.
max_result_repr_lenfocuses on communicating the result of a tool call. In general, such tool responses will only focus on one or two results at a time, allowing it to be much longer than the quick repr of a variable.
preferred_provider
instance-attribute
¶
preferred_provider: NotRequired[ProviderName | str]
The preferred AI provider to use, if any.
You do NOT have to use this attribute
Task and preloaded tasks never require
this attribute, but figure it out on their own. This is purely meant
for the user to specify which provider they want to use.
Possible values are:
"openai": OpenAI.
In general, BaseTask and BaseAgent
implementations should strive to honor
this attribute when deciding which provider to use, but it is not required.
If not passed, defaults to ConsolidatedTaskConfig.preferred_provider
, which
is "openai".
preferred_model
instance-attribute
¶
preferred_model: NotRequired[BaseAIModel | ModelName | str]
The default model to use, if any.
You do NOT have to use this attribute
Task and preloaded tasks never require
this attribute, but figure it out on their own.
This attribute is offered to the user so that they have the option to specify specifically which model they want to use. This is useful when, for instance, a new model is released and the user wants to use it.
In general, BaseTask and BaseAgent
implementations should strive to honor
this attribute when deciding which model to use, but it is not required.
The ideal format is a string that can be parsed by the ModelName
type, which is something like
"openai:gpt-4o".
The user can also pass an instance of a BaseAIModel
if they want to instantiate a model
with specific parameters.
If not passed, defaults to ConsolidatedTaskConfig.preferred_model
, which
is "openai:gpt-4o".
stream
instance-attribute
¶
stream: NotRequired[bool]
Whether to stream the response or not.
Note that this should be honored by the AI model, but it is not guaranteed.
max_turns
instance-attribute
¶
max_turns: NotRequired[int]
The maximum number of turns to run.
This is used to limit the number of turns that the agent can take. If the agent takes more turns than this, it will be stopped.
If not passed, defaults to ConsolidatedTaskConfig.max_turns
, which is 10.
Type-checker friendly task¶
conatus.tasks.default.StrictlyTypedTask
¶
StrictlyTypedTask(
description: str,
name: str = "my_task",
actions: (
Sequence[RawAction] | ActionStarterPack | None
) = None,
*,
inputs: TypeCollection | TypeOfType | None = None,
outputs: TypeCollection | type[R] | None = None,
starting_variables: (
list[ParamType] | dict[str, ParamType] | None
) = None,
config: TaskConfig | None = None,
model_config: ModelConfig | SimpleDict | None = None,
agent_cls: type[BaseAgent[R, Ps]] | None = None,
mock_responses: list[AIResponse] | None = None
)
Bases: StrictlyTypedBaseTask[R, Ps]
A strictly typed Task implementation.