|
|
|
@ -19,9 +19,11 @@ import { nanoid } from "nanoid";
|
|
|
|
|
import { conversations } from "./conversations.js";
|
|
|
|
|
import { messages } from "./messages.js";
|
|
|
|
|
import { facts, createCaller as createCallerFacts } from "./facts.js";
|
|
|
|
|
import { createCaller as createCallerMessages } from "./messages.js";
|
|
|
|
|
import { openrouter } from "./provider.js";
|
|
|
|
|
|
|
|
|
|
const factsCaller = createCallerFacts({});
|
|
|
|
|
const messagesCaller = createCallerMessages({});
|
|
|
|
|
|
|
|
|
|
const mainSystemPrompt = ({
|
|
|
|
|
systemPrompt,
|
|
|
|
@ -80,43 +82,6 @@ ${factContent}
|
|
|
|
|
|
|
|
|
|
Generate a list of situations in which the fact is useful.`;
|
|
|
|
|
|
|
|
|
|
const runningSummarySystemPrompt = ({
|
|
|
|
|
previousRunningSummary,
|
|
|
|
|
}: {
|
|
|
|
|
previousRunningSummary: string;
|
|
|
|
|
}) => `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 user's pronouns should be either he or she, NOT "they" or "them", because this summary 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 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.
|
|
|
|
|
|
|
|
|
|
<running_summary>
|
|
|
|
|
${previousRunningSummary}
|
|
|
|
|
</running_summary>
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const runningSummaryUserPrompt = ({
|
|
|
|
|
messagesSincePreviousRunningSummary,
|
|
|
|
|
mainResponseContent,
|
|
|
|
|
}: {
|
|
|
|
|
messagesSincePreviousRunningSummary: Array<DraftMessage>;
|
|
|
|
|
mainResponseContent: string;
|
|
|
|
|
}) =>
|
|
|
|
|
`${messagesSincePreviousRunningSummary.map(
|
|
|
|
|
(message) =>
|
|
|
|
|
`<${message.role}_message>${message.content}</${message.role}_message>`,
|
|
|
|
|
)}
|
|
|
|
|
<assistant_response>
|
|
|
|
|
${mainResponseContent}
|
|
|
|
|
</assistant_response>
|
|
|
|
|
|
|
|
|
|
Generate a new running summary of the conversation.`;
|
|
|
|
|
|
|
|
|
|
export const chat = router({
|
|
|
|
|
conversations,
|
|
|
|
|
messages,
|
|
|
|
@ -217,26 +182,10 @@ export const chat = router({
|
|
|
|
|
* 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 runningSummaryResponse = await messagesCaller.generateRunningSummary({
|
|
|
|
|
messagesSincePreviousRunningSummary,
|
|
|
|
|
mainResponseContent: mainResponse.text,
|
|
|
|
|
previousRunningSummary,
|
|
|
|
|
});
|
|
|
|
|
const insertedAssistantMessage: CommittedMessage = {
|
|
|
|
|
id: nanoid(),
|
|
|
|
|