chat-ui / src /lib /buildPrompt.ts
nsarrazin's picture
nsarrazin HF staff
Support custom system prompts from the user (#399)
cd6894d unverified
raw
history blame
1.14 kB
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,
preprompt?: string
): Promise<string> {
if (webSearchId) {
const webSearch = await collections.webSearches.findOne({
_id: new ObjectId(webSearchId),
});
if (!webSearch) throw new Error("Web search not found");
if (webSearch.summary) {
messages = [
{
from: "assistant",
content: `The following context was found while searching the internet: ${webSearch.summary}`,
},
...messages,
];
}
}
return (
model
.chatPromptRender({ messages, preprompt })
// Not super precise, but it's truncated in the model's backend anyway
.split(" ")
.slice(-(model.parameters?.truncate ?? 0))
.join(" ")
);
}