Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import { ai } from "https://esm.town/v/yawnxyz/ai";
export default async (req) => {
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
const textEncoder = new TextEncoder();
// Get the prompt from the URL query
const url = new URL(req.url);
const prompt = url.searchParams.get("prompt") || "tell me a joke";
if (!prompt || prompt.trim() === "") {
return new Response("Please provide a prompt in the URL query.", {
headers: {
"Content-Type": "text/plain",
},
status: 400,
});
}
const result = await ai(prompt, {
provider: "groq",
streaming: true,
max_tokens: 1024
})
new Promise(async () => {
for await (const textPart of result.textStream) {
console.log('text:', textPart);
writer.write(textEncoder.encode(textPart))
}
writer.close(); // This can be awaited if needed
});
// Send the readable stream back to the client
return new Response(readable, {
headers: {
"Content-Type": "text/html",
},
});
};
yawnxyz-aistreamingexample.web.val.run
June 22, 2024