Objective
The companion to Conversation Memory + Rolling Summary.
That recipe used a single prose summary row per session — easy to build, but lossy: contradictions stack up ("I'm vegetarian" + "I eat fish now" coexist in the same paragraph until a humanly-noticeable re-summarization fixes it), and every compression cycle risks summary drift.
This recipe replaces that single row with a typed fact collection — one row per durable user attribute, tagged by category and key, with confidence and recency. Three wins over the summary variant:
- Higher recall — LangChain's own docs find a collection of narrow, individually-updatable memory documents yields higher recall than a single continuously-updated profile (it's easier for an LLM to produce a new fact than to reconcile a new statement with an existing prose paragraph).
- Surgical updates — "I'm vegetarian" later flipping to "I eat fish now" updates one row in place. The two statements never coexist.
- Observable — you can
SELECT * FROM recipe_chat_facts WHERE category = 'dietary'and read the bot's beliefs. The single-summary version hides them inside one prose blob.
Use this variant when the conversation has durable structure (preferences, constraints, goals). Stick with the summary variant for casual / free-flowing chat where there's nothing structured to extract.
Step 1: Create the conversation turn store (same as the summary variant)
CREATE TABLE IF NOT EXISTS recipe_chat_turns (
turn_id INTEGER PRIMARY KEY,
session_id TEXT,
role TEXT,
content TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
embedding VECTOR(384)
);
Step 2: Create the structured fact store (the key swap)
Replace the single recipe_chat_summary row with a fact collection — one row per durable attribute.
CREATE TABLE IF NOT EXISTS recipe_chat_facts (
fact_id INTEGER PRIMARY KEY,
session_id TEXT,
category TEXT, -- 'dietary', 'budget', 'destination', 'travel_mode', 'health', 'goal'
fact_key TEXT, -- 'vegetarian', 'max_usd', 'cities', 'prefers_rail', 'motion_sick', 'trip_length_days'
fact_value TEXT, -- 'true', '3000', 'Tokyo,Kyoto', 'true', 'true', '10'
confidence REAL, -- 0.0–1.0; rises with explicit user statements, falls if contradicted
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
embedding VECTOR(384) -- so facts are themselves semantically searchable
);
The (session_id, category, fact_key) triple is the natural key — the runtime upserts on that, instead of appending. One row per fact.
Step 3: Record a conversation (same as the summary variant)
INSERT INTO recipe_chat_turns (turn_id, session_id, role, content) VALUES
(1,'s1','user','Hi, I need help planning a 10-day trip to Japan in April.'),
(2,'s1','assistant','Great! April is cherry-blossom season. Which cities interest you?'),
(3,'s1','user','Tokyo and Kyoto, and I want to keep it under $3000.'),
(4,'s1','assistant','Got it — budget $3000, Tokyo and Kyoto. Any food preferences?'),
(5,'s1','user','I''m vegetarian, and I love trains over flights.'),
(6,'s1','assistant','Noted: vegetarian, prefers rail. I''ll plan a JR Pass route.'),
(7,'s1','user','Also I get motion sick, so nothing too winding.'),
(8,'s1','assistant','Understood — I''ll avoid mountain bus routes.');
UPDATE recipe_chat_turns SET embedding = EMBED(content);
Step 4: Extract structured facts from the conversation
Ask the model to produce typed, atomic facts — one per category — from the user's turns. We materialize the user-only transcript first, then extract.
CREATE TABLE IF NOT EXISTS recipe_chat_user_transcript (session_id TEXT PRIMARY KEY, transcript TEXT);
INSERT INTO recipe_chat_user_transcript (session_id, transcript)
SELECT 's1', GROUP_CONCAT(content, ' | ')
FROM recipe_chat_turns
WHERE session_id = 's1' AND role = 'user';
Now extract facts from that transcript. The prompt asks for one fact per category, atomic value, and a confidence score the model can justify. (For a production deployment, harden the prompt with explicit category enumerations and few-shot examples — this version is illustrative.)
CREATE TABLE IF NOT EXISTS recipe_chat_facts_raw (
session_id TEXT,
payload TEXT
);
INSERT INTO recipe_chat_facts_raw (session_id, payload)
SELECT session_id,
GENERATE(
'Extract durable user attributes from this chat as a CSV with EXACTLY this header: ' ||
'category,fact_key,fact_value,confidence. ' ||
'Allowed categories: dietary, budget, destination, travel_mode, health, goal. ' ||
'One row per (category,fact_key). Confidence 0.0–1.0. No prose. ' ||
'Chat: ' || transcript)
FROM recipe_chat_user_transcript;
Step 5: Land the extracted facts as rows
Take the CSV the model produced and insert it into the typed table. In a production agent loop, this step uses the SDK's executeQuery to parse + upsert in one round-trip; for the recipe we show the SQL shape.
-- In production this is a parse-and-upsert step in your agent runtime.
-- For the recipe, here's the equivalent INSERTs the runtime would produce
-- from the model's output (the structured-facts variant ALWAYS lands as
-- typed rows, never as prose):
INSERT INTO recipe_chat_facts (fact_id, session_id, category, fact_key, fact_value, confidence) VALUES
(1,'s1','goal', 'trip_length_days', '10', 1.00),
(2,'s1','destination', 'cities', 'Tokyo,Kyoto', 0.95),
(3,'s1','budget', 'max_usd', '3000', 0.95),
(4,'s1','dietary', 'vegetarian', 'true', 0.95),
(5,'s1','travel_mode', 'prefers_rail', 'true', 0.90),
(6,'s1','health', 'motion_sick', 'true', 0.90);
UPDATE recipe_chat_facts SET embedding = EMBED(category || ': ' || fact_key || '=' || fact_value);