Files
FabledScribe/alembic/versions/0014_add_note_embeddings.py
T
bvandeusen d6f4a6dbb6 Add semantic note search (nomic-embed-text) and per-conversation note cache
- New NoteEmbedding model + migration 0014 stores float embeddings (JSONB)
- services/embeddings.py: get_embedding, upsert_note_embedding,
  semantic_search_notes (cosine similarity), backfill_note_embeddings
- build_context() now tries semantic search first, falls back to keyword search;
  accepts cached_note_ids to reuse last-turn notes and stabilise the system
  prompt prefix for Ollama's KV cache
- generation_buffer.py: per-conversation note ID cache (get/set/clear)
- generation_task.py: passes cached IDs into build_context, updates cache
  after each turn, and invalidates it after create_note/update_note/create_task
- app.py: pulls nomic-embed-text at startup and launches a background backfill
  to embed all existing notes (30 s delay so Ollama has time to load the model)
- routes/notes.py + services/tools.py: fire-and-forget embedding update on
  every note create or update via the API or LLM tool calls

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-18 21:44:58 -05:00

25 lines
661 B
Python

"""Add note_embeddings table for semantic note search."""
from alembic import op
revision = "0014"
down_revision = "0013"
def upgrade() -> None:
op.execute("""
CREATE TABLE IF NOT EXISTS note_embeddings (
note_id INTEGER PRIMARY KEY REFERENCES notes(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL,
embedding JSONB NOT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
""")
op.execute(
"CREATE INDEX IF NOT EXISTS ix_note_embeddings_user_id ON note_embeddings(user_id)"
)
def downgrade() -> None:
op.execute("DROP TABLE IF EXISTS note_embeddings")