Ingesting & extracting memory
Turn conversations, documents, notes, and explicit natural-language facts into incremental topic and memory graphs.
Overview
MemoGrafter uses one write-side pipeline for conversational turns and raw text. It segments new material by topic, creates topic summaries, extracts atomic memories, embeds them, and appends graph edges without rebuilding prior memory.
When to use this
invoke() has already handled ingestion.What you’ll build
A document-memory flow that imports a handbook section, records source metadata and tags, waits for ingestion, and recalls a policy fact later.
Memory construction pipeline
Raw text is split into internal chunks. Topic drift detection can create several segments from one input, and each segment can produce one topic node and multiple atomic memory nodes.
Step 1: Choose the write path
invoke() for a user/assistant exchange; it schedules ingestion automatically.ingestText() for documents or notes without a generated response.remember() for a concise natural-language fact or preference.Step 2: Ingest source text
Text ingestion does not add material to getHistory() and does not call the response-generation path. The extraction LLM still summarizes segments and extracts structured memories.
import {
MemoGrafterAgent,
OpenAIEmbedAdapter,
OpenAILLMAdapter,
} from "memo-grafter";
const llm = new OpenAILLMAdapter("gpt-4o");
const embedder = new OpenAIEmbedAdapter("text-embedding-3-small");
const agent = new MemoGrafterAgent({
db: { connectionString: process.env.DATABASE_URL! },
llm,
embedder,
});
await agent.initialize();
await agent.setSessionTags(["project:support", "policy"]);
await agent.ingestText(handbookSection, {
label: "Refund policy",
source: "support-handbook:v3",
});Step 3: Store an explicit fact
remember() is a convenience wrapper around text ingestion. It is suitable for natural-language facts, not exact row-level inserts.
await agent.remember("Refund requests are accepted within 30 days.", {
label: "Refund window",
source: "policy-settings",
});Step 4: Verify the result
const result = await agent.recall("How long can a customer request a refund?", {
tags: ["policy"],
scope: "session-and-tags",
});
console.log(result.facts);
console.log(result.systemPrompt);Full example
import {
MemoGrafterAgent,
OpenAIEmbedAdapter,
OpenAILLMAdapter,
} from "memo-grafter";
const llm = new OpenAILLMAdapter("gpt-4o");
const embedder = new OpenAIEmbedAdapter("text-embedding-3-small");
const agent = new MemoGrafterAgent({
db: { connectionString: process.env.DATABASE_URL! },
llm,
embedder,
});
await agent.initialize();
try {
await agent.setSessionTags(["project:support", "policy"]);
await agent.ingestText(handbookSection, {
label: "Support handbook",
source: "handbook:v3",
});
const memory = await agent.recall("refund eligibility", {
tags: ["policy"],
scope: "session-and-tags",
limit: 5,
});
console.log(memory.facts);
} finally {
await agent.close();
}How incremental ingestion works
Replace versus append
ingestText() appends by default. Use replace: true only when the input represents the complete current document and the stored session graph should be rebuilt.
Best practices
source value for provenance.Common mistakes
invoke().replace: true when content should be appended.remember() as a deterministic database insert.