Reference#
Core#
GreatAI
#
Bases: Generic[T, V]
Source code in great_ai/deploy/great_ai.py
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 151 152 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
create(func)
staticmethod
#
Decorate a function by wrapping it in a GreatAI instance.
The function can be typed, synchronous or async. If it has unwrapped parameters (parameters not affected by a @parameter or @use_model decorator), those will be automatically wrapped.
The return value is replaced by a Trace (or Awaitable[Trace]),
while the original return value is available under the .output
property.
For configuration options, see great_ai.configure.
Examples:
>>> @GreatAI.create
... def my_function(a: int) -> int:
... return a + 2
>>> my_function(3)
Trace[int]...
>>> my_function('3').output
Traceback (most recent call last):
...
TypeError: type of a must be int; got str instead
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
Union[Callable[..., Awaitable[V]], Callable[..., V]]
|
The prediction function that needs to be decorated. |
required |
Returns:
| Type | Description |
|---|---|
Union[GreatAI[Awaitable[Trace[V]], V], GreatAI[Trace[V], V]]
|
A GreatAI instance wrapping |
Source code in great_ai/deploy/great_ai.py
configure(*, version='0.0.1', log_level=DEBUG, seed=42, tracing_database_factory=None, large_file_implementation=None, should_log_exception_stack=None, prediction_cache_size=512, disable_se4ml_banner=False, dashboard_table_size=20, route_config=RouteConfig())
#
Source code in great_ai/context.py
save_model(model, key, *, keep_last_n=None)
#
Save (and optionally serialise) a model in order to use by use_model.
The model can be a Path or string representing a path in which case the
local file/folder is read and saved using the current LargeFile implementation.
In case model is an object, it is serialised using dill before uploading it.
Examples:
>>> @use_model('my_number')
... def my_function(a, model):
... return a + model
>>> my_function(4)
7
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
Union[Path, str, object]
|
The object or path to be uploaded. |
required |
key |
str
|
The model's name. |
required |
keep_last_n |
Optional[int]
|
If specified, remove old models and only keep the latest n. Directly passed to LargeFile. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The key and version of the saved model separated by a colon. Example: "key:version" |
Source code in great_ai/models/save_model.py
use_model(key, *, version='latest', model_kwarg_name='model')
#
Inject a model into a function.
Load a model specified by key and version using the currently active LargeFile
implementation. If it's a single object, it is deserialised using dill. If it's a
directory of files, a pathlib.Path instance is given.
By default, the function's model parameter is replaced by the loaded model. This
can be customised by changing model_kwarg_name. Multiple models can be loaded by
decorating the same function with use_model multiple times.
Examples:
>>> from great_ai import save_model
>>> save_model(3, 'my_number')
'my_number:...'
>>> @use_model('my_number')
... def my_function(a, model):
... return a + model
>>> my_function(4)
7
Args:
key: The model's name as stored by the LargeFile implementation.
version: The model's version as stored by the LargeFile implementation.
model_kwarg_name: the parameter to use for injecting the loaded model
Returns:
A decorator for model injection.
Source code in great_ai/models/use_model.py
parameter(parameter_name, *, validator=lambda _: True, disable_logging=False)
#
Control the validation and logging of function parameters.
Examples:
>>> @parameter('a')
... def my_function(a: int):
... return a + 2
>>> my_function(4)
6
>>> my_function('3')
Traceback (most recent call last):
...
TypeError: type of a must be int; got str instead
>>> @parameter('positive_a', validator=lambda v: v > 0)
... def my_function(positive_a: int):
... return a + 2
>>> my_function(-1)
Traceback (most recent call last):
...
great_ai.errors.argument_validation_error.ArgumentValidationError: ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameter_name |
str
|
Name of parameter to consider |
required |
validator |
Callable[[Any], bool]
|
Optional validator to run against the concrete argument. ArgumentValidationError is thrown when the return value is False. |
lambda _: True
|
disable_logging |
bool
|
Do not save the value in any active TracingContext. |
False
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
A decorator for argument validation. |