- A simple CPU-only “hello world”-Chain.
- A Chain that implements Phi-3 Mini and uses it to write poems.
Prerequisites
You need uv installed and a Baseten account with an API key.Hello World
Chains are written in Python files. In your working directory, createhello_chain/hello.py:
Terminal
HelloWorld, the entrypoint, which handles the input and output.RandInt, which generates a random integer. It is used a as a dependency byHelloWorld.
hello.py
The Chainlet class-contract
Exactly one Chainlet must be marked as the entrypoint with the@chains.mark_entrypoint
decorator. This Chainlet is responsible for
handling public-facing input and output for the whole Chain in response to an
API call.
A Chainlet class has a single public method,
run_remote(), which is
the API
endpoint for the entrypoint Chainlet and the function that other Chainlets can
use as a dependency. The
run_remote()
method must be fully type-annotated
with
or .
Chainlets cannot be instantiated. The only correct usages are:
- Make one Chainlet depend on another one through the
chains.depends()directive as an__init__-argument as shown above for theRandIntChainlet. - In the local debugging mode.
Deploy your Chain to Baseten
To deploy your Chain to Baseten, run:Terminal
Output
ACTIVE and test invoking your Chain (replace
$INVOCATION_URL in below command):
Request
Poetry with LLMs
Our second example also has two Chainlets, but is somewhat more complex and realistic. The Chainlets are:PoemGenerator, the entrypoint, which handles the input and output and orchestrates calls to the LLM.PhiLLM, which runs inference on Phi-3 Mini.
hello_chain/, go up one level with cd .. first):
Terminal
Build the LLM Chainlet
The main difference between this Chain and the previous one is that we now have an LLM that needs a GPU and more complex dependencies. Copy the following code intopoems.py:
poems.py
Build the entrypoint
Now that we have an LLM, we can use it in a poem generator Chainlet. Add the following code topoems.py:
poems.py
asyncio.create_task around each RPC to the LLM chainlet.
This makes the current python process start these remote calls concurrently,
that is, the next call is started before the previous one has finished and we can
minimize our overall runtime. To await the results of all calls,
asyncio.gather is used which gives us back normal python objects.
If the LLM is hit with many concurrent requests, it can auto-scale up (if
autoscaling is configured). More advanced LLM models have batching capabilities,
so for those even a single instance can serve concurrent request.
Deploy your Chain to Baseten
To deploy your Chain to Baseten, run:Terminal
ACTIVE and test invoking your Chain (replace
$INVOCATION_URL in below command):
Request