|
1 | 1 | import type { BackendModel } from "./server/models";
|
2 | 2 | import type { Message } from "./types/Message";
|
3 |
| -import { format } from "date-fns"; |
4 |
| -import type { WebSearch } from "./types/WebSearch"; |
5 |
| -import type { PdfSearch } from "./types/PdfChat"; |
6 | 3 | import { downloadImgFile } from "./server/files/downloadFile";
|
7 | 4 | import type { Conversation } from "./types/Conversation";
|
| 5 | +import RAGs from "./server/rag/rag"; |
| 6 | +import type { RagContext } from "./types/rag"; |
| 7 | + |
| 8 | +export type BuildPromptMessage = Pick<Message, "from" | "content" | "files">; |
8 | 9 |
|
9 | 10 | interface buildPromptOptions {
|
10 |
| - messages: Pick<Message, "from" | "content" | "files">[]; |
| 11 | + messages: BuildPromptMessage[]; |
11 | 12 | id?: Conversation["_id"];
|
12 | 13 | model: BackendModel;
|
13 | 14 | locals?: App.Locals;
|
14 |
| - webSearch?: WebSearch; |
15 |
| - pdfSearch?: PdfSearch; |
| 15 | + ragContext?: RagContext; |
16 | 16 | preprompt?: string;
|
17 | 17 | files?: File[];
|
18 | 18 | }
|
19 | 19 |
|
20 | 20 | export async function buildPrompt({
|
21 | 21 | messages,
|
22 | 22 | model,
|
23 |
| - webSearch, |
24 |
| - pdfSearch, |
| 23 | + ragContext, |
25 | 24 | preprompt,
|
26 | 25 | id,
|
27 | 26 | }: buildPromptOptions): Promise<string> {
|
28 |
| - if (webSearch && webSearch.context) { |
29 |
| - const lastMsg = messages.slice(-1)[0]; |
30 |
| - const messagesWithoutLastUsrMsg = messages.slice(0, -1); |
31 |
| - const previousUserMessages = messages.filter((el) => el.from === "user").slice(0, -1); |
32 |
| - |
33 |
| - const previousQuestions = |
34 |
| - previousUserMessages.length > 0 |
35 |
| - ? `Previous questions: \n${previousUserMessages |
36 |
| - .map(({ content }) => `- ${content}`) |
37 |
| - .join("\n")}` |
38 |
| - : ""; |
39 |
| - const currentDate = format(new Date(), "MMMM d, yyyy"); |
40 |
| - messages = [ |
41 |
| - ...messagesWithoutLastUsrMsg, |
42 |
| - { |
43 |
| - from: "user", |
44 |
| - content: `I searched the web using the query: ${webSearch.searchQuery}. Today is ${currentDate} and here are the results: |
45 |
| - ===================== |
46 |
| - ${webSearch.context} |
47 |
| - ===================== |
48 |
| - ${previousQuestions} |
49 |
| - Answer the question: ${lastMsg.content} |
50 |
| - `, |
51 |
| - }, |
52 |
| - ]; |
53 |
| - } else if (pdfSearch && pdfSearch.context) { |
54 |
| - const lastMsg = messages.slice(-1)[0]; |
55 |
| - const messagesWithoutLastUsrMsg = messages.slice(0, -1); |
56 |
| - const previousUserMessages = messages.filter((el) => el.from === "user").slice(0, -1); |
57 |
| - |
58 |
| - const previousQuestions = |
59 |
| - previousUserMessages.length > 0 |
60 |
| - ? `Previous questions: \n${previousUserMessages |
61 |
| - .map(({ content }) => `- ${content}`) |
62 |
| - .join("\n")}` |
63 |
| - : ""; |
64 |
| - |
65 |
| - messages = [ |
66 |
| - ...messagesWithoutLastUsrMsg, |
67 |
| - { |
68 |
| - from: "user", |
69 |
| - content: `Below are the information I extracted from a PDF file that might be useful: |
70 |
| - ===================== |
71 |
| - ${pdfSearch.context} |
72 |
| - ===================== |
73 |
| - ${previousQuestions} |
74 |
| - Answer the question: ${lastMsg.content} |
75 |
| - `, |
76 |
| - }, |
77 |
| - ]; |
| 27 | + if (ragContext) { |
| 28 | + const { type: ragType } = ragContext; |
| 29 | + messages = RAGs[ragType].buildPrompt(messages, ragContext); |
78 | 30 | }
|
79 | 31 |
|
80 | 32 | // section to handle potential files input
|
|
0 commit comments