maxm-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
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 a poem in the style of beowulf about the DMV" }],
model: "gpt-3.5-turbo",
max_tokens: 2048,
});
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/event-stream" } },
);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
v32 was merged from the PR "Close stream" by stevekrouse
Comments
Nobody has commented on this val yet: be the first!
v32
July 2, 2024