contrag-core 0.1.0

ContRAG - Advanced RAG library for ICP canisters with on-chain storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# ContRAG Rust - ICP Edition

**Bring Retrieval-Augmented Generation (RAG) to Web3 with ICP Canisters**

ContRAG Rust is a powerful library for building RAG systems directly on the Internet Computer Protocol (ICP). Store entity data on-chain, generate embeddings via HTTP outcalls, and build intelligent context for AI applicationsβ€”all within your canister.

## πŸš€ Key Features

- **On-Chain RAG**: Build RAG systems entirely on ICP with stable memory storage
- **External AI Integration**: Use OpenAI, Gemini, or custom embedders via HTTP outcalls
- **Flexible Data Sources**: Read from canister state, stable memory, or inter-canister calls
- **Web3-Native**: Designed specifically for blockchain data and Web3 applications
- **Zero Database Dependencies**: No PostgreSQL, MongoDB, or external vector DBs required
- **Developer-Friendly**: Simple configuration with `.env` for secrets, JSON for schemas

## πŸ“¦ Installation

Add to your canister's `Cargo.toml`:

```toml
[dependencies]
contrag-core = { git = "https://github.com/dhaniverse/contrag", branch = "main" }
ic-cdk = "0.13"
ic-cdk-macros = "0.13"
candid = "0.10"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
```

## 🎯 Quick Start

### 1. Define Your Entities

```rust
use contrag_core::prelude::*;
use candid::{CandidType, Deserialize};
use serde::Serialize;

#[derive(Clone, Debug, CandidType, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub name: String,
    pub email: String,
    pub order_ids: Vec<String>,
}

impl RagEntity for User {
    fn entity_type() -> &'static str {
        "User"
    }

    fn entity_id(&self) -> String {
        self.id.clone()
    }

    fn to_context_map(&self) -> Vec<(String, String)> {
        vec![
            ("id".to_string(), self.id.clone()),
            ("name".to_string(), self.name.clone()),
            ("email".to_string(), self.email.clone()),
        ]
    }

    fn relationships(&self) -> Vec<EntityRelationship> {
        self.order_ids
            .iter()
            .map(|id| EntityRelationship {
                field_name: "orders".to_string(),
                target_entity_type: "Order".to_string(),
                target_id: id.clone(),
                relationship_type: RelationshipType::OneToMany,
            })
            .collect()
    }
}
```

### 2. Create Configuration

Create `contrag.config.json`:

```json
{
  "entities": [
    {
      "name": "User",
      "canister_id": "your-canister-id",
      "fetch_method": "get_user",
      "relationships": [],
      "auto_include": true
    }
  ],
  "embedder": {
    "provider": "openai",
    "model": "text-embedding-3-small",
    "dimensions": 1536
  },
  "chunking": {
    "chunk_size": 1000,
    "overlap": 100,
    "include_field_names": true
  },
  "vector_store": {
    "storage_type": "stable_memory",
    "enable_cache": true
  }
}
```

Create `.env` (DO NOT commit):

```bash
OPENAI_API_KEY=sk-your-key-here
```

### 3. Build RAG Context

```rust
use ic_cdk_macros::*;
use contrag_core::prelude::*;
use contrag_core::embedders::openai::OpenAIEmbedder;
use contrag_core::vector_store::stable_memory_store::StableMemoryVectorStore;

#[update]
async fn build_rag_context(user_id: String, api_key: String) -> Result<String, String> {
    // Load configuration
    let config_json = include_str!("../contrag.config.json");
    let config = contrag_core::config::load_config_from_json(config_json)
        .map_err(|e| e.to_string())?;

    // Get user data
    let user = get_user(user_id.clone())
        .ok_or_else(|| "User not found".to_string())?;

    // Build context
    let context_builder = ContextBuilder::new(config.chunking.clone());
    let context = context_builder.build_entity_context(&user);
    let chunks = context_builder.chunk_text(&context);

    // Generate embeddings
    let embedder = OpenAIEmbedder::new(api_key, config.embedder.model.clone());
    let texts: Vec<String> = chunks.iter().map(|c| c.text.clone()).collect();
    let embeddings = embedder.embed(texts.clone())
        .await
        .map_err(|e| e.to_string())?;

    // Store vectors
    let mut vector_store = StableMemoryVectorStore::new();
    let namespace = format!("User:{}", user_id);
    
    for (idx, (chunk, embedding)) in chunks.iter().zip(embeddings).enumerate() {
        let vector = Vector {
            id: format!("{}::chunk_{}", user_id, idx),
            embedding,
            text: chunk.text.clone(),
            metadata: VectorMetadata {
                entity_type: "User".to_string(),
                entity_id: user_id.clone(),
                chunk_index: idx,
                total_chunks: chunks.len(),
                timestamp: ic_cdk::api::time(),
                custom: None,
            },
        };
        vector_store.store(&namespace, vector).await.map_err(|e| e.to_string())?;
    }

    Ok(format!("Built context with {} chunks", chunks.len()))
}
```

### 4. Search Context

```rust
#[update]
async fn search_context(
    user_id: String,
    query: String,
    api_key: String,
) -> Result<Vec<SearchResult>, String> {
    let config_json = include_str!("../contrag.config.json");
    let config = contrag_core::config::load_config_from_json(config_json)
        .map_err(|e| e.to_string())?;

    // Generate query embedding
    let embedder = OpenAIEmbedder::new(api_key, config.embedder.model.clone());
    let query_embeddings = embedder.embed(vec![query])
        .await
        .map_err(|e| e.to_string())?;
    
    let query_embedding = query_embeddings
        .into_iter()
        .next()
        .ok_or_else(|| "No embedding generated".to_string())?;

    // Search vector store
    let vector_store = StableMemoryVectorStore::new();
    let namespace = format!("User:{}", user_id);
    
    vector_store.search(&namespace, query_embedding, 5)
        .await
        .map_err(|e| e.to_string())
}
```

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Your ICP Canister (Rust)       β”‚
β”‚                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   contrag-core Library       β”‚  β”‚
β”‚  β”‚                              β”‚  β”‚
β”‚  β”‚  β€’ RagEntity Trait           β”‚  β”‚
β”‚  β”‚  β€’ ContextBuilder            β”‚  β”‚
β”‚  β”‚  β€’ StableMemoryVectorStore   β”‚  β”‚
β”‚  β”‚  β€’ Entity Relationships      β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                    β”‚
        ↓                    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ HTTP Outcalls β”‚   β”‚  Stable Memory     β”‚
β”‚               β”‚   β”‚                    β”‚
β”‚ β€’ OpenAI      β”‚   β”‚ β€’ On-chain vectors β”‚
β”‚ β€’ Gemini      β”‚   β”‚ β€’ Persistent state β”‚
β”‚ β€’ Custom APIs β”‚   β”‚ β€’ Upgrade-safe     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## 🎨 Core Concepts

### RagEntity Trait

Implement this trait on your canister data structures to enable RAG:

```rust
pub trait RagEntity {
    fn entity_type() -> &'static str;           // e.g., "User", "Order"
    fn entity_id(&self) -> String;              // Unique ID
    fn to_context_map(&self) -> Vec<(String, String)>;  // Flatten to key-value
    fn relationships(&self) -> Vec<EntityRelationship>;   // Related entities
}
```

### Context Building

```rust
let builder = ContextBuilder::new(config.chunking);

// Single entity
let context = builder.build_entity_context(&user);

// Entity with relationships
let related = vec![order1_context, order2_context];
let graph_context = builder.build_graph_context(&user, related);

// Chunk long text
let chunks = builder.chunk_text(&graph_context);
```

### Vector Storage

```rust
let mut store = StableMemoryVectorStore::new();

// Store vector
store.store(namespace, vector).await?;

// Search
let results = store.search(namespace, query_embedding, k).await?;

// Manage
store.count(namespace).await?;
store.delete_namespace(namespace).await?;
```

## πŸ”§ Configuration

### Entity Configuration

```json
{
  "name": "User",
  "canister_id": "rrkah-fqaaa-aaaaa-aaaaq-cai",
  "fetch_method": "get_user",
  "fetch_many_method": "list_users",
  "relationships": [
    {
      "field_name": "order_ids",
      "target_entity": "Order",
      "relationship_type": "one_to_many"
    }
  ],
  "auto_include": true
}
```

### Embedder Configuration

**OpenAI:**
```json
{
  "provider": "openai",
  "model": "text-embedding-3-small",
  "dimensions": 1536
}
```

**Gemini:**
```json
{
  "provider": "gemini",
  "model": "embedding-001",
  "dimensions": 768
}
```

### Chunking Configuration

```json
{
  "chunk_size": 1000,        // Characters per chunk
  "overlap": 100,            // Overlap between chunks
  "include_field_names": true  // Include "field: value" format
}
```

## 🌟 Advanced Features

### Cached Embeddings

```rust
use contrag_core::embedders::{CachedEmbedder, Embedder};

let embedder = OpenAIEmbedder::new(api_key, model);
let mut cached = CachedEmbedder::new(embedder, 1000); // Cache 1000 entries

let embeddings = cached.embed_with_cache(texts).await?;
```

### Inter-Canister Data Sources

```rust
use contrag_core::data_sources::canister_state::CanisterStateSource;

let source = CanisterStateSource::new(entity_configs);
let user = source.read_entity::<User>("User", "user_123").await?;
```

### Custom Similarity Metrics

```rust
use contrag_core::vector_store::{cosine_similarity, euclidean_distance};

let similarity = cosine_similarity(&embedding1, &embedding2);
let distance = euclidean_distance(&embedding1, &embedding2);
```

## πŸ“Š Comparison: TypeScript vs Rust

| Feature | TypeScript ContRAG | Rust ContRAG (ICP) |
|---------|-------------------|-------------------|
| **Data Sources** | PostgreSQL, MongoDB | Canister state, Stable memory |
| **Vector Storage** | Weaviate, pgvector | On-chain (stable memory) |
| **Embedders** | OpenAI, Gemini | OpenAI, Gemini (HTTP outcalls) |
| **Configuration** | Config file + .env | Config file + .env |
| **Schema Introspection** | βœ… Automatic | ❌ Manual (trait impl) |
| **Runtime** | Node.js | WASM (ICP canister) |
| **Storage Cost** | External DB fees | ~$5/GB/year on-chain |
| **Query Speed** | Fast (indexed DB) | Fast (in-memory) |
| **Web3 Native** | ❌ | βœ… |

## 🎯 Use Cases

1. **DeFi Personalization**: Build user profiles from transaction history
2. **NFT Recommendations**: Suggest NFTs based on user preferences and activity
3. **DAO Governance**: Context-aware proposal summaries
4. **Web3 Social**: Personalized content feeds from on-chain data
5. **Gaming**: Player profiles and achievement context
6. **Identity Systems**: Rich user context from verifiable credentials

## 🚧 Limitations & Roadmap

### Current Limitations

- ❌ No automatic schema introspection (requires manual `RagEntity` impl)
- ❌ Simple cosine similarity (no advanced indexing like HNSW)
- ❌ Single-canister vector storage (no distributed sharding yet)
- ⚠️ HTTP outcall costs (cycles consumed per API call)

### Roadmap

- [ ] Derive macro for `RagEntity` (auto-implementation)
- [ ] HNSW indexing for faster similarity search
- [ ] Multi-canister vector sharding
- [ ] IPFS/Arweave vector storage adapter
- [ ] Preference tracking (port from TypeScript)
- [ ] Cross-chain data sources (Ethereum, Bitcoin)
- [ ] Built-in embedding caching in stable memory

## πŸ“š Examples

See `/examples/user-canister` for a complete working example with:

- User and Order entities
- Relationship mapping
- Context building
- Vector storage and search
- Demo data seeding

## 🀝 Contributing

Contributions welcome! This is an experimental project bringing RAG to Web3.

## πŸ“„ License

MIT License - see LICENSE file

## πŸ™ Acknowledgments

- Based on [ContRAG TypeScript]../README.md
- Built for the Internet Computer Protocol
- Inspired by the vision of AI-powered Web3 applications

---

**Ready to unlock RAG in Web3?** πŸš€

Start building intelligent, context-aware canisters today!