Updated semantic search to use OllamaEmbeddingService instead of OpenAI: - Replaced EmbeddingService with OllamaEmbeddingService in SearchService - Added configurable similarity threshold (SEMANTIC_SEARCH_SIMILARITY_THRESHOLD) - Updated both semanticSearch() and hybridSearch() methods - Added comprehensive tests for semantic search functionality - Updated controller documentation to reflect Ollama requirement - All tests passing with 85%+ coverage Related changes: - Updated knowledge.service.versions.spec.ts to include OllamaEmbeddingService - Added similarity threshold environment variable to .env.example Fixes #70 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2.0 KiB
2.0 KiB
Issue #70: [KNOW-018] Semantic Search API
Objective
Implement semantic (vector) search endpoint that uses embeddings generated by issue #69 to enable natural language search over knowledge entries.
Approach
- Review existing embedding schema and pgvector setup
- Review OllamaEmbeddingService from issue #69
- Create DTOs for semantic search request/response
- Write tests first (TDD)
- Implement semantic search in SearchService using pgvector cosine similarity
- Create controller endpoint POST /api/knowledge/search/semantic
- Add configurable similarity threshold
- Test with real queries
- Run quality checks and code review
Progress
- Create scratchpad
- Review existing code (embedding schema, OllamaEmbeddingService)
- Add similarity threshold environment variable
- Write tests (TDD - RED)
- Update SearchService to use OllamaEmbeddingService instead of OpenAI (TDD - GREEN)
- Update hybridSearch to use OllamaEmbeddingService
- Update test files to include OllamaEmbeddingService mocks
- All tests passing
- Type check and build successful
- Run code review
- Run QA checks
- Commit changes
- Close issue
Testing
- Unit tests for SearchService.semanticSearch()
- Controller tests for POST /api/knowledge/search/semantic
- Integration tests with real embeddings
- Target: 85%+ coverage
Notes
- Use pgvector cosine similarity operator (<=>)
- Lower distance = higher similarity
- Results should include similarity scores
- Similarity threshold should be configurable via environment variable
- Reuse OllamaEmbeddingService from issue #69
Findings
- The semantic search endpoint already exists in search.controller.ts (line 111)
- The SearchService already has semanticSearch() method (line 449)
- BUT: It currently uses OpenAI-based EmbeddingService instead of OllamaEmbeddingService
- Need to update SearchService to inject and use OllamaEmbeddingService
- Need to add configurable similarity threshold
- Controller endpoint already properly configured with guards and permissions