From 1f80ad4ba9fb53fed6654a58b6de0255deabc396 Mon Sep 17 00:00:00 2001 From: Avraham Sakal Date: Tue, 22 Jul 2025 08:03:15 -0400 Subject: [PATCH] factored-out system prompts into their own functions --- pages/chat/trpc.ts | 54 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/pages/chat/trpc.ts b/pages/chat/trpc.ts index 0bf5395..31e1ec4 100644 --- a/pages/chat/trpc.ts +++ b/pages/chat/trpc.ts @@ -21,6 +21,26 @@ import { db } from "../../database/postgres"; import type { ConversationsId } from "../../database/generated/public/Conversations"; import type { UsersId } from "../../database/generated/public/Users"; +const mainSystemPrompt = ({ + systemPrompt, + previousRunningSummary, +}: { systemPrompt: string; previousRunningSummary: string }) => `${systemPrompt} + +This is a summary of the conversation so far, from your point-of-view (so "I" and "me" refer to you): + +${previousRunningSummary} + +`; +const runningSummarySystemPrompt = ({ + previousRunningSummary, +}: { + previousRunningSummary: string; +}) => `Given the following summary of a conversation, coupled with the messages exchanged since that summary was produced, produce a new summary of the conversation. + + ${previousRunningSummary} + +`; + const openrouter = createOpenRouter({ apiKey: env.OPENROUTER_API_KEY, }); @@ -145,13 +165,10 @@ export const chat = router({ ? { role: "system" as const, content: systemPrompt } : { role: "system" as const, - content: `${systemPrompt} - -This is a summary of the conversation so far, from your point-of-view (so "I" and "me" refer to you): - -${previousRunningSummary} - -`, + content: mainSystemPrompt({ + systemPrompt, + previousRunningSummary, + }), }, ...messages.slice(previousRunningSummaryIndex + 1), ], @@ -159,21 +176,6 @@ ${previousRunningSummary} tools: undefined, ...parameters, }); - console.log("sent", [ - previousRunningSummary === "" - ? { role: "system" as const, content: systemPrompt } - : { - role: "system" as const, - content: `${systemPrompt} - -This is a summary of the conversation so far, from your point-of-view (so "I" and "me" refer to you): - -${previousRunningSummary} - -`, - }, - ...messages.slice(previousRunningSummaryIndex + 1), - ]); /** Extract Facts from the user's message, and add them to the database, * linking the Facts with the messages they came from. (Yes, this should * be done *after* the model response, not before; because when we run a @@ -199,11 +201,9 @@ ${previousRunningSummary} messages: [ { role: "system" as const, - content: `Given the following summary of a conversation, coupled with the messages exchanged since that summary was produced, produce a new summary of the conversation. - - ${previousRunningSummary} - -`, + content: runningSummarySystemPrompt({ + previousRunningSummary, + }), }, ...messages.slice(previousRunningSummaryIndex + 1), {