Skip to content

Commit dc5c573

Browse files
committed
feat: prompts reorg to maximize cache hits
1 parent 536beb2 commit dc5c573

4 files changed

Lines changed: 65 additions & 18 deletions

File tree

src/app/api/chat/openai-realtime/route.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
mergeSystemPrompt,
88
} from "../shared.chat";
99
import {
10+
buildCurrentDateTimeSystemPrompt,
1011
buildMcpServerCustomizationsSystemPrompt,
11-
buildSpeechSystemPrompt,
12+
buildSpeechSystemStaticPrompt,
1213
} from "lib/ai/prompts";
1314

1415
import { safe } from "ts-safe";
@@ -84,14 +85,16 @@ export async function POST(request: NextRequest) {
8485
);
8586

8687
const systemPrompt = mergeSystemPrompt(
87-
buildSpeechSystemPrompt(
88+
buildSpeechSystemStaticPrompt(
8889
session.user,
8990
userPreferences ?? undefined,
9091
agent,
9192
),
9293
buildMcpServerCustomizationsSystemPrompt(mcpServerCustomizations),
9394
);
9495

96+
const dynamicSystemPrompt = buildCurrentDateTimeSystemPrompt();
97+
9598
const bindingTools = [...openAITools, ...DEFAULT_VOICE_TOOLS];
9699

97100
const r = await fetch("https://api.openai.com/v1/realtime/sessions", {
@@ -107,7 +110,7 @@ export async function POST(request: NextRequest) {
107110
input_audio_transcription: {
108111
model: "whisper-1",
109112
},
110-
instructions: systemPrompt,
113+
instructions: systemPrompt + "\n\n" + dynamicSystemPrompt,
111114
tools: bindingTools,
112115
}),
113116
});

src/app/api/chat/route.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import { customModelProvider, isToolCallUnsupportedModel } from "lib/ai/models";
1414
import { agentRepository, chatRepository } from "lib/db/repository";
1515
import globalLogger from "logger";
1616
import {
17+
buildCurrentDateTimeSystemPrompt,
1718
buildMcpServerCustomizationsSystemPrompt,
18-
buildUserSystemPrompt,
19+
buildUserSystemStaticPrompt,
1920
buildToolCallUnsupportedModelSystemPrompt,
2021
} from "lib/ai/prompts";
2122
import {
@@ -263,11 +264,13 @@ export async function POST(request: Request) {
263264
.orElse({});
264265

265266
const systemPrompt = mergeSystemPrompt(
266-
buildUserSystemPrompt(session.user, userPreferences, agent),
267+
buildUserSystemStaticPrompt(session.user, userPreferences, agent),
267268
buildMcpServerCustomizationsSystemPrompt(mcpServerCustomizations),
268269
!supportToolCall && buildToolCallUnsupportedModelSystemPrompt,
269270
);
270271

272+
const dynamicSystemPrompt = buildCurrentDateTimeSystemPrompt();
273+
271274
const IMAGE_TOOL: Record<string, Tool> = useImageTool
272275
? {
273276
[ImageToolName]:
@@ -318,7 +321,10 @@ export async function POST(request: Request) {
318321
const result = streamText({
319322
model,
320323
system: systemPrompt,
321-
messages: convertToModelMessages(messages),
324+
messages: [
325+
{ role: "system", content: dynamicSystemPrompt },
326+
...convertToModelMessages(messages),
327+
],
322328
experimental_transform: smoothStream({ chunking: "word" }),
323329
maxRetries: 2,
324330
tools: vercelAITooles,

src/app/api/chat/temporary/route.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
} from "ai";
88
import { customModelProvider } from "lib/ai/models";
99
import globalLogger from "logger";
10-
import { buildUserSystemPrompt } from "lib/ai/prompts";
10+
import {
11+
buildCurrentDateTimeSystemPrompt,
12+
buildUserSystemStaticPrompt,
13+
} from "lib/ai/prompts";
1114
import { getUserPreferences } from "lib/user/server";
1215

1316
import { colorize } from "consola/utils";
@@ -40,10 +43,16 @@ export async function POST(request: Request) {
4043

4144
return streamText({
4245
model,
43-
system: `${buildUserSystemPrompt(session.user, userPreferences)} ${
44-
instructions ? `\n\n${instructions}` : ""
45-
}`.trim(),
46-
messages: convertToModelMessages(messages),
46+
system: [
47+
buildUserSystemStaticPrompt(session.user, userPreferences),
48+
instructions,
49+
]
50+
.filter(Boolean)
51+
.join("\n\n"),
52+
messages: [
53+
{ role: "system", content: buildCurrentDateTimeSystemPrompt() },
54+
...convertToModelMessages(messages),
55+
],
4756
experimental_transform: smoothStream({ chunking: "word" }),
4857
}).toUIMessageStreamResponse();
4958
} catch (error: any) {

src/lib/ai/prompts.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { createMCPToolId } from "./mcp/mcp-tool-id";
66
import { format } from "date-fns";
77
import { Agent } from "app-types/agent";
88

9+
const CURRENT_TIME_FORMAT = "EEEE, MMMM d, yyyy 'at' h:mm:ss a";
10+
911
export const CREATE_THREAD_TITLE_PROMPT = `
1012
You are a chat title generation expert.
1113
@@ -52,19 +54,34 @@ export const buildUserSystemPrompt = (
5254
user?: User,
5355
userPreferences?: UserPreferences,
5456
agent?: Agent,
57+
) => {
58+
return [
59+
buildUserSystemStaticPrompt(user, userPreferences, agent),
60+
buildCurrentDateTimeSystemPrompt(),
61+
]
62+
.filter(Boolean)
63+
.join("\n\n");
64+
};
65+
66+
export const buildCurrentDateTimeSystemPrompt = () => {
67+
const currentTime = format(new Date(), CURRENT_TIME_FORMAT);
68+
return `The current date and time is ${currentTime}.`;
69+
};
70+
71+
export const buildUserSystemStaticPrompt = (
72+
user?: User,
73+
userPreferences?: UserPreferences,
74+
agent?: Agent,
5575
) => {
5676
const assistantName =
5777
agent?.name || userPreferences?.botName || "better-chatbot";
58-
const currentTime = format(new Date(), "EEEE, MMMM d, yyyy 'at' h:mm:ss a");
5978

60-
let prompt = `You are ${assistantName}`;
79+
let prompt = `You are ${assistantName}.`;
6180

6281
if (agent?.instructions?.role) {
63-
prompt += `. You are an expert in ${agent.instructions.role}`;
82+
prompt += ` You are an expert in ${agent.instructions.role}.`;
6483
}
6584

66-
prompt += `. The current date and time is ${currentTime}.`;
67-
6885
// Agent-specific instructions as primary core
6986
if (agent?.instructions?.systemPrompt) {
7087
prompt += `
@@ -136,17 +153,29 @@ export const buildSpeechSystemPrompt = (
136153
user: User,
137154
userPreferences?: UserPreferences,
138155
agent?: Agent,
156+
) => {
157+
return [
158+
buildSpeechSystemStaticPrompt(user, userPreferences, agent),
159+
buildCurrentDateTimeSystemPrompt(),
160+
]
161+
.filter(Boolean)
162+
.join("\n\n");
163+
};
164+
165+
export const buildSpeechSystemStaticPrompt = (
166+
user: User,
167+
userPreferences?: UserPreferences,
168+
agent?: Agent,
139169
) => {
140170
const assistantName = agent?.name || userPreferences?.botName || "Assistant";
141-
const currentTime = format(new Date(), "EEEE, MMMM d, yyyy 'at' h:mm:ss a");
142171

143172
let prompt = `You are ${assistantName}`;
144173

145174
if (agent?.instructions?.role) {
146175
prompt += `. You are an expert in ${agent.instructions.role}`;
147176
}
148177

149-
prompt += `. The current date and time is ${currentTime}.`;
178+
prompt += ".";
150179

151180
// Agent-specific instructions as primary core
152181
if (agent?.instructions?.systemPrompt) {

0 commit comments

Comments
 (0)