2025-05-23 10:55:20 -07:00
|
|
|
# Soulcraft Brainy
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
A combined Graph and Vector database that runs in browsers and Node.js environments. Brainy provides efficient vector
|
|
|
|
|
search capabilities with persistent storage options.
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
## What is Brainy?
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
Brainy is a lightweight database that combines:
|
|
|
|
|
|
|
|
|
|
- Vector search (for similarity-based retrieval)
|
|
|
|
|
- Graph relationships (for structured data connections)
|
|
|
|
|
- Persistent storage (works across sessions)
|
|
|
|
|
|
|
|
|
|
It's designed to work seamlessly in both browser and server environments.
|
|
|
|
|
|
|
|
|
|
## How It Works
|
|
|
|
|
|
|
|
|
|
Brainy combines three key technologies:
|
|
|
|
|
|
|
|
|
|
1. **Vector Embeddings**: Converts data (text, images, etc.) into numerical vectors that capture semantic meaning
|
|
|
|
|
2. **HNSW Algorithm**: Enables fast similarity search through a hierarchical graph structure
|
|
|
|
|
3. **Persistent Storage**: Uses the best available storage option for your environment:
|
|
|
|
|
- Browser: Origin Private File System (OPFS)
|
|
|
|
|
- Node.js: File system
|
|
|
|
|
- Server: S3-compatible storage (optional)
|
|
|
|
|
- Fallback: In-memory storage
|
|
|
|
|
|
|
|
|
|
## What Can You Use It For?
|
|
|
|
|
|
|
|
|
|
- **Semantic Search**: Find content based on meaning, not just keywords
|
|
|
|
|
- **Recommendation Systems**: Suggest similar items based on vector similarity
|
|
|
|
|
- **Knowledge Graphs**: Build connected data structures with relationships
|
|
|
|
|
- **Data Organization**: Automatically categorize and connect related information
|
|
|
|
|
- **AI Applications**: Store and retrieve embeddings for machine learning models
|
2025-05-23 10:55:20 -07:00
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
|
|
```bash
|
2025-05-23 11:08:02 -07:00
|
|
|
npm install @soulcraft/brainy
|
2025-05-23 10:55:20 -07:00
|
|
|
```
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
## Basic Usage
|
2025-05-23 10:55:20 -07:00
|
|
|
|
|
|
|
|
```typescript
|
2025-05-23 11:08:02 -07:00
|
|
|
import {BrainyData} from '@soulcraft/brainy';
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Create and initialize the database
|
2025-05-23 10:55:20 -07:00
|
|
|
const db = new BrainyData();
|
|
|
|
|
await db.init();
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Add text data (automatically converted to vectors)
|
|
|
|
|
const catId = await db.add("Cats are independent pets", {type: 'animal'});
|
|
|
|
|
const dogId = await db.add("Dogs are loyal companions", {type: 'animal'});
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Search for similar content
|
|
|
|
|
const results = await db.searchText("feline pets", 2);
|
2025-05-23 10:55:20 -07:00
|
|
|
console.log(results);
|
2025-06-05 08:26:12 -07:00
|
|
|
// Returns items similar to "feline pets" with their similarity scores
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Add relationships between items
|
|
|
|
|
await db.addEdge(catId, dogId, {type: 'related_to'});
|
2025-05-27 13:58:49 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Retrieve data
|
2025-05-23 10:55:20 -07:00
|
|
|
const cat = await db.get(catId);
|
|
|
|
|
console.log(cat);
|
|
|
|
|
|
|
|
|
|
// Update metadata
|
2025-06-05 08:26:12 -07:00
|
|
|
await db.updateMetadata(catId, {type: 'animal', size: 'small'});
|
2025-06-04 10:00:00 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Delete data
|
|
|
|
|
await db.delete(dogId);
|
2025-05-23 10:55:20 -07:00
|
|
|
```
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
## Key Features
|
2025-05-27 13:58:49 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
- **Automatic Vectorization**: Converts text and data to vector embeddings
|
|
|
|
|
- **Fast Similarity Search**: Uses HNSW algorithm for efficient retrieval
|
|
|
|
|
- **Cross-Platform**: Works in browsers, Node.js, and server environments
|
|
|
|
|
- **Persistent Storage**: Multiple storage options (OPFS, filesystem, cloud)
|
|
|
|
|
- **Graph Capabilities**: Create relationships between data points
|
|
|
|
|
- **TypeScript Support**: Fully typed API with generics
|
|
|
|
|
- **Flexible Configuration**: Customize distance functions, embedding models, and more
|
2025-05-27 15:11:12 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
## Advanced Usage
|
2025-05-27 15:11:12 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
### Custom Embedding
|
2025-05-27 15:11:12 -07:00
|
|
|
|
|
|
|
|
```typescript
|
2025-05-29 10:29:52 -07:00
|
|
|
import {BrainyData, createSimpleEmbeddingFunction} from '@soulcraft/brainy';
|
2025-05-27 15:11:12 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Use a custom embedding function (faster but less accurate)
|
2025-05-27 15:11:12 -07:00
|
|
|
const db = new BrainyData({
|
2025-05-29 10:29:52 -07:00
|
|
|
embeddingFunction: createSimpleEmbeddingFunction()
|
2025-05-27 15:11:12 -07:00
|
|
|
});
|
|
|
|
|
await db.init();
|
2025-06-05 08:26:12 -07:00
|
|
|
|
|
|
|
|
// Directly embed text to vectors
|
|
|
|
|
const vector = await db.embed("Some text to convert to a vector");
|
2025-05-27 15:11:12 -07:00
|
|
|
```
|
|
|
|
|
|
2025-05-23 10:55:20 -07:00
|
|
|
### Configuration Options
|
|
|
|
|
|
|
|
|
|
```typescript
|
2025-06-05 08:26:12 -07:00
|
|
|
import {BrainyData, euclideanDistance} from '@soulcraft/brainy';
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Configure with custom options
|
2025-05-23 10:55:20 -07:00
|
|
|
const db = new BrainyData({
|
2025-06-05 08:26:12 -07:00
|
|
|
// Use Euclidean distance instead of default cosine distance
|
2025-05-23 10:55:20 -07:00
|
|
|
distanceFunction: euclideanDistance,
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// HNSW index configuration for search performance
|
|
|
|
|
hnsw: {
|
|
|
|
|
M: 16, // Max connections per node
|
|
|
|
|
efConstruction: 200, // Construction candidate list size
|
|
|
|
|
efSearch: 50, // Search candidate list size
|
2025-05-29 10:29:52 -07:00
|
|
|
},
|
2025-05-28 10:37:55 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Storage configuration
|
|
|
|
|
storage: {
|
|
|
|
|
requestPersistentStorage: true,
|
|
|
|
|
// Uncomment to use cloud storage:
|
|
|
|
|
// s3Storage: {
|
|
|
|
|
// bucketName: 'your-bucket',
|
|
|
|
|
// accessKeyId: 'your-key',
|
|
|
|
|
// secretAccessKey: 'your-secret',
|
|
|
|
|
// region: 'us-east-1'
|
|
|
|
|
// }
|
2025-05-29 10:29:52 -07:00
|
|
|
}
|
2025-06-04 10:00:00 -07:00
|
|
|
});
|
2025-05-29 10:29:52 -07:00
|
|
|
```
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
## API Reference
|
2025-05-29 10:29:52 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
### Core Methods
|
2025-05-23 10:55:20 -07:00
|
|
|
|
|
|
|
|
```typescript
|
2025-06-05 08:26:12 -07:00
|
|
|
// Initialize the database
|
|
|
|
|
await db.init();
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Add data (automatically vectorized)
|
|
|
|
|
const id = await db.add(textOrVector, metadata);
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Search by vector or text
|
|
|
|
|
const results = await db.search(vectorOrText, numResults);
|
|
|
|
|
const textResults = await db.searchText("query text", numResults);
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Manage data
|
|
|
|
|
const item = await db.get(id);
|
|
|
|
|
await db.updateMetadata(id, newMetadata);
|
|
|
|
|
await db.delete(id);
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Graph relationships
|
|
|
|
|
await db.addEdge(sourceId, targetId, metadata);
|
|
|
|
|
const edges = await db.getAllEdges();
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
// Database management
|
|
|
|
|
await db.clear();
|
|
|
|
|
const size = db.size();
|
|
|
|
|
const status = await db.status();
|
2025-05-23 10:55:20 -07:00
|
|
|
```
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
### Distance Functions
|
2025-06-02 12:23:11 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
- `cosineDistance` (default)
|
|
|
|
|
- `euclideanDistance`
|
|
|
|
|
- `manhattanDistance`
|
|
|
|
|
- `dotProductDistance`
|
2025-06-02 12:23:11 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
### Embedding Options
|
2025-06-02 12:23:11 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
- Default: TensorFlow Universal Sentence Encoder (high quality)
|
|
|
|
|
- Alternative: Simple character-based embedding (faster)
|
2025-06-02 12:23:11 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
## Extensions
|
2025-06-02 12:23:11 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
Brainy includes an augmentation system for extending functionality:
|
2025-06-02 12:23:11 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
- **Memory Augmentations**: Different storage backends
|
|
|
|
|
- **Sense Augmentations**: Process raw data
|
|
|
|
|
- **Cognition Augmentations**: Reasoning and inference
|
|
|
|
|
- **Dialog Augmentations**: Natural language processing
|
|
|
|
|
- **Perception Augmentations**: Data interpretation and/or visualization
|
|
|
|
|
- **Activation Augmentations**: Trigger actions
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
For detailed documentation on extensions, see the [API docs](https://github.com/soulcraft-labs/brainy/docs).
|
2025-05-23 10:55:20 -07:00
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
The repository includes several examples:
|
2025-05-23 10:55:20 -07:00
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
- Web demo: `examples/demo.html`
|
|
|
|
|
- Basic usage: `examples/basicUsage.js`
|
|
|
|
|
- Custom storage: `examples/customStorage.js`
|
|
|
|
|
- Memory augmentations: `examples/memoryAugmentationExample.js`
|
2025-05-23 10:55:20 -07:00
|
|
|
|
|
|
|
|
## Browser Compatibility
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
Works in all modern browsers:
|
2025-05-23 10:55:20 -07:00
|
|
|
|
|
|
|
|
- Chrome 86+
|
|
|
|
|
- Edge 86+
|
|
|
|
|
- Opera 72+
|
|
|
|
|
- Chrome for Android 86+
|
|
|
|
|
|
2025-06-05 08:26:12 -07:00
|
|
|
For browsers without OPFS support, falls back to in-memory storage.
|
|
|
|
|
|
|
|
|
|
## Requirements
|
|
|
|
|
|
|
|
|
|
- Node.js >= 18.0.0
|
2025-05-23 10:55:20 -07:00
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
MIT
|