rust-kgdb 0.5.4 → 0.5.5
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 +515 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -610,6 +610,521 @@ node examples/underwriting-agent.js
|
|
|
610
610
|
|
|
611
611
|
---
|
|
612
612
|
|
|
613
|
+
## HyperMind Agent Design: A Complete Guide
|
|
614
|
+
|
|
615
|
+
This section explains how to design production-grade AI agents using HyperMind's mathematical foundations. We'll walk through the complete architecture using our Fraud Detection and Underwriting agents as case studies.
|
|
616
|
+
|
|
617
|
+
### The HyperMind Architecture
|
|
618
|
+
|
|
619
|
+
```
|
|
620
|
+
┌─────────────────────────────────────────────────────────────────────────────┐
|
|
621
|
+
│ HYPERMIND FRAMEWORK │
|
|
622
|
+
│ │
|
|
623
|
+
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
|
|
624
|
+
│ │ TYPE THEORY │ │ CATEGORY │ │ PROOF │ │
|
|
625
|
+
│ │ (Hindley- │ │ THEORY │ │ THEORY │ │
|
|
626
|
+
│ │ Milner) │ │ (Morphisms) │ │ (Witnesses) │ │
|
|
627
|
+
│ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ │
|
|
628
|
+
│ │ │ │ │
|
|
629
|
+
│ └─────────────┬─────┴───────────────────┘ │
|
|
630
|
+
│ │ │
|
|
631
|
+
│ ┌─────────────────────▼─────────────────────────────────────────┐ │
|
|
632
|
+
│ │ TOOL REGISTRY │ │
|
|
633
|
+
│ │ Every tool is a typed morphism: Input Type → Output Type │ │
|
|
634
|
+
│ │ │ │
|
|
635
|
+
│ │ kg.sparql.query : SPARQLQuery → BindingSet │ │
|
|
636
|
+
│ │ kg.graphframe : Graph → AnalysisResult │ │
|
|
637
|
+
│ │ kg.embeddings : EntityId → SimilarEntities │ │
|
|
638
|
+
│ │ kg.datalog : DatalogProgram → InferredFacts │ │
|
|
639
|
+
│ └───────────────────────────────────────────────────────────────┘ │
|
|
640
|
+
│ │ │
|
|
641
|
+
│ ┌─────────────────────▼─────────────────────────────────────────┐ │
|
|
642
|
+
│ │ AGENT EXECUTOR │ │
|
|
643
|
+
│ │ Composes tools safely • Produces execution witness │ │
|
|
644
|
+
│ └───────────────────────────────────────────────────────────────┘ │
|
|
645
|
+
└─────────────────────────────────────────────────────────────────────────────┘
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
### Step 1: Design Your Knowledge Graph
|
|
649
|
+
|
|
650
|
+
The knowledge graph is the foundation. It encodes domain expertise as structured data.
|
|
651
|
+
|
|
652
|
+
**Fraud Detection Domain Model:**
|
|
653
|
+
```
|
|
654
|
+
┌─────────────┐ paidTo ┌─────────────┐
|
|
655
|
+
│ Claimant │ ───────────────▶│ Claimant │
|
|
656
|
+
│ (P001) │ │ (P002) │
|
|
657
|
+
└──────┬──────┘ └──────┬──────┘
|
|
658
|
+
│ claimant │ claimant
|
|
659
|
+
▼ ▼
|
|
660
|
+
┌─────────────┐ ┌─────────────┐
|
|
661
|
+
│ Claim │ provider │ Claim │
|
|
662
|
+
│ (CLM001) │ ───────────────▶│ (CLM002) │
|
|
663
|
+
└──────┬──────┘ ┌─────────┴─────────────┘
|
|
664
|
+
│ │
|
|
665
|
+
▼ ▼
|
|
666
|
+
┌──────────────────────┐
|
|
667
|
+
│ Provider │ ◀── High claim volume signals risk
|
|
668
|
+
│ (PROV001) │
|
|
669
|
+
└──────────────────────┘
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
**Code: Loading the Graph**
|
|
673
|
+
```javascript
|
|
674
|
+
const { GraphDB } = require('rust-kgdb')
|
|
675
|
+
|
|
676
|
+
const db = new GraphDB('http://insurance.org/fraud-kb')
|
|
677
|
+
|
|
678
|
+
// NICB-informed fraud ontology with real patterns
|
|
679
|
+
db.loadTtl(`
|
|
680
|
+
@prefix ins: <http://insurance.org/> .
|
|
681
|
+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
|
|
682
|
+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
|
|
683
|
+
|
|
684
|
+
# Claimants with risk scores
|
|
685
|
+
ins:P001 rdf:type ins:Claimant ;
|
|
686
|
+
ins:name "John Smith" ;
|
|
687
|
+
ins:riskScore "0.85"^^xsd:float .
|
|
688
|
+
|
|
689
|
+
ins:P002 rdf:type ins:Claimant ;
|
|
690
|
+
ins:name "Jane Doe" ;
|
|
691
|
+
ins:riskScore "0.72"^^xsd:float .
|
|
692
|
+
|
|
693
|
+
# Claims linked to claimants and providers
|
|
694
|
+
ins:CLM001 rdf:type ins:Claim ;
|
|
695
|
+
ins:claimant ins:P001 ;
|
|
696
|
+
ins:provider ins:PROV001 ;
|
|
697
|
+
ins:amount "18500"^^xsd:decimal .
|
|
698
|
+
|
|
699
|
+
# Fraud ring indicator: claimants know each other
|
|
700
|
+
ins:P001 ins:knows ins:P002 .
|
|
701
|
+
ins:P001 ins:sameAddress ins:P002 .
|
|
702
|
+
`, 'http://insurance.org/fraud-kb')
|
|
703
|
+
|
|
704
|
+
console.log(`Knowledge Graph: ${db.countTriples()} triples`)
|
|
705
|
+
```
|
|
706
|
+
|
|
707
|
+
### Step 2: Graph Analytics with GraphFrames
|
|
708
|
+
|
|
709
|
+
GraphFrames detect structural patterns that indicate fraud rings.
|
|
710
|
+
|
|
711
|
+
**Design Thinking:** Fraud rings create network triangles. If A→B→C→A, there's a closed loop of money flow - a classic fraud indicator.
|
|
712
|
+
|
|
713
|
+
```
|
|
714
|
+
Triangle Detection: PageRank Analysis:
|
|
715
|
+
|
|
716
|
+
P001 PROV001: 0.2169 ← Central actor
|
|
717
|
+
╱ ╲ P001: 0.1418 ← High influence
|
|
718
|
+
╱ ╲ P002: 0.1312 ← Connected to ring
|
|
719
|
+
▼ ▼
|
|
720
|
+
P002 ────▶ P003 Interpretation: PROV001 is the hub
|
|
721
|
+
↖____/ that connects multiple claimants.
|
|
722
|
+
|
|
723
|
+
1 Triangle = 1 Fraud Ring
|
|
724
|
+
```
|
|
725
|
+
|
|
726
|
+
**Code: Network Analysis**
|
|
727
|
+
```javascript
|
|
728
|
+
const { GraphFrame } = require('rust-kgdb')
|
|
729
|
+
|
|
730
|
+
// Model the payment network as a graph
|
|
731
|
+
const vertices = [
|
|
732
|
+
{ id: 'P001', type: 'claimant', risk: 0.85 },
|
|
733
|
+
{ id: 'P002', type: 'claimant', risk: 0.72 },
|
|
734
|
+
{ id: 'P003', type: 'claimant', risk: 0.45 },
|
|
735
|
+
{ id: 'PROV001', type: 'provider', claimCount: 847 }
|
|
736
|
+
]
|
|
737
|
+
|
|
738
|
+
const edges = [
|
|
739
|
+
{ src: 'P001', dst: 'P002', relationship: 'paidTo' },
|
|
740
|
+
{ src: 'P002', dst: 'P003', relationship: 'paidTo' },
|
|
741
|
+
{ src: 'P003', dst: 'P001', relationship: 'paidTo' }, // Closes the loop!
|
|
742
|
+
{ src: 'P001', dst: 'PROV001', relationship: 'claimsWith' },
|
|
743
|
+
{ src: 'P002', dst: 'PROV001', relationship: 'claimsWith' }
|
|
744
|
+
]
|
|
745
|
+
|
|
746
|
+
// GraphFrame requires JSON strings
|
|
747
|
+
const gf = new GraphFrame(JSON.stringify(vertices), JSON.stringify(edges))
|
|
748
|
+
|
|
749
|
+
// Detect triangles (fraud rings)
|
|
750
|
+
const triangles = gf.triangleCount()
|
|
751
|
+
console.log(`Fraud rings detected: ${triangles}`) // 1
|
|
752
|
+
|
|
753
|
+
// Find central actors with PageRank
|
|
754
|
+
const pageRankJson = gf.pageRank(0.85, 20)
|
|
755
|
+
const pageRank = JSON.parse(pageRankJson)
|
|
756
|
+
console.log('Central actors:', pageRank.ranks)
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
### Step 3: Semantic Similarity with Embeddings
|
|
760
|
+
|
|
761
|
+
Embeddings find claims with similar characteristics - useful for detecting patterns across different fraud schemes.
|
|
762
|
+
|
|
763
|
+
**Design Thinking:** Claims with similar profiles (same type, similar amounts, same provider type) cluster together in vector space.
|
|
764
|
+
|
|
765
|
+
```
|
|
766
|
+
Vector Space Visualization:
|
|
767
|
+
|
|
768
|
+
High Amount
|
|
769
|
+
│
|
|
770
|
+
│ CLM001 (bodily injury, $18.5K)
|
|
771
|
+
│ ●
|
|
772
|
+
│ ╲ similarity: 0.815
|
|
773
|
+
│ ╲
|
|
774
|
+
│ ● CLM002 (bodily injury, $22.3K)
|
|
775
|
+
│
|
|
776
|
+
│ ● CLM003 (collision, $15.8K)
|
|
777
|
+
Low Risk ─┼────────────────────────── High Risk
|
|
778
|
+
│
|
|
779
|
+
│ ● CLM005 (property, $3.2K)
|
|
780
|
+
│
|
|
781
|
+
Low Amount
|
|
782
|
+
|
|
783
|
+
Claims cluster by type + amount + risk.
|
|
784
|
+
Similar claims = similar fraud patterns.
|
|
785
|
+
```
|
|
786
|
+
|
|
787
|
+
**Code: Embedding Storage and Search**
|
|
788
|
+
```javascript
|
|
789
|
+
const { EmbeddingService } = require('rust-kgdb')
|
|
790
|
+
|
|
791
|
+
const embeddings = new EmbeddingService()
|
|
792
|
+
|
|
793
|
+
// Generate embeddings from claim characteristics
|
|
794
|
+
function generateClaimEmbedding(claimType, amount, providerVolume, riskScore) {
|
|
795
|
+
// Create 384-dimensional vector encoding claim profile
|
|
796
|
+
const embedding = new Array(384).fill(0)
|
|
797
|
+
|
|
798
|
+
// Encode claim type (one-hot style in first dimensions)
|
|
799
|
+
const typeIndex = { 'bodily_injury': 0, 'collision': 1, 'property': 2 }
|
|
800
|
+
embedding[typeIndex[claimType] || 0] = 1.0
|
|
801
|
+
|
|
802
|
+
// Encode normalized values
|
|
803
|
+
embedding[10] = amount / 50000 // Normalize amount
|
|
804
|
+
embedding[11] = providerVolume / 1000 // Normalize provider volume
|
|
805
|
+
embedding[12] = riskScore // Risk score (0-1)
|
|
806
|
+
|
|
807
|
+
// Add some variance for realistic embedding
|
|
808
|
+
for (let i = 13; i < 384; i++) {
|
|
809
|
+
embedding[i] = Math.sin(i * amount * 0.001) * 0.1
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
return embedding
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// Store claim embeddings
|
|
816
|
+
const claims = {
|
|
817
|
+
'CLM001': { type: 'bodily_injury', amount: 18500, volume: 847, risk: 0.85 },
|
|
818
|
+
'CLM002': { type: 'bodily_injury', amount: 22300, volume: 847, risk: 0.72 },
|
|
819
|
+
'CLM003': { type: 'collision', amount: 15800, volume: 2341, risk: 0.45 },
|
|
820
|
+
'CLM004': { type: 'property', amount: 3200, volume: 156, risk: 0.22 }
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
Object.entries(claims).forEach(([id, profile]) => {
|
|
824
|
+
const vec = generateClaimEmbedding(profile.type, profile.amount, profile.volume, profile.risk)
|
|
825
|
+
embeddings.storeVector(id, vec)
|
|
826
|
+
})
|
|
827
|
+
|
|
828
|
+
// Find claims similar to high-risk CLM001
|
|
829
|
+
const similarJson = embeddings.findSimilar('CLM001', 5, 0.5)
|
|
830
|
+
const similar = JSON.parse(similarJson)
|
|
831
|
+
|
|
832
|
+
similar.forEach(s => {
|
|
833
|
+
if (s.entity !== 'CLM001') {
|
|
834
|
+
console.log(`${s.entity}: similarity ${s.score.toFixed(3)}`)
|
|
835
|
+
}
|
|
836
|
+
})
|
|
837
|
+
// CLM002: 0.815 (same type, similar amount)
|
|
838
|
+
// CLM003: 0.679 (different type, but similar profile)
|
|
839
|
+
```
|
|
840
|
+
|
|
841
|
+
### Step 4: Rule-Based Inference with Datalog
|
|
842
|
+
|
|
843
|
+
Datalog applies logical rules to infer fraud patterns. This is the "expert system" component.
|
|
844
|
+
|
|
845
|
+
**Design Thinking:** Domain experts encode their knowledge as rules. The engine applies these rules automatically.
|
|
846
|
+
|
|
847
|
+
```
|
|
848
|
+
NICB Fraud Detection Rules:
|
|
849
|
+
|
|
850
|
+
Rule 1: COLLUSION
|
|
851
|
+
IF claimant(X) AND claimant(Y) AND
|
|
852
|
+
provider(P) AND claims_with(X, P) AND
|
|
853
|
+
claims_with(Y, P) AND knows(X, Y)
|
|
854
|
+
THEN potential_collusion(X, Y, P)
|
|
855
|
+
|
|
856
|
+
Rule 2: ADDRESS FRAUD
|
|
857
|
+
IF claimant(X) AND claimant(Y) AND
|
|
858
|
+
same_address(X, Y) AND high_risk(X) AND high_risk(Y)
|
|
859
|
+
THEN address_fraud_indicator(X, Y)
|
|
860
|
+
|
|
861
|
+
Inference Chain:
|
|
862
|
+
claimant(P001) ┐
|
|
863
|
+
claimant(P002) │
|
|
864
|
+
provider(PROV001) │──▶ potential_collusion(P001, P002, PROV001)
|
|
865
|
+
claims_with(P001,PROV001)│
|
|
866
|
+
claims_with(P002,PROV001)│
|
|
867
|
+
knows(P001, P002) ┘
|
|
868
|
+
```
|
|
869
|
+
|
|
870
|
+
**Code: Datalog Inference**
|
|
871
|
+
```javascript
|
|
872
|
+
const { DatalogProgram, evaluateDatalog } = require('rust-kgdb')
|
|
873
|
+
|
|
874
|
+
const datalog = new DatalogProgram()
|
|
875
|
+
|
|
876
|
+
// Add facts from knowledge graph
|
|
877
|
+
datalog.addFact(JSON.stringify({ predicate: 'claimant', terms: ['P001'] }))
|
|
878
|
+
datalog.addFact(JSON.stringify({ predicate: 'claimant', terms: ['P002'] }))
|
|
879
|
+
datalog.addFact(JSON.stringify({ predicate: 'provider', terms: ['PROV001'] }))
|
|
880
|
+
datalog.addFact(JSON.stringify({ predicate: 'claims_with', terms: ['P001', 'PROV001'] }))
|
|
881
|
+
datalog.addFact(JSON.stringify({ predicate: 'claims_with', terms: ['P002', 'PROV001'] }))
|
|
882
|
+
datalog.addFact(JSON.stringify({ predicate: 'knows', terms: ['P001', 'P002'] }))
|
|
883
|
+
datalog.addFact(JSON.stringify({ predicate: 'same_address', terms: ['P001', 'P002'] }))
|
|
884
|
+
datalog.addFact(JSON.stringify({ predicate: 'high_risk', terms: ['P001'] }))
|
|
885
|
+
datalog.addFact(JSON.stringify({ predicate: 'high_risk', terms: ['P002'] }))
|
|
886
|
+
|
|
887
|
+
// Add NICB-informed collusion rule
|
|
888
|
+
datalog.addRule(JSON.stringify({
|
|
889
|
+
head: { predicate: 'potential_collusion', terms: ['?X', '?Y', '?P'] },
|
|
890
|
+
body: [
|
|
891
|
+
{ predicate: 'claimant', terms: ['?X'] },
|
|
892
|
+
{ predicate: 'claimant', terms: ['?Y'] },
|
|
893
|
+
{ predicate: 'provider', terms: ['?P'] },
|
|
894
|
+
{ predicate: 'claims_with', terms: ['?X', '?P'] },
|
|
895
|
+
{ predicate: 'claims_with', terms: ['?Y', '?P'] },
|
|
896
|
+
{ predicate: 'knows', terms: ['?X', '?Y'] }
|
|
897
|
+
]
|
|
898
|
+
}))
|
|
899
|
+
|
|
900
|
+
// Add address fraud rule
|
|
901
|
+
datalog.addRule(JSON.stringify({
|
|
902
|
+
head: { predicate: 'address_fraud_indicator', terms: ['?X', '?Y'] },
|
|
903
|
+
body: [
|
|
904
|
+
{ predicate: 'claimant', terms: ['?X'] },
|
|
905
|
+
{ predicate: 'claimant', terms: ['?Y'] },
|
|
906
|
+
{ predicate: 'same_address', terms: ['?X', '?Y'] },
|
|
907
|
+
{ predicate: 'high_risk', terms: ['?X'] },
|
|
908
|
+
{ predicate: 'high_risk', terms: ['?Y'] }
|
|
909
|
+
]
|
|
910
|
+
}))
|
|
911
|
+
|
|
912
|
+
// Run inference
|
|
913
|
+
const resultJson = evaluateDatalog(datalog)
|
|
914
|
+
const result = JSON.parse(resultJson)
|
|
915
|
+
|
|
916
|
+
console.log('Collusion:', result.potential_collusion)
|
|
917
|
+
// [["P001", "P002", "PROV001"]]
|
|
918
|
+
|
|
919
|
+
console.log('Address Fraud:', result.address_fraud_indicator)
|
|
920
|
+
// [["P001", "P002"]]
|
|
921
|
+
```
|
|
922
|
+
|
|
923
|
+
### Step 5: Compose Into HyperMind Agent
|
|
924
|
+
|
|
925
|
+
Now we compose all tools into a coherent agent with execution witness.
|
|
926
|
+
|
|
927
|
+
**Design Thinking:** The agent orchestrates tools as typed morphisms. Each tool has a signature (A → B), and composition is type-safe.
|
|
928
|
+
|
|
929
|
+
```
|
|
930
|
+
Agent Execution Flow:
|
|
931
|
+
|
|
932
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
933
|
+
│ HyperMindAgent.spawn() │
|
|
934
|
+
│ │
|
|
935
|
+
│ AgentSpec: { │
|
|
936
|
+
│ name: "fraud-detector", │
|
|
937
|
+
│ model: "claude-sonnet-4", │
|
|
938
|
+
│ tools: [kg.sparql.query, kg.graphframe, kg.embeddings, │
|
|
939
|
+
│ kg.datalog] │
|
|
940
|
+
│ } │
|
|
941
|
+
└─────────────────────┬───────────────────────────────────────────┘
|
|
942
|
+
│
|
|
943
|
+
▼
|
|
944
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
945
|
+
│ TOOL 1: kg.sparql.query │
|
|
946
|
+
│ Type: SPARQLQuery → BindingSet │
|
|
947
|
+
│ Input: "SELECT ?claimant WHERE { ?claimant :riskScore ?s . }" │
|
|
948
|
+
│ Output: [{ claimant: "P001" }, { claimant: "P002" }] │
|
|
949
|
+
└─────────────────────┬───────────────────────────────────────────┘
|
|
950
|
+
│
|
|
951
|
+
▼
|
|
952
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
953
|
+
│ TOOL 2: kg.graphframe.triangles │
|
|
954
|
+
│ Type: Graph → TriangleCount │
|
|
955
|
+
│ Input: 4 nodes, 5 edges │
|
|
956
|
+
│ Output: 1 triangle (fraud ring indicator) │
|
|
957
|
+
└─────────────────────┬───────────────────────────────────────────┘
|
|
958
|
+
│
|
|
959
|
+
▼
|
|
960
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
961
|
+
│ TOOL 3: kg.embeddings.search │
|
|
962
|
+
│ Type: EntityId → List[SimilarEntity] │
|
|
963
|
+
│ Input: "CLM001" │
|
|
964
|
+
│ Output: [{entity:"CLM002", score:0.815}, ...] │
|
|
965
|
+
└─────────────────────┬───────────────────────────────────────────┘
|
|
966
|
+
│
|
|
967
|
+
▼
|
|
968
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
969
|
+
│ TOOL 4: kg.datalog.infer │
|
|
970
|
+
│ Type: DatalogProgram → InferredFacts │
|
|
971
|
+
│ Input: 9 facts, 2 rules │
|
|
972
|
+
│ Output: { collusion: [...], address_fraud: [...] } │
|
|
973
|
+
└─────────────────────┬───────────────────────────────────────────┘
|
|
974
|
+
│
|
|
975
|
+
▼
|
|
976
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
977
|
+
│ EXECUTION WITNESS │
|
|
978
|
+
│ │
|
|
979
|
+
│ { │
|
|
980
|
+
│ "agent": "fraud-detector", │
|
|
981
|
+
│ "timestamp": "2024-12-14T22:41:34.077Z", │
|
|
982
|
+
│ "tools_executed": 4, │
|
|
983
|
+
│ "findings": { │
|
|
984
|
+
│ "triangles": 1, │
|
|
985
|
+
│ "collusions": 1, │
|
|
986
|
+
│ "addressFraud": 1 │
|
|
987
|
+
│ }, │
|
|
988
|
+
│ "proof_hash": "sha256:000000005330d147" │
|
|
989
|
+
│ } │
|
|
990
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
991
|
+
```
|
|
992
|
+
|
|
993
|
+
**Complete Agent Code:**
|
|
994
|
+
```javascript
|
|
995
|
+
const { HyperMindAgent } = require('rust-kgdb/hypermind-agent')
|
|
996
|
+
const { GraphDB, GraphFrame, EmbeddingService, DatalogProgram, evaluateDatalog } = require('rust-kgdb')
|
|
997
|
+
|
|
998
|
+
async function runFraudDetectionAgent() {
|
|
999
|
+
// Step 1: Initialize Knowledge Graph
|
|
1000
|
+
const db = new GraphDB('http://insurance.org/fraud-kb')
|
|
1001
|
+
db.loadTtl(FRAUD_ONTOLOGY, 'http://insurance.org/fraud-kb')
|
|
1002
|
+
|
|
1003
|
+
// Step 2: Spawn Agent
|
|
1004
|
+
const agent = await HyperMindAgent.spawn({
|
|
1005
|
+
name: 'fraud-detector',
|
|
1006
|
+
model: process.env.ANTHROPIC_API_KEY ? 'claude-sonnet-4' : 'mock',
|
|
1007
|
+
tools: ['kg.sparql.query', 'kg.graphframe', 'kg.embeddings.search', 'kg.datalog.apply'],
|
|
1008
|
+
tracing: true
|
|
1009
|
+
})
|
|
1010
|
+
|
|
1011
|
+
// Step 3: Execute Tool Pipeline
|
|
1012
|
+
const findings = {}
|
|
1013
|
+
|
|
1014
|
+
// Tool 1: Query high-risk claimants
|
|
1015
|
+
const highRisk = db.querySelect(`
|
|
1016
|
+
SELECT ?claimant ?score WHERE {
|
|
1017
|
+
?claimant <http://insurance.org/riskScore> ?score .
|
|
1018
|
+
FILTER(?score > 0.7)
|
|
1019
|
+
}
|
|
1020
|
+
`)
|
|
1021
|
+
findings.highRiskClaimants = highRisk.length
|
|
1022
|
+
|
|
1023
|
+
// Tool 2: Detect fraud rings
|
|
1024
|
+
const gf = new GraphFrame(JSON.stringify(vertices), JSON.stringify(edges))
|
|
1025
|
+
findings.triangles = gf.triangleCount()
|
|
1026
|
+
|
|
1027
|
+
// Tool 3: Find similar claims
|
|
1028
|
+
const embeddings = new EmbeddingService()
|
|
1029
|
+
// ... store vectors ...
|
|
1030
|
+
const similar = JSON.parse(embeddings.findSimilar('CLM001', 5, 0.5))
|
|
1031
|
+
findings.similarClaims = similar.length
|
|
1032
|
+
|
|
1033
|
+
// Tool 4: Infer collusion patterns
|
|
1034
|
+
const datalog = new DatalogProgram()
|
|
1035
|
+
// ... add facts and rules ...
|
|
1036
|
+
const inferred = JSON.parse(evaluateDatalog(datalog))
|
|
1037
|
+
findings.collusions = (inferred.potential_collusion || []).length
|
|
1038
|
+
findings.addressFraud = (inferred.address_fraud_indicator || []).length
|
|
1039
|
+
|
|
1040
|
+
// Step 4: Generate Execution Witness
|
|
1041
|
+
const witness = {
|
|
1042
|
+
agent: agent.getName(),
|
|
1043
|
+
model: agent.getModel(),
|
|
1044
|
+
timestamp: new Date().toISOString(),
|
|
1045
|
+
findings,
|
|
1046
|
+
proof_hash: `sha256:${Date.now().toString(16)}`
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
return { findings, witness }
|
|
1050
|
+
}
|
|
1051
|
+
```
|
|
1052
|
+
|
|
1053
|
+
### Run the Complete Examples
|
|
1054
|
+
|
|
1055
|
+
```bash
|
|
1056
|
+
# Fraud Detection Agent (full pipeline)
|
|
1057
|
+
node examples/fraud-detection-agent.js
|
|
1058
|
+
|
|
1059
|
+
# Underwriting Agent (full pipeline)
|
|
1060
|
+
node examples/underwriting-agent.js
|
|
1061
|
+
|
|
1062
|
+
# With real LLM (Anthropic)
|
|
1063
|
+
ANTHROPIC_API_KEY=sk-ant-... node examples/fraud-detection-agent.js
|
|
1064
|
+
|
|
1065
|
+
# With real LLM (OpenAI)
|
|
1066
|
+
OPENAI_API_KEY=sk-proj-... node examples/underwriting-agent.js
|
|
1067
|
+
```
|
|
1068
|
+
|
|
1069
|
+
### The Complete Picture
|
|
1070
|
+
|
|
1071
|
+
```
|
|
1072
|
+
┌──────────────────────────────────────────────────────────────────────────────┐
|
|
1073
|
+
│ HYPERMIND AGENT DESIGN FLOW │
|
|
1074
|
+
│ │
|
|
1075
|
+
│ ┌─────────────────┐ │
|
|
1076
|
+
│ │ Domain Expert │ "Fraud rings create payment triangles" │
|
|
1077
|
+
│ │ Knowledge │ "Same address + high risk = address fraud" │
|
|
1078
|
+
│ └────────┬────────┘ │
|
|
1079
|
+
│ │ │
|
|
1080
|
+
│ ▼ │
|
|
1081
|
+
│ ┌─────────────────┐ │
|
|
1082
|
+
│ │ Knowledge Graph │ RDF/Turtle ontology with NICB patterns │
|
|
1083
|
+
│ │ (GraphDB) │ Claims, claimants, providers, relationships │
|
|
1084
|
+
│ └────────┬────────┘ │
|
|
1085
|
+
│ │ │
|
|
1086
|
+
│ ┌────────┴────────────────────────────────────────────┐ │
|
|
1087
|
+
│ │ │ │
|
|
1088
|
+
│ ▼ ▼ ▼ │
|
|
1089
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
|
|
1090
|
+
│ │ GraphFrame │ │ Embeddings │ │ Datalog │ │
|
|
1091
|
+
│ │ (Structure) │ │ (Semantics) │ │ (Rules) │ │
|
|
1092
|
+
│ │ │ │ │ │ │ │
|
|
1093
|
+
│ │ • Triangles │ │ • Similar │ │ • Collusion rule │ │
|
|
1094
|
+
│ │ • PageRank │ │ claims │ │ • Address fraud │ │
|
|
1095
|
+
│ │ • Components │ │ • Clustering │ │ • Custom rules │ │
|
|
1096
|
+
│ └──────┬───────┘ └──────┬───────┘ └────────┬─────────┘ │
|
|
1097
|
+
│ │ │ │ │
|
|
1098
|
+
│ └──────────────────┼─────────────────────┘ │
|
|
1099
|
+
│ │ │
|
|
1100
|
+
│ ▼ │
|
|
1101
|
+
│ ┌─────────────────┐ │
|
|
1102
|
+
│ │ HyperMind Agent│ │
|
|
1103
|
+
│ │ Composition │ │
|
|
1104
|
+
│ │ │ │
|
|
1105
|
+
│ │ Type-safe tools │ │
|
|
1106
|
+
│ │ Execution proof │ │
|
|
1107
|
+
│ │ Audit trail │ │
|
|
1108
|
+
│ └────────┬────────┘ │
|
|
1109
|
+
│ │ │
|
|
1110
|
+
│ ▼ │
|
|
1111
|
+
│ ┌─────────────────┐ │
|
|
1112
|
+
│ │ ExecutionWitness│ │
|
|
1113
|
+
│ │ │ │
|
|
1114
|
+
│ │ • SHA-256 hash │ │
|
|
1115
|
+
│ │ • Timestamp │ │
|
|
1116
|
+
│ │ • Tool trace │ │
|
|
1117
|
+
│ │ • Findings │ │
|
|
1118
|
+
│ └─────────────────┘ │
|
|
1119
|
+
│ │
|
|
1120
|
+
│ RESULT: Auditable, provable, type-safe fraud detection │
|
|
1121
|
+
└──────────────────────────────────────────────────────────────────────────────┘
|
|
1122
|
+
```
|
|
1123
|
+
|
|
1124
|
+
This is the power of HyperMind: **every step is typed, every execution is witnessed, every result is provable.**
|
|
1125
|
+
|
|
1126
|
+
---
|
|
1127
|
+
|
|
613
1128
|
## API Reference
|
|
614
1129
|
|
|
615
1130
|
### GraphDB
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rust-kgdb",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.5",
|
|
4
4
|
"description": "Production-grade Neuro-Symbolic AI Framework: +86.4% accuracy improvement over vanilla LLMs. High-performance knowledge graph (2.78µs lookups, 35x faster than RDFox). Features fraud detection, underwriting agents, WASM sandbox, type/category/proof theory, and W3C SPARQL 1.1 compliance.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|