Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions mem0-ts/src/oss/src/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,13 @@ export class Memory {
}
const parsedMessages = messages.map((m) => m.content).join("\n");

const [systemPrompt, userPrompt] = this.customPrompt
? [this.customPrompt, `Input:\n${parsedMessages}`]
: getFactRetrievalMessages(parsedMessages);
let systemPrompt: string;
let userPrompt: string;

[systemPrompt, userPrompt] = getFactRetrievalMessages(
parsedMessages,
this.customPrompt
);

const response = await this.llm.generateResponse(
[
Expand Down
8 changes: 8 additions & 0 deletions mem0-ts/src/oss/src/prompts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const MemoryUpdateSchema = z.object({

export function getFactRetrievalMessages(
parsedMessages: string,
customPrompt?: string,
): [string, string] {
const systemPrompt = `You are a Personal Information Organizer, specialized in accurately storing facts, user memories, and preferences. Your primary role is to extract relevant pieces of information from conversations and organize them into distinct, manageable facts. This allows for easy retrieval and personalization in future interactions. Below are the types of information you need to focus on and the detailed instructions on how to handle the input data.

Expand Down Expand Up @@ -89,6 +90,13 @@ export function getFactRetrievalMessages(

const userPrompt = `Following is a conversation between the user and the assistant. You have to extract the relevant facts and preferences about the user, if any, from the conversation and return them in the JSON format as shown above.\n\nInput:\n${parsedMessages}`;

const customPromptWithJsonHandling = customPrompt ? ` ${customPrompt} Your response must be a valid JSON.` : "";

// If custom prompt is provided, that will be your system prompt now and user prompt will be parsed message [ based on existing functionality ]
if (customPrompt) {
return [customPromptWithJsonHandling, "Input:\n" + parsedMessages];
}

return [systemPrompt, userPrompt];
}

Expand Down