Spaces:
Runtime error
Runtime error
format
Browse files
src/lib/components/Conversation.svelte
CHANGED
|
@@ -9,7 +9,11 @@
|
|
| 9 |
export let viewCode;
|
| 10 |
export let sideBySide = false;
|
| 11 |
|
| 12 |
-
const dispatch = createEventDispatcher<{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
let messageContainer: HTMLDivElement | null = null;
|
| 15 |
|
|
|
|
| 9 |
export let viewCode;
|
| 10 |
export let sideBySide = false;
|
| 11 |
|
| 12 |
+
const dispatch = createEventDispatcher<{
|
| 13 |
+
addMessage: void;
|
| 14 |
+
deleteMessage: number;
|
| 15 |
+
deleteConversation: number;
|
| 16 |
+
}>();
|
| 17 |
|
| 18 |
let messageContainer: HTMLDivElement | null = null;
|
| 19 |
|
src/lib/components/Message.svelte
CHANGED
|
@@ -7,7 +7,10 @@
|
|
| 7 |
export let messageIdx: number;
|
| 8 |
export let autofocus: boolean = false;
|
| 9 |
|
| 10 |
-
const dispatch = createEventDispatcher<{
|
|
|
|
|
|
|
|
|
|
| 11 |
</script>
|
| 12 |
|
| 13 |
<div
|
|
@@ -19,7 +22,8 @@
|
|
| 19 |
<textarea
|
| 20 |
{autofocus}
|
| 21 |
value={message.content}
|
| 22 |
-
on:input={(e) =>
|
|
|
|
| 23 |
placeholder="Enter {message.role} message"
|
| 24 |
class="resize-none rounded bg-transparent px-2 py-2.5 ring-gray-100 [field-sizing:content] hover:resize-y hover:bg-white focus:resize-y focus:bg-white focus:ring group-hover/message:ring @2xl:px-3 dark:ring-gray-600 dark:hover:bg-gray-900 dark:focus:bg-gray-900"
|
| 25 |
rows="1"
|
|
|
|
| 7 |
export let messageIdx: number;
|
| 8 |
export let autofocus: boolean = false;
|
| 9 |
|
| 10 |
+
const dispatch = createEventDispatcher<{
|
| 11 |
+
delete: void;
|
| 12 |
+
messageValueChanged: { conversationIdx: number; messageIdx: number; value: string };
|
| 13 |
+
}>();
|
| 14 |
</script>
|
| 15 |
|
| 16 |
<div
|
|
|
|
| 22 |
<textarea
|
| 23 |
{autofocus}
|
| 24 |
value={message.content}
|
| 25 |
+
on:input={(e) =>
|
| 26 |
+
dispatch('messageValueChanged', { conversationIdx, messageIdx, value: e.target.value })}
|
| 27 |
placeholder="Enter {message.role} message"
|
| 28 |
class="resize-none rounded bg-transparent px-2 py-2.5 ring-gray-100 [field-sizing:content] hover:resize-y hover:bg-white focus:resize-y focus:bg-white focus:ring group-hover/message:ring @2xl:px-3 dark:ring-gray-600 dark:hover:bg-gray-900 dark:focus:bg-gray-900"
|
| 29 |
rows="1"
|
src/routes/+page.svelte
CHANGED
|
@@ -37,7 +37,7 @@
|
|
| 37 |
let loading = false;
|
| 38 |
let tokens = 0;
|
| 39 |
let latency = 0;
|
| 40 |
-
let abortControllers: AbortController[] = []
|
| 41 |
let waitForNonStreaming = true;
|
| 42 |
|
| 43 |
onMount(() => {
|
|
@@ -51,14 +51,14 @@
|
|
| 51 |
})();
|
| 52 |
|
| 53 |
return () => {
|
| 54 |
-
for(const abortController of abortControllers){
|
| 55 |
abortController.abort();
|
| 56 |
}
|
| 57 |
-
}
|
| 58 |
});
|
| 59 |
|
| 60 |
function addMessage() {
|
| 61 |
-
conversations = conversations.map(conversation => {
|
| 62 |
conversation.messages = [
|
| 63 |
...conversation.messages,
|
| 64 |
{
|
|
@@ -75,8 +75,8 @@
|
|
| 75 |
const msg = conversations[conversationIdx].messages[messageIdx];
|
| 76 |
msg.content = value;
|
| 77 |
const { role } = msg;
|
| 78 |
-
if(messageIdx === lastMsgIdx && role ===
|
| 79 |
-
conversations = conversations.map(conversation => {
|
| 80 |
conversation.messages[messageIdx].content = value;
|
| 81 |
return conversation;
|
| 82 |
});
|
|
@@ -92,11 +92,8 @@
|
|
| 92 |
}
|
| 93 |
|
| 94 |
function deleteMessage(idx: number) {
|
| 95 |
-
conversations = conversations.map(conversation => {
|
| 96 |
-
const deletedMsg = deleteAndGetItem<ChatCompletionInputMessage>(
|
| 97 |
-
conversation.messages,
|
| 98 |
-
idx
|
| 99 |
-
);
|
| 100 |
// delete messages in user/assistant pairs. otherwise, the chat template will be broken
|
| 101 |
if (deletedMsg) {
|
| 102 |
const { role } = deletedMsg;
|
|
@@ -114,7 +111,7 @@
|
|
| 114 |
|
| 115 |
function reset() {
|
| 116 |
systemMessage.content = '';
|
| 117 |
-
conversations = conversations.map(conversation => {
|
| 118 |
conversation.messages = [...startMessages];
|
| 119 |
return conversation;
|
| 120 |
});
|
|
@@ -122,7 +119,7 @@
|
|
| 122 |
|
| 123 |
function abort() {
|
| 124 |
if (abortControllers.length) {
|
| 125 |
-
for(const abortController of abortControllers){
|
| 126 |
abortController.abort();
|
| 127 |
}
|
| 128 |
abortControllers = [];
|
|
@@ -131,7 +128,7 @@
|
|
| 131 |
waitForNonStreaming = false;
|
| 132 |
}
|
| 133 |
|
| 134 |
-
async function runInference(conversation: Conversation){
|
| 135 |
const startTime = performance.now();
|
| 136 |
const hf = createHfInference(hfToken);
|
| 137 |
const requestMessages = prepareRequestMessages(systemMessage, conversation.messages);
|
|
@@ -140,7 +137,7 @@
|
|
| 140 |
const streamingMessage = { role: 'assistant', content: '' };
|
| 141 |
conversation.messages = [...conversation.messages, streamingMessage];
|
| 142 |
const abortController = new AbortController();
|
| 143 |
-
abortControllers.push(abortController)
|
| 144 |
|
| 145 |
await handleStreamingResponse(
|
| 146 |
hf,
|
|
@@ -192,11 +189,11 @@
|
|
| 192 |
(document.activeElement as HTMLElement).blur();
|
| 193 |
loading = true;
|
| 194 |
|
| 195 |
-
try{
|
| 196 |
-
const promises = conversations.map(conversation => runInference(conversation));
|
| 197 |
await Promise.all(promises);
|
| 198 |
addMessage();
|
| 199 |
-
}catch (error){
|
| 200 |
if (error.name !== 'AbortError') {
|
| 201 |
alert('error: ' + (error as Error).message);
|
| 202 |
}
|
|
@@ -266,7 +263,7 @@
|
|
| 266 |
sideBySide={conversations.length > 1}
|
| 267 |
on:addMessage={addMessage}
|
| 268 |
on:messageValueChanged={(e) => {
|
| 269 |
-
const {conversationIdx, messageIdx, value} = e.detail;
|
| 270 |
updateMessage(value, conversationIdx, messageIdx);
|
| 271 |
}}
|
| 272 |
on:deleteMessage={(e) => deleteMessage(e.detail)}
|
|
|
|
| 37 |
let loading = false;
|
| 38 |
let tokens = 0;
|
| 39 |
let latency = 0;
|
| 40 |
+
let abortControllers: AbortController[] = [];
|
| 41 |
let waitForNonStreaming = true;
|
| 42 |
|
| 43 |
onMount(() => {
|
|
|
|
| 51 |
})();
|
| 52 |
|
| 53 |
return () => {
|
| 54 |
+
for (const abortController of abortControllers) {
|
| 55 |
abortController.abort();
|
| 56 |
}
|
| 57 |
+
};
|
| 58 |
});
|
| 59 |
|
| 60 |
function addMessage() {
|
| 61 |
+
conversations = conversations.map((conversation) => {
|
| 62 |
conversation.messages = [
|
| 63 |
...conversation.messages,
|
| 64 |
{
|
|
|
|
| 75 |
const msg = conversations[conversationIdx].messages[messageIdx];
|
| 76 |
msg.content = value;
|
| 77 |
const { role } = msg;
|
| 78 |
+
if (messageIdx === lastMsgIdx && role === 'user') {
|
| 79 |
+
conversations = conversations.map((conversation) => {
|
| 80 |
conversation.messages[messageIdx].content = value;
|
| 81 |
return conversation;
|
| 82 |
});
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
function deleteMessage(idx: number) {
|
| 95 |
+
conversations = conversations.map((conversation) => {
|
| 96 |
+
const deletedMsg = deleteAndGetItem<ChatCompletionInputMessage>(conversation.messages, idx);
|
|
|
|
|
|
|
|
|
|
| 97 |
// delete messages in user/assistant pairs. otherwise, the chat template will be broken
|
| 98 |
if (deletedMsg) {
|
| 99 |
const { role } = deletedMsg;
|
|
|
|
| 111 |
|
| 112 |
function reset() {
|
| 113 |
systemMessage.content = '';
|
| 114 |
+
conversations = conversations.map((conversation) => {
|
| 115 |
conversation.messages = [...startMessages];
|
| 116 |
return conversation;
|
| 117 |
});
|
|
|
|
| 119 |
|
| 120 |
function abort() {
|
| 121 |
if (abortControllers.length) {
|
| 122 |
+
for (const abortController of abortControllers) {
|
| 123 |
abortController.abort();
|
| 124 |
}
|
| 125 |
abortControllers = [];
|
|
|
|
| 128 |
waitForNonStreaming = false;
|
| 129 |
}
|
| 130 |
|
| 131 |
+
async function runInference(conversation: Conversation) {
|
| 132 |
const startTime = performance.now();
|
| 133 |
const hf = createHfInference(hfToken);
|
| 134 |
const requestMessages = prepareRequestMessages(systemMessage, conversation.messages);
|
|
|
|
| 137 |
const streamingMessage = { role: 'assistant', content: '' };
|
| 138 |
conversation.messages = [...conversation.messages, streamingMessage];
|
| 139 |
const abortController = new AbortController();
|
| 140 |
+
abortControllers.push(abortController);
|
| 141 |
|
| 142 |
await handleStreamingResponse(
|
| 143 |
hf,
|
|
|
|
| 189 |
(document.activeElement as HTMLElement).blur();
|
| 190 |
loading = true;
|
| 191 |
|
| 192 |
+
try {
|
| 193 |
+
const promises = conversations.map((conversation) => runInference(conversation));
|
| 194 |
await Promise.all(promises);
|
| 195 |
addMessage();
|
| 196 |
+
} catch (error) {
|
| 197 |
if (error.name !== 'AbortError') {
|
| 198 |
alert('error: ' + (error as Error).message);
|
| 199 |
}
|
|
|
|
| 263 |
sideBySide={conversations.length > 1}
|
| 264 |
on:addMessage={addMessage}
|
| 265 |
on:messageValueChanged={(e) => {
|
| 266 |
+
const { conversationIdx, messageIdx, value } = e.detail;
|
| 267 |
updateMessage(value, conversationIdx, messageIdx);
|
| 268 |
}}
|
| 269 |
on:deleteMessage={(e) => deleteMessage(e.detail)}
|