From bf0bfe5b0791f0e38dc0cee940d28a0a6467e6de Mon Sep 17 00:00:00 2001 From: Avraham Sakal Date: Sun, 27 Jul 2025 18:25:40 -0400 Subject: [PATCH] improve prompt quality --- pages/chat/trpc.ts | 105 +++++++++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 38 deletions(-) diff --git a/pages/chat/trpc.ts b/pages/chat/trpc.ts index cd7cc03..bb9125c 100644 --- a/pages/chat/trpc.ts +++ b/pages/chat/trpc.ts @@ -37,7 +37,16 @@ const factsFromUserMessageSystemPrompt = ({ previousRunningSummary, }: { previousRunningSummary: string; -}) => `You are an expert at extracting facts from conversations. Given the following summary of a conversation, coupled with the messages exchanged since that summary was produced (which will be provided by the user), extract new facts that can be gleaned from the messages exchanged since the summary was produced. +}) => `You are an expert at extracting facts from conversations. + +You will be given a summary of a conversation, and the messages exchanged since that summary was produced. + +Your task is to extract *new* facts that can be gleaned from the messages exchanged since the summary was produced. + +* You should not extract any facts that are already in the summary. +* The user should be referred to as "the user" in the fact text. +* The assistant should be referred to as "I" or "me", because these facts will be read by an AI assistant to give it context. + ${previousRunningSummary} @@ -59,7 +68,16 @@ const factsFromAssistantMessageSystemPrompt = ({ previousRunningSummary, }: { previousRunningSummary: string; -}) => `You are an insightful fact extractor. Given the following summary of a conversation, coupled with the messages exchanged since that summary was produced (which will be provided by the user), extract new facts that can be gleaned from the final assistant response. +}) => `You are an expert at extracting facts from conversations. + +You will be given a summary of a conversation, and the messages exchanged since that summary was produced. + +Your task is to extract *new* facts that can be gleaned from the *final assistant response*. + +* You should not extract any facts that are already in the summary or in the ensuing conversation; you should only extract new facts from the final assistant response. +* The user should be referred to as "the user" in the fact text. +* The assistant should be referred to as "I" or "me", because these facts will be read by an AI assistant to give it context. + ${previousRunningSummary} @@ -86,7 +104,16 @@ const runningSummarySystemPrompt = ({ previousRunningSummary, }: { previousRunningSummary: string; -}) => `You are an expert at summarizing conversations. Given the following summary of a conversation, coupled with the messages exchanged since that summary was produced (which will be provided by the user), produce a new summary of the conversation. +}) => `You are an expert at summarizing conversations. + +You will be given a summary of a conversation, and the messages exchanged since that summary was produced. + +Your task is to produce a new summary of the conversation. + +* The user should be referred to as "the user" in the summary. +* The assistant should be referred to as "I" or "me", because this summary will be read by an AI assistant to give it context. +* The new summary may omit details present in the old summary, provided that the messages that were exchanged since that summary was produced indictae that those details are becoming less relevant to the continuation of the conversation. + ${previousRunningSummary} @@ -235,6 +262,42 @@ export const chat = router({ createdAt: new Date().toISOString(), })); db.data.facts.push(...insertedFactsFromUserMessage); + + /** Produce a running summary of the conversation, and save that along + * with the model's response to the database. The new running summary is + * based on the previous running summary combined with the all messages + * since that summary was produced. */ + const runningSummaryResponse = await generateText({ + model: openrouter("mistralai/mistral-nemo"), + messages: [ + { + role: "system" as const, + content: runningSummarySystemPrompt({ + previousRunningSummary, + }), + }, + { + role: "user" as const, + content: runningSummaryUserPrompt({ + messagesSincePreviousRunningSummary, + mainResponseContent: mainResponse.text, + }), + }, + ], + maxSteps: 3, + tools: undefined, + ...parameters, + }); + const insertedAssistantMessage: CommittedMessage = { + id: nanoid(), + conversationId, + content: mainResponse.text, + runningSummary: runningSummaryResponse.text, + role: "assistant" as const, + index: messages.length, + createdAt: new Date().toISOString(), + }; + db.data.messages.push(insertedAssistantMessage); /** Extract Facts from the model's response, and add them to the database, * linking the Facts with the messages they came from. */ const factsFromAssistantMessageResponse = await generateObject<{ @@ -285,46 +348,12 @@ export const chat = router({ createdAt: new Date().toISOString(), })); db.data.facts.push(...insertedFactsFromAssistantMessage); + /** For each Fact produced in the two fact-extraction steps, generate * FactTriggers and add them to the database, linking the FactTriggers * with the Facts they came from. A FactTrigger is a natural language * phrase that describes a situation in which it would be useful to invoke * the Fact. (e.g., "When food preferences are discussed"). */ - /** Produce a running summary of the conversation, and save that along - * with the model's response to the database. The new running summary is - * based on the previous running summary combined with the all messages - * since that summary was produced. */ - const runningSummaryResponse = await generateText({ - model: openrouter("mistralai/mistral-nemo"), - messages: [ - { - role: "system" as const, - content: runningSummarySystemPrompt({ - previousRunningSummary, - }), - }, - { - role: "user" as const, - content: runningSummaryUserPrompt({ - messagesSincePreviousRunningSummary, - mainResponseContent: mainResponse.text, - }), - }, - ], - maxSteps: 3, - tools: undefined, - ...parameters, - }); - const insertedAssistantMessage: CommittedMessage = { - id: nanoid(), - conversationId, - content: mainResponse.text, - runningSummary: runningSummaryResponse.text, - role: "assistant" as const, - index: messages.length, - createdAt: new Date().toISOString(), - }; - db.data.messages.push(insertedAssistantMessage); await db.write();