Add deduplication to store_memory

Before inserting, check if a near-identical memory exists (cosine > 0.85).
If found, skip insert. Prevents flooding the DB with repeated facts
from the same conversation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jze9
2026-05-28 20:39:28 +05:00
parent 8e5149a7c3
commit 74c3fac2cc

View File

@@ -25,6 +25,16 @@ async def store_memory(content: str, user_id: str = "default",
pool = await get_pool() pool = await get_pool()
vec = await embed(content) vec = await embed(content)
async with pool.acquire() as conn: async with pool.acquire() as conn:
# skip if near-duplicate already exists (cosine > 0.85)
existing = await conn.fetchval("""
SELECT id FROM memories
WHERE user_id = $1
AND (metadata->>'archived') IS NULL
AND 1 - (embedding <=> $2::vector) > 0.85
LIMIT 1
""", user_id, _vec_str(vec))
if existing:
return
await conn.execute(""" await conn.execute("""
INSERT INTO memories (user_id, content, embedding, importance, memory_type, metadata) INSERT INTO memories (user_id, content, embedding, importance, memory_type, metadata)
VALUES ($1, $2, $3::vector, $4, $5, $6) VALUES ($1, $2, $3::vector, $4, $5, $6)