Reference
Quick reference
֍
A concise collection of usage examples

Connecting text nodes

Python
TypeScript

from substrate import Substrate, ComputeText, sb
# 1) Initialize Substrate
s = Substrate(api_key=YOUR_API_KEY)
# 2) Construct a graph
a = ComputeText(prompt="tell me a story")
# use sb.concat to join a list of static or future strings
b = ComputeText(prompt=sb.concat("summarize: ", a.future.text))
cs = []
for i in list(range(2)):
cs.append(ComputeText(
# or sb.jinja to use a jinja template
prompt=sb.jinja("summarize: {{b.future.text}}", {"b": b})
))
# 3) Call run with terminal nodes
res = s.run(*cs)
# 4) Get the output of nodes
print(res.get(b).text) # summary
print(res.get(cs[0]).text) # summary of summary
print(res.get(cs[1]).text) # alternate summary of summary

Generating JSON

Python
TypeScript

from substrate import Substrate, ComputeJSON, sb
from pydantic import BaseModel, Field
class Book(BaseModel):
author: str = Field(..., description="The book's title")
characters: list[str] = Field(..., description="List of main characters.")
a = ComputeJSON(
prompt="Tell me the author and two main characters of Don Quixote",
json_schema=Book.model_json_schema(),
)
b = ComputeText(
prompt=sb.concat("Tell me about ", a.future.json_object["author"]),
)
c = ComputeText(
prompt=sb.concat("Tell me about ", a.future.json_object.characters[0]),
)

Running custom code

Python

def markdown(url: str):
import requests
from bs4 import BeautifulSoup
from markdownify import markdownify
res = requests.get(url)
soup = BeautifulSoup(res.content, "html.parser")
return markdownify(str(soup))
md = RunPython(
function=markdown,
kwargs={
"url": "https://metrograph.com/film/?vista_film_id=9999001208",
},
pip_install=["requests", "beautifulsoup4", "markdownify"],
)
res = substrate.run(md)
out = res.get(md)
print(out.output)