Cloudflare Worker is waiting for the whole response before streaming the output to the client, which defeats the entire point of streaming. I don't know what I am doing wrong.
router.post("/v1/streaming", async (request, { OPENAI_API }) => { let { readable, writable } = new TransformStream(); const client = new ChatOpenAI({ openAIApiKey: OPENAI_API, }); const stream = await client.stream( "Write a 2 sentence poem about a sunset." ); let writer = writable.getWriter(); const textEncoder = new TextEncoder(); for await (const part of stream) { if (part.content.length) { // Assuming part.content is a string containing the text const words = part.content.split(" "); // Split the text into words for (const word of words) { const sseFormattedData = `data: ${word}\n\n`; // Format each word as SSE await writer.write(textEncoder.encode(sseFormattedData)); console.log("🚀 - forawait - sseFormattedData:", sseFormattedData); await new Promise((r) => setTimeout(r, 1000)); // Wait for 1 second before sending the next word } } } writer.close(); return new Response(readable, { // headers: { // "Content-Type": "text/event-stream", // "Cache-Control": "no-cache", // Connection: "keep-alive", // }, }); })
router.post("/v1/streaming", async (request, { OPENAI_API }) => { let { readable, writable } = new TransformStream(); const client = new ChatOpenAI({ openAIApiKey: OPENAI_API, }); const stream = await client.stream( "Write a 2 sentence poem about a sunset." ); let writer = writable.getWriter(); const textEncoder = new TextEncoder(); for await (const part of stream) { if (part.content.length) { // Assuming part.content is a string containing the text const words = part.content.split(" "); // Split the text into words for (const word of words) { const sseFormattedData = `data: ${word}\n\n`; // Format each word as SSE await writer.write(textEncoder.encode(sseFormattedData)); console.log("🚀 - forawait - sseFormattedData:", sseFormattedData); await new Promise((r) => setTimeout(r, 1000)); // Wait for 1 second before sending the next word } } } writer.close(); return new Response(readable, { // headers: { // "Content-Type": "text/event-stream", // "Cache-Control": "no-cache", // Connection: "keep-alive", // }, }); })
Recent Announcements
Continue the conversation
Join the Discord to ask follow-up questions and connect with the community
CD
Cloudflare Developers
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news