Python SDK Reference for Chains
truss_chains.ChainletBase
truss_chains.ModelBase
truss_chains.EngineBuilderLLMChainlet
Name | Type | Description |
---|---|---|
llm_input | EngineBuilderLLMInput | OpenAI compatible request. |
truss_chains.depends
depends
is intended to be
used as a default argument in a chainlet’s __init__
-method.
When deploying a chain remotely, a corresponding stub to the remote is injected in
its place. In run_local
mode an instance
of a local chainlet is injected.
Refer to the docs and this
example chainlet
for more guidance on how make one chainlet depend on another chainlet.
run_local
a
chainlet instance is provided.Name | Type | Default | Description |
---|---|---|---|
chainlet_cls | Type[ChainletBase] | The chainlet class of the dependency. | |
retries | int | 1 | The number of times to retry the remote chainlet in case of failures (e.g. due to transient network issues). For streaming, retries are only made if the request fails before streaming any results back. Failures mid-stream not retried. |
timeout_sec | float | 600.0 | Timeout for the HTTP request to this chainlet. |
use_binary | bool | False | Whether to send data in binary format. This can give a parsing speedup and message size reduction (~25%) for numpy arrays. Use NumpyArrayField as a field type on pydantic models for integration and set this option to True . For simple text data, there is no significant benefit. |
concurrency_limit | int | 300 | The maximum number of concurrent requests to send to the remote chainlet. Excessive requests will be queued and a warning will be shown. Try to design your algorithm in a way that spreads requests evenly over time so that this the default value can be used. |
truss_chains.depends_context
__init__
-signature of chainlets.
run_local
a
context instance is provided.truss_chains.DeploymentContext
pydantic.BaseModel
Bundles config values and resources needed to instantiate Chainlets.
The context can optionally be added as a trailing argument in a Chainlet’s
__init__
method and then used to set up the chainlet (e.g. using a secret as
an access token for downloading model weights).
Parameters:
Name | Type | Default | Description |
---|---|---|---|
chainlet_to_service | Mapping[str,DeployedServiceDescriptor] | A mapping from chainlet names to service descriptors. This is used to create RPC sessions to dependency chainlets. It contains only the chainlet services that are dependencies of the current chainlet. | |
secrets | Mapping[str,str] | A mapping from secret names to secret values. It contains only the secrets that are listed in remote_config.assets.secret_keys of the current chainlet. | |
data_dir | Path|None | None | The directory where the chainlet can store and access data, e.g. for downloading model weights. |
environment | Environment|None | None | The environment that the chainlet is deployed in. None if the chainlet is not associated with an environment. |
Name | Type | Description |
---|---|---|
chainlet_name | str | The name of the chainlet. |
truss_chains.Environment
pydantic.BaseModel
The environment the chainlet is deployed in.
truss_chains.ChainletOptions
pydantic.BaseModel
Parameters:
Name | Type | Default | Description |
---|---|---|---|
enable_b10_tracing | bool | False | enables baseten-internal trace data collection. This helps baseten engineers better analyze chain performance in case of issues. It is independent of a potentially user-configured tracing instrumentation. Turning this on, could add performance overhead. |
enable_debug_logs | bool | False | Sets log level to debug in deployed server. |
env_variables | Mapping[str,str] | {} | static environment variables available to the deployed chainlet. |
health_checks | HealthChecks | truss.base.truss_config.HealthChecks() | Configures health checks for the chainlet. See guide. |
metadata | JsonValue|None | None | Arbitrary JSON object to describe chainlet. |
streaming_read_timeout | int | 60 | Amount of time (in seconds) between each streamed chunk before a timeout is triggered. |
transport | Union[HTTPOptions|WebsocketOptions|GRPCOptions]‘ | None | Allows to customize certain transport protocols, e.g. websocket pings. |
truss_chains.RPCOptions
pydantic.BaseModel
Options to customize RPCs to dependency chainlets.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
retries | int | 1 | The number of times to retry the remote chainlet in case of failures (e.g. due to transient network issues). For streaming, retries are only made if the request fails before streaming any results back. Failures mid-stream not retried. |
timeout_sec | float | 600.0 | Timeout for the HTTP request to this chainlet. |
use_binary | bool | False | Whether to send data in binary format. This can give a parsing speedup and message size reduction (~25%) for numpy arrays. Use NumpyArrayField as a field type on pydantic models for integration and set this option to True . For simple text data, there is no significant benefit. |
concurrency_limit | int | 300 | The maximum number of concurrent requests to send to the remote chainlet. Excessive requests will be queued and a warning will be shown. Try to design your algorithm in a way that spreads requests evenly over time so that this the default value can be used. |
truss_chains.mark_entrypoint
name
arg of the CLI push command).
Example usage:
truss_chains.RemoteConfig
pydantic.BaseModel
Bundles config values needed to deploy a chainlet remotely.
This is specified as a class variable for each chainlet class, e.g.:
Name | Type | Default |
---|---|---|
docker_image | DockerImage | truss_chains.DockerImage() |
compute | Compute | truss_chains.Compute() |
assets | Assets | truss_chains.Assets() |
name | str|None | None |
options | ChainletOptions | truss_chains.ChainletOptions() |
truss_chains.DockerImage
pydantic.BaseModel
Configures the docker image in which a remoted chainlet is deployed.
DockerImage
is
defined and must be created with the helper function [make_abs_path_here
]
(#function-truss-chains-make-abs-path-here).
This allows you for example organize chainlets in different (potentially nested)
modules and keep their requirement files right next their python source files.Name | Type | Default | Description |
---|---|---|---|
base_image | BasetenImage|CustomImage | truss_chains.BasetenImage() | The base image used by the chainlet. Other dependencies and assets are included as additional layers on top of that image. You can choose a Baseten default image for a supported python version (e.g. BasetenImage.PY311 ), this will also include GPU drivers if needed, or provide a custom image (e.g. CustomImage(image="python:3.11-slim") ). |
pip_requirements_file | AbsPath|None | None | Path to a file containing pip requirements. The file content is naively concatenated with pip_requirements . |
pip_requirements | list[str] | [] | A list of pip requirements to install. The items are naively concatenated with the content of the pip_requirements_file . |
apt_requirements | list[str] | [] | A list of apt requirements to install. |
data_dir | AbsPath|None | None | Data from this directory is copied into the docker image and accessible to the remote chainlet at runtime. |
external_package_dirs | list[AbsPath]|None | None | A list of directories containing additional python packages outside the chain’s workspace dir, e.g. a shared library. This code is copied into the docker image and importable at runtime. |
truss_server_version_override | str|None | None | By default, deployed Chainlets use the truss server implementation corresponding to the truss version of the user’s CLI. To use a specific version, e.g. pinning it for exact reproducibility, the version can be overridden here. Valid versions correspond to truss releases on PyPi: https://pypi.org/project/truss/#history, e.g. “0.9.80”. |
truss_chains.BasetenImage
Enum
Default images, curated by baseten, for different python versions. If a Chainlet
uses GPUs, drivers will be included in the image.
Enum Member | Value |
---|---|
PY39 | py39 |
PY310 | py310 |
PY311 | py311 |
truss_chains.CustomImage
pydantic.BaseModel
Configures the usage of a custom image hosted on dockerhub.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
image | str | Reference to image on dockerhub. | |
python_executable_path | str|None | None | Absolute path to python executable (if default python is ambiguous). |
docker_auth | DockerAuthSettings|None | None | See corresponding truss config. |
truss_chains.Compute
Name | Type | Default | Description |
---|---|---|---|
cpu_count | int | 1 | Minimum number of CPUs to allocate. |
memory | str | '2Gi' | Minimum memory to allocate, e.g. “2Gi” (2 gibibytes). |
gpu | str|Accelerator|None | None | GPU accelerator type, e.g. “A10G”, “A100”, refer to the truss config for more choices. |
gpu_count | int | 1 | Number of GPUs to allocate. |
predict_concurrency | int|Literal[‘cpu_count’] | 1 | Number of concurrent requests a single replica of a deployed chainlet handles. |
predict_concurrency
of a single instance is implemented in
two ways:
asyncio
, if run_remote
is an async def. This
requires that run_remote
yields to the event loop.truss_chains.Assets
Name | Type | Default | Description |
---|---|---|---|
cached | Iterable[ModelRepo] | () | One or more truss_config.ModelRepo objects. |
secret_keys | Iterable[str] | () | Names of secrets stored on baseten, that the chainlet should have access to. You can manage secrets on baseten here. |
external_data | Iterable[ExternalDataItem] | () | Data to be downloaded from public URLs and made available in the deployment (via context.data_dir ). |
truss_chains.push
Name | Type | Default | Description |
---|---|---|---|
entrypoint | Type[ChainletT] | The chainlet class that serves as the entrypoint to the chain. | |
chain_name | str | The name of the chain. | |
publish | bool | True | Whether to publish the chain as a published deployment (it is a draft deployment otherwise) |
promote | bool | True | Whether to promote the chain to be the production deployment (this implies publishing as well). |
only_generate_trusses | bool | False | Used for debugging purposes. If set to True, only the the underlying truss models for the chainlets are generated in /tmp/.chains_generated . |
remote | str | 'baseten' | name of a remote config in .trussrc. If not provided, it will be inquired. |
environment | str|None | None | The name of an environment to promote deployment into. |
progress_bar | Type[progress.Progress]|None | None | Optional rich.progress.Progress if output is desired. |
include_git_info | bool | False | Whether to attach git versioning info (sha, branch, tag) to deployments made from within a git repo. If set to True in .trussrc, it will always be attached. |
truss_chains.deployment.deployment_client.ChainService
ChainService
is created and returned when using push
. It
bundles the individual services for each chainlet in the chain, and provides
utilities to query their status, invoke the entrypoint etc.
DeployedChainlet
, (name, is_entrypoint, status, logs_url)
for each chainlet.Name | Type | Description |
---|---|---|
json | JSON dict | Input data to the entrypoint |
truss_chains.make_abs_path_here
root/sub_package/chainlet.py
point to the requirements
file like this:
make_abs_path_here
into a
function (e.g. applying decorators) or use dynamic code execution.Ok:Name | Type | Description |
---|---|---|
file_path | str | Absolute or relative path. |
truss_chains.run_local
DeploymentContext
.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
secrets | Mapping[str,str]|None | None | A dict of secrets keys and values to provide to the chainlets. |
data_dir | Path|str|None | None | Path to a directory with data files. |
chainlet_to_service | Mapping[str,DeployedServiceDescriptor] | None | A dict of chainlet names to service descriptors. |
truss_chains.DeployedServiceDescriptor
pydantic.BaseModel
Bundles values to establish an RPC session to a dependency chainlet,
specifically with StubBase
.
Parameters:
Name | Type | Default |
---|---|---|
name | str | |
display_name | str | |
options | RPCOptions | |
predict_url | str|None | None |
internal_url | InternalURL | None |
truss_chains.StubBase
BasetenSession
, ABC
Base class for stubs that invoke remote chainlets.
Extends BasetenSession
with methods for data serialization, de-serialization
and invoking other endpoints.
It is used internally for RPCs to dependency chainlets, but it can also be used
in user-code for wrapping a deployed truss model into the Chains framework. It
flexibly supports JSON and pydantic inputs and output. Example usage:
Name | Type | Description |
---|---|---|
service_descriptor | DeployedServiceDescriptor] | Contains the URL and other configuration. |
api_key | str | A baseten API key to authorize requests. |
__init__
-method.
Parameters:
Name | Type | Description |
---|---|---|
predict_url | str | URL to predict endpoint of another chain / truss model. |
context_or_api_key | DeploymentContext | Deployment context object, obtained in the chainlet’s __init__ or Baseten API key. |
options | RPCOptions | RPC options, e.g. retries. |
async predict_async(inputs: PydanticModel, output_model: Type[PydanticModel]) → PydanticModel
async predict_async(inputs: JSON, output_model: Type[PydanticModel]) → PydanticModel
async predict_async(inputs: JSON) → JSON
async predict_async_stream(inputs: PydanticModel | JSON) -> AsyncIterator[bytes]
predict_sync(inputs: PydanticModel, output_model: Type[PydanticModel]) → PydanticModel
predict_sync(inputs: JSON, output_model: Type[PydanticModel]) → PydanticModel
predict_sync(inputs: JSON) → JSON
truss_chains.RemoteErrorDetail
pydantic.BaseModel
When a remote chainlet raises an exception, this pydantic model contains
information about the error and stack trace and is included in JSON form in the
error response.
Parameters:
Name | Type |
---|---|
exception_cls_name | str |
exception_module_name | str|None |
exception_message | str |
user_stack_trace | list[StackFrame] |
truss_chains.GenericRemoteException
Exception
Raised when calling a remote chainlet results in an error and it is not possible
to re-raise the same exception that was raise remotely in the caller.