How to create a GreatAI service#
The core value of
great-ai lies in its GreatAI class. To take advantage of it, you need to create an instance wrapping your code.
Let's say that you have the following greeter function:
You can simply decorate (wrap) this function using the @GreatAI.create factory.
Why not simply use
The purpose of @GreatAI.create is simply to provide you with type-checking through MyPy, Pylance, and similar libraries. However, the overloading support for
__new__ is lacking in MyPy. Thus, a static factory method is used instead.
Even though it's not required by GreatAI, type annotating your codebase can save you from lots of trivial mistakes; that's why it's highly advised. Simply add the expected types to your function's signature.
This not only allows you to statically type-check your code, but by default, GreatAI will check it during runtime as well using typeguard.
Asynchronous code can result in immense performance gains in some instances. For example, you might rely on a third-party service, do database access, or call a remote GreatAI instance. In these cases, you can make your function
async without any other changes.
GreatAI can decorate already decorated functions. The only restriction is that @GreatAI.create must come last. There are two built-in decorators that you can use to customise your function, but you can use any third-party decorator as well.
- By default, the parameter named
modelwill be replaced by the loaded model. This behaviour can be customised by setting the
model_kwarg_name. This way, even multiple models can be injected into a single function.
You must call @use_model before @GreatAI.create. Note that decorators are applied starting from the bottom-most one. Feel free to use @use_model in other places of the codebase, and it works equally well outside GreatAI services.
If you wish to turn off logging or specify custom validation for your parameters, you can use the @parameter decorator.
You must call @parameter before @GreatAI.create. Note that decorators are applied starting from the bottom-most one. Feel free to use @parameter in other places of the codebase, and it works equally well outside GreatAI services.
The following example summarises the options you have when instantiating a GreatAI service.
from great_ai import save_model, GreatAI, parameter, use_model, log_metric save_model(4, 'secret-number') #(1) @GreatAI.create @parameter('positive_number', validate=lambda n: n > 0, disable_logging=True) @use_model('secret-number', version='latest', model_kwarg_name='secret') def add_number(positive_number: int, secret: int) -> int: log_metric( 'log directly into the returned Trace', positive_number * 2 ) return positive_number + secret assert add_number(1).output == 5
- Refer to the configuration page for specifying where to store your models.