File size: 1,462 Bytes
2606dde 5da61b4 ebac87f e943a05 ad02fa3 0c4cf03 e91b76c e943a05 e91b76c e943a05 e91b76c e943a05 0c4cf03 447c0ca cd6894d 447c0ca b7b2c8c 447c0ca 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 |
import type { BackendModel } from "./server/models";
import type { Message } from "./types/Message";
import { format } from "date-fns";
import type { WebSearch } from "./types/WebSearch";
/**
* Convert [{user: "assistant", content: "hi"}, {user: "user", content: "hello"}] to:
*
* <|assistant|>hi<|endoftext|><|prompter|>hello<|endoftext|><|assistant|>
*/
interface buildPromptOptions {
messages: Pick<Message, "from" | "content">[];
model: BackendModel;
locals?: App.Locals;
webSearch?: WebSearch;
preprompt?: string;
}
export async function buildPrompt({
messages,
model,
webSearch,
preprompt,
}: buildPromptOptions): Promise<string> {
if (webSearch && webSearch.context) {
const messagesWithoutLastUsrMsg = messages.slice(0, -1);
const lastUserMsg = messages.slice(-1)[0];
const currentDate = format(new Date(), "MMMM d, yyyy");
messages = [
...messagesWithoutLastUsrMsg,
{
from: "user",
content: `Please answer my question "${lastUserMsg.content}" using the supplied context below (paragraphs from various websites). For the context, today is ${currentDate}:
=====================
${webSearch.context}
=====================
So my question is "${lastUserMsg.content}"`,
},
];
}
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(" ")
);
}
|