File size: 2,795 Bytes
e6addfc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import type { Conversation } from "$lib/types/Conversation";
import type { Message } from "$lib/types/Message";
import { format } from "date-fns";
import { downloadFile } from "./files/downloadFile";
export async function preprocessMessages(
messages: Message[],
multimodal: boolean,
id: Conversation["_id"]
): Promise<Message[]> {
return await Promise.all(
messages.map(async (message, idx) => {
// start by adding websearch to the last message
if (idx === messages.length - 1 && message.webSearch && message.webSearch.context) {
const lastUsrMsgIndex = messages.map((el) => el.from).lastIndexOf("user");
const previousUserMessages = messages.filter((el) => el.from === "user").slice(0, -1);
const previousQuestions =
previousUserMessages.length > 0
? `Previous questions: \n${previousUserMessages
.map(({ content }) => `- ${content}`)
.join("\n")}`
: "";
const currentDate = format(new Date(), "MMMM d, yyyy");
message.content = `I searched the web using the query: ${message.webSearch.searchQuery}. Today is ${currentDate} and here are the results:
=====================
${message.webSearch.context}
=====================
${previousQuestions}
Answer the question: ${messages[lastUsrMsgIndex].content}`;
}
// handle files if model is multimodal
if (multimodal) {
if (message.files && message.files.length > 0) {
const markdowns = await Promise.all(
message.files.map(async (hash) => {
try {
const { content: image, mime } = await downloadFile(hash, id);
const b64 = image.toString("base64");
return `})`;
} catch (e) {
console.error(e);
}
})
);
message.content += markdowns.join("\n ");
} else {
// if no image, append an empty white image
message.content +=
"\n";
}
}
return message;
})
);
}
|