afnio.serialization
afnio.serialization.save(obj, f, pickle_protocol=DEFAULT_PROTOCOL)
Saves an object to a disk file using zip compression and pickle serialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
The object to be saved. |
required |
f
|
str | PathLike | BinaryIO | IO[bytes]
|
A file-like object (must
implement write/flush) or a string or |
required |
pickle_protocol
|
int
|
Pickle protocol version. |
DEFAULT_PROTOCOL
|
Note
A common Afnio convention is to save variables using .hf file extension.
The .hf extension is a naming convention inspired by the chemical symbol
for Hafnium (Hf). "Afnio" is the
Italian word for Hafnium.
Examples:
>>> # Save to file
>>> x = afnio.Variable(data="You are a doctor.", role="system prompt")
>>> afnio.save(x, 'variable.hf')
>>> # Save to io.BytesIO buffer
>>> buffer = io.BytesIO()
>>> afnio.save(x, buffer)
Source code in afnio/serialization.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
afnio.serialization.load(f)
Loads an object from a disk file using zip compression and pickle serialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
str | PathLike | BinaryIO | IO[bytes]
|
A file-like object (must
implement |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The deserialized object. |
Examples:
>>> # Load from file
>>> obj = afnio.load('model.hf')
>>> # Load from io.BytesIO buffer
>>> buffer = io.BytesIO()
>>> obj = afnio.load(buffer)
Source code in afnio/serialization.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |