Program saving and loading
Saving programs into a JSON file
Saves a program as a .json file.
Example:
import synalinks
class Query(synalinks.DataModel):
query: str
class AnswerWithRationale(synalinks.DataModel):
rationale: str
answer: str
language_model = synalinks.LanguageModel("ollama/mistral")
program = synalinks.Sequential(
[
synalinks.Input(data_model=Query),
synalinks.Generator(
data_model=AnswerWithRationale,
language_model=language_model,
),
],
name="answer_with_rationale",
description="Answer a query with a rationale.",
)
program.save("program.json")
loaded_program = synalinks.Program.load("program.json")
The saved .json file contains:
- The Synalinks version used to save it
- The program's configuration (architecture)
- The program's variables
- The program's optimizer's state (if any)
- The program's reward's state (if any)
Thus programs can be reinstantiated in the exact same state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | PathLike
|
|
required |
overwrite
|
bool
|
Whether we should overwrite any existing program at
the target location, or instead ask the user via
an interactive prompt. Default to |
True
|
Source code in synalinks/src/programs/program.py
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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | |
Saving programs into a JSON string
Returns a JSON string containing the network configuration.
To load a network from a JSON save file, use
synalinks.programs.program_from_json(json_string, custom_objects={...}).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
keyword arguments
|
Additional keyword arguments to be passed to
|
{}
|
Returns:
| Type | Description |
|---|---|
str
|
A JSON string. |
Source code in synalinks/src/programs/program.py
Loading programs from a JSON file
Load a program from a JSON file.
Example:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
|
required |
custom_objects
|
dict
|
Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization. |
None
|
Returns:
| Type | Description |
|---|---|
Program
|
A Synalinks program instance (uncompiled). |
Source code in synalinks/src/programs/program.py
Loading a program from a JSON string
Parses a JSON program configuration string and returns a program instance.
Example:
import synalinks
class Query(synalinks.DataModel):
query: str
class AnswerWithRationale(synalinks.DataModel):
rationale: str
answer: str
language_model = synalinks.LanguageModel("ollama/mistral")
program = synalinks.Sequential(
[
synalinks.Input(data_model=Query),
synalinks.Generator(
data_model=AnswerWithRationale,
language_model=language_model,
),
],
name="answer_with_rationale",
description="Answer a query with a rationale.",
)
config = program.to_json()
loaded_program = synalinks.programs.program_from_json(config)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_string
|
str
|
JSON string encoding a program configuration. |
required |
custom_objects
|
dict
|
Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization. |
None
|
Returns:
| Type | Description |
|---|---|
Program
|
A Synalinks program instance (uncompiled). |