File size: 924 Bytes
2606dde
5da61b4
ad02fa3
 
 
 
 
 
2606dde
 
 
 
3c650ed
ad02fa3
 
 
5da61b4
2606dde
 
ad6275a
 
2606dde
ad6275a
2606dde
ad02fa3
2606dde
3c650ed
 
b7b2c8c
 
 
 
 
 
 
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
import type { BackendModel } from "./server/models";
import type { Message } from "./types/Message";

/**
 * Convert [{user: "assistant", content: "hi"}, {user: "user", content: "hello"}] to:
 *
 * <|assistant|>hi<|endoftext|><|prompter|>hello<|endoftext|><|assistant|>
 */
export function buildPrompt(
	messages: Pick<Message, "from" | "content">[],
	model: BackendModel
): string {
	const prompt =
		messages
			.map(
				(m) =>
					(m.from === "user"
						? model.userMessageToken + m.content
						: model.assistantMessageToken + m.content) +
					(model.messageEndToken
						? m.content.endsWith(model.messageEndToken)
							? ""
							: model.messageEndToken
						: "")
			)
			.join("") + model.assistantMessageToken;

	// Not super precise, but it's truncated in the model's backend anyway
	return (
		model.preprompt +
		prompt
			.split(" ")
			.slice(-(model.parameters?.truncate ?? 0))
			.join(" ")
	);
}