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)
¶
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
|
Source code in langroid/agent/batch.py
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 |
|
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)
¶
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
(not-None) result. In this case all other tasks are
cancelled, and their corresponding result is None in the
returned list.
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)
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
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 |
|
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)
¶
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
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)
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 |
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
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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|