File size: 594 Bytes
ce7e38e
dd2ba48
ce7e38e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}