Reference
Quick reference
֍
A concise collection of usage examples
Connecting text nodes
Python
TypeScript
from substrate import Substrate, ComputeText, sb# 1) Initialize Substrates = Substrate(api_key=YOUR_API_KEY)# 2) Construct a grapha = ComputeText(prompt="tell me a story")# use sb.concat to join a list of static or future stringsb = 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 nodesres = s.run(*cs)# 4) Get the output of nodesprint(res.get(b).text) # summaryprint(res.get(cs[0]).text) # summary of summaryprint(res.get(cs[1]).text) # alternate summary of summary
Generating JSON
Python
TypeScript
from substrate import Substrate, ComputeJSON, sbfrom pydantic import BaseModel, Fieldclass 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)