stevekrouse-openaistreaminghtml.web.val.run
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
import OpenAI from "npm:openai";
const openai = new OpenAI();
export default async (req) => {
const stream = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{
role: "user",
content: "an html page that contains a story. use tailwindcss for styling in the style of the story",
}],
stream: true,
});
let { readable, writable } = new TransformStream();
let writer = writable.getWriter();
const textEncoder = new TextEncoder();
new Promise(async () => {
// loop over the data as it is streamed and write to the writeable
for await (const part of stream) {
writer.write(textEncoder.encode(part.choices[0]?.delta?.content || ""));
}
writer.close();
});
// Send the readable back to the browser
return new Response(readable, {
headers: {
"Content-Type": "text/html",
},
});
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
1
tr3ntg avatar

Wow this is so fun. Hitting refresh and getting a new story AND new webpage is pretty cool.

v3
June 6, 2024