Install the SDK
The official Substrate SDKs are the recommended way to interact with Substrate from TypeScript, JavaScript, or Python.
We plan to generate idiomatic SDKs for the Substrate protocol in every popular language. Let us know (opens in a new tab) which languages to prioritize next.
pip install substrate
Initialize Substrate
Substrate uses API keys to authenticate requests. Create an API key in your Substrate Dashboard. Then, import and initialize the Substrate
client with your API key.
Your API keys carry the ability to make requests using your Substrate account, so be sure to keep them secure. Do not share your API keys in areas such as publicly-accessible client-side code, social media, or public GitHub repos.
Create a graph
Generate a story with a language model using ComputeText. Then, summarize the story by passing the future output of the story
to a summary
node.
Because story
has not been run yet, we must use sb.concat
to create a prompt using a string from its future output.
Run the graph
We've created a simple graph chaining story
→ summary
. Run the graph by calling substrate.run
.
Substrate automatically finds the upstream dependencies for nodes.
- You must pass all terminal nodes to
substrate.run
. - Passing upstream nodes is optional.
- You may pass disconnected nodes (or multiple graphs) to run them in parallel.
Get the output
After running the graph, use response.get
to retrieve the output of any of the nodes in the graph.
If you want to combine the outputs of multiple nodes into a single response, you can use a Box
:
You can also stream outputs.