File size: 13,557 Bytes
fe643f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 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 |
import marimo
__generated_with = "0.11.9"
app = marimo.App()
@app.cell(hide_code=True)
def _():
import marimo as mo
import synalinks
synalinks.backend.clear_session()
return mo, synalinks
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# Control Flow
Controlling the flow of information in a program is an essential feature of any LM framework.
In Synalinks, we implemented it in circuit-like fashion, where the flow of information can be
conditionaly or logically restricted to only flow in a subset of a computation graph.
## Parallel Branches
To create parallel branches, all you need to do is using the same inputs when declaring the modules.
Then Synalinks will automatically detect them and run them in parrallel with asyncio.
"""
)
return
@app.cell
async def _(synalinks):
class Query(synalinks.DataModel):
query: str = synalinks.Field(
description="The user query",
)
class AnswerWithThinking(synalinks.DataModel):
thinking: str = synalinks.Field(
description="Your step by step thinking process",
)
answer: str = synalinks.Field(
description="The correct answer",
)
language_model = synalinks.LanguageModel(model="ollama_chat/deepseek-r1")
_x0 = synalinks.Input(data_model=Query)
_x1 = await synalinks.Generator(
data_model=AnswerWithThinking,
language_model=language_model,
)(_x0)
_x2 = await synalinks.Generator(
data_model=AnswerWithThinking,
language_model=language_model,
)(_x0)
program = synalinks.Program(
inputs=_x0,
outputs=[_x1, _x2],
name="parallel_branches",
description="Illustrate the use of parallel branching",
)
return AnswerWithThinking, Query, language_model, program, synalinks
@app.cell
def _(mo, program, synalinks):
synalinks.utils.plot_program(
program,
show_module_names=True,
show_schemas=True,
show_trainable=True,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Decisions
Decisions in Synalinks can be viewed as a single label classification, they allow
the system to classify the inputs based on a question and labels to choose from.
The labels are used to create on the fly a Enum schema that ensure, thanks to
constrained structured output, that the system will answer one of the provided labels.
"""
)
return
@app.cell
async def _(Query, language_model, synalinks):
_x0 = synalinks.Input(data_model=Query)
_x1 = await synalinks.Decision(
question="Evaluate the difficulty to answer the provided query",
labels=["easy", "difficult"],
language_model=language_model,
)(_x0)
program_1 = synalinks.Program(
inputs=_x0,
outputs=_x1,
name="decision_making",
description="Illustrate the decision making process",
)
return (program_1,)
@app.cell
def _(mo, program_1, synalinks):
synalinks.utils.plot_program(
program_1,
show_module_names=True,
show_schemas=True,
show_trainable=True,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Conditional Branches
To make conditional branches, we will need the help of a core module: The Branch
module. This module use a decision and route the input data model to the selected
branch. When a branch is not selected, that branch output a None.
"""
)
return
@app.cell
async def _(AnswerWithThinking, Query, language_model, synalinks):
class Answer(synalinks.DataModel):
answer: str = synalinks.Field(
description="The correct answer",
)
_x0 = synalinks.Input(data_model=Query)
(_x1, _x2) = await synalinks.Branch(
question="Evaluate the difficulty to answer the provided query",
labels=["easy", "difficult"],
branches=[
synalinks.Generator(
data_model=Answer,
language_model=language_model,
),
synalinks.Generator(
data_model=AnswerWithThinking,
language_model=language_model,
),
],
)(_x0)
program_2 = synalinks.Program(
inputs=_x0,
outputs=[_x1, _x2],
name="conditional_branches",
description="Illustrate the conditional branches",
)
return Answer, program_2
@app.cell
def _(mo, program_2, synalinks):
synalinks.utils.plot_program(
program_2,
show_module_names=True,
show_schemas=True,
show_trainable=True,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Data Models Operators
Synalinks implement few operators that works with data models, some of them are
straightforward, like the concatenation, implemented in the Python `+` operator.
But others like the `logical_and` and `logical_or` implemented respectively
in the `&` and `|` operator are more difficult to grasp at first. As explained
above, in the conditional branches, the branch not selected will have a None
as output. To account that fact and to implement logical flows, we need operators
that can work with them. See the [Ops API](https://synalinks.github.io/synalinks/Synalinks%20API/Ops%20API/)
section for an extensive list of all data model operations.
### Concatenation
The concatenation, consist in creating a data model that have the fields of both
inputs. When one of the input is `None`, it raise an exception. Note that you can
use the concatenation, like any other operator, at a meta-class level, meaning
you can actually concatenate data model types.
### Concatenation Table
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
| `x1` | `x2` | Concat (`+`) |
| ------ | ------ | ----------------- |
| `x1` | `x2` | `x1 + x2` |
| `x1` | `None` | `Exception` |
| `None` | `x2` | `Exception` |
| `None` | `None` | `Exception` |
"""
).center()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Concatenation Example
"""
)
return
@app.cell
async def _(AnswerWithThinking, Query, language_model, synalinks):
_x0 = synalinks.Input(data_model=Query)
_x1 = await synalinks.Generator(
data_model=AnswerWithThinking,
language_model=language_model,
)(_x0)
_x2 = await synalinks.Generator(
data_model=AnswerWithThinking,
language_model=language_model,
)(_x0)
_x3 = _x1 + _x2
program_3 = synalinks.Program(
inputs=_x0,
outputs=_x3,
name="concatenation",
description="Illustrate the use of concatenate",
)
return (program_3,)
@app.cell
def _(mo, program_3, synalinks):
synalinks.utils.plot_program(
program_3,
show_module_names=True,
show_schemas=True,
show_trainable=True,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Logical And
The `logical_and` is a concatenation that instead of raising an `Exception`,
output a `None`. This operator should be used, when you have to concatenate
a data model with an another one that can be `None`, like a `Branch` output.
### Logical And Table
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
| `x1` | `x2` | Logical And (`&`) |
| ------ | ------ | ----------------- |
| `x1` | `x2` | `x1 + x2` |
| `x1` | `None` | `None` |
| `None` | `x2` | `None` |
| `None` | `None` | `None` |
"""
).center()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Logical And Example
"""
)
return
@app.cell
async def _(Answer, AnswerWithThinking, Query, language_model, synalinks):
class Critique(synalinks.DataModel):
critique: str = synalinks.Field(
description="The critique of the answer",
)
_x0 = synalinks.Input(data_model=Query)
(_x1, _x2) = await synalinks.Branch(
question="Evaluate the difficulty to answer the provided query",
labels=["easy", "difficult"],
branches=[
synalinks.Generator(
data_model=Answer,
language_model=language_model,
),
synalinks.Generator(
data_model=AnswerWithThinking,
language_model=language_model,
),
],
return_decision=False,
)(_x0)
_x3 = _x0 & _x1
_x4 = _x0 & _x2
_x5 = await synalinks.Generator(
data_model=Critique,
language_model=language_model,
return_inputs=True,
)(_x3)
_x6 = await synalinks.Generator(
data_model=Critique,
language_model=language_model,
return_inputs=True,
)(_x4)
_x7 = _x5 | _x6
_x8 = await synalinks.Generator(
data_model=Answer,
language_model=language_model,
)(_x7)
program_4 = synalinks.Program(
inputs=_x0,
outputs=_x8,
name="logical_and",
description="Illustrate the use of logical and",
)
return Critique, program_4
@app.cell
def _(mo, program_4, synalinks):
synalinks.utils.plot_program(
program_4,
show_module_names=True,
show_schemas=True,
show_trainable=True,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Logical Or
The `logical_or` is used when you want to combine two data models, but you can
accomodate that one of them is `None`. Another use, is to gather the outputs of
a `Branch`, as only one branch is active, it allows you merge the branches outputs
into a unique data model.
### Logical Or Table
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
| `x1` | `x2` | Logical Or (`|`) |
| ------ | ------ | ---------------- |
| `x1` | `x2` | `x1 + x2` |
| `x1` | `None` | `x1` |
| `None` | `x2` | `x2` |
| `None` | `None` | `None` |
"""
).center()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Logical Or Example
"""
)
return
@app.cell
async def _(Answer, AnswerWithThinking, Query, language_model, synalinks):
_x0 = synalinks.Input(data_model=Query)
(_x1, _x2) = await synalinks.Branch(
question="Evaluate the difficulty to answer the provided query",
labels=["easy", "difficult"],
branches=[
synalinks.Generator(
data_model=Answer,
language_model=language_model,
),
synalinks.Generator(
data_model=AnswerWithThinking, language_model=language_model
),
],
return_decision=False,
)(_x0)
_x3 = _x1 | _x2
program_5 = synalinks.Program(
inputs=_x0,
outputs=_x3,
name="logical_or",
description="Illustrate the use of logical or",
)
return (program_5,)
@app.cell
def _(mo, program_5, synalinks):
synalinks.utils.plot_program(
program_5,
show_module_names=True,
show_schemas=True,
show_trainable=True,
)
return
@app.cell(hide_code=True)
async def _(mo):
mo.md(
r"""
## Conclusion
In this notebook, we explored the fundamental concepts of controlling information
flow within Synalinks programs. We intoduced the creation of parallel branches,
decision-making processes, and conditional branching, all of which are essential
for building dynamic and robust applications.
### Key Takeaways
- **Parallel Branches**: We demonstrated how to run modules in parallel using
the same inputs, leveraging asyncio for concurrent execution.
This approach enhances performance and allows for simultaneous processing of tasks.
- **Decision-Making**: We introduced decision-making as a form of single-label
classification, enabling the system to classify inputs based on predefined
questions and labels. This ensures that the system's responses are structured
and adhere to the specified schemas.
- **Conditional Branching**: We explored the use of the Branch module to route
input data models based on decisions, allowing for conditional execution of
branches. This feature is essential for creating adaptive and context-aware
applications.
- **Data Model Operators**: We discussed various data model operators, such as
concatenation, logical AND, and logical OR. These operators enable
sophisticated data manipulation and flow control, ensuring robust program
execution even when branches output None.
"""
)
return
if __name__ == "__main__":
app.run()
|