Spaces:
Running
Running
Improve messages UX (#5)
Browse files# Improve messages UX
When sending messages to an assistant, the messages has to alternate in
user/assistant pairs. In the inference-playground, we allow editing or
deleting messages, which can break this "alternate in user/assistant
pairs" format. This PR fixes the issue
src/lib/components/Playground/Playground.svelte
CHANGED
@@ -73,8 +73,21 @@
|
|
73 |
conversations = conversations;
|
74 |
}
|
75 |
|
76 |
-
function
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
conversations = conversations;
|
79 |
}
|
80 |
|
@@ -95,6 +108,11 @@
|
|
95 |
}
|
96 |
|
97 |
async function submit() {
|
|
|
|
|
|
|
|
|
|
|
98 |
if (!hfToken) {
|
99 |
showTokenModal = true;
|
100 |
return;
|
@@ -147,6 +165,8 @@
|
|
147 |
scrollToBottom();
|
148 |
}
|
149 |
}
|
|
|
|
|
150 |
} catch (error) {
|
151 |
if (error.name !== 'AbortError') {
|
152 |
alert('error: ' + (error as Error).message);
|
|
|
73 |
conversations = conversations;
|
74 |
}
|
75 |
|
76 |
+
function deleteAndGetItem<T>(array: T[], index: number) {
|
77 |
+
if (index >= 0 && index < array.length) {
|
78 |
+
return array.splice(index, 1)[0];
|
79 |
+
}
|
80 |
+
return undefined;
|
81 |
+
}
|
82 |
+
|
83 |
+
function deleteMessage(idx: number) {
|
84 |
+
const deletedMsg = deleteAndGetItem<ChatCompletionInputMessage>(currentConversation.messages, idx);
|
85 |
+
// delete messages in user/assistant pairs. otherwise, the chat template will be broken
|
86 |
+
if(deletedMsg){
|
87 |
+
const { role } = deletedMsg;
|
88 |
+
const pairIdx = role === "user" ? idx : idx - 1;
|
89 |
+
deleteAndGetItem<ChatCompletionInputMessage>(currentConversation.messages, pairIdx);
|
90 |
+
}
|
91 |
conversations = conversations;
|
92 |
}
|
93 |
|
|
|
108 |
}
|
109 |
|
110 |
async function submit() {
|
111 |
+
// last message has to be from user
|
112 |
+
if(currentConversation.messages?.at(-1)?.role !== "user"){
|
113 |
+
addMessage();
|
114 |
+
return;
|
115 |
+
}
|
116 |
if (!hfToken) {
|
117 |
showTokenModal = true;
|
118 |
return;
|
|
|
165 |
scrollToBottom();
|
166 |
}
|
167 |
}
|
168 |
+
|
169 |
+
addMessage();
|
170 |
} catch (error) {
|
171 |
if (error.name !== 'AbortError') {
|
172 |
alert('error: ' + (error as Error).message);
|