|
import type { BackendModel } from "./server/models"; |
|
import type { Message } from "./types/Message"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
return ( |
|
model.preprompt + |
|
prompt |
|
.split(" ") |
|
.slice(-(model.parameters?.truncate ?? 0)) |
|
.join(" ") |
|
); |
|
} |
|
|