Replies: 3 comments 1 reply
-
Hi, have you figured it out by any chance? It looks like the response is actually in the SSE format, intentionally prepended with the 'data: '. I assume I can strip it from the string but I don't understand why I have to do that instead of actually getting a valid object directly from the model. |
Beta Was this translation helpful? Give feedback.
-
Did anyone find a way to handle this? |
Beta Was this translation helpful? Give feedback.
-
@Levelleor @osseonews A bit late, but I figured out a way to hack it.. Just respond with: const responseStream = await ai.run('@cf/google/gemma-2b-it-lora', {
stream: true,
messages: messages }
});
return new Response(responseStream as ReadableStream, {
headers: {
"content-type": "text/event-stream",
},
}); Now you can consume it on the client side (for me it's a Nuxt App) like this: const url = "/api/test";
const source = new EventSource(url, null);
source.onmessage = (event) => {
if (event.data === "[DONE]") {
source.close();
return;
}
const data = JSON.parse(event.data)
// console.log(data.response);
quote.value += data.response;
} Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
I am trying to stream responses with the Cloudflare AI, using their starter example here: https://developers.cloudflare.com/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai/
My basic code is below. The problem is that I can stream the message, but the browser shows the entire response like this:
data: {"response":" ","p":"abcdefghijklmnopqrstuvwxyz01234567"} data: {"response":" Based","p":"abcdefghijklmnopqrstuvwxyz0123456789abcde"}
Obviously, the user just needs to see the response. But, how do you stream that? Here is my code:
Beta Was this translation helpful? Give feedback.
All reactions