Skip to content

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

str or os.PathLike object. The path where to save the model. Must end in .json.

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.

True
Source code in synalinks/src/programs/program.py
def save(self, filepath, overwrite=True, **kwargs):
    """Saves a program as a `.json` file.

    Example:

    ```python
    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.

    Args:
        filepath (str | os.PathLike): `str` or `os.PathLike` object.
            The path where to save the model. Must end in `.json`.
        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`.
    """
    from synalinks.src.saving import serialization_lib

    filepath = file_utils.path_to_string(filepath)
    if not filepath.endswith(".json"):
        raise ValueError(
            f"The filepath should ends with '.json', received filepath={filepath}"
        )
    program_config = serialization_lib.serialize_synalinks_object(self)
    variables_config = self.get_state_tree()
    program_config.update({"variables": variables_config})
    program_config_string = json.dumps(program_config, indent=2)
    if file_utils.exists(filepath) and not overwrite:
        io_utils.ask_to_proceed_with_overwrite(filepath)
    with open(filepath, "w") as f:
        f.write(program_config_string)

Saving programs into a JSON string

Returns a JSON string containing the network configuration.

json_string = program.to_json()

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 json.dumps().

{}

Returns:

Type Description
str

A JSON string.

Source code in synalinks/src/programs/program.py
def to_json(self, **kwargs):
    """Returns a JSON string containing the network configuration.

    ```python
    json_string = program.to_json()
    ```

    To load a network from a JSON save file, use
    `synalinks.programs.program_from_json(json_string, custom_objects={...})`.

    Args:
        **kwargs (keyword arguments): Additional keyword arguments to be passed to
            `json.dumps()`.

    Returns:
        (str): A JSON string.
    """
    from synalinks.src.saving import serialization_lib

    program_config = serialization_lib.serialize_synalinks_object(self)
    return json.dumps(program_config, **kwargs)

Loading programs from a JSON file

Load a program from a JSON file.

Example:

import synalinks

loaded_program = synalinks.Program.load("program.json")

Parameters:

Name Type Description Default
filepath str | Path

str or pathlib.Path object. Path to load the program's variables from. Must end in .variables.json.

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
@classmethod
def load(cls, filepath, custom_objects=None):
    """Load a program from a JSON file.

    Example:

    ```python
    import synalinks

    loaded_program = synalinks.Program.load("program.json")
    ```

    Args:
        filepath (str | pathlib.Path): `str` or `pathlib.Path` object.
            Path to load the program's variables from.
            Must end in `.variables.json`.
        custom_objects (dict): Optional dictionary mapping names
            (strings) to custom classes or functions to be
            considered during deserialization.

    Returns:
        (Program): A Synalinks program instance (uncompiled).
    """
    filepath = file_utils.path_to_string(filepath)
    if not filepath.endswith(".json"):
        raise ValueError(
            f"The filepath should ends with '.json', received filepath={filepath}"
        )
    with open(filepath, "r") as f:
        json_config = f.read()
    return program_from_json(json_config, custom_objects=custom_objects)

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).

Source code in synalinks/src/programs/program.py
@synalinks_export("synalinks.programs.program_from_json")
def program_from_json(json_string, custom_objects=None):
    """Parses a JSON program configuration string and returns a program instance.

    Example:

    ```python
    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)
    ```

    Args:
        json_string (str): JSON string encoding a program configuration.
        custom_objects (dict): Optional dictionary mapping names
            (strings) to custom classes or functions to be
            considered during deserialization.

    Returns:
        (Program): A Synalinks program instance (uncompiled).
    """
    from synalinks.src.saving import serialization_lib

    program_config = json.loads(json_string)
    variables_config = program_config.get("variables")
    program = serialization_lib.deserialize_synalinks_object(
        program_config, custom_objects=custom_objects
    )
    program.set_state_tree(variables_config)
    return program