Serde
serde
(De/re)serialization interfaces for saving Cost Model objects to file and loading them back
ISerializable
Bases: Protocol
from_dict
classmethod
from_dict(**kwargs)
Load an instance of this class from a JSON-able dictionary representation
Source code in llmeter/callbacks/cost/serde.py
26 27 28 29 | |
to_dict
to_dict()
Represent this object with a fully JSON-serializable dictionary
Source code in llmeter/callbacks/cost/serde.py
22 23 24 | |
JSONableBase
A base class for speeding up implementation of JSON-serializable objects
from_dict
classmethod
from_dict(raw, alt_classes={}, **kwargs)
Initialize an instance of this class from a plain dict (with optional extra kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
dict
|
A plain Python dict, for example loaded from a JSON file |
required |
alt_classes
|
Dict[str, TJSONable]
|
By default, this method will only use the class of the current object
(i.e. |
{}
|
**kwargs
|
Any
|
Optional extra keyword arguments to pass to the constructor |
{}
|
Source code in llmeter/callbacks/cost/serde.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
from_file
classmethod
from_file(input_path, **kwargs)
Initialize an instance of this class from a (local or Cloud) JSON file
Args:
input_path: The path to the JSON data file.
**kwargs: Optional extra keyword arguments to pass to from_dict()
Source code in llmeter/callbacks/cost/serde.py
146 147 148 149 150 151 152 153 154 155 | |
from_json
classmethod
from_json(json_string, **kwargs)
Initialize an instance of this class from a JSON string (with optional extra kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_string
|
str
|
A string containing valid JSON data |
required |
**kwargs
|
Any
|
Optional extra keyword arguments to pass to `from_dict()`` |
{}
|
Source code in llmeter/callbacks/cost/serde.py
157 158 159 160 161 162 163 164 165 | |
to_dict
to_dict(**kwargs)
Save the state of the object to a JSON-dumpable dictionary (with optional extra kwargs)
Implementers of this method should ensure that the returned dict is fully JSON-compatible: Mapping any child fields from Python classes to dicts if necessary, avoiding any circular references, etc.
Source code in llmeter/callbacks/cost/serde.py
167 168 169 170 171 172 173 174 | |
to_file
to_file(output_path, indent=4, default=str, **kwargs)
Save the state of the object to a (local or Cloud) JSON file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
PathLike
|
The path where the configuration file will be saved. |
required |
indent
|
int | str | None
|
Optional indentation passed through to |
4
|
default
|
Callable[[Any], Any] | None
|
Optional function to convert non-JSON-serializable objects to strings, passed
through to |
str
|
**kwargs
|
Any
|
Optional extra keyword arguments to pass to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
output_path |
UPath
|
Universal Path representation of the target file. |
Source code in llmeter/callbacks/cost/serde.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
to_json
to_json(**kwargs)
Serialize this object to JSON, with optional kwargs passed through to json.dumps()
Source code in llmeter/callbacks/cost/serde.py
201 202 203 | |
from_dict_with_class
from_dict_with_class(raw, cls, **kwargs)
Initialize an instance of a class from a plain dict (with optional extra kwargs)
If the input dictionary contains a _type key, and this doesn't match the provided
cls.__name__, a warning will be logged.
Args:
raw: A plain Python dict, for example loaded from a JSON file
cls: The class to create an instance of
**kwargs: Optional extra keyword arguments to pass to the constructor
Source code in llmeter/callbacks/cost/serde.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
from_dict_with_class_map
from_dict_with_class_map(raw, class_map, **kwargs)
Initialize an instance of a class from a plain dict (with optional extra kwargs)
Args:
raw: A plain Python dict which must contain a _type key
classes: A mapping from _type string to class to create an instance of
**kwargs: Optional extra keyword arguments to pass to the constructor
Source code in llmeter/callbacks/cost/serde.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
is_dataclass_instance
is_dataclass_instance(obj)
Check whether obj is an instance of any dataclass
See: https://docs.python.org/3/library/dataclasses.html#dataclasses.is_dataclass
Source code in llmeter/callbacks/cost/serde.py
32 33 34 35 36 | |
to_dict_recursive_generic
to_dict_recursive_generic(obj, **kwargs)
Convert a vaguely dataclass-like object (with maybe IJSONable fields) to a JSON-ready dict
The output dict is augmented with _type storing the __class__.__name__ of the provided
obj.
Args:
obj: The object to convert
**kwargs: Optional extra parameters to insert in the output dictionary
Source code in llmeter/callbacks/cost/serde.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |