generate fact triggers on each turn

master
Avraham Sakal 3 months ago
parent 55d35e150c
commit c2815b4d83

@ -15,6 +15,16 @@ export type Fact = {
createdAt: string; createdAt: string;
}; };
export type FactTrigger = {
id: string;
sourceFactId: string;
content: string;
priorityMultiplier: number;
priorityMultiplierReason: string;
scopeConversationId: string;
createdAt: string;
};
type DB = { type DB = {
conversations: Array<Conversation>; conversations: Array<Conversation>;
messages: Array<{ messages: Array<{
@ -27,12 +37,14 @@ type DB = {
runningSummary?: string; runningSummary?: string;
}>; }>;
facts: Array<Fact>; facts: Array<Fact>;
factTriggers: Array<FactTrigger>;
}; };
export const db = new Low<DB>(new JSONFile("db.json"), { export const db = new Low<DB>(new JSONFile("db.json"), {
conversations: [], conversations: [],
messages: [], messages: [],
facts: [], facts: [],
factTriggers: [],
}); });
/** Initialize the database. Sets `db.data` to the default state if the file doesn't exist. */ /** Initialize the database. Sets `db.data` to the default state if the file doesn't exist. */
await db.read(); await db.read();

@ -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);

@ -17,7 +17,7 @@ import { env } from "../../server/env.js";
// ConsistencyLevelEnum, // ConsistencyLevelEnum,
// type NumberArrayId, // type NumberArrayId,
// } from "@zilliz/milvus2-sdk-node"; // } 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 { nanoid } from "nanoid";
import { conversations } from "./conversations.js"; import { conversations } from "./conversations.js";
import { messages } from "./messages.js"; import { messages } from "./messages.js";
@ -102,6 +102,52 @@ ${mainResponseContent}
Extract facts from the assistant's response.`; Extract facts from the assistant's response.`;
const factTriggersSystemPrompt = ({
previousRunningSummary,
messagesSincePreviousRunningSummary,
mainResponseContent,
}: {
previousRunningSummary: string;
messagesSincePreviousRunningSummary: Array<DraftMessage>;
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.
<running_summary>
${previousRunningSummary}
</running_summary>
${messagesSincePreviousRunningSummary.map(
(message) =>
`<${message.role}_message>${message.content}</${message.role}_message>`,
)}
<assistant_response>
${mainResponseContent}
</assistant_response>
`;
const factTriggersUserPrompt = ({
factContent,
}: {
factContent: string;
}) => `<fact_content>
${factContent}
</fact_content>
Generate a list of situations in which the fact is useful.`;
const runningSummarySystemPrompt = ({ const runningSummarySystemPrompt = ({
previousRunningSummary, previousRunningSummary,
}: { }: {
@ -352,21 +398,71 @@ export const chat = router({
})); }));
db.data.facts.push(...insertedFactsFromAssistantMessage); db.data.facts.push(...insertedFactsFromAssistantMessage);
const insertedFacts = [
...insertedFactsFromUserMessage,
...insertedFactsFromAssistantMessage,
];
/** For each Fact produced in the two fact-extraction steps, generate /** For each Fact produced in the two fact-extraction steps, generate
* FactTriggers and add them to the database, linking the FactTriggers * FactTriggers and add them to the database, linking the FactTriggers
* with the Facts they came from. A FactTrigger is a natural language * 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 * phrase that describes a situation in which it would be useful to invoke
* the Fact. (e.g., "When food preferences are discussed"). */ * the Fact. (e.g., "When food preferences are discussed"). */
for (const fact of insertedFacts) {
const factTriggers = await generateObject<{
factTriggers: Array<string>;
}>({
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<FactTrigger> =
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(); await db.write();
return { return {
insertedAssistantMessage, insertedAssistantMessage,
insertedUserMessage, insertedUserMessage,
insertedFacts: [ insertedFacts,
...insertedFactsFromUserMessage,
...insertedFactsFromAssistantMessage,
],
}; };
}, },
), ),

Loading…
Cancel
Save