Saving & Loading Kani State¶
To save and load a Kani object’s state, use Kani.save() and Kani.load().
These methods will save and load the kani’s chat state to a .kani or .json file. You can use this to
save common prompts or log conversations.
Note
The save and load methods only save the chat state, not the engine or any functions included in the Kani.
The chat state includes all always_included_messages and chat_history only.
- Kani.save(fp: str | bytes | PathLike, *, save_format: Literal['json', 'kani'] | None = None, **kwargs)[source]
Save the chat state of this kani to a
.kanifile or JSON. This will overwrite the file if it exists!- Parameters:
fp – The path to the file to save.
save_format – Whether to save the chat state as a
.kanifile or JSON. If not set, determines format by file path extension (defaulting to.kaniif uncertain).kwargs – Additional arguments to pass to Pydantic’s
model_dump_json.
- Kani.load(fp: str | bytes | PathLike, **kwargs)[source]
Load a chat state from a
.kanifile or JSON file into this instance. This will overwrite any existing chat state!- Parameters:
fp – The path to the file containing the chat state.
kwargs – Additional arguments to pass to Pydantic’s
model_validate_json.
The .kani File Format¶
What is a .kani file?
A .kani file is a self-contained ZIP archive. It contains an index.json file with the saved chat state.
Additional blobs MAY be saved at /blobs/{first 2 characters of hash}/{SHA256}[.suffix].
It is safe to change the file extension of a .kani file to .zip for manual unzipping and inspection.
For example, let’s save the following chat state, with kani-multimodal-core installed:
ai = Kani(engine, chat_history=[
ChatMessage.user([BinaryFilePart.from_file("myfile.pdf"), "What is in this file?"]),
ChatMessage.assistant("..."),
...
ChatMessage.user([ImagePart.from_file("bird.png")])
])
ai.save("mykani.zip")
The contents of the mykani.zip archive might look like:
mykani.zip/
index.json
blobs/
e1/
e1ab6f04[...].pdf
f0/
f01a15c1[...].png