File size: 1,674 Bytes
2606dde
5da61b4
0c4cf03
 
ad02fa3
 
 
 
 
0c4cf03
 
2606dde
0c4cf03
 
 
f209301
 
 
3c650ed
ad02fa3
f209301
 
 
 
 
 
 
 
ad02fa3
2606dde
3c650ed
0c4cf03
 
 
 
 
 
 
 
 
 
 
 
 
f209301
0c4cf03
 
 
b7b2c8c
0c4cf03
b7b2c8c
 
 
0c4cf03
 
 
 
ad02fa3
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
import type { BackendModel } from "./server/models";
import type { Message } from "./types/Message";
import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
/**
 * Convert [{user: "assistant", content: "hi"}, {user: "user", content: "hello"}] to:
 *
 * <|assistant|>hi<|endoftext|><|prompter|>hello<|endoftext|><|assistant|>
 */

export async function buildPrompt(
	messages: Pick<Message, "from" | "content">[],
	model: BackendModel,
	webSearchId?: string
): Promise<string> {
	const userEndToken = model.userMessageEndToken ?? model.messageEndToken;
	const assistantEndToken = model.assistantMessageEndToken ?? model.messageEndToken;

	const prompt =
		messages
			.map((m) =>
				m.from === "user"
					? model.userMessageToken +
					  m.content +
					  (m.content.endsWith(userEndToken) ? "" : userEndToken)
					: model.assistantMessageToken +
					  m.content +
					  (m.content.endsWith(assistantEndToken) ? "" : assistantEndToken)
			)
			.join("") + model.assistantMessageToken;

	let webPrompt = "";

	if (webSearchId) {
		const webSearch = await collections.webSearches.findOne({
			_id: new ObjectId(webSearchId),
		});

		if (!webSearch) throw new Error("Web search not found");

		if (webSearch.summary) {
			webPrompt =
				model.assistantMessageToken +
				`The following context was found while searching the internet: ${webSearch.summary}` +
				model.assistantMessageEndToken;
		}
	}
	const finalPrompt =
		model.preprompt +
		webPrompt +
		prompt
			.split(" ")
			.slice(-(model.parameters?.truncate ?? 0))
			.join(" ");

	// Not super precise, but it's truncated in the model's backend anyway
	return finalPrompt;
}