Task agents reference
Task agents
encord_agents.tasks.dependencies
Twin
dataclass
dep_client
Dependency to provide an authenticated user client.
Example:
from encord.user_client import EncordUserClient
from encord_agents.tasks.depencencies import dep_client
...
@runner.stage("<my_stage_name>")
def my_agent(
client: Annotated[EncordUserClient, Depends(dep_client)]
) -> str:
# Client will authenticated and ready to use.
client.get_dataset("")
Source code in encord_agents/tasks/dependencies.py
dep_data_lookup
Get a lookup to easily retrieve data rows and storage items associated with the given task.
Info
If you're just looking to get the associated storage item to a task, consider using dep_storage_item
instead.
The lookup can, e.g., be useful for
- Updating client metadata
- Downloading data from signed urls
- Matching data to other projects
Example:
from encord.orm.dataset import DataRow
from encord.stotage import StorageItem
from encord.workflow.stages.agent import AgentTask
@runner.stage(stage="Agent 1")
def my_agent(
task: AgentTask,
lookup: Annotated[DataLookup, Depends(dep_data_lookup)]
) -> str:
# Data row from the underlying dataset
data_row: DataRow = lookup.get_data_row(task.data_hash)
# Storage item from Encord Index
storage_item: StorageItem = lookup.get_storage_item(task.data_hash)
# Current metadata
client_metadata = storage_item.client_metadata
# Update metadata
storage_item.update(
client_metadata={
"new": "entry",
**(client_metadata or {})
}
) # metadata. Make sure not to update in place!
...
Parameters:
-
lookup
(Annotated[DataLookup, Depends(sharable)]
) –The object that you can use to lookup data rows and storage items. Automatically injected.
Returns:
-
DataLookup
–The (shared) lookup object.
Source code in encord_agents/tasks/dependencies.py
dep_single_frame
Dependency to inject the first frame of the underlying asset.
The downloaded asset will be named lr.data_hash.{suffix}
.
When the function has finished, the downloaded file will be removed from the file system.
Example:
from encord_agents import FrameData
from encord_agents.tasks.depencencies import dep_single_frame
...
@runner.stage("<my_stage_name>")
def my_agent(
lr: LabelRowV2, # <- Automatically injected
frame: Annotated[NDArray[np.uint8], Depends(dep_single_frame)]
) -> str:
assert frame.ndim == 3, "Will work"
Parameters:
-
lr
(LabelRowV2
) –The label row. Automatically injected (see example above).
Returns:
-
NDArray[uint8]
–Numpy array of shape [h, w, 3] RGB colors.
Source code in encord_agents/tasks/dependencies.py
dep_storage_item
dep_storage_item(lookup: Annotated[DataLookup, Depends(dep_data_lookup)], task: AgentTask) -> StorageItem
Get the storage item associated with the underlying agent task.
The StorageItem
is useful for multiple things like
- Updating client metadata
- Reading file properties like storage location, fps, duration, DICOM tags, etc.
Example
from encord.storage import StorageItem
from encord_agents.tasks.dependencies import dep_storage_item
@runner.stage(stage="<my_stage_name>")
def my_agent(storage_item: Annotated[StorageItem, Depends(dep_storage_item)]) -> str:
print(storage_item.name)
print(storage_item.client_metadata)
...
Source code in encord_agents/tasks/dependencies.py
dep_twin_label_row
dep_twin_label_row(twin_project_hash: str, init_labels: bool = True, include_task: bool = False) -> Callable[[LabelRowV2], Twin | None]
Dependency to link assets between two Projects. When your Runner
in running on
<project_hash_a>
, you can use this to get a Twin
of labels and the underlying
task in the "twin project" with <project_hash_b>
.
This is useful in situations like:
- When you want to transfer labels from a source project" to a sink project.
- If you want to compare labels to labels from other projects upon label submission.
- If you want to extend an existing project with labels from another project on the same underlying data.
Example:
from encord.workflow.common import WorkflowTask
from encord.objects.ontology_labels_impl import LabelRowV2
from encord_agents.tasks.dependencies import Twin, dep_twin_label_row
...
runner = Runner(project_hash="<project_hash_a>")
@runner.stage("<my_stage_name_in_project_a>")
def my_agent(
project_a_label_row: LabelRowV2,
twin: Annotated[
Twin, Depends(dep_twin_label_row(twin_project_hash="<project_hash_b>"))
],
) -> str | None:
label_row_from_project_b: LabelRowV2 = twin.label_row
task_from_project_b: WorkflowTask = instance.get_answer(attribute=checklist_attribute)
Parameters:
-
twin_project_hash
(str
) –The project has of the twin project (attached to the same datasets) from which you want to load the additional data.
-
init_labels
(bool
, default:True
) –If true, the label row will be initialized before calling the agent.
-
include_task
(bool
, default:False
) –If true, the
task
field of theTwin
will be populated. If population failes, e.g., for non-workflow projects, the task will also be None.
Returns:
-
Callable[[LabelRowV2], Twin | None]
–The twin.
Source code in encord_agents/tasks/dependencies.py
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 |
|
dep_video_iterator
Dependency to inject a video frame iterator for doing things over many frames.
Intended use
from encord_agents import FrameData
from encord_agents.tasks.depencencies import dep_video_iterator
...
@runner.stage("<my_stage_name>")
def my_agent(
lr: LabelRowV2, # <- Automatically injected
video_frames: Annotated[Iterator[Frame], Depends(dep_video_iterator)]
) -> str:
for frame in video_frames:
print(frame.frame, frame.content.shape)
Parameters:
-
lr
(LabelRowV2
) –Automatically injected label row dependency.
Raises:
-
NotImplementedError
–Will fail for other data types than video.
Yields:
-
Iterator[Frame]
–An iterator.
Source code in encord_agents/tasks/dependencies.py
encord_agents.tasks.runner
Runner
Runs agents against Workflow projects.
When called, it will iteratively run agent stages till they are empty.
By default, runner will exit after finishing the tasks identified at the point of trigger.
To automatically re-run, you can use the refresh_every
keyword.
Example:
from uuid import UUID
from encord_agents.tasks import Runner
runner = Runner()
@runner.stage("<workflow_node_name>")
# or
@runner.stage("<workflow_node_uuid>")
def my_agent(task: AgentTask) -> str | UUID | None:
...
return "pathway name" # or pathway uuid
runner(project_hash="<project_hash>") # (see __call__ for more arguments)
# or
if __name__ == "__main__":
# for CLI usage: `python example_agent.py --project-hash "<project_hash>"`
runner.run()
Source code in encord_agents/tasks/runner.py
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 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 |
|
__call__
__call__(refresh_every: int | None = None, num_retries: int = 3, task_batch_size: int = 300, project_hash: Optional[str] = None)
Run your task agent.
The runner can continuously keep looking for new tasks in the project and execute the agent.
Parameters:
-
refresh_every
(int | None
, default:None
) –Fetch task statuses from the Encord projecet every
refresh_every
seconds. IfNone
, they the runner will exit once task queue is empty. -
num_retries
(int
, default:3
) –If an agent fails on a task, how many times should the runner retry it?
-
task_batch_size
(int
, default:300
) –Number of tasks for which labels are loaded into memory at once.
-
project_hash
(Optional[str]
, default:None
) –the project hash if not defined at runner instantiation.
Returns: None
Source code in encord_agents/tasks/runner.py
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 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 |
|
run
Execute the runner.
This function is intended to be called from the "main file". It is an entry point to be able to run the agent(s) via your shell with command line arguments.
Example:
runner = Runner(project_hash="<your_project_hash>")
@runner.stage(stage="...")
def your_func() -> str:
...
if __name__ == "__main__":
runner.run()
You can then run execute the runner with:
to see the options is has (it's those from Runner.__call__
).
Source code in encord_agents/tasks/runner.py
stage
Decorator to associate a function with an agent stage.
A function decorated with a stage is added to the list of stages that will be handled by the runner. The runner will call the function for every task which is in that stage.
Example:
runner = Runner()
@runner.stage("<stage_name_or_uuid>")
def my_func() -> str | None:
...
return "<pathway_name or pathway_uuid>"
The function declaration can be any function that takes parameters that are type annotated with the following types:
- Project: the
encord.project.Project
that the runner is operating on. - LabelRowV2: the
encord.objects.LabelRowV2
that the task is associated with. - AgentTask: the
encord.workflow.stages.agent.AgentTask
that the task is associated with. - Any other type: which is annotated with a dependency
All those parameters will be automatically injected when the agent is called.
Example:
from typing import Iterator
from typing_extensions import Annotated
from encord.project import Project
from encord_agents.tasks import Depends
from encord_agents.tasks.dependencies import dep_video_iterator
from encord.workflow.stages.agent import AgentTask
runner = Runner()
def random_value() -> float:
import random
return random.random()
@runner.stage("<stage_name_or_uuid>")
def my_func(
project: Project,
lr: LabelRowV2,
task: AgentTask,
video_frames: Annotated[Iterator[Frame], Depends(dep_video_iterator)],
custom: Annotated[float, Depends(random_value)]
) -> str | None:
...
return "<pathway_name or pathway_uuid>"
Parameters:
-
stage
(str | UUID
) –The name or uuid of the stage that the function should be associated with.
Returns:
-
Callable[[DecoratedCallable], DecoratedCallable]
–The decorated function.
Source code in encord_agents/tasks/runner.py
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 |
|