forked from mongodb/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeRagGenerateUserPrompt.ts
214 lines (198 loc) · 6.34 KB
/
makeRagGenerateUserPrompt.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { stripIndents } from "common-tags";
import { FindContentFunc } from "../routes";
import { GenerateUserPromptFunc } from "./GenerateUserPromptFunc";
import { QueryPreprocessorFunc } from "./QueryPreprocessorFunc";
import { logRequest } from "../utils";
import { Conversation, UserMessage } from "../services";
import { EmbeddedContent } from "mongodb-rag-core";
import { MakeReferenceLinksFunc } from "./MakeReferenceLinksFunc";
import { makeDefaultReferenceLinks } from "./makeDefaultReferenceLinks";
export interface MakeRagGenerateUserPromptParams {
/**
Transform the user's message before sending it to the `findContent` function.
*/
queryPreprocessor?: QueryPreprocessorFunc;
/**
Find content based on the user's message and preprocessing.
*/
findContent: FindContentFunc;
/**
If not specified, uses {@link makeDefaultReferenceLinks}.
*/
makeReferenceLinks?: MakeReferenceLinksFunc;
/**
Number of tokens from the found context to send to the `makeUserMessage` function.
All chunks that exceed this threshold are discarded.
If not specified, uses {@link DEFAULT_MAX_CONTEXT_TOKENS}.
*/
maxChunkContextTokens?: number;
/**
Construct user message which is sent to the LLM and stored in the database.
*/
makeUserMessage: MakeUserMessageFunc;
}
export interface MakeUserMessageFuncParams {
content: EmbeddedContent[];
originalUserMessage: string;
preprocessedUserMessage?: string;
queryEmbedding?: number[];
rejectQuery?: boolean;
}
export type MakeUserMessageFunc = (
params: MakeUserMessageFuncParams
) => Promise<UserMessage>;
const DEFAULT_MAX_CONTEXT_TOKENS = 1500; // magic number for max context tokens for LLM
/**
Construct a {@link GenerateUserPromptFunc} function
that uses retrieval augmented generation (RAG) to generate the user prompt
and return references to use in the answer.
The returned RAG user prompt generator performs the following steps:
1. Preprocess the user's message using the query preprocessor.
2. Find content using vector search.
3. Removes any chunks that would exceed the max context tokens.
4. Generate the user message using the make user message function.
5. Return the user message and references. */
export function makeRagGenerateUserPrompt({
queryPreprocessor,
findContent,
makeReferenceLinks = makeDefaultReferenceLinks,
maxChunkContextTokens = DEFAULT_MAX_CONTEXT_TOKENS,
makeUserMessage,
}: MakeRagGenerateUserPromptParams): GenerateUserPromptFunc {
return async ({ userMessageText, conversation, reqId }) => {
// --- PREPROCESS ---
const preprocessResult = preProcessUserMessage
? await preProcessUserMessage({
queryPreprocessor,
userMessageText,
conversation,
reqId,
})
: undefined;
const { rejectQuery, query: preprocessedUserMessageContent } =
preprocessResult ?? {
rejectQuery: false,
query: userMessageText,
};
if (rejectQuery) {
logRequest({
reqId,
message: "Preprocessor rejected query",
});
return {
rejectQuery: true,
userMessage: { role: "user", content: userMessageText },
};
}
// --- VECTOR SEARCH / RETRIEVAL ---
const findContentQuery = preprocessedUserMessageContent ?? userMessageText;
const { content, queryEmbedding } = await findContent({
query: findContentQuery,
});
if (content.length === 0) {
logRequest({
reqId,
message: "No matching content found",
});
return {
userMessage: {
role: "user",
content: userMessageText,
embedding: queryEmbedding,
},
rejectQuery: true,
};
}
logRequest({
reqId,
message: stripIndents`Chunks found: ${JSON.stringify(
content.map(
({ embedding, chunkAlgoHash, ...wantedProperties }) =>
wantedProperties
)
)}`,
});
const references = makeReferenceLinks(content);
const includedContent = includeChunksForMaxTokensPossible({
maxTokens: maxChunkContextTokens,
content,
});
const userMessage = await makeUserMessage({
content: includedContent,
originalUserMessage: userMessageText,
preprocessedUserMessage: preprocessedUserMessageContent,
queryEmbedding,
rejectQuery,
});
logRequest({
reqId,
message: `Latest message sent to LLM: ${JSON.stringify({
role: userMessage.role,
content: userMessage.content,
})}`,
});
return {
userMessage,
references,
rejectQuery: false,
};
};
}
interface PreProcessUserMessageParams {
queryPreprocessor?: QueryPreprocessorFunc;
userMessageText: string;
conversation?: Conversation;
reqId: string;
}
async function preProcessUserMessage({
queryPreprocessor,
userMessageText,
conversation,
reqId,
}: PreProcessUserMessageParams): Promise<
{ query: string; rejectQuery?: boolean } | undefined
> {
// Try to preprocess the user's message. If the user's message cannot be preprocessed
// (likely due to LLM timeout), then we will just use the original message.
if (!queryPreprocessor) {
return undefined;
}
try {
const { query, rejectQuery } = await queryPreprocessor({
query: userMessageText,
messages: conversation?.messages,
});
logRequest({
reqId,
message: stripIndents`Successfully preprocessed user query.
Original query: ${userMessageText}
Preprocessed query: ${query}`,
});
return { query: query ?? userMessageText, rejectQuery };
} catch (err: unknown) {
logRequest({
reqId,
type: "error",
message: `Error preprocessing query: ${JSON.stringify(
err
)}. Using original query: ${userMessageText}`,
});
}
}
/**
This function returns the chunks that can fit in the maxTokens.
It limits the number of tokens that are sent to the LLM.
*/
export function includeChunksForMaxTokensPossible({
maxTokens,
content,
}: {
maxTokens: number;
content: EmbeddedContent[];
}): EmbeddedContent[] {
let total = 0;
const fitRangeEndIndex = content.findIndex(
({ tokenCount }) => (total += tokenCount) > maxTokens
);
return fitRangeEndIndex === -1 ? content : content.slice(0, fitRangeEndIndex);
}