File size: 951 Bytes
e6addfc
2606dde
0c4cf03
e6addfc
e91b76c
e6addfc
e91b76c
 
 
 
 
e6addfc
e91b76c
1305730
 
e6addfc
1305730
e6addfc
 
 
 
 
 
 
 
 
 
 
 
0e5c445
 
e6addfc
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
import type { EndpointParameters } from "./server/endpoints/endpoints";
import type { BackendModel } from "./server/models";

type buildPromptOptions = Pick<EndpointParameters, "messages" | "preprompt" | "continueMessage"> & {
	model: BackendModel;
};

export async function buildPrompt({
	messages,
	model,
	preprompt,
	continueMessage,
}: buildPromptOptions): Promise<string> {
	const filteredMessages = messages.filter((m) => m.from !== "system");

	let prompt = model
		.chatPromptRender({ messages: filteredMessages, preprompt })
		// Not super precise, but it's truncated in the model's backend anyway
		.split(" ")
		.slice(-(model.parameters?.truncate ?? 0))
		.join(" ");

	if (continueMessage && model.parameters?.stop) {
		prompt = model.parameters.stop.reduce((acc: string, curr: string) => {
			if (acc.endsWith(curr)) {
				return acc.slice(0, acc.length - curr.length);
			}
			return acc;
		}, prompt.trimEnd());
	}

	return prompt;
}