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
29 30 31 32 | |
to_dict
to_dict()
Represent this object with a fully JSON-serializable dictionary
Source code in llmeter/callbacks/cost/serde.py
25 26 27 | |
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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
from_file
classmethod
from_file(input_path, **kwargs)
Initialize an instance of this class from a (local or Cloud) JSON file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
ReadablePathLike
|
The path to the JSON data file. |
required |
**kwargs
|
Optional extra keyword arguments to pass to |
{}
|
Source code in llmeter/callbacks/cost/serde.py
153 154 155 156 157 158 159 160 161 162 163 164 165 | |
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 |
{}
|
Source code in llmeter/callbacks/cost/serde.py
167 168 169 170 171 172 173 174 175 | |
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
177 178 179 180 181 182 183 184 | |
to_file
to_file(output_path, indent=4, **kwargs)
Save the state of the object to a (local or Cloud) JSON file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
WritablePathLike
|
The path where the configuration file will be saved. |
required |
indent
|
int | str | None
|
Optional indentation passed through to |
4
|
**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
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
to_json
to_json(default=llmeter_default_serializer, **kwargs)
Serialize this object to JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Fallback serializer. Defaults to
:func: |
llmeter_default_serializer
|
|
**kwargs
|
Extra keyword arguments passed to :func: |
{}
|
Source code in llmeter/callbacks/cost/serde.py
209 210 211 212 213 214 215 216 217 | |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
dict
|
A plain Python dict, for example loaded from a JSON file |
required |
cls
|
type[TFromDict]
|
The class to create an instance of |
required |
**kwargs
|
Optional extra keyword arguments to pass to the constructor |
{}
|
Source code in llmeter/callbacks/cost/serde.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
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)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
dict
|
A plain Python dict which must contain a |
required |
class_map
|
dict[str, type[TFromDict]]
|
A mapping from |
required |
**kwargs
|
Optional extra keyword arguments to pass to the constructor |
{}
|
Source code in llmeter/callbacks/cost/serde.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
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
35 36 37 38 39 40 | |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
The object to convert |
required |
**kwargs
|
Optional extra parameters to insert in the output dictionary |
{}
|
Source code in llmeter/callbacks/cost/serde.py
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 72 73 74 75 | |