Skip to main content
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 (for example, 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. For example, 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. These patterns can be combined with each other.

Example base class

Define the base Chainlet and verify its behavior locally:
base_chainlet.py

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 (for example, 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:
string_io_adapter.py

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 that uses a different dependency, Preprocess8x, which multiplies by 8 instead of 2:
substituted_dependency.py

Override remote config

If you want to re-deploy a chain, but change some deployment options, for example, run on different hardware, you can create a subclass and override remote_config:
override_config.py
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 for example, for the image:
shared_config.py