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 = LanguageModel("ollama/mistral")
program = synalinks.Sequential(
[
synalinks.Input(data_model=Query),
synalinks.Generator(
data_model=AnswerWithRationale,
language_model=language_model,
),
],
)
program.save("program.json")
loaded_program = synalinks.programs.program_from_json("program.json")
The saved .json
file contains:
- The program's configuration (architecture)
- The program's variables
- The program's optimizer'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
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 = LanguageModel("ollama/mistral")
program = synalinks.Sequential(
[
synalinks.Input(data_model=Query),
synalinks.Generator(
data_model=AnswerWithRationale,
language_model=language_model,
),
],
)
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). |