Subclassing
Modularize and re-use Chainlet implementations
Sometimes you want to write one βmainβ implementation of a complicated inference task, but then re-use it for similar variations. For example:
- Deploy it on different hardware and with different concurrency.
- Replace a dependency (e.g. silence detection in audio files) with a different implementation of that step - while keeping all other processing the same.
- Deploy the same inference flow, but exchange the model weights used. E.g. for
a large and small version of an LLM or different model weights fine-tuned to
domains. - Add an adapter to convert between a different input/output schema.
In all of those cases, you can create lightweight subclasses of your main chainlet.
Below are some example code snippets - they can all be combined with each other!
Example base class
Adapter for different I/O
The base class MyBaseChainlet
works with integer inputs and returns floats. If
you want to reuse the computation, but provide an alternative interface (e.g.
for a different client with different request/response schema), you can create
a subclass which does the I/O conversion. The actual computation is delegated to
the base classes above.
Chain with substituted dependency
The base class MyBaseChainlet
uses preprocessing that doubles the input. If
you want to use a different variant of preprocessing - while keeping
MyBaseChainlet.run_remote
and everything else as is - you can define a shallow
subclass of MyBaseChainlet
where you use a different dependency
Preprocess8x
, which multiplies by 8 instead of 2.
Override remote config.
If you want to re-deploy a chain, but change some deployment options, e.g. run
on different hardware, you can create a subclass and override remote_config
.
Be aware that remote_config
is a class variable. In the example above we
created a completely new RemoteConfig
value, because changing fields
inplace would also affect the base class.
If you want to share config between the base class and subclasses, you can define them in additional variables e.g. for the image: