mirror of
https://github.com/ruvnet/RuVector.git
synced 2026-05-23 21:25:02 +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>
327 lines
11 KiB
SQL
327 lines
11 KiB
SQL
-- Graph Operations Examples for ruvector-postgres
|
|
-- This file demonstrates the graph database capabilities
|
|
|
|
-- ============================================================================
|
|
-- Basic Graph Operations
|
|
-- ============================================================================
|
|
|
|
-- Create a new graph
|
|
SELECT ruvector_create_graph('social_network');
|
|
|
|
-- List all graphs
|
|
SELECT ruvector_list_graphs();
|
|
|
|
-- ============================================================================
|
|
-- Social Network Example
|
|
-- ============================================================================
|
|
|
|
-- Add users
|
|
SELECT ruvector_add_node(
|
|
'social_network',
|
|
ARRAY['Person'],
|
|
jsonb_build_object('name', 'Alice', 'age', 30, 'city', 'New York')
|
|
) AS alice_id;
|
|
|
|
SELECT ruvector_add_node(
|
|
'social_network',
|
|
ARRAY['Person'],
|
|
jsonb_build_object('name', 'Bob', 'age', 25, 'city', 'San Francisco')
|
|
) AS bob_id;
|
|
|
|
SELECT ruvector_add_node(
|
|
'social_network',
|
|
ARRAY['Person'],
|
|
jsonb_build_object('name', 'Charlie', 'age', 35, 'city', 'Boston')
|
|
) AS charlie_id;
|
|
|
|
SELECT ruvector_add_node(
|
|
'social_network',
|
|
ARRAY['Person'],
|
|
jsonb_build_object('name', 'Diana', 'age', 28, 'city', 'Seattle')
|
|
) AS diana_id;
|
|
|
|
-- Create friendships
|
|
SELECT ruvector_add_edge(
|
|
'social_network',
|
|
1, 2, -- Alice -> Bob
|
|
'FRIENDS',
|
|
jsonb_build_object('since', '2020-01-15', 'strength', 0.9)
|
|
);
|
|
|
|
SELECT ruvector_add_edge(
|
|
'social_network',
|
|
2, 3, -- Bob -> Charlie
|
|
'FRIENDS',
|
|
jsonb_build_object('since', '2019-06-20', 'strength', 0.8)
|
|
);
|
|
|
|
SELECT ruvector_add_edge(
|
|
'social_network',
|
|
1, 4, -- Alice -> Diana
|
|
'FRIENDS',
|
|
jsonb_build_object('since', '2021-03-10', 'strength', 0.7)
|
|
);
|
|
|
|
SELECT ruvector_add_edge(
|
|
'social_network',
|
|
3, 4, -- Charlie -> Diana
|
|
'FRIENDS',
|
|
jsonb_build_object('since', '2020-09-05', 'strength', 0.85)
|
|
);
|
|
|
|
-- Get graph statistics
|
|
SELECT ruvector_graph_stats('social_network');
|
|
|
|
-- Find nodes by label
|
|
SELECT ruvector_find_nodes_by_label('social_network', 'Person');
|
|
|
|
-- Get neighbors of Alice (node 1)
|
|
SELECT ruvector_get_neighbors('social_network', 1);
|
|
|
|
-- Find shortest path from Alice to Charlie
|
|
SELECT ruvector_shortest_path('social_network', 1, 3, 10);
|
|
|
|
-- Find weighted shortest path
|
|
SELECT ruvector_shortest_path_weighted('social_network', 1, 3, 'strength');
|
|
|
|
-- ============================================================================
|
|
-- Cypher Query Examples
|
|
-- ============================================================================
|
|
|
|
-- Create nodes with Cypher
|
|
SELECT ruvector_cypher(
|
|
'social_network',
|
|
'CREATE (n:Person {name: ''Eve'', age: 27, city: ''Austin''}) RETURN n',
|
|
NULL
|
|
);
|
|
|
|
-- Match all persons
|
|
SELECT ruvector_cypher(
|
|
'social_network',
|
|
'MATCH (n:Person) RETURN n.name, n.age',
|
|
NULL
|
|
);
|
|
|
|
-- Match with WHERE clause
|
|
SELECT ruvector_cypher(
|
|
'social_network',
|
|
'MATCH (n:Person) WHERE n.age > 28 RETURN n.name, n.age',
|
|
NULL
|
|
);
|
|
|
|
-- Parameterized query
|
|
SELECT ruvector_cypher(
|
|
'social_network',
|
|
'MATCH (n:Person) WHERE n.name = $name RETURN n',
|
|
jsonb_build_object('name', 'Alice')
|
|
);
|
|
|
|
-- Create relationship with Cypher
|
|
SELECT ruvector_cypher(
|
|
'social_network',
|
|
'CREATE (a:Person {name: ''Frank''})-[:KNOWS {since: 2022}]->(b:Person {name: ''Grace''}) RETURN a, b',
|
|
NULL
|
|
);
|
|
|
|
-- ============================================================================
|
|
-- Knowledge Graph Example
|
|
-- ============================================================================
|
|
|
|
SELECT ruvector_create_graph('knowledge');
|
|
|
|
-- Add concepts
|
|
SELECT ruvector_cypher(
|
|
'knowledge',
|
|
'CREATE (ml:Concept {name: ''Machine Learning'', category: ''AI''})
|
|
CREATE (nn:Concept {name: ''Neural Networks'', category: ''AI''})
|
|
CREATE (dl:Concept {name: ''Deep Learning'', category: ''AI''})
|
|
CREATE (cv:Concept {name: ''Computer Vision'', category: ''AI''})
|
|
CREATE (nlp:Concept {name: ''Natural Language Processing'', category: ''AI''})
|
|
RETURN ml, nn, dl, cv, nlp',
|
|
NULL
|
|
);
|
|
|
|
-- Create relationships between concepts
|
|
WITH ids AS (
|
|
SELECT generate_series(1, 5) AS id
|
|
)
|
|
SELECT
|
|
CASE
|
|
WHEN i.id = 1 THEN ruvector_add_edge('knowledge', 1, 2, 'INCLUDES', '{"strength": 0.9}'::jsonb)
|
|
WHEN i.id = 2 THEN ruvector_add_edge('knowledge', 2, 3, 'SPECIALIZES_IN', '{"strength": 0.95}'::jsonb)
|
|
WHEN i.id = 3 THEN ruvector_add_edge('knowledge', 3, 4, 'APPLIES_TO', '{"strength": 0.85}'::jsonb)
|
|
WHEN i.id = 4 THEN ruvector_add_edge('knowledge', 3, 5, 'APPLIES_TO', '{"strength": 0.9}'::jsonb)
|
|
END AS edge_id
|
|
FROM ids i
|
|
WHERE i.id <= 4;
|
|
|
|
-- Find path from Machine Learning to Computer Vision
|
|
SELECT ruvector_shortest_path('knowledge', 1, 4, 10);
|
|
|
|
-- ============================================================================
|
|
-- Recommendation System Example
|
|
-- ============================================================================
|
|
|
|
SELECT ruvector_create_graph('recommendations');
|
|
|
|
-- Add users and movies
|
|
SELECT ruvector_cypher(
|
|
'recommendations',
|
|
'CREATE (u1:User {name: ''Alice'', preference: ''SciFi''})
|
|
CREATE (u2:User {name: ''Bob'', preference: ''Action''})
|
|
CREATE (u3:User {name: ''Charlie'', preference: ''SciFi''})
|
|
CREATE (m1:Movie {title: ''Inception'', genre: ''SciFi''})
|
|
CREATE (m2:Movie {title: ''Interstellar'', genre: ''SciFi''})
|
|
CREATE (m3:Movie {title: ''The Matrix'', genre: ''SciFi''})
|
|
CREATE (m4:Movie {title: ''Die Hard'', genre: ''Action''})
|
|
RETURN u1, u2, u3, m1, m2, m3, m4',
|
|
NULL
|
|
);
|
|
|
|
-- Create watch history
|
|
SELECT ruvector_add_edge('recommendations', 1, 4, 'WATCHED', '{"rating": 5, "timestamp": "2024-01-15"}'::jsonb);
|
|
SELECT ruvector_add_edge('recommendations', 1, 5, 'WATCHED', '{"rating": 4, "timestamp": "2024-01-20"}'::jsonb);
|
|
SELECT ruvector_add_edge('recommendations', 2, 7, 'WATCHED', '{"rating": 5, "timestamp": "2024-01-18"}'::jsonb);
|
|
SELECT ruvector_add_edge('recommendations', 3, 4, 'WATCHED', '{"rating": 5, "timestamp": "2024-01-22"}'::jsonb);
|
|
SELECT ruvector_add_edge('recommendations', 3, 6, 'WATCHED', '{"rating": 4, "timestamp": "2024-01-25"}'::jsonb);
|
|
|
|
-- Get statistics
|
|
SELECT ruvector_graph_stats('recommendations');
|
|
|
|
-- ============================================================================
|
|
-- Organizational Hierarchy Example
|
|
-- ============================================================================
|
|
|
|
SELECT ruvector_create_graph('org_chart');
|
|
|
|
-- Create organizational structure
|
|
SELECT ruvector_cypher(
|
|
'org_chart',
|
|
'CREATE (ceo:Employee {name: ''Jane Doe'', title: ''CEO'', level: 1})
|
|
CREATE (cto:Employee {name: ''John Smith'', title: ''CTO'', level: 2})
|
|
CREATE (cfo:Employee {name: ''Emily Brown'', title: ''CFO'', level: 2})
|
|
CREATE (dev1:Employee {name: ''Alex Johnson'', title: ''Senior Dev'', level: 3})
|
|
CREATE (dev2:Employee {name: ''Sarah Wilson'', title: ''Senior Dev'', level: 3})
|
|
CREATE (acc1:Employee {name: ''Michael Davis'', title: ''Accountant'', level: 3})
|
|
RETURN ceo, cto, cfo, dev1, dev2, acc1',
|
|
NULL
|
|
);
|
|
|
|
-- Create reporting structure
|
|
SELECT ruvector_add_edge('org_chart', 2, 1, 'REPORTS_TO', '{}'::jsonb);
|
|
SELECT ruvector_add_edge('org_chart', 3, 1, 'REPORTS_TO', '{}'::jsonb);
|
|
SELECT ruvector_add_edge('org_chart', 4, 2, 'REPORTS_TO', '{}'::jsonb);
|
|
SELECT ruvector_add_edge('org_chart', 5, 2, 'REPORTS_TO', '{}'::jsonb);
|
|
SELECT ruvector_add_edge('org_chart', 6, 3, 'REPORTS_TO', '{}'::jsonb);
|
|
|
|
-- Find all employees reporting to CTO (directly or indirectly)
|
|
SELECT ruvector_shortest_path('org_chart', 4, 1, 5); -- Path from dev1 to CEO
|
|
SELECT ruvector_shortest_path('org_chart', 5, 1, 5); -- Path from dev2 to CEO
|
|
|
|
-- ============================================================================
|
|
-- Transport Network Example
|
|
-- ============================================================================
|
|
|
|
SELECT ruvector_create_graph('transport');
|
|
|
|
-- Add cities as nodes
|
|
SELECT ruvector_add_node('transport', ARRAY['City'], '{"name": "New York", "population": 8336817}'::jsonb);
|
|
SELECT ruvector_add_node('transport', ARRAY['City'], '{"name": "Boston", "population": 692600}'::jsonb);
|
|
SELECT ruvector_add_node('transport', ARRAY['City'], '{"name": "Philadelphia", "population": 1584064}'::jsonb);
|
|
SELECT ruvector_add_node('transport', ARRAY['City'], '{"name": "Washington DC", "population": 705749}'::jsonb);
|
|
|
|
-- Add routes with distances
|
|
SELECT ruvector_add_edge('transport', 1, 2, 'ROUTE', '{"distance": 215, "mode": "train", "duration": 4.5}'::jsonb);
|
|
SELECT ruvector_add_edge('transport', 1, 3, 'ROUTE', '{"distance": 95, "mode": "train", "duration": 1.5}'::jsonb);
|
|
SELECT ruvector_add_edge('transport', 3, 4, 'ROUTE', '{"distance": 140, "mode": "train", "duration": 2.5}'::jsonb);
|
|
SELECT ruvector_add_edge('transport', 2, 3, 'ROUTE', '{"distance": 310, "mode": "train", "duration": 5.5}'::jsonb);
|
|
|
|
-- Find shortest route by distance
|
|
SELECT ruvector_shortest_path_weighted('transport', 2, 4, 'distance');
|
|
|
|
-- Find fastest route by duration
|
|
SELECT ruvector_shortest_path_weighted('transport', 2, 4, 'duration');
|
|
|
|
-- ============================================================================
|
|
-- Analytics Queries
|
|
-- ============================================================================
|
|
|
|
-- Get all graphs with their statistics
|
|
SELECT
|
|
name,
|
|
(ruvector_graph_stats(name)::jsonb)->>'node_count' AS nodes,
|
|
(ruvector_graph_stats(name)::jsonb)->>'edge_count' AS edges
|
|
FROM (
|
|
SELECT unnest(ruvector_list_graphs()) AS name
|
|
) graphs;
|
|
|
|
-- ============================================================================
|
|
-- Cleanup
|
|
-- ============================================================================
|
|
|
|
-- Delete specific graph
|
|
-- SELECT ruvector_delete_graph('social_network');
|
|
|
|
-- Delete all graphs
|
|
-- SELECT ruvector_delete_graph(name)
|
|
-- FROM unnest(ruvector_list_graphs()) AS name;
|
|
|
|
-- ============================================================================
|
|
-- Performance Testing
|
|
-- ============================================================================
|
|
|
|
-- Create a larger graph for performance testing
|
|
SELECT ruvector_create_graph('perf_test');
|
|
|
|
-- Generate random nodes
|
|
DO $$
|
|
DECLARE
|
|
i INTEGER;
|
|
BEGIN
|
|
FOR i IN 1..1000 LOOP
|
|
PERFORM ruvector_add_node(
|
|
'perf_test',
|
|
ARRAY['Node'],
|
|
jsonb_build_object('id', i, 'value', random() * 100)
|
|
);
|
|
END LOOP;
|
|
END $$;
|
|
|
|
-- Generate random edges
|
|
DO $$
|
|
DECLARE
|
|
i INTEGER;
|
|
source_id INTEGER;
|
|
target_id INTEGER;
|
|
BEGIN
|
|
FOR i IN 1..5000 LOOP
|
|
source_id := 1 + floor(random() * 1000)::INTEGER;
|
|
target_id := 1 + floor(random() * 1000)::INTEGER;
|
|
IF source_id <> target_id THEN
|
|
BEGIN
|
|
PERFORM ruvector_add_edge(
|
|
'perf_test',
|
|
source_id,
|
|
target_id,
|
|
'CONNECTS',
|
|
jsonb_build_object('weight', random())
|
|
);
|
|
EXCEPTION WHEN OTHERS THEN
|
|
-- Ignore errors (e.g., duplicate edges)
|
|
NULL;
|
|
END;
|
|
END IF;
|
|
END LOOP;
|
|
END $$;
|
|
|
|
-- Check performance stats
|
|
SELECT ruvector_graph_stats('perf_test');
|
|
|
|
-- Test path finding performance
|
|
\timing on
|
|
SELECT ruvector_shortest_path('perf_test', 1, 500, 20);
|
|
SELECT ruvector_shortest_path_weighted('perf_test', 1, 500, 'weight');
|
|
\timing off
|
|
|
|
-- Cleanup performance test
|
|
-- SELECT ruvector_delete_graph('perf_test');
|