Substrate Python SDK
Project description
꩜ Substrate Python SDK
The Substrate Python SDK is the recommended way to interact with the Substrate API from any Python application.
Documentation
If you're just getting started, head to guides.substrate.run.
For a detailed API reference covering the nodes available on Substrate, see substrate.run/nodes.
For an interactive reference, check out explore.substrate.run. You can call Substrate.visualize(...nodes...)
to generate an interactive visualization of any graph.
Installation
# install from PyPI
pip install substrate
Usage
from substrate import Substrate, GenerateText, sb
Initialize the Substrate client.
substrate = Substrate(api_key=SUBSTRATE_API_KEY)
Generate a story using the GenerateText
node.
story = GenerateText({"prompt": "tell me a story"})
Summarize the output of the story
node using another GenerateText
node. Because story
has not yet been run, we use sb.concat
to work with its future output.
summary = GenerateText({"prompt": sb.concat("summarize this story in one sentence: ", story.future.text)})
Run the graph chaining story
→ summary
. This is a simple example, but you can easily build arbitrarily complex branching workflows.
response = substrate.run(story, summary)
(To run the graph asynchronously, simply use async_run
and await
.)
response = await substrate.async_run(story, summary)
Get the output of the summary node by passing it to response.get
.
summary_out = response.get(summary)
print(summary_out.text)
// Princess Lily, a kind-hearted young princess, discovers a book of spells and uses it to grant her family and kingdom happiness.
Examples
To run the above example as a notebook, navigate to the examples/notebooks
directory and run:
make ensure # install dependencies
poetry run marimo edit basic.py # run the notebook
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for substrate-120240502.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c33f2bc1d80c2d8d5f7e257772d16bed49a9fa5b33ca50a975455c3a9db9b3f9 |
|
MD5 | 6b453343aafbdccac96093188a863506 |
|
BLAKE2b-256 | 6ff1e2ed1a7d68c25b37b25f04bc316c9212bf80874a1be18726d0e4cef14b3e |