Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

How do I stream an OpenAI API response back from a Svelte server to deploy on cloudflare?

$
0
0

The svelte documentation says you can return a response to a post call in a +svelte.js file with

export async function POST({ request }) {    const { a, b } = await request.json();    return json(a + b);}

But it doesn't say how I can stream back a response. In my post call, I make a request to the OpenAI API and get back a streaming response.

export async function POST ({ request }) {    const completion = await openai.chat.completions.create({        messages: [{ role: "system", content: prompt }],        model: "gpt-3.5-turbo",        stream: true,    });    for await (const chunk of completion) {        console.log(chunk.choices[0].delta.content);    }

I receive the Open AI response piece by piece, and need to stream each "chunk.choices[0].delta.content" back to the page that made the fetch request.

Initially I wrote an express app in an index.js file that could communicate with the frontend page and stream a response back. With express I could just call res.write(data). However, express does not seem to work with deployment on cloudflare, so I have been trying to find an equivalent method using Svelte that will work with cloudflare.


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>