Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

docs: examples for saving and loading modules after optimizing

Nikolad5bc784ef8a2
diff --git a/docs/docs/building-blocks/6-optimizers.md b/docs/docs/building-blocks/6-optimizers.md
index 6c9c3603a..52061a215 100644
--- a/docs/docs/building-blocks/6-optimizers.md
+++ b/docs/docs/building-blocks/6-optimizers.md
@@ -95,3 +95,24 @@ config = dict(max_bootstrapped_demos=3, max_labeled_demos=3, num_candidate_progr
teleprompter = BootstrapFewShotWithRandomSearch(metric=YOUR_METRIC_HERE, **config)
optimized_program = teleprompter.compile(YOUR_PROGRAM_HERE, trainset=YOUR_TRAINSET_HERE)
```
+
+## Saving and loading optimizer output
+
+After running a program through an optimizer, it's useful to also save it. At a later point, a program can be loaded from a file and used for inference. For this, the `load` and `save` methods can be used.
+
+### Saving a program
+
+```python
+optimized_program.save(YOUR_SAVE_PATH)
+```
+
+The resulting file is in plain-text JSON format. It contains all the parameters and steps in the source program. You can always read it and see what the optimizer generated.
+
+### Loading a program
+
+To load a program from a file, you can instantiate an object from that class and then call the load method on it.
+
+```python
+loaded_program = YOUR_PROGRAM_CLASS()
+loaded_program.load(path=YOUR_SAVE_PATH)
+```
\ No newline at end of file
diff --git a/docs/docs/cheatsheet.md b/docs/docs/cheatsheet.md
index af62bd248..e0116bfcd 100644
--- a/docs/docs/cheatsheet.md
+++ b/docs/docs/cheatsheet.md
@@ -290,6 +290,18 @@ your_dspy_program_compiledx2 = teleprompter.compile(
)
```
+#### Saving/loading a compiled program
+
+```python
+save_path = './v1.json'
+your_dspy_program_compiledx2.save(save_path)
+```
+
+```python
+loaded_program = YourProgramClass()
+loaded_program.load(path=save_path)
+```
+
### dspy.BootstrapFewShotWithRandomSearch
```python