substrate-template.web.val.run
Readme

Starter template for Substrate demos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import promptHeader from "https://esm.town/v/substrate/promptHeader";
import wrapper from "https://esm.town/v/substrate/substrateBadgeMiddleware";
import { ComputeJSON, ComputeText, GenerateImage, sb, Substrate } from "npm:substrate";
async function handler(req: Request): Promise<Response> {
const searchParams = new URL(req.url).searchParams;
const { prompt = "modernism" } = Object.fromEntries(searchParams);
// Substrate graph
const substrate = new Substrate({ apiKey: Deno.env.get("SUBSTRATE_API_KEY") });
const startup_name = new ComputeText({
prompt:
`Give me a unique startup name inside <name></name> tags related to ${prompt}. Example: <name>Hyphae</name><description>A startup that ... </description>`,
});
const haiku = new ComputeText({
prompt: sb.concat(
"Write a haiku about the startup names: ",
startup_name.future.text,
),
});
const stream = await substrate.stream(haiku);
// UI
return new Response(
new ReadableStream({
async start(controller) {
controller.enqueue(new TextEncoder().encode(promptHeader()));
for await (const event of stream) {
if (event.object === "node.result") {
if (event.data.text) {
controller.enqueue(
new TextEncoder().encode(
`<div style="background-color: #f0f0f0; border-radius: 8px; padding: 4px; margin: 8px;">${event.data.text}</div>`,
),
);
}
}
}
controller.close();
},
}),
{ headers: { "Content-Type": "text/html" } },
);
}
export default wrapper(handler, import.meta.url);
Only the latest version can be previewed
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v39
July 10, 2024