:boom: [Fix] Truncated json chunks of response
Browse files- networks/llm_requester.js +17 -6
networks/llm_requester.js
CHANGED
@@ -74,17 +74,28 @@ export class ChatCompletionsRequester {
|
|
74 |
create_messager("assistant", "", this.model, this.temperature);
|
75 |
}
|
76 |
async handle_read_stream_data(reader) {
|
|
|
77 |
while (true) {
|
78 |
const { done, value } = await reader.read();
|
79 |
-
if (done) {
|
80 |
break;
|
81 |
}
|
82 |
-
|
83 |
-
let
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
}
|
87 |
-
update_message(json_chunks, this.content_displayer_updater);
|
88 |
}
|
89 |
}
|
90 |
async post() {
|
|
|
74 |
create_messager("assistant", "", this.model, this.temperature);
|
75 |
}
|
76 |
async handle_read_stream_data(reader) {
|
77 |
+
let buffer = "";
|
78 |
while (true) {
|
79 |
const { done, value } = await reader.read();
|
80 |
+
if (done && buffer.length === 0) {
|
81 |
break;
|
82 |
}
|
83 |
+
buffer += done ? "" : stringify_stream_bytes(value);
|
84 |
+
let new_line_index;
|
85 |
+
while ((new_line_index = buffer.indexOf("\n")) !== -1) {
|
86 |
+
let json_line = buffer.slice(0, new_line_index);
|
87 |
+
buffer = buffer.slice(new_line_index + 1);
|
88 |
+
try {
|
89 |
+
let json_chunks = jsonize_stream_data(json_line);
|
90 |
+
if (!this.content_displayer_updater) {
|
91 |
+
this.content_displayer_updater =
|
92 |
+
new ContentDisplayerUpdater();
|
93 |
+
}
|
94 |
+
update_message(json_chunks, this.content_displayer_updater);
|
95 |
+
} catch (e) {
|
96 |
+
console.warn("Invalid JSON:", json_line);
|
97 |
+
}
|
98 |
}
|
|
|
99 |
}
|
100 |
}
|
101 |
async post() {
|