From c2815b4d837e64bd0c647265658136c0f482dbbf Mon Sep 17 00:00:00 2001 From: Avraham Sakal Date: Mon, 28 Jul 2025 22:15:59 -0400 Subject: [PATCH] generate fact triggers on each turn --- database/lowdb.ts | 12 ++++ pages/chat/fact-triggers.ts | 34 ++++++++++++ pages/chat/trpc.ts | 106 ++++++++++++++++++++++++++++++++++-- 3 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 pages/chat/fact-triggers.ts diff --git a/database/lowdb.ts b/database/lowdb.ts index 97ce713..0205e5f 100644 --- a/database/lowdb.ts +++ b/database/lowdb.ts @@ -15,6 +15,16 @@ export type Fact = { createdAt: string; }; +export type FactTrigger = { + id: string; + sourceFactId: string; + content: string; + priorityMultiplier: number; + priorityMultiplierReason: string; + scopeConversationId: string; + createdAt: string; +}; + type DB = { conversations: Array; messages: Array<{ @@ -27,12 +37,14 @@ type DB = { runningSummary?: string; }>; facts: Array; + factTriggers: Array; }; export const db = new Low(new JSONFile("db.json"), { conversations: [], messages: [], facts: [], + factTriggers: [], }); /** Initialize the database. Sets `db.data` to the default state if the file doesn't exist. */ await db.read(); diff --git a/pages/chat/fact-triggers.ts b/pages/chat/fact-triggers.ts new file mode 100644 index 0000000..9eda7f5 --- /dev/null +++ b/pages/chat/fact-triggers.ts @@ -0,0 +1,34 @@ +import { + router, + publicProcedure, + createCallerFactory, +} from "../../trpc/server.js"; +import { db } from "../../database/lowdb.js"; + +export const factTriggers = router({ + fetchByFactId: publicProcedure + .input((x) => x as { factId: string }) + .query(async ({ input: { factId } }) => { + return db.data.factTriggers.filter( + (factTrigger) => factTrigger.sourceFactId === factId, + ); + }), + deleteOne: publicProcedure + .input( + (x) => + x as { + factTriggerId: string; + }, + ) + .mutation(async ({ input: { factTriggerId } }) => { + const deletedFactTriggerIndex = db.data.facts.findIndex( + (fact) => fact.id === factTriggerId, + ); + if (deletedFactTriggerIndex === -1) throw new Error("Fact not found"); + db.data.factTriggers.splice(deletedFactTriggerIndex, 1); + db.write(); + return { ok: true }; + }), +}); + +export const createCaller = createCallerFactory(factTriggers); diff --git a/pages/chat/trpc.ts b/pages/chat/trpc.ts index 7af2558..0bcf29e 100644 --- a/pages/chat/trpc.ts +++ b/pages/chat/trpc.ts @@ -17,7 +17,7 @@ import { env } from "../../server/env.js"; // ConsistencyLevelEnum, // type NumberArrayId, // } from "@zilliz/milvus2-sdk-node"; -import { db, type Fact } from "../../database/lowdb.js"; +import { db, type FactTrigger, type Fact } from "../../database/lowdb.js"; import { nanoid } from "nanoid"; import { conversations } from "./conversations.js"; import { messages } from "./messages.js"; @@ -102,6 +102,52 @@ ${mainResponseContent} Extract facts from the assistant's response.`; +const factTriggersSystemPrompt = ({ + previousRunningSummary, + messagesSincePreviousRunningSummary, + mainResponseContent, +}: { + previousRunningSummary: string; + messagesSincePreviousRunningSummary: Array; + mainResponseContent: string; +}) => `You are an expert at idenitfying situations when facts are useful. + +You will be given a summary of a conversation, and the messages exchanged since that summary was produced. + +Then you will be given a fact that was extracted from that conversation, and you will need to identify a natural language phrase that describes a situation in which it would be useful to invoke the fact. + +The facts will be used to enrich context of conversations with AI assistants: upon each turn, a semantic database will be searched to see whether the current situation in the conversation matches any situations that are deemed to render the fact useful, and the fact will be injected into the context of the conversation. + +Your task is to produce a list of triggers for the fact. + +* 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 user's pronouns should be either he or she, NOT "they" or "them", because these triggers will be read by an AI assistant to give it context; and excessive use of "they" or "them" will make what they refer to unclear or ambiguous. +* The assistant should be referred to as "I" or "me", because these triggers will be read by an AI assistant to give it context. + + +${previousRunningSummary} + + +${messagesSincePreviousRunningSummary.map( + (message) => + `<${message.role}_message>${message.content}`, +)} + +${mainResponseContent} + +`; + +const factTriggersUserPrompt = ({ + factContent, +}: { + factContent: string; +}) => ` +${factContent} + + +Generate a list of situations in which the fact is useful.`; + const runningSummarySystemPrompt = ({ previousRunningSummary, }: { @@ -352,21 +398,71 @@ export const chat = router({ })); db.data.facts.push(...insertedFactsFromAssistantMessage); + const insertedFacts = [ + ...insertedFactsFromUserMessage, + ...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"). */ + for (const fact of insertedFacts) { + const factTriggers = await generateObject<{ + factTriggers: Array; + }>({ + model: openrouter("mistralai/mistral-nemo"), + messages: [ + { + role: "system" as const, + content: factTriggersSystemPrompt({ + previousRunningSummary, + messagesSincePreviousRunningSummary, + mainResponseContent: mainResponse.text, + }), + }, + { + role: "user" as const, + content: factTriggersUserPrompt({ + factContent: fact.content, + }), + }, + ], + schema: jsonSchema({ + type: "object", + properties: { + factTriggers: { + type: "array", + items: { + type: "string", + }, + }, + }, + }), + maxSteps: 3, + tools: undefined, + ...parameters, + }); + const insertedFactTriggers: Array = + factTriggers.object.factTriggers.map((factTrigger) => ({ + id: nanoid(), + sourceFactId: fact.id, + content: factTrigger, + priorityMultiplier: 1, + priorityMultiplierReason: "", + scopeConversationId: conversationId, + createdAt: new Date().toISOString(), + })); + db.data.factTriggers.push(...insertedFactTriggers); + } await db.write(); return { insertedAssistantMessage, insertedUserMessage, - insertedFacts: [ - ...insertedFactsFromUserMessage, - ...insertedFactsFromAssistantMessage, - ], + insertedFacts, }; }, ),