Skip to content

feat: Add semantic memory search endpoint with vector similarity#33

Merged
elmorem merged 1 commit intomainfrom
feat/memory-search-endpoint
Dec 11, 2025
Merged

feat: Add semantic memory search endpoint with vector similarity#33
elmorem merged 1 commit intomainfrom
feat/memory-search-endpoint

Conversation

@elmorem
Copy link
Owner

@elmorem elmorem commented Dec 11, 2025

Summary

  • Add vector similarity search endpoint to Memory Service for semantic memory retrieval
  • Implement pgvector-based cosine similarity search in repository layer
  • Add search method to service layer with scope, topic, and confidence filtering
  • Create SearchMemoriesRequest and SearchMemoriesResponse schemas
  • Add POST /api/v1/memories/search endpoint with similarity scoring

Implementation Details

Repository Layer (memory_repository.py:344-395)

  • Added search_by_similarity method using pgvector's cosine distance operator
  • Converts distance (0-2 range) to similarity score (0-1 range) for intuitive results
  • Filters out memories without embeddings
  • Supports optional scope, topic, and min_confidence filtering

Service Layer (memory_service.py:338-366)

  • Added search_memories method that wraps repository search
  • Enforces include_deleted=False for production queries

API Layer (memories.py:128-160)

  • POST /api/v1/memories/search endpoint
  • Request includes query_embedding, optional scope/topic filters, limit (1-100), min_confidence
  • Returns results with memory data and similarity scores
  • Supports pagination with configurable result limit

Schemas

  • SearchMemoriesRequest: Query embedding, optional filters, limit, min_confidence
  • MemorySearchResult: Individual result with memory data and similarity score
  • SearchMemoriesResponse: List of results with total count and limit

API Gateway

The existing API Gateway catch-all route (/api/v1/memories/{path:path}) automatically proxies requests to the new search endpoint with no additional configuration needed.

Test Plan

  • Type checks pass (mypy) for memory service
  • Code formatting applied (black)
  • Manual testing of search endpoint with sample embeddings
  • Verify scope and topic filtering work correctly
  • Verify similarity scores are in 0-1 range
  • Verify limit parameter controls result count
  • Test via API Gateway proxy

🤖 Generated with Claude Code

Implement vector similarity search for memories using pgvector's cosine distance operator. This enables semantic retrieval of memories based on embedding similarity.

Changes:
- Add search_by_similarity method to MemoryRepository using pgvector
- Add search_memories method to MemoryService layer
- Create SearchMemoriesRequest and SearchMemoriesResponse schemas
- Add POST /api/v1/memories/search endpoint to Memory Service API
- Endpoint supports scope, topic, and confidence filtering
- Returns results with similarity scores (0-1 range, higher is better)
- API Gateway already proxies the search endpoint automatically

Technical details:
- Uses pgvector's cosine_distance operator (<=>)
- Converts distance (0-2 range) to similarity score (0-1 range)
- Filters out memories without embeddings
- Supports pagination with configurable limit (1-100)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@elmorem elmorem merged commit 316f3c9 into main Dec 11, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant