Spaces:
Running
Running
| export async function streamChat({ message, history = [], onChunk, onDone }) { | |
| const res = await fetch("https://fredericksundeep-aichatmatedev.hf.space/chat-stream", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ message, history }), | |
| }); | |
| const reader = res.body.getReader(); | |
| const decoder = new TextDecoder(); | |
| let fullText = ""; | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) break; | |
| const chunk = decoder.decode(value); | |
| fullText += chunk; | |
| onChunk(chunk); | |
| } | |
| onDone(fullText); | |
| } | |