batch
ExceptionHandling
¶
Bases: str, Enum
Enum for exception handling options.
run_batched_tasks(inputs, do_task, batch_size, stop_on_first_result, sequential, handle_exceptions, output_map, message_template, message=None, *, max_concurrency=None)
¶
Common batch processing logic for both agent methods and tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
List[str | ChatDocument]
|
List of inputs to process |
required |
do_task
|
Callable[[str | ChatDocument, int], Coroutine[Any, Any, Any]]
|
Task execution function |
required |
batch_size
|
Optional[int]
|
Size of batches, if None process all at once |
required |
stop_on_first_result
|
bool
|
Whether to stop after first valid result |
required |
sequential
|
bool
|
Whether to process sequentially |
required |
handle_exceptions
|
Union[bool, ExceptionHandling]
|
How to handle exceptions: - RAISE or False: Let exceptions propagate - RETURN_NONE or True: Convert exceptions to None in results - RETURN_EXCEPTION: Include exception objects in results Boolean values are deprecated and will be removed in a future version. |
required |
output_map
|
Callable[[Any], Any]
|
Function to map results |
required |
message_template
|
str
|
Template for status message |
required |
message
|
Optional[str]
|
Optional override for status message |
None
|
max_concurrency
|
int | None
|
Optional positive rolling concurrency limit. Requires batch_size=None, sequential=False and stop_on_first_result=False. Maps successful values on completion and returns input order. None preserves the existing batch behavior. |
None
|
Source code in langroid/agent/batch.py
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 | |
run_batch_task_gen(gen_task, items, input_map=lambda x: str(x), output_map=lambda x: x, stop_on_first_result=False, sequential=True, batch_size=None, turns=-1, message=None, handle_exceptions=ExceptionHandling.RAISE, max_cost=0.0, max_tokens=0, *, max_concurrency=None)
¶
Generate and run copies of a task async/concurrently one per item in items list.
For each item, apply input_map to get the initial message to process.
For each result, apply output_map to get the final result.
Args:
gen_task (Callable[[int], Task]): generates the tasks to run
items (list[T]): list of items to process
input_map (Callable[[T], str|ChatDocument]): function to map item to
initial message to process
output_map (Callable[[ChatDocument|str], U]): function to map result
to final result. If stop_on_first_result is enabled, then
map any invalid output to None. We continue until some non-None
result is obtained.
stop_on_first_result (bool): whether to stop after the first valid
(non-None) result. Successful results are evaluated after
output_map; with ExceptionHandling.RETURN_EXCEPTION the returned
exception object is itself non-None and also stops the batch.
Processing continues past None results; once a non-None result
appears, pending tasks are cancelled and their entries in the
returned list are None.
sequential (bool): whether to run sequentially
(e.g. some APIs such as ooba don't support concurrent requests)
batch_size (Optional[int]): The number of tasks to run at a time,
if None, unbatched
turns (int): number of turns to run, -1 for infinite
message (Optional[str]): optionally overrides the console status messages
handle_exceptions: How to handle exceptions:
- RAISE or False: Let exceptions propagate
- RETURN_NONE or True: Convert exceptions to None in results
- RETURN_EXCEPTION: Include exception objects in results
Boolean values are deprecated and will be removed in a future version.
max_cost: float: maximum cost to run the task (default 0.0 for unlimited)
max_tokens: int: maximum token usage (in and out) (default 0 for unlimited)
max_concurrency: Optional positive rolling concurrency limit. Requires
batch_size=None, sequential=False and stop_on_first_result=False.
Maps successful values on completion and returns input order.
None preserves the existing batch behavior.
Returns:
| Type | Description |
|---|---|
list[Optional[U]]
|
list[Optional[U]]: list of final results. Always list[U] if |
list[Optional[U]]
|
|
Source code in langroid/agent/batch.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |
run_batch_tasks(task, items, input_map=lambda x: str(x), output_map=lambda x: x, stop_on_first_result=False, sequential=True, batch_size=None, turns=-1, max_cost=0.0, max_tokens=0, *, max_concurrency=None)
¶
Run copies of task async/concurrently one per item in items list.
For each item, apply input_map to get the initial message to process.
For each result, apply output_map to get the final result.
Args:
task (Task): task to run
items (list[T]): list of items to process
input_map (Callable[[T], str|ChatDocument]): function to map item to
initial message to process
output_map (Callable[[ChatDocument|str], U]): function to map result
to final result
stop_on_first_result (bool): whether to stop after the first valid
(non-None, after output_map) result. Processing continues past
None-mapped results; exceptions propagate, since this function
always uses the default RAISE exception handling. Once a non-None
result appears, pending tasks are cancelled and their entries in the
returned list are None.
sequential (bool): whether to run sequentially
(e.g. some APIs such as ooba don't support concurrent requests)
batch_size (Optional[int]): The number of tasks to run at a time,
if None, unbatched
turns (int): number of turns to run, -1 for infinite
max_cost: float: maximum cost to run the task (default 0.0 for unlimited)
max_tokens: int: maximum token usage (in and out) (default 0 for unlimited)
max_concurrency: Optional positive rolling concurrency limit. Requires
batch_size=None, sequential=False and stop_on_first_result=False.
Maps successful values on completion and returns input order.
None preserves the existing batch behavior.
Returns:
| Type | Description |
|---|---|
List[Optional[U]]
|
list[Optional[U]]: list of final results. Always list[U] if |
List[Optional[U]]
|
|
Source code in langroid/agent/batch.py
run_batch_agent_method(agent, method, items, input_map=lambda x: str(x), output_map=lambda x: x, sequential=True, stop_on_first_result=False, handle_exceptions=ExceptionHandling.RAISE, batch_size=None)
¶
Run the method on copies of agent, async/concurrently one per
item in items list.
ASSUMPTION: The method is an async method and has signature:
method(self, input: str|ChatDocument|None) -> ChatDocument|None
So this would typically be used for the agent's "responder" methods,
e.g. llm_response_async or agent_responder_async.
For each item, apply input_map to get the initial message to process.
For each result, apply output_map to get the final result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
agent whose method to run |
required |
method
|
str
|
Async method to run on copies of |
required |
input_map
|
Callable[[Any], str | ChatDocument]
|
function to map item to initial message to process |
lambda x: str(x)
|
output_map
|
Callable[[ChatDocument | str], Any]
|
function to map result to final result |
lambda x: x
|
sequential
|
bool
|
whether to run sequentially (e.g. some APIs such as ooba don't support concurrent requests) |
True
|
stop_on_first_result
|
bool
|
whether to stop after the first valid
(non-None) result. Successful results are evaluated after
|
False
|
handle_exceptions
|
Union[bool, ExceptionHandling]
|
How to handle exceptions: - RAISE or False: Let exceptions propagate - RETURN_NONE or True: Convert exceptions to None in results - RETURN_EXCEPTION: Include exception objects in results Boolean values are deprecated and will be removed in a future version. |
RAISE
|
batch_size
|
Optional[int]
|
The number of items to process in each batch. If None, process all items at once. |
None
|
Returns: List[Any]: list of final results
Source code in langroid/agent/batch.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | |
run_batch_function(function, items, sequential=True, batch_size=None)
¶
Apply a synchronous function to each item, optionally concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], U]
|
Blocking, synchronous callable applied to each item. |
required |
items
|
list[T]
|
The items to process. |
required |
sequential
|
bool
|
If True (default), call |
True
|
batch_size
|
Optional[int]
|
If given, process the items in consecutive batches of this size; each batch finishes before the next one starts. Defaults to one batch containing all items. |
None
|
Returns:
| Type | Description |
|---|---|
List[U]
|
The results, in the same order as |
Note
With sequential=False the calls run in worker threads (via
asyncio.to_thread), so function must be thread-safe. The number of
threads is bounded by asyncio's default executor; use batch_size to
bound it further. A function that raises aborts the batch, but
already-running calls cannot be cancelled and will run to completion.