Spaces:
Sleeping
Sleeping
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
72 unmodified lines737475767778798014 unmodified lines959697989910046 unmodified lines14714814915015115272 unmodified linesconversations = conversations;}function deleteMessage(i: number) {currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i);conversations = conversations;}14 unmodified lines}async function submit() {if (!hfToken) {showTokenModal = true;return;46 unmodified linesscrollToBottom();}}} catch (error) {if (error.name !== 'AbortError') {alert('error: ' + (error as Error).message);72 unmodified lines73747576777879808182838485868788899091929314 unmodified lines10810911011111211311411511611711846 unmodified lines16516616716816917017117272 unmodified linesconversations = conversations;}function deleteAndGetItem<T>(array: T[], index: number) {if (index >= 0 && index < array.length) {return array.splice(index, 1)[0];}return undefined;}function deleteMessage(idx: number) {const deletedMsg = deleteAndGetItem<ChatCompletionInputMessage>(currentConversation.messages, idx);// delete messages in user/assistant pairs. otherwise, the chat template will be brokenif(deletedMsg){const { role } = deletedMsg;const pairIdx = role === "user" ? idx : idx - 1;deleteAndGetItem<ChatCompletionInputMessage>(currentConversation.messages, pairIdx);}conversations = conversations;}14 unmodified lines}async function submit() {// last message has to be from userif(currentConversation.messages?.at(-1)?.role !== "user"){addMessage();return;}if (!hfToken) {showTokenModal = true;return;46 unmodified linesscrollToBottom();}}addMessage();} catch (error) {if (error.name !== 'AbortError') {alert('error: ' + (error as Error).message);
src/lib/components/Playground/Playground.svelte+22-2