Skip to content

Commit bb4ac42

Browse files
Improve prompt construction (#36)
1 parent 362f6ab commit bb4ac42

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

src/lib/prompts.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,48 @@ import { CUSTOM_CONTEXT } from '$env/static/private'
22
import type { Message, ToneType } from './types'
33
import { formatMessagesAsText } from './utils'
44

5-
const coreContext = `Analyze messages attributed to "me:" to mimic my vocabulary and tone when suggesting replies.
6-
Additional context about recent conversation history is provided below. Use this to understand the current situation and tone, but focus your reply on the most recent messages.
7-
Do not summarize the history - it is only for context.
8-
I will also provide a desired tone and may include extra context. Always incorporate that tone and context when crafting replies
9-
`
5+
const coreContext = [
6+
'Analyze messages attributed to "me:" to mimic my vocabulary and tone when suggesting replies.',
7+
'Additional context about recent conversation history is provided below. Use this to understand the current situation and tone, but focus your reply on the most recent messages.',
8+
'Do not summarize the history - it is only for context.',
9+
'I will also provide a desired tone and may include extra context. Always incorporate that tone and context when crafting replies.',
10+
].join('\n')
1011

11-
const instructions = `Given the conversation above, provide a brief summary including the emotional tone, main topics, and any changes in mood.
12-
Suggest 3 replies that I might send. Provide one short reply, one medium-length reply, and one long reply.
13-
Always use this tone when drafting replies:`
12+
const instructions = [
13+
'Given the conversation above, provide a brief summary including the emotional tone, main topics, and any changes in mood.',
14+
'Suggest 3 replies that I might send. Provide one short reply, one medium-length reply, and one long reply.',
15+
'Always use this tone when drafting replies:',
16+
].join('\n')
1417

15-
const responseFormat = `Please respond using this format:
16-
Summary: <summary>
17-
Suggested replies:
18-
Reply 1: <short reply>
19-
Reply 2: <medium reply>
20-
Reply 3: <long reply>`
18+
const responseFormat = [
19+
'Please respond using this format:',
20+
'Summary: <summary>',
21+
'Suggested replies:',
22+
'Reply 1: <short reply>',
23+
'Reply 2: <medium reply>',
24+
'Reply 3: <long reply>',
25+
].join('\n')
2126

22-
export const systemContext = `${CUSTOM_CONTEXT}\n\n${coreContext}`
27+
export const systemContext = [CUSTOM_CONTEXT, coreContext].join('\n\n')
2328

24-
const buildPrompt = (tone: string, context: string): string =>
25-
`${instructions} ${tone}${context ? `\nExtra context: ${context}` : ''}`
29+
const buildPrompt = (tone: string, context: string): string => {
30+
const lines = [`${instructions} ${tone}`]
31+
if (context) lines.push(`Extra context: ${context}`)
32+
return lines.join('\n')
33+
}
2634

2735
export const openAiPrompt = (tone: string, context: string): string =>
28-
`\n${buildPrompt(tone, context)}\n`
36+
buildPrompt(tone, context)
2937

30-
export const khojPrompt = (messages: Message[], tone: ToneType, context: string): string =>
31-
`${systemContext}
32-
Here are some text messages between my partner and I:\n
33-
${formatMessagesAsText(messages)}\n
34-
${buildPrompt(tone, context)}\n
35-
${responseFormat}\n
36-
`
38+
export const khojPrompt = (
39+
messages: Message[],
40+
tone: ToneType,
41+
context: string
42+
): string =>
43+
[
44+
systemContext,
45+
'Here are some text messages between my partner and I:\n' +
46+
formatMessagesAsText(messages),
47+
buildPrompt(tone, context),
48+
responseFormat,
49+
].join('\n')

0 commit comments

Comments
 (0)