mirror of
https://github.com/ruvnet/RuVector.git
synced 2026-05-27 00:25:10 +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>
5.8 KiB
5.8 KiB
Graph Operations Quick Reference
Installation
CREATE EXTENSION ruvector_postgres;
Graph Management
-- Create graph
SELECT ruvector_create_graph('my_graph');
-- List graphs
SELECT ruvector_list_graphs();
-- Get statistics
SELECT ruvector_graph_stats('my_graph');
-- Delete graph
SELECT ruvector_delete_graph('my_graph');
Node Operations
-- Add node
SELECT ruvector_add_node(
'graph_name',
ARRAY['Label1', 'Label2'],
'{"property": "value"}'::jsonb
) AS node_id;
-- Get node
SELECT ruvector_get_node('graph_name', 1);
-- Find by label
SELECT ruvector_find_nodes_by_label('graph_name', 'Person');
Edge Operations
-- Add edge
SELECT ruvector_add_edge(
'graph_name',
1, -- source_id
2, -- target_id
'RELATIONSHIP_TYPE',
'{"weight": 1.0}'::jsonb
) AS edge_id;
-- Get edge
SELECT ruvector_get_edge('graph_name', 1);
-- Get neighbors
SELECT ruvector_get_neighbors('graph_name', 1);
Path Finding
-- Shortest path (unweighted)
SELECT ruvector_shortest_path(
'graph_name',
1, -- start_id
10, -- end_id
5 -- max_hops
);
-- Shortest path (weighted)
SELECT ruvector_shortest_path_weighted(
'graph_name',
1, -- start_id
10, -- end_id
'weight' -- property for weights
);
Cypher Queries
CREATE
-- Create node
SELECT ruvector_cypher(
'graph_name',
'CREATE (n:Person {name: ''Alice'', age: 30}) RETURN n',
NULL
);
-- Create relationship
SELECT ruvector_cypher(
'graph_name',
'CREATE (a:Person {name: ''Alice''})-[:KNOWS {since: 2020}]->(b:Person {name: ''Bob''}) RETURN a, b',
NULL
);
MATCH
-- Match all nodes
SELECT ruvector_cypher(
'graph_name',
'MATCH (n:Person) RETURN n',
NULL
);
-- Match with WHERE
SELECT ruvector_cypher(
'graph_name',
'MATCH (n:Person) WHERE n.age > 25 RETURN n.name, n.age',
NULL
);
-- Parameterized query
SELECT ruvector_cypher(
'graph_name',
'MATCH (n:Person) WHERE n.name = $name RETURN n',
'{"name": "Alice"}'::jsonb
);
Common Patterns
Social Network
-- Setup
SELECT ruvector_create_graph('social');
-- Add users
SELECT ruvector_add_node('social', ARRAY['Person'],
jsonb_build_object('name', 'Alice', 'age', 30));
SELECT ruvector_add_node('social', ARRAY['Person'],
jsonb_build_object('name', 'Bob', 'age', 25));
-- Create friendship
SELECT ruvector_add_edge('social', 1, 2, 'FRIENDS',
'{"since": "2020-01-15"}'::jsonb);
-- Find path
SELECT ruvector_shortest_path('social', 1, 2, 10);
Knowledge Graph
-- Setup
SELECT ruvector_create_graph('knowledge');
-- Add concepts with Cypher
SELECT ruvector_cypher('knowledge',
'CREATE (ml:Concept {name: ''Machine Learning''})
CREATE (dl:Concept {name: ''Deep Learning''})
CREATE (ml)-[:INCLUDES]->(dl)
RETURN ml, dl',
NULL
);
-- Query relationships
SELECT ruvector_cypher('knowledge',
'MATCH (a:Concept)-[:INCLUDES]->(b:Concept)
RETURN a.name, b.name',
NULL
);
Recommendation
-- Setup
SELECT ruvector_create_graph('recommendations');
-- Add users and items
SELECT ruvector_cypher('recommendations',
'CREATE (u:User {name: ''Alice''})
CREATE (m:Movie {title: ''Inception''})
CREATE (u)-[:WATCHED {rating: 5}]->(m)
RETURN u, m',
NULL
);
-- Find similar users
SELECT ruvector_cypher('recommendations',
'MATCH (u1:User)-[:WATCHED]->(m:Movie)<-[:WATCHED]-(u2:User)
WHERE u1.name = ''Alice''
RETURN u2.name',
NULL
);
Performance Tips
- Use labels for filtering: Labels are indexed
- Limit hop count: Specify reasonable max_hops
- Batch operations: Use Cypher for multiple creates
- Property indexes: Filter on indexed properties
- Parameterized queries: Reuse query plans
Return Value Formats
Graph Stats
{
"name": "my_graph",
"node_count": 100,
"edge_count": 250,
"labels": ["Person", "Movie"],
"edge_types": ["KNOWS", "WATCHED"]
}
Path Result
{
"nodes": [1, 3, 5, 10],
"edges": [12, 45, 78],
"length": 4,
"cost": 2.5
}
Node
{
"id": 1,
"labels": ["Person"],
"properties": {
"name": "Alice",
"age": 30
}
}
Edge
{
"id": 1,
"source": 1,
"target": 2,
"edge_type": "KNOWS",
"properties": {
"since": "2020-01-15",
"weight": 0.9
}
}
Error Handling
-- Check if graph exists before operations
DO $$
BEGIN
IF 'my_graph' = ANY(ruvector_list_graphs()) THEN
-- Perform operations
RAISE NOTICE 'Graph exists';
ELSE
PERFORM ruvector_create_graph('my_graph');
END IF;
END $$;
-- Handle missing nodes
DO $$
DECLARE
result jsonb;
BEGIN
result := ruvector_get_node('my_graph', 999);
IF result IS NULL THEN
RAISE NOTICE 'Node not found';
END IF;
END $$;
Best Practices
- Name graphs clearly: Use descriptive names
- Use labels consistently: Establish naming conventions
- Index frequently queried properties: Plan for performance
- Batch similar operations: Use Cypher for efficiency
- Clean up unused graphs: Use delete_graph when done
- Monitor statistics: Check graph_stats regularly
- Test queries: Verify results before production
- Use parameters: Prevent injection, enable caching
Limitations
- In-memory only: No persistence across restarts
- Single-node: No distributed graph support
- Simplified Cypher: Basic patterns only
- No transactions: Operations are atomic but not grouped
- No constraints: No unique or foreign key constraints