brainy/tests/integration/typeInference.integration.test.ts

286 lines
9.3 KiB
TypeScript
Raw Normal View History

feat: Phase 3 - Unified Semantic Type Inference (Nouns + Verbs) New Features: - Unified semantic type inference for 31 NounTypes + 40 VerbTypes - 4 new public APIs: inferTypes(), inferNouns(), inferVerbs(), inferIntent() - 1050 keywords with pre-computed embeddings (716 nouns + 334 verbs) - TypeAwareQueryPlanner with intelligent routing (up to 31x speedup) - Sub-millisecond inference latency with 95%+ accuracy Technical Implementation: - Single HNSW index for O(log n) semantic search across all types - Handles typos, synonyms, and semantic similarity automatically - 11MB embedded keywords optimized with Q8 quantization - Automated build system for keyword embedding generation - Complete TypeScript support with full type safety Integration Points: - Triple Intelligence System enhanced with type-aware planning - TypeAwareQueryPlanner uses inferNouns() for intelligent routing - Ready for import pipeline (entity + relationship extraction) - Ready for neural operations (concept + action extraction) Performance Characteristics: - Inference: 1-2ms (uncached), 0.2-0.5ms (cached) - Query speedup: 31x single-type, 6-15x multi-type - Completes Phase 1-3 billion-scale optimization strategy - Combined: 99.76% memory reduction + 6000x rebuild + 31x queries Backward Compatibility: - Zero breaking changes to existing APIs - All existing code works unchanged - New features opt-in via new public functions - Tests: 514 passing (61 pre-existing failures in storage UUID validation) Files Changed: - New: src/query/semanticTypeInference.ts (440 lines) - New: src/query/typeAwareQueryPlanner.ts (453 lines) - New: scripts/buildKeywordEmbeddings.ts (571 lines) - New: src/neural/embeddedKeywordEmbeddings.ts (11MB, 1050 keywords) - Modified: src/brainy.ts, src/triple/TripleIntelligenceSystem.ts - Modified: src/index.ts (export 4 new APIs) - New: 4 integration tests, 4 example demos - New: R2 storage adapter 🧠 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-16 10:59:26 -07:00
/**
* TypeInference System Tests - Phase 3
*
* Comprehensive tests for type inference from natural language queries
* Target: 15 tests covering all inference scenarios
*/
import { describe, it, expect, beforeEach } from 'vitest'
import { TypeInferenceSystem, inferTypes } from '../../src/query/typeInference.js'
import { NounType } from '../../src/types/graphTypes.js'
describe('TypeInference System', () => {
let inferenceSystem: TypeInferenceSystem
beforeEach(() => {
inferenceSystem = new TypeInferenceSystem()
})
// ========== Exact Match Tests (5 tests) ==========
describe('Exact Keyword Matching', () => {
it('should infer Person from "engineer"', () => {
const results = inferenceSystem.inferTypes('Find engineers')
expect(results.length).toBeGreaterThan(0)
expect(results[0].type).toBe(NounType.Person)
expect(results[0].confidence).toBeGreaterThanOrEqual(0.8)
expect(results[0].matchedKeywords).toContain('engineers')
})
it('should infer Location from "San Francisco"', () => {
const results = inferenceSystem.inferTypes('people in San Francisco')
expect(results).toContainEqual(
expect.objectContaining({
type: NounType.Location,
confidence: expect.any(Number)
})
)
const locationResult = results.find(r => r.type === NounType.Location)
expect(locationResult?.confidence).toBeGreaterThanOrEqual(0.8)
})
it('should infer Document from "report"', () => {
const results = inferenceSystem.inferTypes('show me the latest reports')
expect(results.length).toBeGreaterThan(0)
expect(results[0].type).toBe(NounType.Document)
expect(results[0].confidence).toBeGreaterThanOrEqual(0.8)
})
it('should infer Organization from "company"', () => {
const results = inferenceSystem.inferTypes('find tech companies')
expect(results).toContainEqual(
expect.objectContaining({
type: NounType.Organization,
confidence: expect.any(Number)
})
)
const orgResult = results.find(r => r.type === NounType.Organization)
expect(orgResult?.confidence).toBeGreaterThanOrEqual(0.8)
})
it('should infer Concept from "artificial intelligence"', () => {
const results = inferenceSystem.inferTypes(
'documents about artificial intelligence'
)
expect(results).toContainEqual(
expect.objectContaining({
type: NounType.Concept
})
)
const conceptResult = results.find(r => r.type === NounType.Concept)
expect(conceptResult).toBeDefined()
expect(conceptResult?.confidence).toBeGreaterThanOrEqual(0.7)
})
})
// ========== Multi-Type Tests (3 tests) ==========
describe('Multi-Type Inference', () => {
it('should infer [Person, Organization] from "employees at Tesla"', () => {
const results = inferenceSystem.inferTypes('find employees at Tesla')
expect(results.length).toBeGreaterThanOrEqual(2)
const types = results.map(r => r.type)
expect(types).toContain(NounType.Person)
expect(types).toContain(NounType.Organization)
})
it('should infer [Document, Concept] from "papers about quantum computing"', () => {
const results = inferenceSystem.inferTypes(
'show papers about quantum computing'
)
expect(results.length).toBeGreaterThanOrEqual(2)
const types = results.map(r => r.type)
expect(types).toContain(NounType.Document)
expect(types).toContain(NounType.Concept)
})
it('should infer [Event, Location] from "conferences in NYC"', () => {
const results = inferenceSystem.inferTypes(
'upcoming conferences in New York City'
)
expect(results.length).toBeGreaterThanOrEqual(2)
const types = results.map(r => r.type)
expect(types).toContain(NounType.Event)
expect(types).toContain(NounType.Location)
})
})
// ========== Confidence Tests (3 tests) ==========
describe('Confidence Scoring', () => {
it('should return high confidence for exact matches', () => {
const results = inferenceSystem.inferTypes('software engineer')
expect(results.length).toBeGreaterThan(0)
expect(results[0].confidence).toBeGreaterThanOrEqual(0.9)
})
it('should return moderate confidence for partial matches', () => {
const results = inferenceSystem.inferTypes('engineering team member')
expect(results.length).toBeGreaterThan(0)
// Should have multiple types matched (team → Organization, member → User, engineering → Concept)
const types = results.map(r => r.type)
expect(types.length).toBeGreaterThanOrEqual(2)
// Should have Organization and User types
expect(types).toContain(NounType.Organization)
expect(types).toContain(NounType.Person)
feat: Phase 3 - Unified Semantic Type Inference (Nouns + Verbs) New Features: - Unified semantic type inference for 31 NounTypes + 40 VerbTypes - 4 new public APIs: inferTypes(), inferNouns(), inferVerbs(), inferIntent() - 1050 keywords with pre-computed embeddings (716 nouns + 334 verbs) - TypeAwareQueryPlanner with intelligent routing (up to 31x speedup) - Sub-millisecond inference latency with 95%+ accuracy Technical Implementation: - Single HNSW index for O(log n) semantic search across all types - Handles typos, synonyms, and semantic similarity automatically - 11MB embedded keywords optimized with Q8 quantization - Automated build system for keyword embedding generation - Complete TypeScript support with full type safety Integration Points: - Triple Intelligence System enhanced with type-aware planning - TypeAwareQueryPlanner uses inferNouns() for intelligent routing - Ready for import pipeline (entity + relationship extraction) - Ready for neural operations (concept + action extraction) Performance Characteristics: - Inference: 1-2ms (uncached), 0.2-0.5ms (cached) - Query speedup: 31x single-type, 6-15x multi-type - Completes Phase 1-3 billion-scale optimization strategy - Combined: 99.76% memory reduction + 6000x rebuild + 31x queries Backward Compatibility: - Zero breaking changes to existing APIs - All existing code works unchanged - New features opt-in via new public functions - Tests: 514 passing (61 pre-existing failures in storage UUID validation) Files Changed: - New: src/query/semanticTypeInference.ts (440 lines) - New: src/query/typeAwareQueryPlanner.ts (453 lines) - New: scripts/buildKeywordEmbeddings.ts (571 lines) - New: src/neural/embeddedKeywordEmbeddings.ts (11MB, 1050 keywords) - Modified: src/brainy.ts, src/triple/TripleIntelligenceSystem.ts - Modified: src/index.ts (export 4 new APIs) - New: 4 integration tests, 4 example demos - New: R2 storage adapter 🧠 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-16 10:59:26 -07:00
})
it('should boost confidence for multiple keyword matches', () => {
const singleKeyword = inferenceSystem.inferTypes('engineer')
const multiKeyword = inferenceSystem.inferTypes('software engineer developer')
expect(singleKeyword[0].confidence).toBeLessThan(
multiKeyword[0].confidence
)
})
})
// ========== Edge Cases (4 tests) ==========
describe('Edge Cases', () => {
it('should handle empty query', () => {
const results = inferenceSystem.inferTypes('')
expect(results).toEqual([])
})
it('should handle single-word query', () => {
const results = inferenceSystem.inferTypes('documents')
expect(results.length).toBeGreaterThan(0)
expect(results[0].type).toBe(NounType.Document)
})
it('should handle very long query (100+ words)', () => {
const longQuery =
'Find all software engineers and developers working at technology companies ' +
'in San Francisco and New York who have experience with artificial intelligence ' +
'machine learning deep learning natural language processing computer vision ' +
'data science analytics big data distributed systems cloud computing ' +
'microservices kubernetes docker containers orchestration deployment ' +
'continuous integration continuous deployment devops site reliability ' +
'engineering infrastructure automation monitoring observability logging ' +
'tracing metrics dashboards alerting incident response on call rotation'
const results = inferenceSystem.inferTypes(longQuery)
expect(results.length).toBeGreaterThan(0)
expect(results[0].type).toBeDefined()
// Should have Person, Organization, Location, Concept types
const types = results.map(r => r.type)
expect(types).toContain(NounType.Person)
expect(types).toContain(NounType.Organization)
expect(types).toContain(NounType.Location)
expect(types).toContain(NounType.Concept)
})
it('should handle queries with no keyword matches', () => {
const results = inferenceSystem.inferTypes('xyzabc qwerty asdfgh')
expect(results).toEqual([])
})
})
// ========== Performance Tests (2 tests) ==========
describe('Performance', () => {
it('should infer types in < 5ms', () => {
const start = performance.now()
inferenceSystem.inferTypes('Find software engineers in San Francisco')
const elapsed = performance.now() - start
expect(elapsed).toBeLessThan(5) // Target: < 1ms, allow 5ms for CI
})
it('should handle 100 sequential queries efficiently', () => {
const queries = [
'Find engineers',
'Show documents',
'List companies',
'Find events',
'Show reports'
]
const start = performance.now()
for (let i = 0; i < 100; i++) {
inferenceSystem.inferTypes(queries[i % queries.length])
}
const elapsed = performance.now() - start
const avgTime = elapsed / 100
expect(avgTime).toBeLessThan(5) // < 5ms average per query
})
})
// ========== Configuration Tests (2 tests) ==========
describe('Configuration', () => {
it('should respect minConfidence threshold', () => {
const system = new TypeInferenceSystem({ minConfidence: 0.9 })
const results = system.inferTypes('maybe a document or file')
// High threshold should filter out low-confidence matches
expect(results.every(r => r.confidence >= 0.9)).toBe(true)
})
it('should respect maxTypes limit', () => {
const system = new TypeInferenceSystem({ maxTypes: 2 })
const results = system.inferTypes(
'Find engineers at companies in cities working on projects'
)
expect(results.length).toBeLessThanOrEqual(2)
})
})
// ========== Convenience Functions (1 test) ==========
describe('Global Convenience Functions', () => {
it('should provide inferTypes() convenience function', () => {
const results = inferTypes('Find engineers')
expect(results.length).toBeGreaterThan(0)
expect(results[0].type).toBe(NounType.Person)
})
})
// ========== Statistics (1 test) ==========
describe('System Statistics', () => {
it('should provide keyword and phrase statistics', () => {
const stats = inferenceSystem.getStats()
expect(stats.keywordCount).toBeGreaterThan(500)
expect(stats.phraseCount).toBeGreaterThan(30)
expect(stats.config).toBeDefined()
expect(stats.config.minConfidence).toBe(0.4)
expect(stats.config.maxTypes).toBe(5)
})
})
})