nsarrazin HF staff commited on
Commit
00443e1
1 Parent(s): 482a094

Pass websearch to `preprocessMessages` (#876)

Browse files

There was a bug where the websearch was fetching relevant chunks for
context correctly but it wasn't getting injected into the prompt, this
PR fixes that.

src/lib/server/preprocessMessages.ts CHANGED
@@ -5,29 +5,28 @@ import { downloadFile } from "./files/downloadFile";
5
 
6
  export async function preprocessMessages(
7
  messages: Message[],
 
8
  multimodal: boolean,
9
  id: Conversation["_id"]
10
  ): Promise<Message[]> {
11
  return await Promise.all(
12
- messages.map(async (message, idx) => {
13
  // start by adding websearch to the last message
14
- if (idx === messages.length - 1 && message.webSearch && message.webSearch.context) {
15
- const lastUsrMsgIndex = messages.map((el) => el.from).lastIndexOf("user");
16
- const previousUserMessages = messages.filter((el) => el.from === "user").slice(0, -1);
17
- const previousQuestions =
18
- previousUserMessages.length > 0
19
- ? `Previous questions: \n${previousUserMessages
20
- .map(({ content }) => `- ${content}`)
21
- .join("\n")}`
22
- : "";
23
  const currentDate = format(new Date(), "MMMM d, yyyy");
24
 
25
- message.content = `I searched the web using the query: ${message.webSearch.searchQuery}. Today is ${currentDate} and here are the results:
 
26
  =====================
27
- ${message.webSearch.context}
28
  =====================
29
- ${previousQuestions}
30
- Answer the question: ${messages[lastUsrMsgIndex].content}`;
31
  }
32
  // handle files if model is multimodal
33
  if (multimodal) {
 
5
 
6
  export async function preprocessMessages(
7
  messages: Message[],
8
+ webSearch: Message["webSearch"],
9
  multimodal: boolean,
10
  id: Conversation["_id"]
11
  ): Promise<Message[]> {
12
  return await Promise.all(
13
+ structuredClone(messages).map(async (message, idx) => {
14
  // start by adding websearch to the last message
15
+ if (idx === messages.length - 1 && webSearch && webSearch.context) {
16
+ const lastQuestion = messages.findLast((el) => el.from === "user")?.content ?? "";
17
+ const previousQuestions = messages
18
+ .filter((el) => el.from === "user")
19
+ .slice(0, -1)
20
+ .map((el) => el.content);
 
 
 
21
  const currentDate = format(new Date(), "MMMM d, yyyy");
22
 
23
+ message.content = `I searched the web using the query: ${webSearch.searchQuery}.
24
+ Today is ${currentDate} and here are the results:
25
  =====================
26
+ ${webSearch.context}
27
  =====================
28
+ ${previousQuestions.length > 0 ? `Previous questions: \n- ${previousQuestions.join("\n- ")}` : ""}
29
+ Answer the question: ${lastQuestion}`;
30
  }
31
  // handle files if model is multimodal
32
  if (multimodal) {
src/routes/conversation/[id]/+server.ts CHANGED
@@ -324,6 +324,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
324
  // inject websearch result & optionally images into the messages
325
  const processedMessages = await preprocessMessages(
326
  messagesForPrompt,
 
327
  model.multimodal,
328
  convId
329
  );
 
324
  // inject websearch result & optionally images into the messages
325
  const processedMessages = await preprocessMessages(
326
  messagesForPrompt,
327
+ messageToWriteTo.webSearch,
328
  model.multimodal,
329
  convId
330
  );