-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpromptChat.ts
32 lines (29 loc) · 926 Bytes
/
promptChat.ts
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
export type ChatMessage = {
role: string;
content: string;
// chatMessageId: string;
};
export type Chat = {
messages: ChatMessage[];
chatId: string;
date: number;
title: string;
};
export const getPromptChat = (chatMessages: ChatMessage[]) => {
const systemPrompt =
chatMessages.find((message) => message.role === "system")?.content || "";
const promptHistory = chatMessages
.filter((chatMessage) => chatMessage.role !== "system")
.map((chatMessage, index) => {
const partOfPrompt =
index === 0 && chatMessage.role === "user"
? "<start_of_turn>user\n" + systemPrompt + "\n"
: chatMessage.role === "user"
? "<start_of_turn>user\n"
: "<start_of_turn>model\n";
return partOfPrompt + chatMessage.content + "<end_of_turn>\n";
})
.join("");
const prompt = "<bos>" + promptHistory + "<start_of_turn>model\n";
return prompt;
};