From 6c7c35ad12b716bed85b890cb72653fe6bf8f71d Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 4 Feb 2024 00:29:40 -0800 Subject: [PATCH] feat: include only last images --- src/routes/(app)/+page.svelte | 56 +++++++++++++++++--------- src/routes/(app)/c/[id]/+page.svelte | 59 +++++++++++++++++++--------- 2 files changed, 77 insertions(+), 38 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 376c4e37..8b16ade0 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -316,27 +316,45 @@ // Scroll down window.scrollTo({ top: document.body.scrollHeight }); + const messagesBody = [ + $settings.system + ? { + role: 'system', + content: $settings.system + } + : undefined, + ...messages + ] + .filter((message) => message) + .map((message, idx, arr) => ({ + role: message.role, + content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, + ...(message.files && { + images: message.files + .filter((file) => file.type === 'image') + .map((file) => file.url.slice(file.url.indexOf(',') + 1)) + }) + })); + + let lastImageIndex = -1; + + // Find the index of the last object with images + messagesBody.forEach((item, index) => { + if (item.images) { + lastImageIndex = index; + } + }); + + // Remove images from all but the last one + messagesBody.forEach((item, index) => { + if (index !== lastImageIndex) { + delete item.images; + } + }); + const [res, controller] = await generateChatCompletion(localStorage.token, { model: model, - messages: [ - $settings.system - ? { - role: 'system', - content: $settings.system - } - : undefined, - ...messages - ] - .filter((message) => message) - .map((message, idx, arr) => ({ - role: message.role, - content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, - ...(message.files && { - images: message.files - .filter((file) => file.type === 'image') - .map((file) => file.url.slice(file.url.indexOf(',') + 1)) - }) - })), + messages: messagesBody, options: { ...($settings.options ?? {}) }, diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 83e72c62..60eb7cec 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -330,27 +330,45 @@ // Scroll down window.scrollTo({ top: document.body.scrollHeight }); + const messagesBody = [ + $settings.system + ? { + role: 'system', + content: $settings.system + } + : undefined, + ...messages + ] + .filter((message) => message) + .map((message, idx, arr) => ({ + role: message.role, + content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, + ...(message.files && { + images: message.files + .filter((file) => file.type === 'image') + .map((file) => file.url.slice(file.url.indexOf(',') + 1)) + }) + })); + + let lastImageIndex = -1; + + // Find the index of the last object with images + messagesBody.forEach((item, index) => { + if (item.images) { + lastImageIndex = index; + } + }); + + // Remove images from all but the last one + messagesBody.forEach((item, index) => { + if (index !== lastImageIndex) { + delete item.images; + } + }); + const [res, controller] = await generateChatCompletion(localStorage.token, { model: model, - messages: [ - $settings.system - ? { - role: 'system', - content: $settings.system - } - : undefined, - ...messages - ] - .filter((message) => message) - .map((message, idx, arr) => ({ - role: message.role, - content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, - ...(message.files && { - images: message.files - .filter((file) => file.type === 'image') - .map((file) => file.url.slice(file.url.indexOf(',') + 1)) - }) - })), + messages: messagesBody, options: { ...($settings.options ?? {}) }, @@ -358,6 +376,8 @@ }); if (res && res.ok) { + console.log('controller', controller); + const reader = res.body .pipeThrough(new TextDecoderStream()) .pipeThrough(splitStream('\n')) @@ -375,6 +395,7 @@ } currentRequestId = null; + break; }