BASETEN_API_KEY and the Baseten CLI and Truss installed. Install the client dependencies with pip install requests msgpack.
Deploy a basic Truss for binary I/O
If you need a deployed model to try the invocation examples below, follow these steps to create and deploy a minimal Truss that accepts and returns binary data. The Truss performs no operations and is purely illustrative.Steps for deploying an example Truss
Steps for deploying an example Truss
To create and deploy the example Truss:
-
Create a Truss:
This creates a Truss in a new directoryTerminal
binary_test. By default, newly created Trusses implement an identity function that returns the exact input they are given. -
Optionally, modify
binary_test/model/model.pyto log that the data received is of typebytes:binary_test/model/model.py -
Deploy the Truss to Baseten. Keep the watch process running, wait for the deployment to become active, and use another terminal for the client requests:
Terminal
Send raw bytes as model input
To send bytes to your model: Configure the request as follows:- Set the
content-typeHTTP header toapplication/octet-stream. - Use
msgpackto encode the data or file. - Make a POST request to the model.
BASETEN_MODEL_ID to the identity Truss model ID. Run the following script to send a short byte string and check that the returned bytes match:
--watch deployment above. For a published production deployment, use /environments/production/predict.
To support certain types like numpy and datetime values, you may need to
extend client-side
msgpack encoding with the same encoder and decoder used
by
Truss.Parse raw bytes from model output
To save the returned bytes: Continue the previous script and write the decoded byte field to a file:Streaming binary outputs
Streaming sends raw response chunks rather than one MessagePack object. Decode them according to the format your model yields. To stream bytes from a Truss:-
Replace the model’s
predictmethod with a generator and let the watch process update the deployment:model/model.py -
Run the following client to save chunks directly and read the completed file:
Next steps
File I/O
Pass files and URLs as model input and save output to disk
Streaming
Stream text responses token by token