|
1 | 1 | import { AppSchema } from '../../db/schema'
|
2 | 2 | import gpt from 'gpt-3-encoder'
|
3 | 3 | import { logger } from '../../logger'
|
| 4 | +import { store } from '../../db' |
4 | 5 |
|
5 | 6 | type PromptOpts = {
|
6 | 7 | sender: AppSchema.Profile
|
7 | 8 | chat: AppSchema.Chat
|
8 | 9 | char: AppSchema.Character
|
9 |
| - history: AppSchema.ChatMessage[] |
10 | 10 | message: string
|
11 | 11 | members: AppSchema.Profile[]
|
| 12 | + retry?: AppSchema.ChatMessage |
12 | 13 | }
|
13 | 14 |
|
| 15 | +const MAX_TOKENS = 2048 |
14 | 16 | const BOT_REPLACE = /\{\{char\}\}/g
|
15 | 17 | const SELF_REPLACE = /\{\{user\}\}/g
|
16 | 18 |
|
17 |
| -export function createPrompt({ sender, chat, char, history, message, members }: PromptOpts) { |
| 19 | +export async function createPrompt({ sender, chat, char, message, members }: PromptOpts) { |
18 | 20 | const username = sender.handle || 'You'
|
19 | 21 |
|
20 |
| - const lines: string[] = [`${char.name}'s Persona: ${formatCharacter(char.name, chat.overrides)}`] |
| 22 | + const pre: string[] = [`${char.name}'s Persona: ${formatCharacter(char.name, chat.overrides)}`] |
21 | 23 |
|
22 | 24 | if (chat.scenario) {
|
23 |
| - lines.push(`Scenario: ${chat.scenario}`) |
| 25 | + pre.push(`Scenario: ${chat.scenario}`) |
24 | 26 | }
|
25 | 27 |
|
26 |
| - lines.push( |
27 |
| - `<START>`, |
28 |
| - ...chat.sampleChat.split('\n'), |
29 |
| - ...history.map((chat) => prefix(chat, char.name, members) + chat.msg), |
30 |
| - `${username}: ${message}`, |
31 |
| - `${char.name}:` |
32 |
| - ) |
| 28 | + pre.push(`<START>`, ...chat.sampleChat.split('\n')) |
| 29 | + const post = [`${username}: ${message}`, `${char.name}:`] |
33 | 30 |
|
34 |
| - const prompt = lines |
| 31 | + const prompt = await appendHistory(chat, members, char, pre, post) |
| 32 | + return prompt |
| 33 | +} |
| 34 | + |
| 35 | +async function appendHistory( |
| 36 | + chat: AppSchema.Chat, |
| 37 | + members: AppSchema.Profile[], |
| 38 | + char: AppSchema.Character, |
| 39 | + pre: string[], |
| 40 | + post: string[], |
| 41 | + retry?: AppSchema.ChatMessage |
| 42 | +) { |
| 43 | + const owner = members.find((mem) => mem.userId === chat.userId) |
| 44 | + if (!owner) { |
| 45 | + throw new Error(`Cannot produce prompt: Owner profile not found`) |
| 46 | + } |
| 47 | + |
| 48 | + // We need to do this early for accurate token counts |
| 49 | + const preamble = pre |
| 50 | + .filter(removeEmpty) |
| 51 | + .join('\n') |
| 52 | + .replace(BOT_REPLACE, char.name) |
| 53 | + .replace(SELF_REPLACE, owner.handle) |
| 54 | + const postamble = post |
35 | 55 | .filter(removeEmpty)
|
36 | 56 | .join('\n')
|
37 | 57 | .replace(BOT_REPLACE, char.name)
|
38 |
| - .replace(SELF_REPLACE, username) |
| 58 | + .replace(SELF_REPLACE, owner.handle) |
39 | 59 |
|
40 |
| - const tokens = gpt.encode(prompt).length |
41 |
| - logger.debug({ tokens, prompt }, 'Tokens') |
| 60 | + let tokens = gpt.encode(preamble + '\n' + postamble).length |
| 61 | + const lines: string[] = [] |
| 62 | + let before = retry?.updatedAt |
| 63 | + |
| 64 | + do { |
| 65 | + const messages = await store.chats.getRecentMessages(chat._id, before) |
| 66 | + const history = messages.map((chat) => prefix(chat, char.name, members) + chat.msg) |
| 67 | + |
| 68 | + for (const hist of history) { |
| 69 | + const nextTokens = gpt.encode(hist).length |
| 70 | + if (nextTokens + tokens > MAX_TOKENS) break |
| 71 | + tokens += nextTokens |
| 72 | + lines.unshift(hist) |
| 73 | + } |
| 74 | + |
| 75 | + if (tokens >= MAX_TOKENS || messages.length < 50) break |
| 76 | + before = messages.slice(-1)[0].createdAt |
| 77 | + } while (true) |
| 78 | + |
| 79 | + const middle = lines |
| 80 | + .join('\n') |
| 81 | + .replace(BOT_REPLACE, char.name) |
| 82 | + .replace(SELF_REPLACE, owner.handle) |
42 | 83 |
|
| 84 | + const prompt = [preamble, middle, postamble].join('\n') |
| 85 | + logger.info({ tokens, prompt }, 'Tokens used') |
43 | 86 | return prompt
|
44 | 87 | }
|
45 | 88 |
|
|
0 commit comments