Flow
lazyllm.flow.FlowBase
A base class for creating flow-like structures that can contain various items.
This class provides a way to organize items, which can be instances of FlowBase
or other types, into a hierarchical structure. Each item can have a name and the structure can be traversed or modified dynamically.
Parameters:
-
items
(iterable
, default:()
) –An iterable of items to be included in the flow. These can be instances of
FlowBase
or other objects. -
item_names
(list of str
, default:[]
) –A list of names corresponding to the items. This allows items to be accessed by name. If not provided, items can only be accessed by index.
Source code in lazyllm/flow/flow.py
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 |
|
ancestor
property
is_root
property
A property that indicates whether the current flow item is the root of the flow structure.
Returns:
- bool: True if the current item has no parent (
_father
is None), otherwise False.
Examples:
for_each(filter, action)
Performs an action on each item in the flow that matches a given filter.
The method recursively traverses the flow structure, applying the action to each item that passes the filter.
Parameters:
-
filter
(callable
) –A function that takes an item as input and returns True if the item should have the action applied.
-
action
(callable
) –A function that takes an item as input and performs some operation on it.
Returns:
- None
Examples:
>>> import lazyllm
>>> def test1(): print('1')
...
>>> def test2(): print('2')
...
>>> def test3(): print('3')
...
>>> flow = lazyllm.pipeline(test1, lazyllm.pipeline(test2, test3))
>>> flow.for_each(lambda x: callable(x), lambda x: print(x))
<Function type=test1>
<Function type=test2>
<Function type=test3>
Source code in lazyllm/flow/flow.py
lazyllm.flow.Pipeline
Bases: LazyLLMFlowsBase
A sequential execution model that forms a pipeline of processing stages.
The Pipeline
class is a linear sequence of processing stages, where the output of one stage becomes the input to the next. It supports the addition of post-actions that can be performed after the last stage. It is a subclass of LazyLLMFlowsBase
which provides a lazy execution model and allows for functions to be wrapped and registered in a lazy manner.
Parameters:
-
args
(list of callables or single callable
, default:()
) –The processing stages of the pipeline. Each element can be a callable function or an instance of
LazyLLMFlowsBase.FuncWrap
. If a single list or tuple is provided, it is unpacked as the stages of the pipeline. -
post_action
(callable
, default:None
) –An optional action to perform after the last stage of the pipeline. Defaults to None.
-
kwargs
(dict of callables
) –Named processing stages of the pipeline. Each key-value pair represents a named stage, where the key is the name and the value is the callable stage.
Returns:
- The output of the last stage of the pipeline.
Examples:
>>> import lazyllm
>>> ppl = lazyllm.pipeline(
... stage1=lambda x: x+1,
... stage2=lambda x: f'get {x}'
... )
>>> ppl(1)
'get 2'
>>> ppl.stage2
<Function type=lambda>
Source code in lazyllm/flow/flow.py
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 |
|
lazyllm.flow.Parallel
Bases: LazyLLMFlowsBase
A class for managing parallel flows in LazyLLMFlows.
This class inherits from LazyLLMFlowsBase and provides an interface for running operations in parallel or sequentially. It supports concurrent execution using threads and allows for the return of results as a dictionary.
The Parallel
class can be visualized as follows:
# /> module11 -> ... -> module1N -> out1 \
# input -> module21 -> ... -> module2N -> out2 -> (out1, out2, out3)
# \> module31 -> ... -> module3N -> out3 /
The Parallel.sequential
method can be visualized as follows:
Parameters:
-
_scatter
(bool
, default:False
) –If
True
, the input is split across the items. IfFalse
, the same input is passed to all items. Defaults toFalse
. -
_concurrent
(bool
, default:True
) –If
True
, operations will be executed concurrently using threading. IfFalse
, operations will be executed sequentially. Defaults toTrue
. -
args
–Variable length argument list for the base class.
-
kwargs
–Arbitrary keyword arguments for the base class.
asdict property
Tag Parallel
so that the return value of each call to Parallel
is changed from a tuple to a dict. When using asdict
, make sure that the elements of parallel
are named, for example: parallel(name=value)
.
tuple property
Mark Parallel so that the return value of Parallel changes from package to tuple each time it is called.
list property
Mark Parallel so that the return value of Parallel changes from package to list each time it is called.
sum property
Mark Parallel so that the return value of Parallel is accumulated each time it is called.
join(self, string)
Mark Parallel so that the return value of Parallel is joined by string
each time it is called.
Examples:
>>> import lazyllm
>>> test1 = lambda a: a + 1
>>> test2 = lambda a: a * 4
>>> test3 = lambda a: a / 2
>>> ppl = lazyllm.parallel(test1, test2, test3)
>>> ppl(1)
(2, 4, 0.5)
>>> ppl = lazyllm.parallel(a=test1, b=test2, c=test3)
>>> ppl(1)
{2, 4, 0.5}
>>> ppl = lazyllm.parallel(a=test1, b=test2, c=test3).asdict
>>> ppl(2)
{'a': 3, 'b': 8, 'c': 1.0}
>>> ppl = lazyllm.parallel(a=test1, b=test2, c=test3).astuple
>>> ppl(-1)
(0, -4, -0.5)
>>> ppl = lazyllm.parallel(a=test1, b=test2, c=test3).aslist
>>> ppl(0)
[1, 0, 0.0]
>>> ppl = lazyllm.parallel(a=test1, b=test2, c=test3).join('\n')
>>> ppl(1)
'2\n4\n0.5'
Source code in lazyllm/flow/flow.py
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 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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
|
lazyllm.flow.Diverter
Bases: Parallel
A flow diverter that routes inputs through different modules in parallel.
The Diverter class is a specialized form of parallel processing where multiple inputs are each processed by a separate sequence of modules in parallel. The outputs are then aggregated and returned as a tuple.
This class is useful when you have distinct data processing pipelines that can be executed concurrently, and you want to manage them within a single flow construct.
# /> in1 -> module11 -> ... -> module1N -> out1 \
# (in1, in2, in3) -> in2 -> module21 -> ... -> module2N -> out2 -> (out1, out2, out3)
# \> in3 -> module31 -> ... -> module3N -> out3 /
Parameters:
-
args
–Variable length argument list representing the modules to be executed in parallel.
-
_concurrent
(bool
, default:True
) –A flag to control whether the modules should be run concurrently. Defaults to
True
. You can useDiverter.sequential
instead ofDiverter
to set this variable. -
kwargs
–Arbitrary keyword arguments representing additional modules, where the key is the name of the module.
Examples:
>>> import lazyllm
>>> div = lazyllm.diverter(lambda x: x+1, lambda x: x*2, lambda x: -x)
>>> div(1, 2, 3)
(2, 4, -3)
>>> div = lazyllm.diverter(a=lambda x: x+1, b=lambda x: x*2, c=lambda x: -x).asdict
>>> div(1, 2, 3)
{'a': 2, 'b': 4, 'c': -3}
>>> div(dict(c=3, b=2, a=1))
{'a': 2, 'b': 4, 'c': -3}
Source code in lazyllm/flow/flow.py
lazyllm.flow.Warp
Bases: Parallel
A flow warp that applies a single module to multiple inputs in parallel.
The Warp class is designed to apply the same processing module to a set of inputs. It effectively 'warps' the single module around the inputs so that each input is processed in parallel. The outputs are collected and returned as a tuple. It is important to note that this class cannot be used for asynchronous tasks, such as training and deployment.
# /> in1 \ /> out1 \
# (in1, in2, in3) -> in2 -> module1 -> ... -> moduleN -> out2 -> (out1, out2, out3)
# \> in3 / \> out3 /
Parameters:
-
args
–Variable length argument list representing the single module to be applied to all inputs.
-
kwargs
–Arbitrary keyword arguments for future extensions.
Note
- Only one function is allowed in warp.
- The Warp flow should not be used for asynchronous tasks such as training and deployment.
Examples:
>>> import lazyllm
>>> warp = lazyllm.warp(lambda x: x * 2)
>>> warp(1, 2, 3, 4)
(2, 4, 6, 8)
>>> warp = lazyllm.warp(lazyllm.pipeline(lambda x: x * 2, lambda x: f'get {x}'))
>>> warp(1, 2, 3, 4)
('get 2', 'get 4', 'get 6', 'get 8')
Source code in lazyllm/flow/flow.py
lazyllm.flow.IFS
Bases: LazyLLMFlowsBase
Implements an If-Else functionality within the LazyLLMFlows framework.
The IFS (If-Else Flow Structure) class is designed to conditionally execute one of two provided paths (true path or false path) based on the evaluation of a given condition. After the execution of the selected path, an optional post-action can be applied, and the input can be returned alongside the output if specified.
Parameters:
-
cond
(callable
) –A callable that takes the input and returns a boolean. It determines which path to execute. If
cond(input)
evaluates to True,tpath
is executed; otherwise,fpath
is executed. -
tpath
(callable
) –The path to be executed if the condition is True.
-
fpath
(callable
) –The path to be executed if the condition is False.
-
post_action
(callable
, default:None
) –An optional callable that is executed after the selected path. It can be used to perform cleanup or further processing. Defaults to None.
Returns:
- The output of the executed path.
Examples:
>>> import lazyllm
>>> cond = lambda x: x > 0
>>> tpath = lambda x: x * 2
>>> fpath = lambda x: -x
>>> ifs_flow = lazyllm.ifs(cond, tpath, fpath)
>>> ifs_flow(10)
20
>>> ifs_flow(-5)
5
Source code in lazyllm/flow/flow.py
lazyllm.flow.Switch
Bases: LazyLLMFlowsBase
A control flow mechanism that selects and executes a flow based on a condition.
The Switch
class provides a way to choose between different flows depending on the value of an expression or the truthiness of conditions. It is similar to a switch-case statement found in other programming languages.
# switch(exp):
# case cond1: input -> module11 -> ... -> module1N -> out; break
# case cond2: input -> module21 -> ... -> module2N -> out; break
# case cond3: input -> module31 -> ... -> module3N -> out; break
Parameters:
-
args
–A variable length argument list, alternating between conditions and corresponding flows or functions. Conditions are either callables returning a boolean or values to be compared with the input expression.
-
post_action
(callable
, default:None
) –A function to be called on the output after the selected flow is executed. Defaults to
None
. -
judge_on_full_input(bool)
–If set to
True
, the conditional judgment will be performed through the input ofswitch
, otherwise the input will be split into two parts: the judgment condition and the actual input, and only the judgment condition will be judged. -
kwargs
–Arbitrary keyword arguments representing named conditions and corresponding flows or functions.
Raises:
-
TypeError
–If an odd number of arguments are provided, or if the first argument is not a dictionary and the conditions are not provided in pairs.
Examples:
>>> import lazyllm
>>> def is_positive(x): return x > 0
...
>>> def is_negative(x): return x < 0
...
>>> switch = lazyllm.switch(is_positive, lambda x: 2 * x, is_negative, lambda x : -x, 'default', lambda x : '000', judge_on_full_input=True)
>>>
>>> switch(1)
2
>>> switch(0)
'000'
>>> switch(-4)
4
>>>
>>> def is_1(x): return True if x == 1 else False
...
>>> def is_2(x): return True if x == 2 else False
...
>>> def is_3(x): return True if x == 3 else False
...
>>> def t1(x): return 2 * x
...
>>> def t2(x): return 3 * x
...
>>> def t3(x): return x
...
>>> with lazyllm.switch(judge_on_full_input=True) as sw:
... sw.case[is_1::t1]
... sw.case(is_2, t2)
... sw.case[is_3, t3]
...
>>> sw(1)
2
>>> sw(2)
6
>>> sw(3)
3
Source code in lazyllm/flow/flow.py
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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 |
|
lazyllm.flow.Loop
Bases: Pipeline
Initializes a Loop flow structure which repeatedly applies a sequence of functions to an input until a stop condition is met or a specified count of iterations is reached.
The Loop structure allows for the definition of a simple control flow where a series of steps are applied in a loop, with an optional stop condition that can be used to exit the loop based on the output of the steps.
Parameters:
-
*item
(callable or list of callables
, default:()
) –The function(s) or callable object(s) that will be applied in the loop.
-
stop_condition
(callable
, default:None
) –A function that takes the output of the last item in the loop as input and returns a boolean. If it returns
True
, the loop will stop. IfNone
, the loop will continue untilcount
is reached. Defaults toNone
. -
count
(int
, default:maxsize
) –The maximum number of iterations to run the loop for. If
None
, the loop will continue indefinitely or untilstop_condition
returnsTrue
. Defaults toNone
. -
post_action
(callable
, default:None
) –A function to be called with the final output after the loop ends. Defaults to
None
. -
judge_on_full_input(bool)
–If set to
True
, the conditional judgment will be performed through the input ofstop_condition
, otherwise the input will be split into two parts: the judgment condition and the actual input, and only the judgment condition will be judged.
Raises:
-
AssertionError
–If both
stop_condition
andcount
are provided or ifcount
is not an integer when provided.
Examples:
>>> import lazyllm
>>> loop = lazyllm.loop(lambda x: x * 2, stop_condition=lambda x: x > 10, judge_on_full_input=True)
>>> loop(1)
16
>>> loop(3)
12
>>>
>>> with lazyllm.loop(stop_condition=lambda x: x > 10, judge_on_full_input=True) as lp:
... lp.f1 = lambda x: x + 1
... lp.f2 = lambda x: x * 2
...
>>> lp(0)
14