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 lines737475767776777879808182838485868788899091929314 unmodified lines10810911011111211311411511611711846 unmodified lines16516616716816917017117272 unmodified lines conversations = conversations; }
function deleteMessage(i: number) { currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i); 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 broken if(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 user if(currentConversation.messages?.at(-1)?.role !== "user"){ addMessage(); return; } if (!hfToken) { showTokenModal = true; return;46 unmodified lines scrollToBottom(); } }
addMessage(); } catch (error) { if (error.name !== 'AbortError') { alert('error: ' + (error as Error).message);src/lib/components/Playground/Playground.svelte+22-2