stevekrouse-openaistreamingexample.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
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
const openai = new OpenAI();
const stream = await openai.chat.completions.create({
stream: true,
messages: [{
role: "user",
content: "Write an HTML & CSS page with a short poem in the style of beowulf about the DMV",
}],
model: "gpt-3.5-turbo",
max_tokens: 4000,
});
return new Response(
new ReadableStream({
async start(controller) {
for await (const chunk of stream) {
controller.enqueue(new TextEncoder().encode(chunk.choices[0]?.delta?.content));
}
controller.close();
},
}),
{ 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
Nobody has commented on this val yet: be the first!
v8
June 13, 2024