mirror of
https://github.com/ruvnet/RuVector.git
synced 2026-05-22 19:56:25 +00:00
* feat(postgres): Add 7 advanced AI modules to ruvector-postgres Comprehensive implementation of advanced AI capabilities: ## New Modules (23,541 lines of code) ### 1. Self-Learning / ReasoningBank (`src/learning/`) - Trajectory tracking for query optimization - Pattern extraction using K-means clustering - ReasoningBank for pattern storage and matching - Adaptive search parameter optimization ### 2. Attention Mechanisms (`src/attention/`) - Scaled dot-product attention (core) - Multi-head attention with parallel heads - Flash Attention v2 (memory-efficient) - 10 attention types with PostgresEnum support ### 3. GNN Layers (`src/gnn/`) - Message passing framework - GCN (Graph Convolutional Network) - GraphSAGE with mean/max aggregation - Configurable aggregation methods ### 4. Hyperbolic Embeddings (`src/hyperbolic/`) - Poincaré ball model - Lorentz hyperboloid model - Hyperbolic distance metrics - Möbius operations ### 5. Sparse Vectors (`src/sparse/`) - COO format sparse vector type - Efficient sparse-sparse distance functions - BM25/SPLADE compatible - Top-k pruning operations ### 6. Graph Operations & Cypher (`src/graph/`) - Property graph storage (nodes/edges) - BFS, DFS, Dijkstra traversal - Cypher query parser (AST-based) - Query executor with pattern matching ### 7. Tiny Dancer Routing (`src/routing/`) - FastGRNN neural network - Agent registry with capabilities - Multi-objective routing optimization - Cost/latency/quality balancing ## Docker Infrastructure - Dockerfile with pgrx 0.12.6 and PostgreSQL 16 - docker-compose.yml with test runner - Initialization SQL with test tables - Shell scripts for dev/test/benchmark ## Feature Flags - `learning`, `attention`, `gnn`, `hyperbolic` - `sparse`, `graph`, `routing` - `ai-complete` and `graph-complete` bundles 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix(docker): Copy entire workspace for pgrx build 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix(docker): Build standalone crate without workspace 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * docs: Update README to enhance clarity and structure * fix(postgres): Resolve compilation errors and Docker build issues - Fix simsimd Option/Result type mismatch in scaled_dot.rs - Fix f32/f64 type conversions in poincare.rs and lorentz.rs - Fix AVX512 missing wrapper functions by using AVX2 fallback - Fix Vec<Vec<f32>> to JsonB for pgrx pg_extern compatibility - Fix DashMap get() to get_mut() for mutable access - Fix router.rs dereference for best_score comparison - Update Dockerfile to copy pre-written SQL file for pgrx - Simplify init.sql to use correct function names - Add postgres-cli npm package for CLI tooling All changes tested successfully in Docker with: - Extension loads with AVX2 SIMD support (8 floats/op) - Distance functions verified working - PostgreSQL 16 container runs successfully 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * feat: Add ruvLLM examples and enhanced postgres-cli Added from claude/ruvector-lfm2-llm-01YS5Tc7i64PyYCLecT9L1dN branch: - examples/ruvLLM: Complete LLM inference system with SIMD optimization - Pretraining, benchmarking, and optimization system - Real SIMD-optimized CPU inference engine - Comprehensive SOTA benchmark suite - Attention mechanisms, memory management, router Enhanced postgres-cli with full ruvector-postgres integration: - Sparse vector operations (BM25, top-k, prune, conversions) - Hyperbolic geometry (Poincare, Lorentz, Mobius operations) - Agent routing (Tiny Dancer system) - Vector quantization (binary, scalar, product) - Enhanced graph and learning commands 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix(postgres-cli): Use native ruvector type instead of pgvector - Change createVectorTable to use ruvector type (native RuVector extension) - Add dimensions column for metadata since ruvector is variable-length - Update index creation to use simple btree (HNSW/IVFFlat TBD) - Tested against Docker container with ruvector extension 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * feat(postgres): Add 53 SQL function definitions for all advanced modules Enable all advanced PostgreSQL extension functions by adding their SQL definitions to the extension file. This exposes all Rust #[pg_extern] functions to PostgreSQL. ## New SQL Functions (53 total) ### Hyperbolic Geometry (8 functions) - ruvector_poincare_distance, ruvector_lorentz_distance - ruvector_mobius_add, ruvector_exp_map, ruvector_log_map - ruvector_poincare_to_lorentz, ruvector_lorentz_to_poincare - ruvector_minkowski_dot ### Sparse Vectors (14 functions) - ruvector_sparse_create, ruvector_sparse_from_dense - ruvector_sparse_dot, ruvector_sparse_cosine, ruvector_sparse_l2_distance - ruvector_sparse_add, ruvector_sparse_scale, ruvector_sparse_to_dense - ruvector_sparse_nnz, ruvector_sparse_dim - ruvector_bm25_score, ruvector_tf_idf, ruvector_sparse_normalize - ruvector_sparse_topk ### GNN - Graph Neural Networks (5 functions) - ruvector_gnn_gcn_layer, ruvector_gnn_graphsage_layer - ruvector_gnn_gat_layer, ruvector_gnn_message_pass - ruvector_gnn_aggregate ### Routing/Agents - "Tiny Dancer" (11 functions) - ruvector_route_query, ruvector_route_with_context - ruvector_calculate_agent_affinity, ruvector_select_best_agent - ruvector_multi_agent_route, ruvector_create_agent_embedding - ruvector_get_routing_stats, ruvector_register_agent - ruvector_update_agent_performance, ruvector_adaptive_route - ruvector_fastgrnn_forward ### Learning/ReasoningBank (7 functions) - ruvector_record_trajectory, ruvector_get_verdict - ruvector_distill_memory, ruvector_adaptive_search - ruvector_learning_feedback, ruvector_get_learning_patterns - ruvector_optimize_search_params ### Graph/Cypher (8 functions) - ruvector_graph_create_node, ruvector_graph_create_edge - ruvector_graph_get_neighbors, ruvector_graph_shortest_path - ruvector_graph_pagerank, ruvector_cypher_query - ruvector_graph_traverse, ruvector_graph_similarity_search ## CLI Updates - Enabled hyperbolic geometry commands in postgres-cli - Added vector distance and normalize commands - Enhanced client with connection pooling and retry logic 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
256 lines
8.4 KiB
SQL
256 lines
8.4 KiB
SQL
-- Sparse Vectors Example Usage
|
|
-- This file demonstrates the sparse vector functionality
|
|
|
|
-- ============================================================================
|
|
-- Setup
|
|
-- ============================================================================
|
|
|
|
-- Create extension (assuming already installed)
|
|
-- CREATE EXTENSION IF NOT EXISTS ruvector_postgres;
|
|
|
|
-- Create sample tables
|
|
CREATE TABLE IF NOT EXISTS sparse_documents (
|
|
id SERIAL PRIMARY KEY,
|
|
title TEXT,
|
|
content TEXT,
|
|
sparse_embedding sparsevec,
|
|
created_at TIMESTAMP DEFAULT NOW()
|
|
);
|
|
|
|
-- ============================================================================
|
|
-- Inserting Data
|
|
-- ============================================================================
|
|
|
|
-- Method 1: String format
|
|
INSERT INTO sparse_documents (title, content, sparse_embedding) VALUES
|
|
('Machine Learning Basics',
|
|
'Introduction to neural networks and deep learning',
|
|
'{1024:0.5, 2048:0.3, 4096:0.8, 8192:0.2}'::sparsevec),
|
|
|
|
('Natural Language Processing',
|
|
'Text processing and language models',
|
|
'{1024:0.3, 3072:0.7, 4096:0.4, 9216:0.6}'::sparsevec),
|
|
|
|
('Computer Vision',
|
|
'Image recognition and object detection',
|
|
'{2048:0.9, 5120:0.4, 6144:0.5, 7168:0.3}'::sparsevec);
|
|
|
|
-- Method 2: Array construction
|
|
INSERT INTO sparse_documents (title, content, sparse_embedding) VALUES
|
|
('Reinforcement Learning',
|
|
'Q-learning and policy gradients',
|
|
ruvector_to_sparse(
|
|
ARRAY[1024, 4096, 10240]::int[],
|
|
ARRAY[0.6, 0.8, 0.4]::real[],
|
|
30000
|
|
));
|
|
|
|
-- Method 3: Convert from dense
|
|
INSERT INTO sparse_documents (title, sparse_embedding)
|
|
SELECT 'From Dense Vector',
|
|
ruvector_dense_to_sparse(
|
|
ARRAY[0, 0.5, 0, 0.3, 0, 0, 0.8, 0, 0, 0.2]::real[]
|
|
);
|
|
|
|
-- ============================================================================
|
|
-- Basic Queries
|
|
-- ============================================================================
|
|
|
|
-- View all documents with sparse vectors
|
|
SELECT id, title,
|
|
ruvector_sparse_nnz(sparse_embedding) as num_nonzero,
|
|
ruvector_sparse_dim(sparse_embedding) as dimension,
|
|
ruvector_sparse_norm(sparse_embedding) as l2_norm
|
|
FROM sparse_documents;
|
|
|
|
-- ============================================================================
|
|
-- Similarity Search
|
|
-- ============================================================================
|
|
|
|
-- Define a query vector
|
|
WITH query AS (
|
|
SELECT '{1024:0.5, 2048:0.3, 4096:0.8}'::sparsevec AS query_vec
|
|
)
|
|
-- Search by dot product (inner product)
|
|
SELECT d.id, d.title,
|
|
ruvector_sparse_dot(d.sparse_embedding, q.query_vec) AS dot_product,
|
|
ruvector_sparse_cosine(d.sparse_embedding, q.query_vec) AS cosine_sim,
|
|
ruvector_sparse_euclidean(d.sparse_embedding, q.query_vec) AS euclidean_dist
|
|
FROM sparse_documents d, query q
|
|
ORDER BY dot_product DESC
|
|
LIMIT 5;
|
|
|
|
-- Find documents with high cosine similarity
|
|
WITH query AS (
|
|
SELECT '{1024:0.5, 4096:0.8}'::sparsevec AS query_vec
|
|
)
|
|
SELECT id, title,
|
|
ruvector_sparse_cosine(sparse_embedding, query_vec) AS similarity
|
|
FROM sparse_documents, query
|
|
WHERE ruvector_sparse_cosine(sparse_embedding, query_vec) > 0.3
|
|
ORDER BY similarity DESC;
|
|
|
|
-- ============================================================================
|
|
-- Sparsification Operations
|
|
-- ============================================================================
|
|
|
|
-- Keep only top-k elements
|
|
SELECT id, title,
|
|
sparse_embedding AS original,
|
|
ruvector_sparse_top_k(sparse_embedding, 2) AS top_2_elements
|
|
FROM sparse_documents
|
|
LIMIT 3;
|
|
|
|
-- Prune small values
|
|
SELECT id, title,
|
|
sparse_embedding AS original,
|
|
ruvector_sparse_prune(sparse_embedding, 0.4) AS pruned
|
|
FROM sparse_documents
|
|
LIMIT 3;
|
|
|
|
-- ============================================================================
|
|
-- BM25 Text Search Example
|
|
-- ============================================================================
|
|
|
|
-- Create BM25-specific table
|
|
CREATE TABLE IF NOT EXISTS bm25_articles (
|
|
id SERIAL PRIMARY KEY,
|
|
title TEXT,
|
|
content TEXT,
|
|
term_frequencies sparsevec, -- TF values
|
|
doc_length REAL
|
|
);
|
|
|
|
-- Insert sample documents with term frequencies
|
|
INSERT INTO bm25_articles (title, content, term_frequencies, doc_length) VALUES
|
|
('AI Research Paper',
|
|
'Deep learning models for natural language processing',
|
|
'{100:2.0, 200:1.0, 300:3.0, 400:1.0}'::sparsevec, -- TF values
|
|
7.0),
|
|
|
|
('Machine Learning Tutorial',
|
|
'Introduction to supervised and unsupervised learning',
|
|
'{100:1.0, 250:2.0, 300:1.0, 500:2.0}'::sparsevec,
|
|
6.0),
|
|
|
|
('Data Science Guide',
|
|
'Statistical analysis and data visualization techniques',
|
|
'{150:1.0, 250:1.0, 350:2.0, 450:1.0}'::sparsevec,
|
|
6.0);
|
|
|
|
-- BM25 search
|
|
WITH
|
|
query AS (
|
|
-- Query with IDF weights (normally computed from corpus)
|
|
SELECT '{100:1.5, 300:2.0, 400:1.2}'::sparsevec AS query_idf
|
|
),
|
|
collection_stats AS (
|
|
SELECT AVG(doc_length) AS avg_doc_len
|
|
FROM bm25_articles
|
|
)
|
|
SELECT a.id, a.title,
|
|
ruvector_sparse_bm25(
|
|
q.query_idf,
|
|
a.term_frequencies,
|
|
a.doc_length,
|
|
cs.avg_doc_len,
|
|
1.2, -- k1 parameter
|
|
0.75 -- b parameter
|
|
) AS bm25_score
|
|
FROM bm25_articles a, query q, collection_stats cs
|
|
ORDER BY bm25_score DESC
|
|
LIMIT 5;
|
|
|
|
-- ============================================================================
|
|
-- Hybrid Search (Dense + Sparse)
|
|
-- ============================================================================
|
|
|
|
-- Create hybrid table (requires vector extension)
|
|
-- Uncomment if you have dense vector support
|
|
/*
|
|
CREATE TABLE IF NOT EXISTS hybrid_documents (
|
|
id SERIAL PRIMARY KEY,
|
|
title TEXT,
|
|
dense_embedding vector(768),
|
|
sparse_embedding sparsevec
|
|
);
|
|
|
|
-- Hybrid search combining both signals
|
|
WITH query AS (
|
|
SELECT
|
|
random_vector(768) AS query_dense, -- Replace with actual query
|
|
'{1024:0.5, 2048:0.3}'::sparsevec AS query_sparse
|
|
)
|
|
SELECT id, title,
|
|
0.7 * (1 - (dense_embedding <=> query_dense)) + -- Dense similarity
|
|
0.3 * ruvector_sparse_dot(sparse_embedding, query_sparse) AS hybrid_score
|
|
FROM hybrid_documents, query
|
|
ORDER BY hybrid_score DESC
|
|
LIMIT 10;
|
|
*/
|
|
|
|
-- ============================================================================
|
|
-- Utility Operations
|
|
-- ============================================================================
|
|
|
|
-- Convert sparse to dense
|
|
SELECT id, title,
|
|
ruvector_sparse_to_dense(sparse_embedding) AS dense_array
|
|
FROM sparse_documents
|
|
LIMIT 3;
|
|
|
|
-- Get vector statistics
|
|
SELECT
|
|
COUNT(*) as num_documents,
|
|
AVG(ruvector_sparse_nnz(sparse_embedding)) AS avg_nonzero,
|
|
MIN(ruvector_sparse_nnz(sparse_embedding)) AS min_nonzero,
|
|
MAX(ruvector_sparse_nnz(sparse_embedding)) AS max_nonzero,
|
|
AVG(ruvector_sparse_norm(sparse_embedding)) AS avg_norm
|
|
FROM sparse_documents;
|
|
|
|
-- Find documents with similar sparsity
|
|
WITH target AS (
|
|
SELECT sparse_embedding, ruvector_sparse_nnz(sparse_embedding) AS target_nnz
|
|
FROM sparse_documents
|
|
WHERE id = 1
|
|
)
|
|
SELECT d.id, d.title,
|
|
ruvector_sparse_nnz(d.sparse_embedding) AS doc_nnz,
|
|
ABS(ruvector_sparse_nnz(d.sparse_embedding) - t.target_nnz) AS nnz_diff
|
|
FROM sparse_documents d, target t
|
|
WHERE d.id != 1
|
|
ORDER BY nnz_diff
|
|
LIMIT 5;
|
|
|
|
-- ============================================================================
|
|
-- Performance Analysis
|
|
-- ============================================================================
|
|
|
|
-- Check storage size
|
|
SELECT id, title,
|
|
pg_column_size(sparse_embedding) AS sparse_bytes,
|
|
ruvector_sparse_nnz(sparse_embedding) AS num_nonzero,
|
|
pg_column_size(sparse_embedding)::float /
|
|
GREATEST(ruvector_sparse_nnz(sparse_embedding), 1) AS bytes_per_element
|
|
FROM sparse_documents
|
|
ORDER BY sparse_bytes DESC;
|
|
|
|
-- Batch similarity computation
|
|
EXPLAIN ANALYZE
|
|
WITH queries AS (
|
|
SELECT generate_series(1, 3) AS query_id,
|
|
'{1024:0.5, 2048:0.3}'::sparsevec AS query_vec
|
|
)
|
|
SELECT q.query_id, d.id, d.title,
|
|
ruvector_sparse_dot(d.sparse_embedding, q.query_vec) AS score
|
|
FROM sparse_documents d
|
|
CROSS JOIN queries q
|
|
ORDER BY q.query_id, score DESC;
|
|
|
|
-- ============================================================================
|
|
-- Cleanup (optional)
|
|
-- ============================================================================
|
|
|
|
-- DROP TABLE IF EXISTS sparse_documents CASCADE;
|
|
-- DROP TABLE IF EXISTS bm25_articles CASCADE;
|
|
-- DROP TABLE IF EXISTS hybrid_documents CASCADE;
|