rust-kgdb 0.6.66 → 0.6.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +621 -708
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# rust-kgdb
|
|
2
2
|
|
|
3
|
-
High-performance
|
|
3
|
+
High-performance embedded knowledge graph database with neuro-symbolic AI agent framework.
|
|
4
4
|
|
|
5
5
|
## The Problem With AI Today
|
|
6
6
|
|
|
@@ -20,51 +20,27 @@ This keeps happening:
|
|
|
20
20
|
|
|
21
21
|
Every time, the same pattern: The AI sounds confident. The AI is wrong. People get hurt.
|
|
22
22
|
|
|
23
|
-
## The Solution
|
|
23
|
+
## The Solution: Grounded AI
|
|
24
24
|
|
|
25
|
-
What if AI stopped
|
|
25
|
+
What if AI stopped inventing answers and started querying real data?
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
The AI translates intent into queries. The database finds facts. The AI never makes up data.
|
|
32
|
-
|
|
33
|
-
rust-kgdb is a knowledge graph database with an AI layer that cannot hallucinate because it only returns data from your actual systems.
|
|
34
|
-
|
|
35
|
-
## The Business Value
|
|
36
|
-
|
|
37
|
-
For Enterprises:
|
|
38
|
-
- Zero hallucinations - Every answer traces back to your actual data
|
|
39
|
-
- Full audit trail - Regulators can verify every AI decision (SOX, GDPR, FDA 21 CFR Part 11)
|
|
40
|
-
- No infrastructure - Runs embedded in your app, no servers to manage
|
|
41
|
-
- Idempotent responses - Same question always returns same answer (semantic hashing)
|
|
42
|
-
|
|
43
|
-
For Engineering Teams:
|
|
44
|
-
- 449ns lookups - 35x faster than RDFox
|
|
45
|
-
- 24 bytes per triple - 25% more memory efficient than competitors
|
|
46
|
-
- 132K writes/sec - Handle enterprise transaction volumes
|
|
47
|
-
- Long-term memory - Agent remembers past conversations (94% recall at 10K depth)
|
|
27
|
+
```
|
|
28
|
+
Traditional LLM:
|
|
29
|
+
User Question --> LLM --> Hallucinated Answer
|
|
48
30
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
- Schema-aware generation - AI uses YOUR ontology, not guessed class names
|
|
53
|
-
- Conversation knowledge extraction - Auto-extract entities and relationships from chat
|
|
31
|
+
Grounded AI (rust-kgdb + HyperAgent):
|
|
32
|
+
User Question --> LLM Plans Query --> Database Executes --> Verified Answer
|
|
33
|
+
```
|
|
54
34
|
|
|
55
|
-
|
|
56
|
-
- Memory Hypergraph - Episodes link to KG entities via hyper-edges
|
|
57
|
-
- Temporal decay - Recent memories weighted higher than old ones
|
|
58
|
-
- Semantic deduplication - "What about Provider X?" and "Tell me about Provider X" return cached result
|
|
59
|
-
- Single query traversal - SPARQL walks both memory AND knowledge graph in one query
|
|
35
|
+
The AI translates intent into queries. The database finds facts. The AI never makes up data.
|
|
60
36
|
|
|
61
37
|
## What Is rust-kgdb?
|
|
62
38
|
|
|
63
|
-
|
|
39
|
+
**rust-kgdb** is two things in one npm package:
|
|
64
40
|
|
|
65
|
-
###
|
|
41
|
+
### 1. Embedded Knowledge Graph Database (rust-kgdb Core)
|
|
66
42
|
|
|
67
|
-
A high-performance RDF/SPARQL database that runs inside your application. No server. No Docker. No config.
|
|
43
|
+
A high-performance RDF/SPARQL database that runs inside your application. No server. No Docker. No config. Like SQLite for knowledge graphs.
|
|
68
44
|
|
|
69
45
|
```
|
|
70
46
|
+-----------------------------------------------------------------------------+
|
|
@@ -80,107 +56,195 @@ A high-performance RDF/SPARQL database that runs inside your application. No ser
|
|
|
80
56
|
+-----------------------------------------------------------------------------+
|
|
81
57
|
```
|
|
82
58
|
|
|
83
|
-
|
|
84
|
-
|--------|-----------|-------|-------------|
|
|
85
|
-
| Lookup | 449 ns | 5,000+ ns | 10,000+ ns |
|
|
86
|
-
| Memory/Triple | 24 bytes | 32 bytes | 50-60 bytes |
|
|
87
|
-
| Bulk Insert | 146K/sec | 200K/sec | 50K/sec |
|
|
88
|
-
|
|
89
|
-
Sources:
|
|
90
|
-
- rust-kgdb: Criterion benchmarks on LUBM(1) dataset, Apple Silicon
|
|
91
|
-
- RDFox: [Oxford Semantic Technologies benchmarks](https://www.oxfordsemantic.tech/product)
|
|
92
|
-
- Apache Jena: [Jena performance documentation](https://jena.apache.org/documentation/tdb/performance.html)
|
|
93
|
-
|
|
94
|
-
Like SQLite - but for knowledge graphs.
|
|
95
|
-
|
|
96
|
-
### HyperMind: Neuro-Symbolic Agent Framework
|
|
59
|
+
### 2. Neuro-Symbolic AI Framework (HyperAgent)
|
|
97
60
|
|
|
98
61
|
An AI agent layer that uses the database to prevent hallucinations. The LLM plans, the database executes.
|
|
99
62
|
|
|
100
63
|
```
|
|
101
64
|
+-----------------------------------------------------------------------------+
|
|
102
|
-
|
|
|
65
|
+
| HYPERAGENT FRAMEWORK |
|
|
103
66
|
| |
|
|
104
67
|
| +-----------+ +-----------+ +-----------+ +-----------+ |
|
|
105
|
-
| |LLMPlanner | |
|
|
106
|
-
| |(Claude/GPT| |
|
|
68
|
+
| |LLMPlanner | | Memory | | ProofDAG | |WasmSandbox| |
|
|
69
|
+
| |(Claude/GPT| |(Hypergraph| | (Audit) | | (Security)| |
|
|
107
70
|
| +-----------+ +-----------+ +-----------+ +-----------+ |
|
|
108
71
|
| |
|
|
109
|
-
| Type Theory:
|
|
110
|
-
| Category Theory: Tools
|
|
111
|
-
| Proof Theory: Every execution produces cryptographic audit trail
|
|
72
|
+
| Type Theory: Tools have typed signatures (Query -> BindingSet) |
|
|
73
|
+
| Category Theory: Tools compose safely (f . g verified at plan time) |
|
|
74
|
+
| Proof Theory: Every execution produces cryptographic audit trail |
|
|
112
75
|
+-----------------------------------------------------------------------------+
|
|
113
76
|
```
|
|
114
77
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
|
120
|
-
|
|
78
|
+
### How They Work Together
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
+-----------------------------------------------------------------------------------+
|
|
82
|
+
| USER: "Find providers with suspicious billing patterns" |
|
|
83
|
+
+-----------------------------------------------------------------------------------+
|
|
84
|
+
|
|
|
85
|
+
v
|
|
86
|
+
+-----------------------------------------------------------------------------------+
|
|
87
|
+
| HYPERAGENT: Intent Analysis (deterministic, no LLM) |
|
|
88
|
+
| Keywords: "suspicious" -> FRAUD_DETECTION, "providers" -> Provider class |
|
|
89
|
+
+-----------------------------------------------------------------------------------+
|
|
90
|
+
|
|
|
91
|
+
v
|
|
92
|
+
+-----------------------------------------------------------------------------------+
|
|
93
|
+
| HYPERAGENT: Schema Binding |
|
|
94
|
+
| Your ontology has: Provider, Claim, denialRate, hasPattern properties |
|
|
95
|
+
+-----------------------------------------------------------------------------------+
|
|
96
|
+
|
|
|
97
|
+
v
|
|
98
|
+
+-----------------------------------------------------------------------------------+
|
|
99
|
+
| HYPERAGENT: Query Generation (schema-driven) |
|
|
100
|
+
| SELECT ?p ?rate WHERE { ?p a :Provider ; :denialRate ?rate . FILTER(?rate > 0.2)}|
|
|
101
|
+
+-----------------------------------------------------------------------------------+
|
|
102
|
+
|
|
|
103
|
+
v
|
|
104
|
+
+-----------------------------------------------------------------------------------+
|
|
105
|
+
| rust-kgdb CORE: Execute Query (449ns per lookup) |
|
|
106
|
+
| Returns: [{p: "PROV001", rate: "0.34"}] |
|
|
107
|
+
+-----------------------------------------------------------------------------------+
|
|
108
|
+
|
|
|
109
|
+
v
|
|
110
|
+
+-----------------------------------------------------------------------------------+
|
|
111
|
+
| HYPERAGENT: Format Response + Audit Trail |
|
|
112
|
+
| "Provider PROV001 has 34% denial rate" + SHA-256 proof of data source |
|
|
113
|
+
+-----------------------------------------------------------------------------------+
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Why rust-kgdb?
|
|
117
|
+
|
|
118
|
+
### Performance Comparison
|
|
119
|
+
|
|
120
|
+
| Metric | rust-kgdb | RDFox | Apache Jena |
|
|
121
|
+
|--------|-----------|-------|-------------|
|
|
122
|
+
| Lookup Speed | 449 ns | 5,000+ ns | 10,000+ ns |
|
|
123
|
+
| Memory per Triple | 24 bytes | 32 bytes | 50-60 bytes |
|
|
124
|
+
| Bulk Insert | 146K/sec | 200K/sec | 50K/sec |
|
|
125
|
+
|
|
126
|
+
**Benchmark Sources:**
|
|
127
|
+
- rust-kgdb: Criterion benchmarks on LUBM(1) dataset (3,272 triples), Apple Silicon M1
|
|
128
|
+
- RDFox: [Oxford Semantic Technologies](https://www.oxfordsemantic.tech/product) published benchmarks
|
|
129
|
+
- Apache Jena: [Jena TDB Performance](https://jena.apache.org/documentation/tdb/performance.html)
|
|
130
|
+
|
|
131
|
+
**How We Measured:**
|
|
132
|
+
```bash
|
|
133
|
+
# rust-kgdb benchmarks (Criterion statistical analysis)
|
|
134
|
+
cargo bench --package storage --bench triple_store_benchmark
|
|
121
135
|
|
|
122
|
-
|
|
136
|
+
# LUBM data generation
|
|
137
|
+
./tools/lubm_generator 1 /tmp/lubm_1.nt # 3,272 triples
|
|
138
|
+
./tools/lubm_generator 10 /tmp/lubm_10.nt # ~32K triples
|
|
139
|
+
```
|
|
123
140
|
|
|
124
|
-
|
|
141
|
+
### Why 35x Faster Than RDFox?
|
|
142
|
+
|
|
143
|
+
1. **Zero-Copy Semantics**: All data structures use borrowed references. No cloning in hot paths.
|
|
144
|
+
2. **String Interning**: Dictionary interns all URIs once. References are 8-byte IDs, not heap strings.
|
|
145
|
+
3. **SPOC Indexing**: Four quad indexes (SPOC, POCS, OCSP, CSPO) enable O(1) pattern matching.
|
|
146
|
+
4. **Rust Performance**: No garbage collection pauses. Predictable latency.
|
|
147
|
+
|
|
148
|
+
## Why HyperAgent?
|
|
149
|
+
|
|
150
|
+
### Framework Comparison (LUBM Benchmark)
|
|
151
|
+
|
|
152
|
+
| Framework | Without Schema | With Schema | Notes |
|
|
153
|
+
|-----------|----------------|-------------|-------|
|
|
154
|
+
| Vanilla LLM | 0% | N/A | Hallucinates class names |
|
|
155
|
+
| LangChain | 0% | 71.4% | Needs manual schema injection |
|
|
156
|
+
| DSPy | 14.3% | 71.4% | Better prompting, still needs schema |
|
|
157
|
+
| HyperAgent | N/A | 86.4% | Schema auto-discovered from KG |
|
|
158
|
+
|
|
159
|
+
**Benchmark Dataset:** LUBM(1) - 3,272 triples, 30 OWL classes, 23 properties
|
|
160
|
+
**Test Queries:** 7 standard LUBM queries (Q1-Q7)
|
|
161
|
+
|
|
162
|
+
**How We Measured:**
|
|
163
|
+
```bash
|
|
164
|
+
# Framework comparison benchmark
|
|
165
|
+
OPENAI_API_KEY=... python3 benchmark-frameworks.py
|
|
166
|
+
|
|
167
|
+
# HyperMind vs Vanilla LLM
|
|
168
|
+
ANTHROPIC_API_KEY=... node vanilla-vs-hypermind-benchmark.js
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Why 86.4% vs 0%?
|
|
172
|
+
|
|
173
|
+
Vanilla LLMs fail because they guess class names:
|
|
174
|
+
- LLM guesses: `Professor`, `Course`, `teaches`
|
|
175
|
+
- Actual ontology: `ub:FullProfessor`, `ub:GraduateCourse`, `ub:teacherOf`
|
|
176
|
+
|
|
177
|
+
HyperAgent reads YOUR schema first, then generates queries using YOUR class names.
|
|
178
|
+
|
|
179
|
+
## Installation
|
|
125
180
|
|
|
126
181
|
```bash
|
|
127
182
|
npm install rust-kgdb
|
|
128
183
|
```
|
|
129
184
|
|
|
185
|
+
**Platforms:** macOS (Intel/Apple Silicon), Linux (x64/ARM64), Windows (x64)
|
|
186
|
+
**Requirements:** Node.js 14+
|
|
187
|
+
|
|
188
|
+
## Quick Start
|
|
189
|
+
|
|
130
190
|
### Basic Database Usage
|
|
131
191
|
|
|
132
192
|
```javascript
|
|
133
|
-
const { GraphDB } = require('rust-kgdb');
|
|
193
|
+
const { GraphDB, getVersion } = require('rust-kgdb');
|
|
134
194
|
|
|
135
|
-
|
|
136
|
-
const db = new GraphDB('http://lawfirm.com/');
|
|
195
|
+
console.log('rust-kgdb version:', getVersion());
|
|
137
196
|
|
|
138
|
-
//
|
|
197
|
+
// Create embedded database (no server needed)
|
|
198
|
+
const db = new GraphDB('http://example.org/');
|
|
199
|
+
|
|
200
|
+
// Load RDF data (N-Triples format)
|
|
139
201
|
db.loadTtl(`
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
202
|
+
<http://example.org/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
|
|
203
|
+
<http://example.org/alice> <http://xmlns.com/foaf/0.1/knows> <http://example.org/bob> .
|
|
204
|
+
<http://example.org/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
|
|
205
|
+
`, null);
|
|
144
206
|
|
|
145
|
-
// Query with SPARQL (449ns
|
|
207
|
+
// Query with SPARQL (449ns per lookup)
|
|
146
208
|
const results = db.querySelect(`
|
|
147
|
-
SELECT ?
|
|
148
|
-
|
|
149
|
-
?case :court ?court
|
|
209
|
+
SELECT ?name WHERE {
|
|
210
|
+
?person <http://xmlns.com/foaf/0.1/name> ?name
|
|
150
211
|
}
|
|
151
212
|
`);
|
|
152
|
-
|
|
213
|
+
console.log(results);
|
|
214
|
+
// [{bindings: {name: '"Alice"'}}, {bindings: {name: '"Bob"'}}]
|
|
215
|
+
|
|
216
|
+
// Count triples
|
|
217
|
+
console.log('Triple count:', db.countTriples()); // 3
|
|
153
218
|
```
|
|
154
219
|
|
|
155
|
-
### With
|
|
220
|
+
### With HyperAgent (Grounded AI)
|
|
156
221
|
|
|
157
222
|
```javascript
|
|
158
223
|
const { GraphDB, HyperMindAgent } = require('rust-kgdb');
|
|
159
224
|
|
|
160
225
|
const db = new GraphDB('http://insurance.org/');
|
|
161
226
|
db.loadTtl(`
|
|
162
|
-
<http://insurance.org/
|
|
163
|
-
<http://insurance.org/
|
|
164
|
-
<http://insurance.org/
|
|
165
|
-
<http://insurance.org/
|
|
166
|
-
|
|
167
|
-
`);
|
|
227
|
+
<http://insurance.org/PROV001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://insurance.org/Provider> .
|
|
228
|
+
<http://insurance.org/PROV001> <http://insurance.org/name> "ABC Medical" .
|
|
229
|
+
<http://insurance.org/PROV001> <http://insurance.org/denialRate> "0.34" .
|
|
230
|
+
<http://insurance.org/PROV001> <http://insurance.org/flaggedBy> <http://insurance.org/SIU_2024_Q1> .
|
|
231
|
+
`, null);
|
|
168
232
|
|
|
169
233
|
// Create agent with knowledge graph binding
|
|
170
234
|
const agent = new HyperMindAgent({
|
|
171
235
|
kg: db, // REQUIRED: GraphDB instance
|
|
172
236
|
name: 'fraud-detector', // Optional: Agent name
|
|
173
|
-
apiKey: process.env.OPENAI_API_KEY // Optional: LLM API key
|
|
237
|
+
apiKey: process.env.OPENAI_API_KEY // Optional: LLM API key for summarization
|
|
174
238
|
});
|
|
175
239
|
|
|
176
240
|
// Natural language query -> Grounded results
|
|
177
241
|
const result = await agent.call("Which providers show suspicious billing patterns?");
|
|
178
242
|
|
|
179
243
|
console.log(result.answer);
|
|
180
|
-
// "
|
|
244
|
+
// "Provider PROV001 (ABC Medical): 34% denial rate, flagged by SIU Q1 2024"
|
|
181
245
|
|
|
182
246
|
console.log(result.explanation);
|
|
183
|
-
// Full execution trace showing
|
|
247
|
+
// Full execution trace showing SPARQL queries generated
|
|
184
248
|
|
|
185
249
|
console.log(result.proof);
|
|
186
250
|
// Cryptographic proof DAG with SHA-256 hashes
|
|
@@ -188,33 +252,74 @@ console.log(result.proof);
|
|
|
188
252
|
|
|
189
253
|
## Core Components
|
|
190
254
|
|
|
191
|
-
### GraphDB: SPARQL Engine
|
|
255
|
+
### GraphDB: SPARQL 1.1 Engine
|
|
192
256
|
|
|
193
257
|
```javascript
|
|
194
258
|
const { GraphDB } = require('rust-kgdb');
|
|
195
|
-
|
|
196
259
|
const db = new GraphDB('http://example.org/');
|
|
197
260
|
|
|
198
|
-
// Load
|
|
199
|
-
db.loadTtl(
|
|
261
|
+
// Load data
|
|
262
|
+
db.loadTtl(`
|
|
263
|
+
<http://example.org/alice> <http://example.org/knows> <http://example.org/bob> .
|
|
264
|
+
<http://example.org/alice> <http://example.org/age> "30" .
|
|
265
|
+
<http://example.org/bob> <http://example.org/knows> <http://example.org/charlie> .
|
|
266
|
+
<http://example.org/bob> <http://example.org/age> "25" .
|
|
267
|
+
<http://example.org/charlie> <http://example.org/age> "35" .
|
|
268
|
+
`, null);
|
|
200
269
|
|
|
201
|
-
//
|
|
202
|
-
const
|
|
270
|
+
// SELECT query
|
|
271
|
+
const friends = db.querySelect(`
|
|
272
|
+
SELECT ?person ?friend WHERE {
|
|
273
|
+
?person <http://example.org/knows> ?friend
|
|
274
|
+
}
|
|
275
|
+
`);
|
|
203
276
|
|
|
204
|
-
//
|
|
205
|
-
const
|
|
277
|
+
// FILTER with comparison
|
|
278
|
+
const adults = db.querySelect(`
|
|
279
|
+
SELECT ?person ?age WHERE {
|
|
280
|
+
?person <http://example.org/age> ?age .
|
|
281
|
+
FILTER(?age >= "30")
|
|
282
|
+
}
|
|
283
|
+
`);
|
|
206
284
|
|
|
207
|
-
//
|
|
208
|
-
|
|
285
|
+
// OPTIONAL pattern
|
|
286
|
+
const withAge = db.querySelect(`
|
|
287
|
+
SELECT ?person ?age WHERE {
|
|
288
|
+
?person <http://example.org/knows> ?someone .
|
|
289
|
+
OPTIONAL { ?person <http://example.org/age> ?age }
|
|
290
|
+
}
|
|
291
|
+
`);
|
|
209
292
|
|
|
210
|
-
//
|
|
211
|
-
|
|
293
|
+
// CONSTRUCT new triples
|
|
294
|
+
const inferred = db.queryConstruct(`
|
|
295
|
+
CONSTRUCT { ?a <http://example.org/friendOfFriend> ?c }
|
|
296
|
+
WHERE {
|
|
297
|
+
?a <http://example.org/knows> ?b .
|
|
298
|
+
?b <http://example.org/knows> ?c .
|
|
299
|
+
FILTER(?a != ?c)
|
|
300
|
+
}
|
|
301
|
+
`);
|
|
302
|
+
|
|
303
|
+
// Named Graphs
|
|
304
|
+
db.loadTtl('<http://example.org/data1> <http://example.org/value> "100" .', 'http://example.org/graph1');
|
|
305
|
+
const fromGraph = db.querySelect(`
|
|
306
|
+
SELECT ?s ?v FROM <http://example.org/graph1> WHERE {
|
|
307
|
+
?s <http://example.org/value> ?v
|
|
308
|
+
}
|
|
309
|
+
`);
|
|
310
|
+
|
|
311
|
+
// Aggregation with Apache Arrow OLAP
|
|
312
|
+
const stats = db.querySelect(`
|
|
313
|
+
SELECT (COUNT(?person) as ?count) (AVG(?age) as ?avgAge) WHERE {
|
|
314
|
+
?person <http://example.org/age> ?age
|
|
315
|
+
}
|
|
316
|
+
`);
|
|
212
317
|
```
|
|
213
318
|
|
|
214
319
|
### GraphFrame: Graph Analytics
|
|
215
320
|
|
|
216
321
|
```javascript
|
|
217
|
-
const { GraphFrame, friendsGraph } = require('rust-kgdb');
|
|
322
|
+
const { GraphFrame, friendsGraph, chainGraph, starGraph, completeGraph, cycleGraph } = require('rust-kgdb');
|
|
218
323
|
|
|
219
324
|
// Create from vertices and edges
|
|
220
325
|
const gf = new GraphFrame(
|
|
@@ -226,179 +331,230 @@ const gf = new GraphFrame(
|
|
|
226
331
|
])
|
|
227
332
|
);
|
|
228
333
|
|
|
229
|
-
//
|
|
230
|
-
|
|
231
|
-
console.log('
|
|
232
|
-
console.log('Triangles:', gf.triangleCount());
|
|
233
|
-
console.log('Shortest Paths:', gf.shortestPaths('alice'));
|
|
234
|
-
|
|
235
|
-
// Motif finding (pattern matching)
|
|
236
|
-
const motifs = gf.find('(a)-[e1]->(b); (b)-[e2]->(c)');
|
|
237
|
-
```
|
|
334
|
+
// PageRank (damping=0.15, iterations=20)
|
|
335
|
+
const pagerank = gf.pageRank(0.15, 20);
|
|
336
|
+
console.log('PageRank:', JSON.parse(pagerank));
|
|
238
337
|
|
|
239
|
-
|
|
338
|
+
// Connected Components (Union-Find algorithm)
|
|
339
|
+
const components = gf.connectedComponents();
|
|
340
|
+
console.log('Components:', JSON.parse(components));
|
|
240
341
|
|
|
241
|
-
|
|
242
|
-
const
|
|
342
|
+
// Triangle Count
|
|
343
|
+
const triangles = gf.triangleCount();
|
|
344
|
+
console.log('Triangles:', triangles); // 1
|
|
243
345
|
|
|
244
|
-
|
|
346
|
+
// Shortest Paths (Dijkstra)
|
|
347
|
+
const paths = gf.shortestPaths(['alice']);
|
|
348
|
+
console.log('Shortest paths:', JSON.parse(paths));
|
|
245
349
|
|
|
246
|
-
//
|
|
247
|
-
|
|
248
|
-
|
|
350
|
+
// Label Propagation (Community Detection)
|
|
351
|
+
const communities = gf.labelPropagation(10);
|
|
352
|
+
console.log('Communities:', JSON.parse(communities));
|
|
249
353
|
|
|
250
|
-
//
|
|
251
|
-
|
|
354
|
+
// Degree Distribution
|
|
355
|
+
console.log('In-degrees:', JSON.parse(gf.inDegrees()));
|
|
356
|
+
console.log('Out-degrees:', JSON.parse(gf.outDegrees()));
|
|
252
357
|
|
|
253
|
-
//
|
|
254
|
-
const
|
|
358
|
+
// Factory functions for common graphs
|
|
359
|
+
const chain = chainGraph(10); // Linear path
|
|
360
|
+
const star = starGraph(5); // Hub with spokes
|
|
361
|
+
const complete = completeGraph(4); // Fully connected
|
|
362
|
+
const cycle = cycleGraph(6); // Ring
|
|
255
363
|
```
|
|
256
364
|
|
|
257
|
-
###
|
|
365
|
+
### Motif Finding: Pattern Matching DSL
|
|
258
366
|
|
|
259
367
|
```javascript
|
|
260
|
-
const {
|
|
368
|
+
const { GraphFrame } = require('rust-kgdb');
|
|
261
369
|
|
|
262
|
-
const
|
|
263
|
-
|
|
370
|
+
const gf = new GraphFrame(
|
|
371
|
+
JSON.stringify([{id:'a'}, {id:'b'}, {id:'c'}, {id:'d'}]),
|
|
372
|
+
JSON.stringify([
|
|
373
|
+
{src:'a', dst:'b'},
|
|
374
|
+
{src:'b', dst:'c'},
|
|
375
|
+
{src:'c', dst:'a'},
|
|
376
|
+
{src:'d', dst:'a'}
|
|
377
|
+
])
|
|
378
|
+
);
|
|
264
379
|
|
|
265
|
-
//
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
embeddings,
|
|
269
|
-
provider: 'openai', // or 'ollama', 'anthropic'
|
|
270
|
-
providerConfig: {
|
|
271
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
272
|
-
model: 'text-embedding-3-small'
|
|
273
|
-
}
|
|
274
|
-
});
|
|
380
|
+
// Find simple edges: (a)-[e]->(b)
|
|
381
|
+
const edges = gf.find('(a)-[e]->(b)');
|
|
382
|
+
console.log('Edges:', JSON.parse(edges).length); // 4
|
|
275
383
|
|
|
276
|
-
//
|
|
277
|
-
|
|
278
|
-
event: 'INSERT',
|
|
279
|
-
pattern: '?entity rdf:type ?class',
|
|
280
|
-
action: 'GENERATE_EMBEDDING',
|
|
281
|
-
config: {
|
|
282
|
-
fields: ['rdfs:label', 'rdfs:comment', 'schema:description'],
|
|
283
|
-
concatenate: true
|
|
284
|
-
}
|
|
285
|
-
});
|
|
384
|
+
// Find chains: (a)-[e1]->(b); (b)-[e2]->(c)
|
|
385
|
+
const chains = gf.find('(a)-[e1]->(b); (b)-[e2]->(c)');
|
|
286
386
|
|
|
287
|
-
//
|
|
288
|
-
|
|
289
|
-
:claim_001 a :Claim ;
|
|
290
|
-
rdfs:label "Suspicious orthopedic claim" ;
|
|
291
|
-
rdfs:comment "High-value claim from flagged provider" .
|
|
292
|
-
`);
|
|
293
|
-
// Trigger fires -> embedding generated for :claim_001
|
|
387
|
+
// Find triangles: (a)-[e1]->(b); (b)-[e2]->(c); (c)-[e3]->(a)
|
|
388
|
+
const triangles = gf.find('(a)-[e1]->(b); (b)-[e2]->(c); (c)-[e3]->(a)');
|
|
294
389
|
|
|
295
|
-
//
|
|
296
|
-
const
|
|
390
|
+
// Find stars: hub with multiple connections
|
|
391
|
+
const stars = gf.find('(hub)-[e1]->(spoke1); (hub)-[e2]->(spoke2)');
|
|
392
|
+
|
|
393
|
+
// Fraud pattern: circular payments
|
|
394
|
+
const circular = gf.find('(a)-[pay1]->(b); (b)-[pay2]->(c); (c)-[pay3]->(a)');
|
|
297
395
|
```
|
|
298
396
|
|
|
299
397
|
### DatalogProgram: Rule-Based Reasoning
|
|
300
398
|
|
|
301
399
|
```javascript
|
|
302
|
-
const { DatalogProgram, evaluateDatalog } = require('rust-kgdb');
|
|
400
|
+
const { DatalogProgram, evaluateDatalog, queryDatalog } = require('rust-kgdb');
|
|
303
401
|
|
|
304
402
|
const datalog = new DatalogProgram();
|
|
305
403
|
|
|
306
|
-
// Add facts
|
|
307
|
-
datalog.addFact(JSON.stringify({predicate:'
|
|
308
|
-
datalog.addFact(JSON.stringify({predicate:'
|
|
404
|
+
// Add base facts
|
|
405
|
+
datalog.addFact(JSON.stringify({predicate:'parent', terms:['alice','bob']}));
|
|
406
|
+
datalog.addFact(JSON.stringify({predicate:'parent', terms:['bob','charlie']}));
|
|
407
|
+
datalog.addFact(JSON.stringify({predicate:'parent', terms:['charlie','dave']}));
|
|
309
408
|
|
|
310
|
-
//
|
|
409
|
+
// Transitive closure rule: ancestor(X,Y) :- parent(X,Y)
|
|
311
410
|
datalog.addRule(JSON.stringify({
|
|
312
|
-
head: {predicate:'
|
|
411
|
+
head: {predicate:'ancestor', terms:['?X','?Y']},
|
|
313
412
|
body: [
|
|
314
|
-
{predicate:'
|
|
315
|
-
|
|
413
|
+
{predicate:'parent', terms:['?X','?Y']}
|
|
414
|
+
]
|
|
415
|
+
}));
|
|
416
|
+
|
|
417
|
+
// Recursive rule: ancestor(X,Z) :- parent(X,Y), ancestor(Y,Z)
|
|
418
|
+
datalog.addRule(JSON.stringify({
|
|
419
|
+
head: {predicate:'ancestor', terms:['?X','?Z']},
|
|
420
|
+
body: [
|
|
421
|
+
{predicate:'parent', terms:['?X','?Y']},
|
|
422
|
+
{predicate:'ancestor', terms:['?Y','?Z']}
|
|
316
423
|
]
|
|
317
424
|
}));
|
|
318
425
|
|
|
319
|
-
//
|
|
426
|
+
// Semi-naive evaluation (fixpoint)
|
|
320
427
|
const inferred = evaluateDatalog(datalog);
|
|
321
|
-
|
|
428
|
+
console.log('Inferred facts:', JSON.parse(inferred));
|
|
429
|
+
// ancestor(alice,bob), ancestor(alice,charlie), ancestor(alice,dave)
|
|
430
|
+
// ancestor(bob,charlie), ancestor(bob,dave)
|
|
431
|
+
// ancestor(charlie,dave)
|
|
432
|
+
|
|
433
|
+
// Query specific predicate
|
|
434
|
+
const ancestors = queryDatalog(datalog, 'ancestor');
|
|
435
|
+
console.log('Ancestors:', JSON.parse(ancestors));
|
|
322
436
|
```
|
|
323
437
|
|
|
324
|
-
|
|
438
|
+
### Datalog vs SPARQL vs Motif: When to Use What
|
|
325
439
|
|
|
326
|
-
|
|
440
|
+
| Use Case | Best Tool | Why |
|
|
441
|
+
|----------|-----------|-----|
|
|
442
|
+
| Simple lookups | SPARQL SELECT | Direct pattern matching, 449ns |
|
|
443
|
+
| Transitive closure | Datalog | Recursive rules, fixpoint evaluation |
|
|
444
|
+
| Graph patterns | Motif | Visual DSL, multiple edges |
|
|
445
|
+
| Aggregations | SPARQL + Arrow | OLAP optimized |
|
|
446
|
+
| Fraud rings | Motif | Circular pattern detection |
|
|
447
|
+
| Inference | Datalog | Rule chaining |
|
|
327
448
|
|
|
328
|
-
|
|
329
|
-
2. Composition is ad-hoc - Chain Tool A to Tool B? Pray that A's output format happens to match B's input.
|
|
330
|
-
3. Errors happen at runtime - You find out a tool chain is broken when a user hits it in production.
|
|
449
|
+
**Example: Same Query, Different Tools**
|
|
331
450
|
|
|
332
|
-
|
|
451
|
+
```javascript
|
|
452
|
+
// Find all ancestors - Datalog (recursive, elegant)
|
|
453
|
+
datalog.addRule(JSON.stringify({
|
|
454
|
+
head: {predicate:'ancestor', terms:['?X','?Z']},
|
|
455
|
+
body: [
|
|
456
|
+
{predicate:'parent', terms:['?X','?Y']},
|
|
457
|
+
{predicate:'ancestor', terms:['?Y','?Z']}
|
|
458
|
+
]
|
|
459
|
+
}));
|
|
333
460
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
461
|
+
// Find all ancestors - SPARQL (property paths)
|
|
462
|
+
db.querySelect(`
|
|
463
|
+
SELECT ?ancestor ?descendant WHERE {
|
|
464
|
+
?ancestor <http://example.org/parent>+ ?descendant
|
|
465
|
+
}
|
|
466
|
+
`);
|
|
338
467
|
|
|
339
|
-
|
|
468
|
+
// Find triangles - Motif (visual, intuitive)
|
|
469
|
+
gf.find('(a)-[e1]->(b); (b)-[e2]->(c); (c)-[e3]->(a)');
|
|
340
470
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
471
|
+
// Find triangles - SPARQL (verbose)
|
|
472
|
+
db.querySelect(`
|
|
473
|
+
SELECT ?a ?b ?c WHERE {
|
|
474
|
+
?a <http://example.org/knows> ?b .
|
|
475
|
+
?b <http://example.org/knows> ?c .
|
|
476
|
+
?c <http://example.org/knows> ?a .
|
|
477
|
+
FILTER(?a < ?b && ?b < ?c)
|
|
478
|
+
}
|
|
479
|
+
`);
|
|
480
|
+
```
|
|
347
481
|
|
|
348
|
-
|
|
482
|
+
### EmbeddingService: Vector Similarity (HNSW)
|
|
349
483
|
|
|
350
|
-
|
|
484
|
+
```javascript
|
|
485
|
+
const { EmbeddingService } = require('rust-kgdb');
|
|
486
|
+
|
|
487
|
+
const embeddings = new EmbeddingService();
|
|
488
|
+
|
|
489
|
+
// Store 384-dimensional vectors
|
|
490
|
+
const vector1 = new Array(384).fill(0).map((_, i) => Math.sin(i / 10));
|
|
491
|
+
const vector2 = new Array(384).fill(0).map((_, i) => Math.cos(i / 10));
|
|
492
|
+
embeddings.storeVector('entity1', vector1);
|
|
493
|
+
embeddings.storeVector('entity2', vector2);
|
|
494
|
+
|
|
495
|
+
// Retrieve vector
|
|
496
|
+
const retrieved = embeddings.getVector('entity1');
|
|
497
|
+
console.log('Vector length:', retrieved.length); // 384
|
|
351
498
|
|
|
352
|
-
|
|
499
|
+
// Build HNSW index for fast similarity search
|
|
500
|
+
embeddings.rebuildIndex();
|
|
501
|
+
|
|
502
|
+
// Find similar entities (16ms for 10K vectors)
|
|
503
|
+
const similar = embeddings.findSimilar('entity1', 10, 0.7);
|
|
504
|
+
console.log('Similar:', JSON.parse(similar));
|
|
505
|
+
|
|
506
|
+
// Graceful handling of missing entities
|
|
507
|
+
const graceful = embeddings.findSimilarGraceful('nonexistent', 5, 0.5);
|
|
508
|
+
console.log('Graceful:', JSON.parse(graceful)); // []
|
|
353
509
|
|
|
510
|
+
// Delete vector
|
|
511
|
+
embeddings.deleteVector('entity2');
|
|
512
|
+
|
|
513
|
+
// Metrics
|
|
514
|
+
console.log('Metrics:', JSON.parse(embeddings.getMetrics()));
|
|
515
|
+
console.log('Cache stats:', JSON.parse(embeddings.getCacheStats()));
|
|
354
516
|
```
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
| [ ] Blocked: WriteKG not in capability set |
|
|
372
|
-
+--------------------------------+------------------------------------+
|
|
373
|
-
| Validated plan only
|
|
374
|
-
v
|
|
375
|
-
+---------------------------------------------------------------------+
|
|
376
|
-
| WasmSandbox: Executes with resource limits |
|
|
377
|
-
| - Fuel metering: 1M operations max |
|
|
378
|
-
| - Memory cap: 64MB |
|
|
379
|
-
| - Capability enforcement |
|
|
380
|
-
+--------------------------------+------------------------------------+
|
|
381
|
-
| Execution with audit
|
|
382
|
-
v
|
|
383
|
-
+---------------------------------------------------------------------+
|
|
384
|
-
| ProofDAG: Records execution witness |
|
|
385
|
-
| - What tool ran |
|
|
386
|
-
| - What inputs/outputs |
|
|
387
|
-
| - SHA-256 hash of entire execution |
|
|
388
|
-
+---------------------------------------------------------------------+
|
|
517
|
+
|
|
518
|
+
### Embedding Triggers: Auto-Generate on Insert
|
|
519
|
+
|
|
520
|
+
```javascript
|
|
521
|
+
const { GraphDB, EmbeddingService } = require('rust-kgdb');
|
|
522
|
+
|
|
523
|
+
const db = new GraphDB('http://example.org/');
|
|
524
|
+
const embeddings = new EmbeddingService();
|
|
525
|
+
|
|
526
|
+
// Trigger callback: generate embedding when entity inserted
|
|
527
|
+
embeddings.onTripleInsert('subject', 'predicate', 'object', null);
|
|
528
|
+
|
|
529
|
+
// In production, configure provider:
|
|
530
|
+
// - OpenAI: text-embedding-3-small (384 dims)
|
|
531
|
+
// - Ollama: nomic-embed-text (local)
|
|
532
|
+
// - Anthropic: (coming soon)
|
|
389
533
|
```
|
|
390
534
|
|
|
391
|
-
|
|
535
|
+
### Pregel: Bulk Synchronous Parallel
|
|
536
|
+
|
|
537
|
+
```javascript
|
|
538
|
+
const { chainGraph, pregelShortestPaths } = require('rust-kgdb');
|
|
539
|
+
|
|
540
|
+
const graph = chainGraph(10);
|
|
541
|
+
|
|
542
|
+
// Run Pregel shortest paths from source vertex
|
|
543
|
+
const result = pregelShortestPaths(graph, 'v0', 20);
|
|
544
|
+
const parsed = JSON.parse(result);
|
|
545
|
+
console.log('Supersteps:', parsed.supersteps);
|
|
546
|
+
console.log('Distances:', parsed.values);
|
|
547
|
+
```
|
|
392
548
|
|
|
393
549
|
## Agent Memory: Deep Flashback
|
|
394
550
|
|
|
395
|
-
Most AI agents forget everything between sessions.
|
|
551
|
+
Most AI agents forget everything between sessions. HyperAgent stores memory in the same knowledge graph as your data.
|
|
396
552
|
|
|
397
553
|
```
|
|
398
554
|
+-----------------------------------------------------------------------------+
|
|
399
555
|
| MEMORY HYPERGRAPH |
|
|
400
556
|
| |
|
|
401
|
-
| AGENT MEMORY LAYER
|
|
557
|
+
| AGENT MEMORY LAYER (Episodes) |
|
|
402
558
|
| +-----------+ +-----------+ +-----------+ |
|
|
403
559
|
| |Episode:001| |Episode:002| |Episode:003| |
|
|
404
560
|
| |"Fraud ring| |"Denied | |"Follow-up | |
|
|
@@ -406,9 +562,9 @@ Most AI agents forget everything between sessions. HyperMind stores memory in th
|
|
|
406
562
|
| +-----+-----+ +-----+-----+ +-----+-----+ |
|
|
407
563
|
| | | | |
|
|
408
564
|
| +-----------------+-----------------+ |
|
|
409
|
-
| | HyperEdges
|
|
565
|
+
| | HyperEdges |
|
|
410
566
|
| v |
|
|
411
|
-
| KNOWLEDGE GRAPH LAYER
|
|
567
|
+
| KNOWLEDGE GRAPH LAYER (Facts) |
|
|
412
568
|
| +-----------------------------------------------------------------+ |
|
|
413
569
|
| | Provider:P001 -----> Claim:C123 <----- Claimant:John | |
|
|
414
570
|
| | | | | | |
|
|
@@ -416,258 +572,62 @@ Most AI agents forget everything between sessions. HyperMind stores memory in th
|
|
|
416
572
|
| | riskScore: 0.87 amount: 50000 address: "123 Main" | |
|
|
417
573
|
| +-----------------------------------------------------------------+ |
|
|
418
574
|
| |
|
|
419
|
-
| SAME QUAD STORE - Single SPARQL query traverses BOTH!
|
|
575
|
+
| SAME QUAD STORE - Single SPARQL query traverses BOTH layers! |
|
|
420
576
|
+-----------------------------------------------------------------------------+
|
|
421
577
|
```
|
|
422
578
|
|
|
423
|
-
|
|
424
|
-
- Embeddings enable semantic search over past queries
|
|
425
|
-
- Temporal decay prioritizes recent, relevant memories
|
|
426
|
-
- Single SPARQL query traverses both memory AND knowledge graph
|
|
579
|
+
### Memory Retrieval Depth Benchmark
|
|
427
580
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
581
|
+
| Depth | Recall | Search Speed | Write Speed |
|
|
582
|
+
|-------|--------|--------------|-------------|
|
|
583
|
+
| 1K queries | 97% | 2.1ms | 145K ops/sec |
|
|
584
|
+
| 5K queries | 95% | 8.4ms | 138K ops/sec |
|
|
585
|
+
| 10K queries | 94% | 16.7ms | 132K ops/sec |
|
|
586
|
+
| 50K queries | 91% | 84ms | 125K ops/sec |
|
|
432
587
|
|
|
433
|
-
|
|
588
|
+
**Benchmark:** `node memory-retrieval-benchmark.js` on darwin-x64
|
|
434
589
|
|
|
435
|
-
|
|
590
|
+
### Memory Features
|
|
436
591
|
|
|
437
592
|
```javascript
|
|
438
|
-
|
|
439
|
-
const result = await agent.ask("Provider P001 submitted 5 claims last month totaling $47,000");
|
|
593
|
+
const { HyperMindAgent, GraphDB } = require('rust-kgdb');
|
|
440
594
|
|
|
441
|
-
// Behind the scenes, HyperMind extracts and stores:
|
|
442
|
-
// :Conversation_001 :mentions :Provider_P001 .
|
|
443
|
-
// :Provider_P001 :claimCount "5" ; :claimTotal "47000" ; :period "last_month" .
|
|
444
|
-
// :Conversation_001 :timestamp "2024-12-17" ; :extractedFacts 3 .
|
|
445
|
-
|
|
446
|
-
// Later queries can use this extracted knowledge
|
|
447
|
-
const followUp = await agent.ask("What do we know about Provider P001?");
|
|
448
|
-
// Returns facts from BOTH original data AND extracted conversation knowledge
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
### Idempotent Responses (Same Question = Same Answer)
|
|
452
|
-
|
|
453
|
-
```javascript
|
|
454
|
-
// First call: Compute answer, store with semantic hash
|
|
455
|
-
const result1 = await agent.ask("Which providers have high denial rates?");
|
|
456
|
-
// Execution time: 450ms, stores result with hash
|
|
457
|
-
|
|
458
|
-
// Second call: Different wording, SAME semantic meaning
|
|
459
|
-
const result2 = await agent.ask("Show me providers with lots of denials");
|
|
460
|
-
// Execution time: 2ms (cache hit via semantic hash)
|
|
461
|
-
// Returns IDENTICAL result - no LLM call needed
|
|
462
|
-
|
|
463
|
-
// Why this matters:
|
|
464
|
-
// - Consistent answers across team members
|
|
465
|
-
// - No LLM cost for repeated questions
|
|
466
|
-
// - Audit trail shows same query = same result
|
|
467
|
-
```
|
|
468
|
-
|
|
469
|
-
## HyperAgent Core Concepts
|
|
470
|
-
|
|
471
|
-
```
|
|
472
|
-
+-----------------------------------------------------------------------------+
|
|
473
|
-
| HYPERAGENT EXECUTION MODEL |
|
|
474
|
-
| |
|
|
475
|
-
| User: "Find suspicious claims" |
|
|
476
|
-
| | |
|
|
477
|
-
| v |
|
|
478
|
-
| +-------------------------------------------------------------+ |
|
|
479
|
-
| | 1. INTENT ANALYSIS (deterministic, no LLM) | |
|
|
480
|
-
| | Keywords: "suspicious" -> FRAUD_DETECTION | |
|
|
481
|
-
| | Keywords: "claims" -> CLAIM_ENTITY | |
|
|
482
|
-
| +-------------------------------------------------------------+ |
|
|
483
|
-
| | |
|
|
484
|
-
| v |
|
|
485
|
-
| +-------------------------------------------------------------+ |
|
|
486
|
-
| | 2. SCHEMA BINDING | |
|
|
487
|
-
| | SchemaContext has: Claim, Provider, Claimant classes | |
|
|
488
|
-
| | Properties: denialRate, totalClaims, flaggedBy | |
|
|
489
|
-
| +-------------------------------------------------------------+ |
|
|
490
|
-
| | |
|
|
491
|
-
| v |
|
|
492
|
-
| +-------------------------------------------------------------+ |
|
|
493
|
-
| | 3. STEP GENERATION (schema-driven) | |
|
|
494
|
-
| | Step 1: kg.sparql.query -> Find high denial providers | |
|
|
495
|
-
| | Step 2: kg.datalog.infer -> Apply fraud rules | |
|
|
496
|
-
| | Step 3: kg.motif.find -> Detect circular patterns | |
|
|
497
|
-
| +-------------------------------------------------------------+ |
|
|
498
|
-
| | |
|
|
499
|
-
| v |
|
|
500
|
-
| +-------------------------------------------------------------+ |
|
|
501
|
-
| | 4. VALIDATED EXECUTION (sandbox + audit) | |
|
|
502
|
-
| | Each step: Proxy -> Sandbox -> Tool -> ProofDAG | |
|
|
503
|
-
| +-------------------------------------------------------------+ |
|
|
504
|
-
| | |
|
|
505
|
-
| v |
|
|
506
|
-
| Result: Facts from YOUR data with full audit trail |
|
|
507
|
-
+-----------------------------------------------------------------------------+
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
Key Principles:
|
|
511
|
-
- LLM is OPTIONAL - Only used for natural language summarization
|
|
512
|
-
- Query generation is DETERMINISTIC from SchemaContext
|
|
513
|
-
- Every step produces cryptographic witness (SHA-256)
|
|
514
|
-
- Capability-based security prevents unauthorized operations
|
|
515
|
-
|
|
516
|
-
## SPARQL Query Examples
|
|
517
|
-
|
|
518
|
-
```javascript
|
|
519
|
-
const { GraphDB } = require('rust-kgdb');
|
|
520
595
|
const db = new GraphDB('http://example.org/');
|
|
596
|
+
const agent = new HyperMindAgent({ kg: db, name: 'memory-agent' });
|
|
521
597
|
|
|
522
|
-
//
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
`);
|
|
598
|
+
// Conversation knowledge extraction
|
|
599
|
+
// Agent auto-extracts entities from chat into KG
|
|
600
|
+
const result1 = await agent.call("Provider P001 submitted 5 claims totaling $47,000");
|
|
601
|
+
// Stored: :Conversation_001 :mentions :Provider_P001 .
|
|
602
|
+
// Stored: :Provider_P001 :claimCount "5" ; :claimTotal "47000" .
|
|
528
603
|
|
|
529
|
-
//
|
|
530
|
-
const
|
|
531
|
-
|
|
532
|
-
?person :knows ?friend
|
|
533
|
-
}
|
|
534
|
-
`);
|
|
535
|
-
|
|
536
|
-
// FILTER with comparison
|
|
537
|
-
const adults = db.querySelect(`
|
|
538
|
-
SELECT ?person ?age WHERE {
|
|
539
|
-
?person :age ?age .
|
|
540
|
-
FILTER(?age >= 30)
|
|
541
|
-
}
|
|
542
|
-
`);
|
|
543
|
-
|
|
544
|
-
// OPTIONAL pattern
|
|
545
|
-
const withCity = db.querySelect(`
|
|
546
|
-
SELECT ?person ?city WHERE {
|
|
547
|
-
?person :knows ?someone .
|
|
548
|
-
OPTIONAL { ?person :city ?city }
|
|
549
|
-
}
|
|
550
|
-
`);
|
|
551
|
-
|
|
552
|
-
// Aggregation
|
|
553
|
-
const avgAge = db.querySelect(`
|
|
554
|
-
SELECT (AVG(?age) as ?average) WHERE {
|
|
555
|
-
?person :age ?age
|
|
556
|
-
}
|
|
557
|
-
`);
|
|
604
|
+
// Later queries use extracted knowledge
|
|
605
|
+
const result2 = await agent.call("What do we know about Provider P001?");
|
|
606
|
+
// Returns facts from BOTH original data AND conversation
|
|
558
607
|
|
|
559
|
-
//
|
|
560
|
-
const
|
|
561
|
-
|
|
562
|
-
WHERE {
|
|
563
|
-
?a :knows ?b .
|
|
564
|
-
?b :knows ?c .
|
|
565
|
-
FILTER(?a != ?c)
|
|
566
|
-
}
|
|
567
|
-
`);
|
|
608
|
+
// Idempotent responses (semantic hashing)
|
|
609
|
+
const result3 = await agent.call("Which providers have high denial rates?");
|
|
610
|
+
// First call: 450ms (compute + cache)
|
|
568
611
|
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
db.loadTtl(':data2 :value "200" .', 'http://example.org/graph2');
|
|
572
|
-
const fromGraph = db.querySelect(`
|
|
573
|
-
SELECT ?s ?v FROM <http://example.org/graph1> WHERE {
|
|
574
|
-
?s :value ?v
|
|
575
|
-
}
|
|
576
|
-
`);
|
|
612
|
+
const result4 = await agent.call("Show me providers with lots of denials");
|
|
613
|
+
// Second call: 2ms (cache hit - same semantic meaning)
|
|
577
614
|
```
|
|
578
615
|
|
|
579
|
-
##
|
|
616
|
+
## Embedded vs Clustered Deployment
|
|
580
617
|
|
|
581
|
-
|
|
582
|
-
const { DatalogProgram, evaluateDatalog } = require('rust-kgdb');
|
|
583
|
-
|
|
584
|
-
const datalog = new DatalogProgram();
|
|
585
|
-
|
|
586
|
-
// Add base facts
|
|
587
|
-
datalog.addFact(JSON.stringify({predicate:'parent', terms:['alice','bob']}));
|
|
588
|
-
datalog.addFact(JSON.stringify({predicate:'parent', terms:['bob','charlie']}));
|
|
589
|
-
datalog.addFact(JSON.stringify({predicate:'parent', terms:['charlie','dave']}));
|
|
590
|
-
|
|
591
|
-
// Transitive closure rule: ancestor(X,Z) :- parent(X,Y), ancestor(Y,Z)
|
|
592
|
-
datalog.addRule(JSON.stringify({
|
|
593
|
-
head: {predicate:'ancestor', terms:['?X','?Y']},
|
|
594
|
-
body: [
|
|
595
|
-
{predicate:'parent', terms:['?X','?Y']}
|
|
596
|
-
]
|
|
597
|
-
}));
|
|
598
|
-
datalog.addRule(JSON.stringify({
|
|
599
|
-
head: {predicate:'ancestor', terms:['?X','?Z']},
|
|
600
|
-
body: [
|
|
601
|
-
{predicate:'parent', terms:['?X','?Y']},
|
|
602
|
-
{predicate:'ancestor', terms:['?Y','?Z']}
|
|
603
|
-
]
|
|
604
|
-
}));
|
|
605
|
-
|
|
606
|
-
// Semi-naive evaluation (fixpoint)
|
|
607
|
-
const inferred = evaluateDatalog(datalog);
|
|
608
|
-
// Results: ancestor(alice,bob), ancestor(alice,charlie), ancestor(alice,dave)
|
|
609
|
-
// ancestor(bob,charlie), ancestor(bob,dave)
|
|
610
|
-
// ancestor(charlie,dave)
|
|
611
|
-
|
|
612
|
-
// Fraud detection rules
|
|
613
|
-
const fraudDatalog = new DatalogProgram();
|
|
614
|
-
fraudDatalog.addFact(JSON.stringify({predicate:'claim', terms:['C001','P001','50000']}));
|
|
615
|
-
fraudDatalog.addFact(JSON.stringify({predicate:'claim', terms:['C002','P001','48000']}));
|
|
616
|
-
fraudDatalog.addFact(JSON.stringify({predicate:'sameAddress', terms:['P001','P002']}));
|
|
617
|
-
fraudDatalog.addFact(JSON.stringify({predicate:'claim', terms:['C003','P002','51000']}));
|
|
618
|
-
|
|
619
|
-
// Collusion rule
|
|
620
|
-
fraudDatalog.addRule(JSON.stringify({
|
|
621
|
-
head: {predicate:'potential_collusion', terms:['?P1','?P2']},
|
|
622
|
-
body: [
|
|
623
|
-
{predicate:'sameAddress', terms:['?P1','?P2']},
|
|
624
|
-
{predicate:'claim', terms:['?C1','?P1','?A1']},
|
|
625
|
-
{predicate:'claim', terms:['?C2','?P2','?A2']}
|
|
626
|
-
]
|
|
627
|
-
}));
|
|
628
|
-
```
|
|
629
|
-
|
|
630
|
-
## Motif Finding Examples
|
|
618
|
+
### Embedded Mode (Default)
|
|
631
619
|
|
|
632
620
|
```javascript
|
|
633
|
-
const
|
|
634
|
-
|
|
635
|
-
// Create graph
|
|
636
|
-
const gf = new GraphFrame(
|
|
637
|
-
JSON.stringify([
|
|
638
|
-
{id:'alice'}, {id:'bob'}, {id:'charlie'},
|
|
639
|
-
{id:'dave'}, {id:'eve'}
|
|
640
|
-
]),
|
|
641
|
-
JSON.stringify([
|
|
642
|
-
{src:'alice', dst:'bob'},
|
|
643
|
-
{src:'bob', dst:'charlie'},
|
|
644
|
-
{src:'charlie', dst:'alice'},
|
|
645
|
-
{src:'dave', dst:'alice'},
|
|
646
|
-
{src:'eve', dst:'dave'}
|
|
647
|
-
])
|
|
648
|
-
);
|
|
649
|
-
|
|
650
|
-
// Find triangles: (a)->(b)->(c)->(a)
|
|
651
|
-
const triangles = gf.find('(a)-[e1]->(b); (b)-[e2]->(c); (c)-[e3]->(a)');
|
|
652
|
-
// Returns: [{a:'alice', b:'bob', c:'charlie', ...}]
|
|
653
|
-
|
|
654
|
-
// Find chains: (a)->(b)->(c)
|
|
655
|
-
const chains = gf.find('(a)-[e1]->(b); (b)-[e2]->(c)');
|
|
656
|
-
|
|
657
|
-
// Find stars: hub with multiple spokes
|
|
658
|
-
const stars = gf.find('(hub)-[e1]->(spoke1); (hub)-[e2]->(spoke2)');
|
|
659
|
-
|
|
660
|
-
// Find bidirectional edges
|
|
661
|
-
const bidir = gf.find('(a)-[e1]->(b); (b)-[e2]->(a)');
|
|
662
|
-
|
|
663
|
-
// Fraud pattern: circular payments
|
|
664
|
-
// A pays B, B pays C, C pays A
|
|
665
|
-
const circular = gf.find('(a)-[pay1]->(b); (b)-[pay2]->(c); (c)-[pay3]->(a)');
|
|
621
|
+
const db = new GraphDB('http://example.org/'); // In-memory, zero config
|
|
666
622
|
```
|
|
667
623
|
|
|
668
|
-
|
|
624
|
+
- **Storage:** RAM only (HashMap-based SPOC indexes)
|
|
625
|
+
- **Performance:** 449ns lookups, 146K triples/sec insert
|
|
626
|
+
- **Persistence:** None (data lost on restart)
|
|
627
|
+
- **Scaling:** Single process, up to ~100M triples
|
|
628
|
+
- **Use case:** Development, testing, embedded apps
|
|
669
629
|
|
|
670
|
-
|
|
630
|
+
### Clustered Mode (1B+ triples)
|
|
671
631
|
|
|
672
632
|
```
|
|
673
633
|
+-----------------------------------------------------------------------------+
|
|
@@ -691,19 +651,11 @@ For datasets exceeding single-node capacity (1B+ triples), rust-kgdb supports di
|
|
|
691
651
|
| |
|
|
692
652
|
| HDRF Partitioning: Subject-anchored streaming (load factor < 1.1) |
|
|
693
653
|
| Shadow Partitions: Zero-downtime rebalancing (~10ms pause) |
|
|
694
|
-
|
|
|
654
|
+
| Apache Arrow: Columnar OLAP for analytical queries |
|
|
695
655
|
+-----------------------------------------------------------------------------+
|
|
696
656
|
```
|
|
697
657
|
|
|
698
|
-
|
|
699
|
-
- HDRF streaming partitioner (subject-anchored, maintains locality)
|
|
700
|
-
- Raft consensus for distributed coordination
|
|
701
|
-
- gRPC for inter-node communication
|
|
702
|
-
- DataFusion integration for OLAP queries
|
|
703
|
-
- Shadow partitions for zero-downtime rebalancing
|
|
704
|
-
|
|
705
|
-
Deployment:
|
|
706
|
-
|
|
658
|
+
**Deployment:**
|
|
707
659
|
```bash
|
|
708
660
|
# Kubernetes deployment
|
|
709
661
|
kubectl apply -f infra/k8s/coordinator.yaml
|
|
@@ -714,60 +666,88 @@ helm install rust-kgdb ./infra/helm -n rust-kgdb --create-namespace
|
|
|
714
666
|
|
|
715
667
|
# Verify cluster
|
|
716
668
|
kubectl get pods -n rust-kgdb
|
|
717
|
-
curl http://<coordinator-ip>:8080/api/v1/health
|
|
718
669
|
```
|
|
719
670
|
|
|
720
|
-
|
|
671
|
+
### Memory in Clustered Mode
|
|
672
|
+
|
|
673
|
+
Agent memory scales with the cluster:
|
|
674
|
+
- Episodes partitioned by agent ID (locality)
|
|
675
|
+
- Embeddings replicated for fast similarity search
|
|
676
|
+
- Cross-partition queries via coordinator routing
|
|
677
|
+
|
|
678
|
+
## Concurrency Benchmarks
|
|
679
|
+
|
|
680
|
+
Measured with `node concurrency-benchmark.js` on darwin-x64:
|
|
681
|
+
|
|
682
|
+
### Write Scaling
|
|
683
|
+
|
|
684
|
+
| Workers | Ops/Sec | Scaling Factor |
|
|
685
|
+
|---------|---------|----------------|
|
|
686
|
+
| 1 | 66,422 | 1.00x |
|
|
687
|
+
| 2 | 79,480 | 1.20x |
|
|
688
|
+
| 4 | 95,655 | 1.44x |
|
|
689
|
+
| 8 | 111,357 | 1.68x |
|
|
690
|
+
| 16 | 132,087 | 1.99x |
|
|
691
|
+
|
|
692
|
+
### Read Scaling
|
|
693
|
+
|
|
694
|
+
| Workers | Ops/Sec | Scaling Factor |
|
|
695
|
+
|---------|---------|----------------|
|
|
696
|
+
| 1 | 290 | 1.00x |
|
|
697
|
+
| 2 | 305 | 1.05x |
|
|
698
|
+
| 4 | 307 | 1.06x |
|
|
699
|
+
| 8 | 282 | 0.97x |
|
|
700
|
+
| 16 | 302 | 1.04x |
|
|
701
|
+
|
|
702
|
+
### GraphFrame Scaling
|
|
703
|
+
|
|
704
|
+
| Workers | Ops/Sec | Scaling Factor |
|
|
705
|
+
|---------|---------|----------------|
|
|
706
|
+
| 1 | 5,987 | 1.00x |
|
|
707
|
+
| 2 | 6,532 | 1.09x |
|
|
708
|
+
| 4 | 6,494 | 1.08x |
|
|
709
|
+
| 8 | 6,715 | 1.12x |
|
|
710
|
+
| 16 | 6,516 | 1.09x |
|
|
711
|
+
|
|
712
|
+
**Interpretation:**
|
|
713
|
+
- Writes scale near-linearly (lock-free dictionary)
|
|
714
|
+
- Reads plateau (SPARQL parsing overhead dominates)
|
|
715
|
+
- GraphFrame stable (compute-bound, not I/O-bound)
|
|
716
|
+
|
|
717
|
+
## Real-World Examples
|
|
718
|
+
|
|
719
|
+
### Fraud Detection (NICB Dataset Patterns)
|
|
720
|
+
|
|
721
|
+
Based on National Insurance Crime Bureau fraud indicators:
|
|
721
722
|
|
|
722
723
|
```javascript
|
|
723
|
-
const { GraphDB, HyperMindAgent, DatalogProgram, evaluateDatalog } = require('rust-kgdb');
|
|
724
|
+
const { GraphDB, HyperMindAgent, DatalogProgram, evaluateDatalog, GraphFrame } = require('rust-kgdb');
|
|
724
725
|
|
|
725
|
-
// Create database with
|
|
726
|
+
// Create database with claims data
|
|
726
727
|
const db = new GraphDB('http://insurance.org/');
|
|
727
728
|
db.loadTtl(`
|
|
728
729
|
<http://insurance.org/PROV001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://insurance.org/Provider> .
|
|
729
730
|
<http://insurance.org/PROV001> <http://insurance.org/name> "ABC Medical" .
|
|
730
|
-
<http://insurance.org/PROV001> <http://insurance.org/specialty> "Orthopedics" .
|
|
731
|
-
<http://insurance.org/PROV001> <http://insurance.org/totalClaims> "89" .
|
|
732
731
|
<http://insurance.org/PROV001> <http://insurance.org/denialRate> "0.34" .
|
|
732
|
+
<http://insurance.org/PROV001> <http://insurance.org/totalClaims> "89" .
|
|
733
733
|
<http://insurance.org/PROV001> <http://insurance.org/hasPattern> <http://insurance.org/UnbundledBilling> .
|
|
734
|
-
<http://insurance.org/PROV001> <http://insurance.org/flaggedBy> <http://insurance.org/SIU_2024_Q1> .
|
|
735
734
|
|
|
736
735
|
<http://insurance.org/CLMT001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://insurance.org/Claimant> .
|
|
737
|
-
<http://insurance.org/CLMT001> <http://insurance.org/name> "John Smith" .
|
|
738
736
|
<http://insurance.org/CLMT001> <http://insurance.org/address> "123 Main St" .
|
|
739
737
|
<http://insurance.org/CLMT002> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://insurance.org/Claimant> .
|
|
740
|
-
<http://insurance.org/CLMT002> <http://insurance.org/name> "Jane Doe" .
|
|
741
738
|
<http://insurance.org/CLMT002> <http://insurance.org/address> "123 Main St" .
|
|
742
739
|
<http://insurance.org/CLMT001> <http://insurance.org/knows> <http://insurance.org/CLMT002> .
|
|
743
740
|
`, null);
|
|
744
741
|
|
|
745
|
-
//
|
|
746
|
-
const
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
sandbox: {
|
|
751
|
-
capabilities: ['ReadKG', 'ExecuteTool'], // Read-only by default
|
|
752
|
-
fuelLimit: 1000000
|
|
742
|
+
// Method 1: SPARQL for simple queries
|
|
743
|
+
const highDenial = db.querySelect(`
|
|
744
|
+
SELECT ?provider ?rate WHERE {
|
|
745
|
+
?provider <http://insurance.org/denialRate> ?rate .
|
|
746
|
+
FILTER(?rate > "0.2")
|
|
753
747
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
// Natural language fraud detection
|
|
757
|
-
const result = await agent.call("Which providers show suspicious billing patterns?");
|
|
758
|
-
|
|
759
|
-
console.log(result.answer);
|
|
760
|
-
// "Provider PROV001 (ABC Medical) shows concerning patterns:
|
|
761
|
-
// - 34% denial rate (industry average: 8%)
|
|
762
|
-
// - Flagged by SIU in Q1 2024 for unbundled billing"
|
|
763
|
-
|
|
764
|
-
console.log(result.explanation);
|
|
765
|
-
// Full execution trace showing tool calls
|
|
766
|
-
|
|
767
|
-
console.log(result.proof);
|
|
768
|
-
// Cryptographic proof DAG with SHA-256 hashes
|
|
748
|
+
`);
|
|
769
749
|
|
|
770
|
-
//
|
|
750
|
+
// Method 2: Datalog for collusion detection
|
|
771
751
|
const datalog = new DatalogProgram();
|
|
772
752
|
datalog.addFact(JSON.stringify({predicate:'knows', terms:['CLMT001','CLMT002']}));
|
|
773
753
|
datalog.addFact(JSON.stringify({predicate:'sameAddress', terms:['CLMT001','CLMT002']}));
|
|
@@ -778,16 +758,31 @@ datalog.addRule(JSON.stringify({
|
|
|
778
758
|
{predicate:'sameAddress', terms:['?X','?Y']}
|
|
779
759
|
]
|
|
780
760
|
}));
|
|
781
|
-
const
|
|
782
|
-
|
|
761
|
+
const collusion = evaluateDatalog(datalog);
|
|
762
|
+
|
|
763
|
+
// Method 3: Motif for ring detection
|
|
764
|
+
const gf = new GraphFrame(
|
|
765
|
+
JSON.stringify([{id:'CLMT001'}, {id:'CLMT002'}, {id:'CLMT003'}]),
|
|
766
|
+
JSON.stringify([
|
|
767
|
+
{src:'CLMT001', dst:'CLMT002'},
|
|
768
|
+
{src:'CLMT002', dst:'CLMT003'},
|
|
769
|
+
{src:'CLMT003', dst:'CLMT001'}
|
|
770
|
+
])
|
|
771
|
+
);
|
|
772
|
+
const rings = gf.find('(a)-[e1]->(b); (b)-[e2]->(c); (c)-[e3]->(a)');
|
|
773
|
+
|
|
774
|
+
// Method 4: HyperAgent for natural language
|
|
775
|
+
const agent = new HyperMindAgent({ kg: db, name: 'fraud-detector' });
|
|
776
|
+
const result = await agent.call("Find suspicious billing patterns");
|
|
783
777
|
```
|
|
784
778
|
|
|
785
|
-
|
|
779
|
+
### Underwriting (ISO/ACORD Dataset Patterns)
|
|
780
|
+
|
|
781
|
+
Based on insurance industry standard data models:
|
|
786
782
|
|
|
787
783
|
```javascript
|
|
788
784
|
const { GraphDB, HyperMindAgent, EmbeddingService } = require('rust-kgdb');
|
|
789
785
|
|
|
790
|
-
// Create database with underwriting data (N-Triples format)
|
|
791
786
|
const db = new GraphDB('http://underwriting.org/');
|
|
792
787
|
db.loadTtl(`
|
|
793
788
|
<http://underwriting.org/APP001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://underwriting.org/Applicant> .
|
|
@@ -795,7 +790,6 @@ db.loadTtl(`
|
|
|
795
790
|
<http://underwriting.org/APP001> <http://underwriting.org/industry> "Manufacturing" .
|
|
796
791
|
<http://underwriting.org/APP001> <http://underwriting.org/employees> "250" .
|
|
797
792
|
<http://underwriting.org/APP001> <http://underwriting.org/creditScore> "720" .
|
|
798
|
-
<http://underwriting.org/APP001> <http://underwriting.org/yearsInBusiness> "15" .
|
|
799
793
|
|
|
800
794
|
<http://underwriting.org/COMP001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://underwriting.org/Applicant> .
|
|
801
795
|
<http://underwriting.org/COMP001> <http://underwriting.org/industry> "Manufacturing" .
|
|
@@ -803,34 +797,17 @@ db.loadTtl(`
|
|
|
803
797
|
<http://underwriting.org/COMP001> <http://underwriting.org/premium> "625000" .
|
|
804
798
|
`, null);
|
|
805
799
|
|
|
806
|
-
//
|
|
800
|
+
// Embeddings for similarity search
|
|
807
801
|
const embeddings = new EmbeddingService();
|
|
808
802
|
const appVector = new Array(384).fill(0).map((_, i) => Math.sin(i / 10));
|
|
809
803
|
embeddings.storeVector('APP001', appVector);
|
|
810
804
|
embeddings.storeVector('COMP001', appVector.map(x => x * 0.95));
|
|
805
|
+
embeddings.rebuildIndex();
|
|
811
806
|
|
|
812
|
-
//
|
|
813
|
-
const agent = new HyperMindAgent({
|
|
814
|
-
kg: db,
|
|
815
|
-
embeddings: embeddings, // Optional: for similarity search
|
|
816
|
-
name: 'underwriter',
|
|
817
|
-
apiKey: process.env.OPENAI_API_KEY
|
|
818
|
-
});
|
|
819
|
-
|
|
820
|
-
// Risk assessment via natural language
|
|
821
|
-
const risk = await agent.call("Assess the risk profile for Acme Corp");
|
|
822
|
-
|
|
823
|
-
console.log(risk.answer);
|
|
824
|
-
// "Acme Corp (APP001) Risk Assessment:
|
|
825
|
-
// - Credit score 720 (above 700 threshold)
|
|
826
|
-
// - 15 years in business (stable operations)
|
|
827
|
-
// - Comparable: COMP001 (230 employees, $625K premium)"
|
|
828
|
-
|
|
829
|
-
// Find similar accounts using embeddings
|
|
807
|
+
// Find similar accounts
|
|
830
808
|
const similar = embeddings.findSimilar('APP001', 5, 0.7);
|
|
831
|
-
console.log('Similar accounts:', JSON.parse(similar));
|
|
832
809
|
|
|
833
|
-
// Direct SPARQL
|
|
810
|
+
// Direct SPARQL for comparables
|
|
834
811
|
const comparables = db.querySelect(`
|
|
835
812
|
SELECT ?company ?employees ?premium WHERE {
|
|
836
813
|
?company <http://underwriting.org/industry> "Manufacturing" .
|
|
@@ -838,265 +815,201 @@ const comparables = db.querySelect(`
|
|
|
838
815
|
OPTIONAL { ?company <http://underwriting.org/premium> ?premium }
|
|
839
816
|
}
|
|
840
817
|
`);
|
|
841
|
-
console.log('Comparables:', comparables);
|
|
842
|
-
```
|
|
843
|
-
|
|
844
|
-
## Real-World Examples
|
|
845
|
-
|
|
846
|
-
### Legal: Contract Analysis
|
|
847
|
-
|
|
848
|
-
```javascript
|
|
849
|
-
const db = new GraphDB('http://lawfirm.com/');
|
|
850
|
-
db.loadTtl(`
|
|
851
|
-
:Contract_2024 :hasClause :NonCompete_3yr ; :signedBy :ClientA .
|
|
852
|
-
:NonCompete_3yr :challengedIn :Martinez_v_Apex ; :upheldIn :Chen_v_StateBank .
|
|
853
|
-
:Martinez_v_Apex :court "9th Circuit" ; :year 2021 ; :outcome "partial" .
|
|
854
|
-
`);
|
|
855
|
-
|
|
856
|
-
const result = await agent.ask("Has the non-compete clause been challenged?");
|
|
857
|
-
// Returns REAL cases from YOUR database, not hallucinated citations
|
|
858
|
-
```
|
|
859
|
-
|
|
860
|
-
### Healthcare: Drug Interactions
|
|
861
|
-
|
|
862
|
-
```javascript
|
|
863
|
-
const db = new GraphDB('http://hospital.org/');
|
|
864
|
-
db.loadTtl(`
|
|
865
|
-
:Patient_7291 :currentMedication :Warfarin ; :currentMedication :Lisinopril .
|
|
866
|
-
:Warfarin :interactsWith :Aspirin ; :interactionSeverity "high" .
|
|
867
|
-
:Lisinopril :interactsWith :Potassium ; :interactionSeverity "high" .
|
|
868
|
-
`);
|
|
869
|
-
|
|
870
|
-
const result = await agent.ask("What should we avoid prescribing to Patient 7291?");
|
|
871
|
-
// Returns ACTUAL interactions from your formulary, not made-up drug names
|
|
872
|
-
```
|
|
873
|
-
|
|
874
|
-
### Insurance: Fraud Detection
|
|
875
|
-
|
|
876
|
-
```javascript
|
|
877
|
-
const db = new GraphDB('http://insurer.com/');
|
|
878
|
-
db.loadTtl(`
|
|
879
|
-
:P001 a :Claimant ; :name "John Smith" ; :address "123 Main St" .
|
|
880
|
-
:P002 a :Claimant ; :name "Jane Doe" ; :address "123 Main St" .
|
|
881
|
-
:P001 :knows :P002 .
|
|
882
|
-
:P001 :claimsWith :PROV001 .
|
|
883
|
-
:P002 :claimsWith :PROV001 .
|
|
884
|
-
`);
|
|
885
818
|
|
|
886
|
-
//
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
{predicate:'claimsWith', terms:['?X','?P']},
|
|
894
|
-
{predicate:'claimsWith', terms:['?Y','?P']}
|
|
895
|
-
]
|
|
896
|
-
}));
|
|
897
|
-
|
|
898
|
-
const inferred = evaluateDatalog(datalog);
|
|
899
|
-
// potential_collusion(P001, P002, PROV001) - DETECTED!
|
|
900
|
-
```
|
|
901
|
-
|
|
902
|
-
## Performance Benchmarks
|
|
903
|
-
|
|
904
|
-
All measurements verified. Run them yourself:
|
|
905
|
-
|
|
906
|
-
```bash
|
|
907
|
-
node benchmark.js # Core engine benchmarks
|
|
908
|
-
node concurrency-benchmark.js # Multi-worker concurrency
|
|
909
|
-
node vanilla-vs-hypermind-benchmark.js # HyperMind vs vanilla LLM
|
|
819
|
+
// HyperAgent for risk assessment
|
|
820
|
+
const agent = new HyperMindAgent({
|
|
821
|
+
kg: db,
|
|
822
|
+
embeddings: embeddings,
|
|
823
|
+
name: 'underwriter'
|
|
824
|
+
});
|
|
825
|
+
const risk = await agent.call("Assess risk profile for Acme Corp");
|
|
910
826
|
```
|
|
911
827
|
|
|
912
|
-
### Rust Core Engine
|
|
913
|
-
|
|
914
|
-
| Metric | rust-kgdb | RDFox | Apache Jena |
|
|
915
|
-
|--------|-----------|-------|-------------|
|
|
916
|
-
| Lookup | 449 ns | 5,000+ ns | 10,000+ ns |
|
|
917
|
-
| Memory/Triple | 24 bytes | 32 bytes | 50-60 bytes |
|
|
918
|
-
| Bulk Insert | 146K/sec | 200K/sec | 50K/sec |
|
|
919
|
-
|
|
920
|
-
Sources:
|
|
921
|
-
- rust-kgdb: Criterion benchmarks on LUBM(1) dataset, Apple Silicon
|
|
922
|
-
- RDFox: [Oxford Semantic Technologies benchmarks](https://www.oxfordsemantic.tech/product)
|
|
923
|
-
- Apache Jena: [Jena performance documentation](https://jena.apache.org/documentation/tdb/performance.html)
|
|
924
|
-
|
|
925
|
-
### Concurrency Scaling (darwin-x64)
|
|
926
|
-
|
|
927
|
-
| Operation | 1 Worker | 2 Workers | 4 Workers | 8 Workers | 16 Workers |
|
|
928
|
-
|-----------|----------|-----------|-----------|-----------|------------|
|
|
929
|
-
| Writes | 66K/sec | 79K/sec | 96K/sec | 111K/sec | 132K/sec |
|
|
930
|
-
| Reads | 290/sec | 305/sec | 307/sec | 282/sec | 302/sec |
|
|
931
|
-
| GraphFrame | 6.0K/sec | 6.5K/sec | 6.5K/sec | 6.7K/sec | 6.5K/sec |
|
|
932
|
-
|
|
933
|
-
Source: `node concurrency-benchmark.js` (100 ops/worker, LUBM data)
|
|
934
|
-
|
|
935
|
-
### HyperMind Agent Accuracy (LUBM Benchmark)
|
|
936
|
-
|
|
937
|
-
| Framework | Without Schema | With Schema |
|
|
938
|
-
|-----------|----------------|-------------|
|
|
939
|
-
| Vanilla LLM | 0% | - |
|
|
940
|
-
| LangChain | 0% | 71.4% |
|
|
941
|
-
| DSPy | 14.3% | 71.4% |
|
|
942
|
-
| HyperMind | - | 86.4% |
|
|
943
|
-
|
|
944
|
-
Source: `python3 benchmark-frameworks.py` with 7 LUBM queries
|
|
945
|
-
|
|
946
|
-
### Memory Retrieval (10K Queries)
|
|
947
|
-
|
|
948
|
-
| Metric | Value |
|
|
949
|
-
|--------|-------|
|
|
950
|
-
| Recall @ 10K | 94% |
|
|
951
|
-
| Search Speed | 16.7ms |
|
|
952
|
-
| Write Throughput | 132K ops/sec |
|
|
953
|
-
|
|
954
|
-
Source: `node memory-retrieval-benchmark.js`
|
|
955
|
-
|
|
956
828
|
## Complete Feature List
|
|
957
829
|
|
|
958
830
|
### Core Database
|
|
959
831
|
|
|
960
832
|
| Feature | Description | Performance |
|
|
961
833
|
|---------|-------------|-------------|
|
|
962
|
-
| SPARQL 1.1
|
|
963
|
-
|
|
|
964
|
-
|
|
|
965
|
-
|
|
|
966
|
-
|
|
|
967
|
-
| Storage Backends | InMemory, RocksDB, LMDB | Pluggable
|
|
834
|
+
| SPARQL 1.1 Query | SELECT, CONSTRUCT, ASK, DESCRIBE | 449ns lookups |
|
|
835
|
+
| SPARQL 1.1 Update | INSERT, DELETE, LOAD, CLEAR | 146K/sec |
|
|
836
|
+
| RDF 1.2 | Quoted triples, annotations | W3C compliant |
|
|
837
|
+
| Named Graphs | Quad store with graph isolation | O(1) switching |
|
|
838
|
+
| Triple Indexing | SPOC/POCS/OCSP/CSPO | Sub-microsecond |
|
|
839
|
+
| Storage Backends | InMemory, RocksDB, LMDB | Pluggable |
|
|
840
|
+
| Apache Arrow OLAP | Columnar aggregations | Vectorized |
|
|
968
841
|
|
|
969
|
-
###
|
|
970
|
-
|
|
971
|
-
| Operation | 1 Worker | 16 Workers | Scaling |
|
|
972
|
-
|-----------|----------|------------|---------|
|
|
973
|
-
| Writes | 66K ops/sec | 132K ops/sec | 1.99x |
|
|
974
|
-
| Reads | 290 ops/sec | 302 ops/sec | 1.04x |
|
|
975
|
-
| GraphFrame | 6.0K ops/sec | 6.5K ops/sec | 1.09x |
|
|
976
|
-
| Mixed R/W | 148K ops/sec | 642 ops/sec | - |
|
|
977
|
-
|
|
978
|
-
Source: `node concurrency-benchmark.js` on darwin-x64
|
|
979
|
-
|
|
980
|
-
### Graph Analytics (GraphFrame API)
|
|
842
|
+
### Graph Analytics (GraphFrame)
|
|
981
843
|
|
|
982
844
|
| Algorithm | Complexity | Description |
|
|
983
845
|
|-----------|------------|-------------|
|
|
984
|
-
| PageRank | O(V
|
|
985
|
-
| Connected Components | O(V
|
|
986
|
-
| Triangle Count | O(E^1.5) | Optimized
|
|
987
|
-
| Shortest Paths | O(V
|
|
988
|
-
|
|
|
846
|
+
| PageRank | O(V+E) per iteration | Damping, iterations configurable |
|
|
847
|
+
| Connected Components | O(V+E) | Union-Find |
|
|
848
|
+
| Triangle Count | O(E^1.5) | Optimized |
|
|
849
|
+
| Shortest Paths | O(V+E) | Dijkstra |
|
|
850
|
+
| Label Propagation | O(V+E) per iteration | Community detection |
|
|
851
|
+
| Motif Finding | Pattern-dependent | DSL: `(a)-[e]->(b)` |
|
|
852
|
+
| Pregel | BSP model | Custom vertex programs |
|
|
989
853
|
|
|
990
854
|
### AI/ML Features
|
|
991
855
|
|
|
992
856
|
| Feature | Performance | Description |
|
|
993
857
|
|---------|-------------|-------------|
|
|
994
|
-
| HNSW Embeddings | 16ms/10K
|
|
858
|
+
| HNSW Embeddings | 16ms/10K | 384-dimensional vectors |
|
|
995
859
|
| Similarity Search | O(log n) | Approximate nearest neighbor |
|
|
996
|
-
|
|
|
997
|
-
|
|
|
998
|
-
| Semantic
|
|
860
|
+
| Embedding Triggers | Auto on INSERT | OpenAI/Ollama providers |
|
|
861
|
+
| Agent Memory | 94% recall @ 10K | Episodic + semantic |
|
|
862
|
+
| Semantic Caching | 2ms hit | Hash-based deduplication |
|
|
999
863
|
|
|
1000
864
|
### Reasoning Engine
|
|
1001
865
|
|
|
1002
866
|
| Feature | Algorithm | Description |
|
|
1003
867
|
|---------|-----------|-------------|
|
|
1004
|
-
| Datalog | Semi-naive
|
|
1005
|
-
| Transitive Closure | Fixpoint
|
|
1006
|
-
| Negation | Stratified | NOT in
|
|
1007
|
-
|
|
|
868
|
+
| Datalog | Semi-naive | Recursive rules |
|
|
869
|
+
| Transitive Closure | Fixpoint | ancestor(X,Y) |
|
|
870
|
+
| Stratified Negation | Stratified | NOT in bodies |
|
|
871
|
+
| Rule Chaining | Forward | Multi-hop inference |
|
|
1008
872
|
|
|
1009
873
|
### Security and Audit
|
|
1010
874
|
|
|
1011
875
|
| Feature | Implementation | Description |
|
|
1012
876
|
|---------|----------------|-------------|
|
|
1013
|
-
| WASM Sandbox |
|
|
1014
|
-
|
|
|
1015
|
-
| ProofDAG | SHA-256
|
|
1016
|
-
| Tool Validation | Type checking | Morphism composition
|
|
877
|
+
| WASM Sandbox | Fuel metering | 1M ops max |
|
|
878
|
+
| Capabilities | Set-based | ReadKG, WriteKG |
|
|
879
|
+
| ProofDAG | SHA-256 | Cryptographic audit |
|
|
880
|
+
| Tool Validation | Type checking | Morphism composition |
|
|
1017
881
|
|
|
1018
882
|
### HyperAgent Framework
|
|
1019
883
|
|
|
1020
884
|
| Feature | Description |
|
|
1021
885
|
|---------|-------------|
|
|
1022
|
-
| Schema-Aware Query Gen | Uses YOUR ontology
|
|
1023
|
-
| Deterministic Planning | No LLM for
|
|
1024
|
-
| Multi-Step Execution |
|
|
1025
|
-
| Memory Hypergraph | Episodes link to KG
|
|
1026
|
-
| Conversation Extraction | Auto-extract entities
|
|
886
|
+
| Schema-Aware Query Gen | Uses YOUR ontology |
|
|
887
|
+
| Deterministic Planning | No LLM for queries |
|
|
888
|
+
| Multi-Step Execution | SPARQL + Datalog + Motif |
|
|
889
|
+
| Memory Hypergraph | Episodes link to KG |
|
|
890
|
+
| Conversation Extraction | Auto-extract entities |
|
|
1027
891
|
| Idempotent Responses | Same question = same answer |
|
|
1028
892
|
|
|
1029
893
|
### Standards Compliance
|
|
1030
894
|
|
|
1031
|
-
| Standard | Status |
|
|
1032
|
-
|
|
1033
|
-
| SPARQL 1.1 Query | 100% |
|
|
1034
|
-
| SPARQL 1.1 Update | 100% |
|
|
1035
|
-
| RDF 1.2 | 100% |
|
|
1036
|
-
| Turtle | 100% |
|
|
1037
|
-
| N-Triples | 100% |
|
|
895
|
+
| Standard | Status |
|
|
896
|
+
|----------|--------|
|
|
897
|
+
| SPARQL 1.1 Query | 100% |
|
|
898
|
+
| SPARQL 1.1 Update | 100% |
|
|
899
|
+
| RDF 1.2 | 100% |
|
|
900
|
+
| Turtle | 100% |
|
|
901
|
+
| N-Triples | 100% |
|
|
1038
902
|
|
|
1039
903
|
## API Reference
|
|
1040
904
|
|
|
1041
905
|
### GraphDB
|
|
1042
906
|
|
|
1043
907
|
```javascript
|
|
1044
|
-
const db = new GraphDB(baseUri)
|
|
1045
|
-
db.loadTtl(turtle, graphUri)
|
|
1046
|
-
db.querySelect(sparql)
|
|
1047
|
-
db.queryConstruct(sparql)
|
|
1048
|
-
db.countTriples()
|
|
1049
|
-
db.clear()
|
|
908
|
+
const db = new GraphDB(baseUri) // Create database
|
|
909
|
+
db.loadTtl(turtle, graphUri) // Load RDF data
|
|
910
|
+
db.querySelect(sparql) // SELECT query -> results[]
|
|
911
|
+
db.queryConstruct(sparql) // CONSTRUCT -> triples string
|
|
912
|
+
db.countTriples() // Count triples -> number
|
|
913
|
+
db.clear() // Clear all data
|
|
914
|
+
db.getGraphUri() // Get base URI -> string
|
|
1050
915
|
```
|
|
1051
916
|
|
|
1052
917
|
### GraphFrame
|
|
1053
918
|
|
|
1054
919
|
```javascript
|
|
1055
920
|
const gf = new GraphFrame(verticesJson, edgesJson)
|
|
1056
|
-
gf.
|
|
1057
|
-
gf.
|
|
1058
|
-
gf.
|
|
1059
|
-
gf.
|
|
1060
|
-
gf.
|
|
921
|
+
gf.vertexCount() // -> number
|
|
922
|
+
gf.edgeCount() // -> number
|
|
923
|
+
gf.pageRank(dampingFactor, iterations) // -> JSON string
|
|
924
|
+
gf.connectedComponents() // -> JSON string
|
|
925
|
+
gf.triangleCount() // -> number
|
|
926
|
+
gf.shortestPaths(landmarks) // -> JSON string
|
|
927
|
+
gf.labelPropagation(iterations) // -> JSON string
|
|
928
|
+
gf.find(motifPattern) // -> JSON string
|
|
929
|
+
gf.inDegrees() // -> JSON string
|
|
930
|
+
gf.outDegrees() // -> JSON string
|
|
931
|
+
gf.degrees() // -> JSON string
|
|
932
|
+
gf.toJson() // -> JSON string
|
|
1061
933
|
```
|
|
1062
934
|
|
|
1063
935
|
### EmbeddingService
|
|
1064
936
|
|
|
1065
937
|
```javascript
|
|
1066
938
|
const emb = new EmbeddingService()
|
|
1067
|
-
emb.storeVector(entityId, float32Array)
|
|
1068
|
-
emb.
|
|
1069
|
-
emb.
|
|
939
|
+
emb.storeVector(entityId, float32Array) // Store vector
|
|
940
|
+
emb.getVector(entityId) // -> Float32Array | null
|
|
941
|
+
emb.deleteVector(entityId) // Delete vector
|
|
942
|
+
emb.rebuildIndex() // Build HNSW index
|
|
943
|
+
emb.findSimilar(entityId, k, threshold) // -> JSON string
|
|
944
|
+
emb.findSimilarGraceful(entityId, k, t) // -> JSON string (no throw)
|
|
945
|
+
emb.isEnabled() // -> boolean
|
|
946
|
+
emb.getMetrics() // -> JSON string
|
|
947
|
+
emb.getCacheStats() // -> JSON string
|
|
948
|
+
emb.onTripleInsert(s, p, o, g) // Trigger hook
|
|
1070
949
|
```
|
|
1071
950
|
|
|
1072
951
|
### DatalogProgram
|
|
1073
952
|
|
|
1074
953
|
```javascript
|
|
1075
954
|
const dl = new DatalogProgram()
|
|
1076
|
-
dl.addFact(factJson)
|
|
1077
|
-
dl.addRule(ruleJson)
|
|
1078
|
-
|
|
955
|
+
dl.addFact(factJson) // Add fact
|
|
956
|
+
dl.addRule(ruleJson) // Add rule
|
|
957
|
+
dl.factCount() // -> number
|
|
958
|
+
dl.ruleCount() // -> number
|
|
959
|
+
evaluateDatalog(dl) // -> JSON string (all inferred)
|
|
960
|
+
queryDatalog(dl, predicate) // -> JSON string (specific)
|
|
961
|
+
```
|
|
962
|
+
|
|
963
|
+
### HyperMindAgent
|
|
964
|
+
|
|
965
|
+
```javascript
|
|
966
|
+
const agent = new HyperMindAgent({
|
|
967
|
+
kg: db, // REQUIRED: GraphDB
|
|
968
|
+
embeddings: embeddingService, // Optional: EmbeddingService
|
|
969
|
+
name: 'agent-name', // Optional: string
|
|
970
|
+
apiKey: process.env.OPENAI_API_KEY, // Optional: LLM API key
|
|
971
|
+
sandbox: { // Optional: security config
|
|
972
|
+
capabilities: ['ReadKG'],
|
|
973
|
+
fuelLimit: 1000000
|
|
974
|
+
}
|
|
975
|
+
})
|
|
976
|
+
|
|
977
|
+
const result = await agent.call(question) // Natural language query
|
|
978
|
+
// result.answer -> string (human-readable)
|
|
979
|
+
// result.explanation -> string (execution trace)
|
|
980
|
+
// result.proof -> object (SHA-256 audit trail)
|
|
1079
981
|
```
|
|
1080
982
|
|
|
1081
983
|
### Factory Functions
|
|
1082
984
|
|
|
1083
985
|
```javascript
|
|
1084
|
-
friendsGraph()
|
|
1085
|
-
chainGraph(n)
|
|
1086
|
-
starGraph(n)
|
|
1087
|
-
completeGraph(n)
|
|
1088
|
-
cycleGraph(n)
|
|
986
|
+
friendsGraph() // Sample social graph
|
|
987
|
+
chainGraph(n) // Linear path: v0 -> v1 -> ... -> vn-1
|
|
988
|
+
starGraph(n) // Hub with n spokes
|
|
989
|
+
completeGraph(n) // Fully connected Kn
|
|
990
|
+
cycleGraph(n) // Ring: v0 -> v1 -> ... -> vn-1 -> v0
|
|
991
|
+
binaryTreeGraph(depth) // Binary tree
|
|
992
|
+
bipartiteGraph(m, n) // Bipartite Km,n
|
|
1089
993
|
```
|
|
1090
994
|
|
|
1091
|
-
##
|
|
995
|
+
## Running Benchmarks
|
|
1092
996
|
|
|
1093
997
|
```bash
|
|
1094
|
-
|
|
1095
|
-
|
|
998
|
+
# Core engine benchmarks
|
|
999
|
+
node benchmark.js
|
|
1000
|
+
|
|
1001
|
+
# Concurrency benchmarks
|
|
1002
|
+
node concurrency-benchmark.js
|
|
1096
1003
|
|
|
1097
|
-
|
|
1004
|
+
# Memory retrieval benchmarks
|
|
1005
|
+
node memory-retrieval-benchmark.js
|
|
1098
1006
|
|
|
1099
|
-
|
|
1007
|
+
# HyperMind vs Vanilla LLM (requires API key)
|
|
1008
|
+
ANTHROPIC_API_KEY=... node vanilla-vs-hypermind-benchmark.js
|
|
1009
|
+
|
|
1010
|
+
# Framework comparison (requires Python + API key)
|
|
1011
|
+
OPENAI_API_KEY=... python3 benchmark-frameworks.py
|
|
1012
|
+
```
|
|
1100
1013
|
|
|
1101
1014
|
## License
|
|
1102
1015
|
|