combine assistant messages
Browse files
src/lib/components/InferencePlayground/InferencePlayground.svelte
CHANGED
@@ -92,18 +92,29 @@
|
|
92 |
|
93 |
if (conversation.streaming) {
|
94 |
const streamingMessage = { role: "assistant", content: "" };
|
95 |
-
conversation.messages
|
96 |
abortController = new AbortController();
|
|
|
|
|
97 |
|
98 |
await handleStreamingResponse(
|
99 |
hf,
|
100 |
conversation,
|
101 |
content => {
|
102 |
-
if (
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
106 |
}
|
|
|
|
|
|
|
|
|
107 |
},
|
108 |
abortController
|
109 |
);
|
|
|
92 |
|
93 |
if (conversation.streaming) {
|
94 |
const streamingMessage = { role: "assistant", content: "" };
|
95 |
+
conversation.messages.push(streamingMessage);
|
96 |
abortController = new AbortController();
|
97 |
+
let firstChunk = true;
|
98 |
+
let previousContent = "";
|
99 |
|
100 |
await handleStreamingResponse(
|
101 |
hf,
|
102 |
conversation,
|
103 |
content => {
|
104 |
+
if (firstChunk && conversation.messages.at(-2)?.role === "assistant") {
|
105 |
+
// if last two messages are both "assistant" messages, then combine the contexts
|
106 |
+
const streamingMessage = conversation.messages.pop();
|
107 |
+
const { content } = conversation.messages.pop();
|
108 |
+
previousContent = content;
|
109 |
+
if (previousContent && !/\s$/.test(previousContent)) {
|
110 |
+
previousContent += " ";
|
111 |
+
}
|
112 |
+
conversation.messages = [...conversation.messages, streamingMessage];
|
113 |
}
|
114 |
+
conversation.messages.at(-1).content = previousContent ? `${previousContent}${content}` : content;
|
115 |
+
conversation.messages = [...conversation.messages];
|
116 |
+
generatedTokensCount += 1;
|
117 |
+
firstChunk = false;
|
118 |
},
|
119 |
abortController
|
120 |
);
|