Spaces:
Sleeping
Sleeping
Update src/api.js
Browse files- src/api.js +31 -0
src/api.js
CHANGED
|
@@ -61,3 +61,34 @@ export async function sendChat(message, strictMode = true, history = [], token)
|
|
| 61 |
|
| 62 |
return json;
|
| 63 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
return json;
|
| 63 |
}
|
| 64 |
+
|
| 65 |
+
export async function streamChat(message, strictMode, history, token, onToken) {
|
| 66 |
+
const res = await fetch(`${API_BASE}/chat/stream`, {
|
| 67 |
+
method: "POST",
|
| 68 |
+
headers: {
|
| 69 |
+
"Content-Type": "application/json",
|
| 70 |
+
"Authorization": `Bearer ${token}`
|
| 71 |
+
},
|
| 72 |
+
body: JSON.stringify({
|
| 73 |
+
message,
|
| 74 |
+
strict_mode: strictMode,
|
| 75 |
+
history
|
| 76 |
+
}),
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
if (!res.body) {
|
| 80 |
+
throw new Error("Streaming not supported by server");
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
const reader = res.body.getReader();
|
| 84 |
+
const decoder = new TextDecoder();
|
| 85 |
+
|
| 86 |
+
while (true) {
|
| 87 |
+
const { value, done } = await reader.read();
|
| 88 |
+
if (done) break;
|
| 89 |
+
|
| 90 |
+
const chunk = decoder.decode(value);
|
| 91 |
+
onToken(chunk);
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|