1240 lines
37 KiB
Markdown
1240 lines
37 KiB
Markdown
|
|
# 🎓 Brainy Developer Learning Path
|
||
|
|
**From Zero to Hero in 5 Progressive Levels**
|
||
|
|
|
||
|
|
> This guide takes you from your first Brainy query to production-scale neural database mastery. Follow each level in order for the best learning experience.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 📋 Quick Navigation
|
||
|
|
|
||
|
|
- [Level 1: Hello Brainy](#level-1-hello-brainy-15-minutes) - Your first neural database
|
||
|
|
- [Level 2: Relationships & Batch Operations](#level-2-relationships--batch-operations-30-minutes) - Scale up your data
|
||
|
|
- [Level 3: Advanced Search & Neural AI](#level-3-advanced-search--neural-ai-45-minutes) - Triple Intelligence
|
||
|
|
- [Level 4: Virtual Filesystem](#level-4-virtual-filesystem-60-minutes) - Files as intelligent entities
|
||
|
|
- [Level 5: Production Scale](#level-5-production-scale-90-minutes) - Planet-scale deployment
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Level 1: Hello Brainy (15 minutes)
|
||
|
|
|
||
|
|
### What You'll Learn
|
||
|
|
- Initialize Brainy
|
||
|
|
- Add your first entity
|
||
|
|
- Perform semantic search
|
||
|
|
- Understand basic types
|
||
|
|
|
||
|
|
### Prerequisites
|
||
|
|
```bash
|
||
|
|
npm install @soulcraft/brainy
|
||
|
|
```
|
||
|
|
|
||
|
|
### Your First Neural Database
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
import { Brainy, NounType } from '@soulcraft/brainy'
|
||
|
|
|
||
|
|
// Step 1: Create and initialize Brainy
|
||
|
|
const brain = new Brainy({
|
||
|
|
storage: { type: 'memory' } // Start simple - no persistence needed
|
||
|
|
})
|
||
|
|
await brain.init()
|
||
|
|
|
||
|
|
// Step 2: Add some data
|
||
|
|
const johnId = await brain.add({
|
||
|
|
data: 'John Smith is a software engineer at TechCorp',
|
||
|
|
type: NounType.Person,
|
||
|
|
metadata: { role: 'Engineer', company: 'TechCorp' }
|
||
|
|
})
|
||
|
|
|
||
|
|
const aliceId = await brain.add({
|
||
|
|
data: 'Alice Johnson is a product manager at TechCorp',
|
||
|
|
type: NounType.Person,
|
||
|
|
metadata: { role: 'Manager', company: 'TechCorp' }
|
||
|
|
})
|
||
|
|
|
||
|
|
const projectId = await brain.add({
|
||
|
|
data: 'AI-powered customer support system using machine learning',
|
||
|
|
type: NounType.Project,
|
||
|
|
metadata: { status: 'active', priority: 'high' }
|
||
|
|
})
|
||
|
|
|
||
|
|
// Step 3: Semantic search (this is where magic happens!)
|
||
|
|
console.log('\n🔍 Searching for "engineers"...')
|
||
|
|
const engineers = await brain.find({ query: 'engineers' })
|
||
|
|
console.log(`Found ${engineers.length} engineers:`)
|
||
|
|
for (const result of engineers) {
|
||
|
|
console.log(` - ${result.entity.data} (score: ${result.score.toFixed(2)})`)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Step 4: Search with filters
|
||
|
|
console.log('\n🔍 Searching for "people at TechCorp"...')
|
||
|
|
const techcorpPeople = await brain.find({
|
||
|
|
query: 'people',
|
||
|
|
type: NounType.Person,
|
||
|
|
where: { company: 'TechCorp' },
|
||
|
|
limit: 10
|
||
|
|
})
|
||
|
|
console.log(`Found ${techcorpPeople.length} people at TechCorp`)
|
||
|
|
|
||
|
|
// Step 5: Get entity by ID
|
||
|
|
const john = await brain.get(johnId)
|
||
|
|
console.log('\n👤 John\'s data:', {
|
||
|
|
type: john?.type,
|
||
|
|
data: john?.data,
|
||
|
|
metadata: john?.metadata
|
||
|
|
})
|
||
|
|
|
||
|
|
// Step 6: Clean up
|
||
|
|
await brain.close()
|
||
|
|
console.log('\n✅ Done! You just created your first neural database!')
|
||
|
|
```
|
||
|
|
|
||
|
|
### Key Concepts
|
||
|
|
|
||
|
|
#### 1. **NounType** - Entity Classification
|
||
|
|
Brainy has 31 built-in types including:
|
||
|
|
- `Person`, `Organization`, `Location`
|
||
|
|
- `Document`, `File`, `Content`
|
||
|
|
- `Product`, `Service`, `Event`
|
||
|
|
- `Project`, `Task`, `Concept`
|
||
|
|
|
||
|
|
**Why it matters**: Proper typing enables intelligent search and organization.
|
||
|
|
|
||
|
|
#### 2. **Semantic Search** - Understanding Meaning
|
||
|
|
```typescript
|
||
|
|
// Traditional search: exact keyword matching
|
||
|
|
// "engineers" would NOT find "software developer"
|
||
|
|
|
||
|
|
// Semantic search: understands meaning
|
||
|
|
await brain.find({ query: 'engineers' })
|
||
|
|
// ✅ Finds: "software engineer", "developer", "programmer", "coder"
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 3. **Metadata Filtering** - Precise Control
|
||
|
|
```typescript
|
||
|
|
// Combine semantic search with structured filters
|
||
|
|
await brain.find({
|
||
|
|
query: 'machine learning', // Semantic: finds AI, ML, neural networks
|
||
|
|
where: { company: 'TechCorp' }, // Structured: exact match
|
||
|
|
type: NounType.Project // Type filter
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
### Practice Exercises
|
||
|
|
|
||
|
|
1. Create a small company directory with 5-10 people
|
||
|
|
2. Search for "managers", "developers", "designers"
|
||
|
|
3. Add projects and search for "active projects"
|
||
|
|
4. Experiment with different metadata filters
|
||
|
|
|
||
|
|
### Next Steps
|
||
|
|
Once you're comfortable with basic operations, move to **Level 2** to learn about relationships and batch operations.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Level 2: Relationships & Batch Operations (30 minutes)
|
||
|
|
|
||
|
|
### What You'll Learn
|
||
|
|
- Create relationships between entities
|
||
|
|
- Batch add/update/delete operations
|
||
|
|
- Query graph relationships
|
||
|
|
- Understand VerbTypes
|
||
|
|
|
||
|
|
### Building a Knowledge Graph
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
|
||
|
|
|
||
|
|
const brain = new Brainy({ storage: { type: 'memory' } })
|
||
|
|
await brain.init()
|
||
|
|
|
||
|
|
// Batch add multiple entities
|
||
|
|
console.log('📦 Adding team members...')
|
||
|
|
const result = await brain.addMany({
|
||
|
|
items: [
|
||
|
|
{ data: 'John Smith - Senior Engineer', type: NounType.Person, metadata: { role: 'Engineer' } },
|
||
|
|
{ data: 'Alice Johnson - Product Manager', type: NounType.Person, metadata: { role: 'Manager' } },
|
||
|
|
{ data: 'Bob Wilson - Designer', type: NounType.Person, metadata: { role: 'Designer' } },
|
||
|
|
{ data: 'TechCorp - Software Company', type: NounType.Organization },
|
||
|
|
{ data: 'AI Assistant Project', type: NounType.Project, metadata: { status: 'active' } }
|
||
|
|
],
|
||
|
|
parallel: true,
|
||
|
|
onProgress: (done, total) => console.log(` Progress: ${done}/${total}`)
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`✅ Added ${result.successful.length} entities`)
|
||
|
|
const [johnId, aliceId, bobId, techcorpId, projectId] = result.successful
|
||
|
|
|
||
|
|
// Create relationships (building the graph!)
|
||
|
|
console.log('\n🔗 Creating relationships...')
|
||
|
|
await brain.relateMany({
|
||
|
|
relations: [
|
||
|
|
// People work for organization
|
||
|
|
{ from: johnId, to: techcorpId, type: VerbType.WorksWith },
|
||
|
|
{ from: aliceId, to: techcorpId, type: VerbType.WorksWith },
|
||
|
|
{ from: bobId, to: techcorpId, type: VerbType.WorksWith },
|
||
|
|
|
||
|
|
// People work on project
|
||
|
|
{ from: johnId, to: projectId, type: VerbType.WorksOn },
|
||
|
|
{ from: bobId, to: projectId, type: VerbType.WorksOn },
|
||
|
|
|
||
|
|
// Alice manages the project
|
||
|
|
{ from: aliceId, to: projectId, type: VerbType.Manages },
|
||
|
|
|
||
|
|
// Team collaboration
|
||
|
|
{ from: johnId, to: aliceId, type: VerbType.CollaboratesWith, bidirectional: true },
|
||
|
|
{ from: bobId, to: johnId, type: VerbType.CollaboratesWith, bidirectional: true }
|
||
|
|
]
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log('✅ Created relationships')
|
||
|
|
|
||
|
|
// Query relationships
|
||
|
|
console.log('\n🔍 Querying relationships...')
|
||
|
|
|
||
|
|
// Who works for TechCorp?
|
||
|
|
const techcorpEmployees = await brain.getRelations({
|
||
|
|
to: techcorpId,
|
||
|
|
type: VerbType.WorksWith
|
||
|
|
})
|
||
|
|
console.log(`TechCorp has ${techcorpEmployees.length} employees`)
|
||
|
|
|
||
|
|
// Who works on the AI project?
|
||
|
|
const projectContributors = await brain.getRelations({
|
||
|
|
to: projectId,
|
||
|
|
type: [VerbType.WorksOn, VerbType.Manages]
|
||
|
|
})
|
||
|
|
console.log(`AI Project has ${projectContributors.length} contributors`)
|
||
|
|
|
||
|
|
// Who does John collaborate with?
|
||
|
|
const johnsCollaborators = await brain.getRelations({
|
||
|
|
from: johnId,
|
||
|
|
type: VerbType.CollaboratesWith
|
||
|
|
})
|
||
|
|
console.log(`John collaborates with ${johnsCollaborators.length} people`)
|
||
|
|
|
||
|
|
// Get graph statistics
|
||
|
|
const stats = brain.getStats()
|
||
|
|
console.log('\n📊 Graph Statistics:', {
|
||
|
|
entities: stats.entities.total,
|
||
|
|
relationships: stats.relationships.totalRelationships,
|
||
|
|
density: stats.density.toFixed(2)
|
||
|
|
})
|
||
|
|
|
||
|
|
// Batch update
|
||
|
|
console.log('\n📝 Updating all team members...')
|
||
|
|
await brain.updateMany({
|
||
|
|
items: [johnId, aliceId, bobId].map(id => ({
|
||
|
|
id,
|
||
|
|
metadata: { team: 'AI Team', updated: new Date().toISOString() },
|
||
|
|
merge: true // Merge with existing metadata (don't replace!)
|
||
|
|
}))
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log('✅ Updated team metadata')
|
||
|
|
|
||
|
|
await brain.close()
|
||
|
|
```
|
||
|
|
|
||
|
|
### Key Concepts
|
||
|
|
|
||
|
|
#### 1. **VerbType** - Relationship Types
|
||
|
|
Brainy has 40 relationship types including:
|
||
|
|
- Work: `WorksWith`, `WorksOn`, `Manages`, `Supervises`
|
||
|
|
- Structure: `PartOf`, `Contains`, `BelongsTo`
|
||
|
|
- Knowledge: `RelatedTo`, `DependsOn`, `Requires`
|
||
|
|
- Creation: `Creates`, `Modifies`, `Transforms`
|
||
|
|
|
||
|
|
#### 2. **Bidirectional Relationships**
|
||
|
|
```typescript
|
||
|
|
await brain.relate({
|
||
|
|
from: personA,
|
||
|
|
to: personB,
|
||
|
|
type: VerbType.CollaboratesWith,
|
||
|
|
bidirectional: true // Creates A→B AND B→A
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 3. **Batch Operations = Performance**
|
||
|
|
```typescript
|
||
|
|
// ❌ Slow: 100 individual operations
|
||
|
|
for (const item of items) {
|
||
|
|
await brain.add(item) // 100 round trips!
|
||
|
|
}
|
||
|
|
|
||
|
|
// ✅ Fast: 1 batch operation
|
||
|
|
await brain.addMany({ items }) // 1 round trip!
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 4. **Metadata Merging**
|
||
|
|
```typescript
|
||
|
|
// Initial metadata
|
||
|
|
await brain.add({
|
||
|
|
data: 'John',
|
||
|
|
metadata: { role: 'Engineer', level: 3 }
|
||
|
|
})
|
||
|
|
|
||
|
|
// Update with merge: true (default)
|
||
|
|
await brain.update({
|
||
|
|
id: johnId,
|
||
|
|
metadata: { team: 'AI Team' },
|
||
|
|
merge: true // Result: { role: 'Engineer', level: 3, team: 'AI Team' }
|
||
|
|
})
|
||
|
|
|
||
|
|
// Update with merge: false
|
||
|
|
await brain.update({
|
||
|
|
id: johnId,
|
||
|
|
metadata: { team: 'AI Team' },
|
||
|
|
merge: false // Result: { team: 'AI Team' } - role and level lost!
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
### Practice Exercises
|
||
|
|
|
||
|
|
1. Create an organizational hierarchy (CEO → Managers → Engineers)
|
||
|
|
2. Build a project dependency graph
|
||
|
|
3. Model a social network with CollaboratesWith relationships
|
||
|
|
4. Query "Who reports to Alice?" using getRelations()
|
||
|
|
5. Batch update all projects to add a "year: 2024" field
|
||
|
|
|
||
|
|
### Next Steps
|
||
|
|
Ready for AI-powered search and clustering? Move to **Level 3**.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Level 3: Advanced Search & Neural AI (45 minutes)
|
||
|
|
|
||
|
|
### What You'll Learn
|
||
|
|
- Triple Intelligence (Vector + Metadata + Graph)
|
||
|
|
- Semantic similarity
|
||
|
|
- Automatic clustering
|
||
|
|
- Outlier detection
|
||
|
|
- Natural language queries
|
||
|
|
|
||
|
|
### Triple Intelligence in Action
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
|
||
|
|
|
||
|
|
const brain = new Brainy({ storage: { type: 'memory' } })
|
||
|
|
await brain.init()
|
||
|
|
|
||
|
|
// Create a realistic dataset
|
||
|
|
console.log('📦 Creating knowledge base...')
|
||
|
|
const knowledgeBase = await brain.addMany({
|
||
|
|
items: [
|
||
|
|
// Research papers
|
||
|
|
{ data: 'Deep Learning for Computer Vision using Convolutional Neural Networks',
|
||
|
|
type: NounType.Document,
|
||
|
|
metadata: { category: 'AI', year: 2024, citations: 150 } },
|
||
|
|
{ data: 'Natural Language Processing with Transformer Models',
|
||
|
|
type: NounType.Document,
|
||
|
|
metadata: { category: 'AI', year: 2024, citations: 200 } },
|
||
|
|
{ data: 'Reinforcement Learning for Robotics Applications',
|
||
|
|
type: NounType.Document,
|
||
|
|
metadata: { category: 'AI', year: 2023, citations: 80 } },
|
||
|
|
|
||
|
|
// Different domain
|
||
|
|
{ data: 'Climate Change Impact on Ocean Ecosystems',
|
||
|
|
type: NounType.Document,
|
||
|
|
metadata: { category: 'Climate', year: 2024, citations: 120 } },
|
||
|
|
{ data: 'Renewable Energy Solutions for Urban Planning',
|
||
|
|
type: NounType.Document,
|
||
|
|
metadata: { category: 'Energy', year: 2024, citations: 95 } },
|
||
|
|
|
||
|
|
// Code projects
|
||
|
|
{ data: 'AI-powered code completion tool using GPT',
|
||
|
|
type: NounType.Project,
|
||
|
|
metadata: { category: 'Tools', status: 'active' } },
|
||
|
|
{ data: 'Neural network visualization dashboard',
|
||
|
|
type: NounType.Project,
|
||
|
|
metadata: { category: 'Tools', status: 'active' } }
|
||
|
|
]
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`✅ Created ${knowledgeBase.successful.length} entities\n`)
|
||
|
|
|
||
|
|
// 1. VECTOR INTELLIGENCE: Semantic similarity
|
||
|
|
console.log('🔍 1. VECTOR INTELLIGENCE: Semantic Search')
|
||
|
|
const aiResults = await brain.find({
|
||
|
|
query: 'machine learning and neural networks', // User's natural language
|
||
|
|
limit: 3
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log('Top 3 semantically similar documents:')
|
||
|
|
aiResults.forEach((r, i) => {
|
||
|
|
console.log(` ${i + 1}. [${r.score.toFixed(3)}] ${r.entity.data?.substring(0, 50)}...`)
|
||
|
|
})
|
||
|
|
|
||
|
|
// 2. METADATA INTELLIGENCE: Structured filtering
|
||
|
|
console.log('\n🔍 2. METADATA INTELLIGENCE: Precise Filtering')
|
||
|
|
const recentHighCitations = await brain.find({
|
||
|
|
query: 'artificial intelligence',
|
||
|
|
where: {
|
||
|
|
year: 2024,
|
||
|
|
citations: { $gte: 100 } // Brainy Field Operator: greater than or equal
|
||
|
|
},
|
||
|
|
limit: 10
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`Found ${recentHighCitations.length} highly-cited AI papers from 2024`)
|
||
|
|
|
||
|
|
// 3. GRAPH INTELLIGENCE: Relationship-aware search
|
||
|
|
console.log('\n🔍 3. GRAPH INTELLIGENCE: Relationship-Aware Search')
|
||
|
|
|
||
|
|
// First, create some relationships
|
||
|
|
const [paper1, paper2] = knowledgeBase.successful
|
||
|
|
await brain.relate({
|
||
|
|
from: paper1,
|
||
|
|
to: paper2,
|
||
|
|
type: VerbType.References
|
||
|
|
})
|
||
|
|
|
||
|
|
// Search with graph constraints
|
||
|
|
const connectedDocs = await brain.find({
|
||
|
|
query: 'deep learning',
|
||
|
|
connected: {
|
||
|
|
to: paper2,
|
||
|
|
via: VerbType.References
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`Found ${connectedDocs.length} papers that reference the NLP paper`)
|
||
|
|
|
||
|
|
// 4. FUSION: Combine all three intelligences!
|
||
|
|
console.log('\n🔍 4. TRIPLE INTELLIGENCE FUSION')
|
||
|
|
const fusionResults = await brain.find({
|
||
|
|
query: 'AI research', // Vector: semantic understanding
|
||
|
|
where: { year: 2024 }, // Metadata: structured filter
|
||
|
|
type: NounType.Document, // Type constraint
|
||
|
|
fusion: {
|
||
|
|
strategy: 'adaptive', // Let Brainy optimize weights
|
||
|
|
weights: {
|
||
|
|
vector: 0.5, // 50% semantic similarity
|
||
|
|
field: 0.3, // 30% metadata match
|
||
|
|
graph: 0.2 // 20% relationship strength
|
||
|
|
}
|
||
|
|
},
|
||
|
|
explain: true // See how the score was calculated
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log('Fusion search results with score explanations:')
|
||
|
|
fusionResults.forEach(r => {
|
||
|
|
console.log(`\n ${r.entity.data?.substring(0, 60)}...`)
|
||
|
|
console.log(` Total score: ${r.score.toFixed(3)}`)
|
||
|
|
if (r.explanation) {
|
||
|
|
console.log(` Vector: ${r.explanation.vector.toFixed(3)}`)
|
||
|
|
console.log(` Metadata: ${r.explanation.metadata.toFixed(3)}`)
|
||
|
|
console.log(` Graph: ${r.explanation.graph.toFixed(3)}`)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
// 5. NEURAL API: Automatic clustering
|
||
|
|
console.log('\n\n🤖 NEURAL API: Automatic Clustering')
|
||
|
|
const neural = brain.neural()
|
||
|
|
|
||
|
|
const clusters = await neural.clusters({
|
||
|
|
maxClusters: 3,
|
||
|
|
minClusterSize: 1
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`Found ${clusters.length} semantic clusters:`)
|
||
|
|
clusters.forEach((cluster, i) => {
|
||
|
|
console.log(`\n Cluster ${i + 1}: ${cluster.label || cluster.id}`)
|
||
|
|
console.log(` Members: ${cluster.members.length}`)
|
||
|
|
console.log(` Centroid topics: ${cluster.metadata?.topics?.join(', ') || 'N/A'}`)
|
||
|
|
})
|
||
|
|
|
||
|
|
// 6. SIMILARITY: Find similar documents
|
||
|
|
console.log('\n\n🔍 SIMILARITY: Find Similar Documents')
|
||
|
|
const similarTo = await brain.similar({
|
||
|
|
to: paper1, // Entity ID of first AI paper
|
||
|
|
limit: 3,
|
||
|
|
threshold: 0.5, // Minimum similarity score
|
||
|
|
type: NounType.Document
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`Documents similar to "${knowledgeBase.successful[0]}":`)
|
||
|
|
similarTo.forEach(r => {
|
||
|
|
console.log(` [${r.score.toFixed(3)}] ${r.entity.data?.substring(0, 50)}...`)
|
||
|
|
})
|
||
|
|
|
||
|
|
// 7. OUTLIER DETECTION
|
||
|
|
console.log('\n\n🚨 OUTLIER DETECTION')
|
||
|
|
const outliers = await neural.outliers({
|
||
|
|
method: 'statistical',
|
||
|
|
threshold: 2.0 // 2 standard deviations
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`Found ${outliers.length} outlier documents:`)
|
||
|
|
outliers.forEach(o => {
|
||
|
|
const entity = await brain.get(o.id)
|
||
|
|
console.log(` [Anomaly score: ${o.score.toFixed(3)}] ${entity?.data?.substring(0, 50)}...`)
|
||
|
|
})
|
||
|
|
|
||
|
|
await brain.close()
|
||
|
|
```
|
||
|
|
|
||
|
|
### Key Concepts
|
||
|
|
|
||
|
|
#### 1. **Triple Intelligence Explained**
|
||
|
|
|
||
|
|
```
|
||
|
|
Traditional Database: WHERE category = 'AI' (exact match only)
|
||
|
|
❌ Misses: "artificial intelligence", "machine learning"
|
||
|
|
|
||
|
|
Vector Search: semantic("AI research") (meaning-based)
|
||
|
|
✅ Finds: AI, ML, neural networks, deep learning
|
||
|
|
❌ No filtering by year, citations, etc.
|
||
|
|
|
||
|
|
Brainy Triple: semantic("AI") + WHERE year=2024 + CONNECTED TO paper123
|
||
|
|
✅ Finds semantically similar + filters + graph aware
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 2. **Score Explanations**
|
||
|
|
```typescript
|
||
|
|
const results = await brain.find({
|
||
|
|
query: 'AI',
|
||
|
|
explain: true // Get score breakdown
|
||
|
|
})
|
||
|
|
|
||
|
|
// result.explanation shows:
|
||
|
|
// {
|
||
|
|
// vector: 0.85, // 85% semantic match
|
||
|
|
// metadata: 0.90, // 90% field match
|
||
|
|
// graph: 0.70, // 70% graph relevance
|
||
|
|
// final: 0.82 // Weighted combination
|
||
|
|
// }
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 3. **Fusion Strategies**
|
||
|
|
```typescript
|
||
|
|
// 'adaptive' - Brainy automatically adjusts weights based on query
|
||
|
|
fusion: { strategy: 'adaptive' }
|
||
|
|
|
||
|
|
// 'balanced' - Equal weights to all signals
|
||
|
|
fusion: { strategy: 'balanced' }
|
||
|
|
|
||
|
|
// 'custom' - You control the weights
|
||
|
|
fusion: {
|
||
|
|
strategy: 'custom',
|
||
|
|
weights: { vector: 0.7, field: 0.2, graph: 0.1 }
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 4. **Brainy Field Operators (BFO)**
|
||
|
|
```typescript
|
||
|
|
where: {
|
||
|
|
age: { $gte: 18, $lte: 65 }, // Range
|
||
|
|
role: { $in: ['Engineer', 'Manager'] }, // One of
|
||
|
|
name: { $contains: 'John' }, // Substring
|
||
|
|
active: true, // Exact match
|
||
|
|
tags: { $includes: 'AI' } // Array contains
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
### Practice Exercises
|
||
|
|
|
||
|
|
1. Create a document collection and find semantically similar items
|
||
|
|
2. Use fusion search with custom weights
|
||
|
|
3. Cluster your data and examine the clusters
|
||
|
|
4. Find outliers in a dataset
|
||
|
|
5. Compare results with/without explain: true
|
||
|
|
|
||
|
|
### Next Steps
|
||
|
|
Want to treat files as intelligent entities? Learn the **Virtual Filesystem** in Level 4.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Level 4: Virtual Filesystem (60 minutes)
|
||
|
|
|
||
|
|
### What You'll Learn
|
||
|
|
- VFS as knowledge operating system
|
||
|
|
- Files with semantic understanding
|
||
|
|
- Semantic file search
|
||
|
|
- Cross-boundary relationships (VFS ↔ Knowledge)
|
||
|
|
- VFS filtering architecture
|
||
|
|
|
||
|
|
### Files as Intelligent Entities
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
|
||
|
|
|
||
|
|
const brain = new Brainy({ storage: { type: 'memory' } })
|
||
|
|
await brain.init()
|
||
|
|
|
||
|
|
// Initialize VFS
|
||
|
|
const vfs = brain.vfs()
|
||
|
|
await vfs.init()
|
||
|
|
|
||
|
|
console.log('📁 Creating semantic filesystem...\n')
|
||
|
|
|
||
|
|
// 1. BASIC FILE OPERATIONS (POSIX-like)
|
||
|
|
await vfs.mkdir('/projects', { recursive: true })
|
||
|
|
await vfs.mkdir('/projects/ai-assistant')
|
||
|
|
await vfs.mkdir('/docs')
|
||
|
|
|
||
|
|
await vfs.writeFile('/projects/ai-assistant/README.md', `
|
||
|
|
# AI Assistant Project
|
||
|
|
|
||
|
|
A neural-powered assistant using transformer models for natural language understanding.
|
||
|
|
|
||
|
|
## Features
|
||
|
|
- Semantic search
|
||
|
|
- Context-aware responses
|
||
|
|
- Multi-turn conversations
|
||
|
|
`)
|
||
|
|
|
||
|
|
await vfs.writeFile('/projects/ai-assistant/architecture.md', `
|
||
|
|
# Architecture
|
||
|
|
|
||
|
|
## Components
|
||
|
|
- NLP Engine: Transformer-based language model
|
||
|
|
- Knowledge Graph: Brainy neural database
|
||
|
|
- API Layer: RESTful endpoints
|
||
|
|
`)
|
||
|
|
|
||
|
|
await vfs.writeFile('/docs/installation.md', `
|
||
|
|
# Installation Guide
|
||
|
|
|
||
|
|
\`\`\`bash
|
||
|
|
npm install ai-assistant
|
||
|
|
\`\`\`
|
||
|
|
`)
|
||
|
|
|
||
|
|
console.log('✅ Created 3 files\n')
|
||
|
|
|
||
|
|
// 2. VFS-ONLY SEMANTIC SEARCH
|
||
|
|
console.log('🔍 Searching VFS for "neural networks"...')
|
||
|
|
const vfsFiles = await vfs.search('neural networks', { limit: 5 })
|
||
|
|
console.log(`Found ${vfsFiles.length} VFS files:`)
|
||
|
|
vfsFiles.forEach(f => {
|
||
|
|
console.log(` [${f.score.toFixed(3)}] ${f.path}`)
|
||
|
|
})
|
||
|
|
|
||
|
|
// 3. VFS FILTERING IN KNOWLEDGE QUERIES
|
||
|
|
console.log('\n🔍 Understanding VFS filtering (v4.4.0)...\n')
|
||
|
|
|
||
|
|
// Create some knowledge entities
|
||
|
|
const conceptId = await brain.add({
|
||
|
|
data: 'Neural networks are computational models inspired by biological neurons',
|
||
|
|
type: NounType.Concept,
|
||
|
|
metadata: { topic: 'AI' }
|
||
|
|
})
|
||
|
|
|
||
|
|
const projectId = await brain.add({
|
||
|
|
data: 'AI Assistant - conversational AI using transformers',
|
||
|
|
type: NounType.Project,
|
||
|
|
metadata: { status: 'active' }
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log('Created 2 knowledge entities\n')
|
||
|
|
|
||
|
|
// DEFAULT: Knowledge queries exclude VFS (clean separation!)
|
||
|
|
console.log('📊 brain.find() - DEFAULT behavior (excludes VFS):')
|
||
|
|
const knowledgeOnly = await brain.find({ query: 'neural networks' })
|
||
|
|
console.log(` Found ${knowledgeOnly.length} entities`)
|
||
|
|
console.log(` VFS files: ${knowledgeOnly.filter(r => r.metadata?.isVFS).length}`) // 0
|
||
|
|
console.log(` Knowledge: ${knowledgeOnly.filter(r => !r.metadata?.isVFS).length}`)
|
||
|
|
|
||
|
|
// OPT-IN: Include VFS when needed
|
||
|
|
console.log('\n📊 brain.find() with includeVFS: true:')
|
||
|
|
const everything = await brain.find({
|
||
|
|
query: 'neural networks',
|
||
|
|
includeVFS: true // Opt-in to include VFS files
|
||
|
|
})
|
||
|
|
console.log(` Found ${everything.length} entities`)
|
||
|
|
console.log(` VFS files: ${everything.filter(r => r.metadata?.isVFS).length}`)
|
||
|
|
console.log(` Knowledge: ${everything.filter(r => !r.metadata?.isVFS).length}`)
|
||
|
|
|
||
|
|
// VFS-ONLY: Search only files
|
||
|
|
console.log('\n📊 Searching ONLY VFS files:')
|
||
|
|
const filesOnly = await brain.find({
|
||
|
|
where: { vfsType: 'file', extension: '.md' },
|
||
|
|
includeVFS: true // Required to find VFS entities
|
||
|
|
})
|
||
|
|
console.log(` Found ${filesOnly.length} markdown files`)
|
||
|
|
|
||
|
|
// 4. CROSS-BOUNDARY RELATIONSHIPS
|
||
|
|
console.log('\n\n🔗 Creating cross-boundary relationships...')
|
||
|
|
|
||
|
|
// Link concept to documentation file
|
||
|
|
const readmeEntity = await brain.find({
|
||
|
|
where: { path: '/projects/ai-assistant/README.md' },
|
||
|
|
includeVFS: true,
|
||
|
|
limit: 1
|
||
|
|
})
|
||
|
|
|
||
|
|
if (readmeEntity.length > 0) {
|
||
|
|
await brain.relate({
|
||
|
|
from: conceptId,
|
||
|
|
to: readmeEntity[0].id,
|
||
|
|
type: VerbType.DocumentedBy,
|
||
|
|
metadata: { section: 'Features' }
|
||
|
|
})
|
||
|
|
console.log('✅ Linked concept to README.md')
|
||
|
|
}
|
||
|
|
|
||
|
|
// Query relationships
|
||
|
|
const conceptDocs = await brain.getRelations({
|
||
|
|
from: conceptId,
|
||
|
|
type: VerbType.DocumentedBy
|
||
|
|
})
|
||
|
|
console.log(`Concept is documented by ${conceptDocs.length} files`)
|
||
|
|
|
||
|
|
// 5. VFS SEMANTIC FEATURES
|
||
|
|
console.log('\n\n🔍 VFS Semantic Features:')
|
||
|
|
|
||
|
|
// Find similar files
|
||
|
|
const similarFiles = await vfs.findSimilar('/projects/ai-assistant/README.md', {
|
||
|
|
limit: 3,
|
||
|
|
threshold: 0.5
|
||
|
|
})
|
||
|
|
console.log(`\nFiles similar to README.md: ${similarFiles.length}`)
|
||
|
|
similarFiles.forEach(f => {
|
||
|
|
console.log(` [${f.score.toFixed(3)}] ${f.path}`)
|
||
|
|
})
|
||
|
|
|
||
|
|
// Get file stats
|
||
|
|
const stats = await vfs.stat('/projects/ai-assistant/README.md')
|
||
|
|
console.log('\nREADME.md stats:', {
|
||
|
|
size: stats.size,
|
||
|
|
type: stats.vfsType,
|
||
|
|
extension: stats.metadata?.extension,
|
||
|
|
created: new Date(stats.metadata?.createdAt || 0).toLocaleString()
|
||
|
|
})
|
||
|
|
|
||
|
|
// Read directory
|
||
|
|
console.log('\n📁 Directory contents of /projects/ai-assistant:')
|
||
|
|
const entries = await vfs.readdir('/projects/ai-assistant')
|
||
|
|
console.log(entries)
|
||
|
|
|
||
|
|
// 6. METADATA & EXTENDED ATTRIBUTES
|
||
|
|
console.log('\n\n📝 Metadata & Extended Attributes:')
|
||
|
|
|
||
|
|
await vfs.setMetadata('/projects/ai-assistant/README.md', {
|
||
|
|
author: 'John Smith',
|
||
|
|
version: '1.0.0',
|
||
|
|
tags: ['AI', 'documentation', 'project']
|
||
|
|
})
|
||
|
|
|
||
|
|
const metadata = await vfs.getMetadata('/projects/ai-assistant/README.md')
|
||
|
|
console.log('README metadata:', metadata)
|
||
|
|
|
||
|
|
// Extended attributes (like file properties)
|
||
|
|
await vfs.setxattr('/projects/ai-assistant/README.md', 'priority', 'high')
|
||
|
|
await vfs.setxattr('/projects/ai-assistant/README.md', 'reviewStatus', 'approved')
|
||
|
|
|
||
|
|
const xattrs = await vfs.listxattr('/projects/ai-assistant/README.md')
|
||
|
|
console.log('Extended attributes:', xattrs)
|
||
|
|
|
||
|
|
// 7. FILE OPERATIONS
|
||
|
|
console.log('\n\n📋 Advanced File Operations:')
|
||
|
|
|
||
|
|
// Copy file
|
||
|
|
await vfs.copy('/docs/installation.md', '/projects/ai-assistant/INSTALL.md')
|
||
|
|
console.log('✅ Copied installation.md')
|
||
|
|
|
||
|
|
// Rename
|
||
|
|
await vfs.rename('/projects/ai-assistant/INSTALL.md', '/projects/ai-assistant/setup.md')
|
||
|
|
console.log('✅ Renamed to setup.md')
|
||
|
|
|
||
|
|
// Check existence
|
||
|
|
const exists = await vfs.exists('/projects/ai-assistant/setup.md')
|
||
|
|
console.log(`setup.md exists: ${exists}`)
|
||
|
|
|
||
|
|
console.log('\n\n✅ VFS Tutorial Complete!')
|
||
|
|
console.log('\n📚 Key Takeaways:')
|
||
|
|
console.log(' 1. VFS files have semantic understanding (search by meaning)')
|
||
|
|
console.log(' 2. brain.find() excludes VFS by default (clean knowledge queries)')
|
||
|
|
console.log(' 3. Use includeVFS: true to include VFS in knowledge queries')
|
||
|
|
console.log(' 4. vfs.search() ONLY searches VFS files (never knowledge entities)')
|
||
|
|
console.log(' 5. Cross-boundary relationships link files to concepts')
|
||
|
|
console.log(' 6. Every file is a full Brainy entity with vector, metadata, and graph')
|
||
|
|
|
||
|
|
await vfs.close()
|
||
|
|
await brain.close()
|
||
|
|
```
|
||
|
|
|
||
|
|
### Key Concepts
|
||
|
|
|
||
|
|
#### 1. **VFS Filtering Architecture (v4.4.0)**
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// 🎯 DEFAULT BEHAVIOR: Clean Separation
|
||
|
|
//
|
||
|
|
// Knowledge queries stay clean (no VFS pollution)
|
||
|
|
const concepts = await brain.find({ query: 'AI' })
|
||
|
|
// Returns: Only NounType.Concept, NounType.Document, etc.
|
||
|
|
// Excludes: VFS files (no .path property)
|
||
|
|
|
||
|
|
// VFS queries work with VFS only
|
||
|
|
const files = await vfs.search('documentation')
|
||
|
|
// Returns: Only VFS files with .path property
|
||
|
|
// Excludes: Knowledge entities
|
||
|
|
|
||
|
|
// 🔄 CROSS-BOUNDARY: Opt-in when needed
|
||
|
|
const everything = await brain.find({
|
||
|
|
query: 'machine learning',
|
||
|
|
includeVFS: true // Include both knowledge AND VFS
|
||
|
|
})
|
||
|
|
// Returns: Knowledge entities + VFS files
|
||
|
|
|
||
|
|
// 📁 VFS-ONLY via brain.find()
|
||
|
|
const markdownFiles = await brain.find({
|
||
|
|
where: { vfsType: 'file', extension: '.md' },
|
||
|
|
includeVFS: true // Required to find VFS entities
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 2. **Cross-Boundary Relationships**
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// Files can relate to knowledge entities
|
||
|
|
await brain.relate({
|
||
|
|
from: conceptId, // Knowledge: NounType.Concept
|
||
|
|
to: fileId, // VFS: File entity
|
||
|
|
type: VerbType.DocumentedBy
|
||
|
|
})
|
||
|
|
|
||
|
|
// Query across boundaries
|
||
|
|
const conceptDocs = await brain.getRelations({
|
||
|
|
from: conceptId,
|
||
|
|
type: VerbType.DocumentedBy
|
||
|
|
})
|
||
|
|
// Returns: VFS files that document the concept
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 3. **VFS vs Traditional Filesystem**
|
||
|
|
|
||
|
|
| Feature | Traditional FS | Brainy VFS |
|
||
|
|
|---------|---------------|------------|
|
||
|
|
| Search | Filename only | Semantic content search |
|
||
|
|
| Organization | Hierarchy only | Hierarchy + Graph |
|
||
|
|
| Metadata | Limited (size, dates) | Unlimited custom metadata |
|
||
|
|
| Relationships | None | Full graph relationships |
|
||
|
|
| Similarity | None | Find similar files |
|
||
|
|
| Understanding | None | Vector embeddings |
|
||
|
|
|
||
|
|
#### 4. **When to Use What**
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// Use vfs.* methods for file operations
|
||
|
|
await vfs.writeFile('/path/to/file.txt', content)
|
||
|
|
await vfs.readFile('/path/to/file.txt')
|
||
|
|
await vfs.search('semantic query')
|
||
|
|
|
||
|
|
// Use brain.* methods for knowledge operations
|
||
|
|
await brain.add({ data: 'concept', type: NounType.Concept })
|
||
|
|
await brain.find({ query: 'concept' }) // Excludes VFS by default
|
||
|
|
|
||
|
|
// Use includeVFS for cross-boundary queries
|
||
|
|
await brain.find({
|
||
|
|
query: 'documentation',
|
||
|
|
includeVFS: true // Search both knowledge AND files
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
### Practice Exercises
|
||
|
|
|
||
|
|
1. Create a project structure with docs, source code, tests
|
||
|
|
2. Add semantic tags to files
|
||
|
|
3. Search for "API documentation" and see VFS filtering in action
|
||
|
|
4. Create relationships between code files and design documents
|
||
|
|
5. Find files similar to a specific README
|
||
|
|
6. Compare results with/without includeVFS
|
||
|
|
|
||
|
|
### Next Steps
|
||
|
|
Ready for production deployment? Level 5 covers **planet-scale architecture**.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Level 5: Production Scale (90 minutes)
|
||
|
|
|
||
|
|
### What You'll Learn
|
||
|
|
- Cloud storage (GCS, S3, R2)
|
||
|
|
- Performance optimization
|
||
|
|
- Batch imports (CSV, Excel, PDF)
|
||
|
|
- Metadata query optimization
|
||
|
|
- Production best practices
|
||
|
|
|
||
|
|
### Production-Ready Deployment
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
import { Brainy, NounType } from '@soulcraft/brainy'
|
||
|
|
|
||
|
|
// 1. PRODUCTION STORAGE - Google Cloud Storage (Native SDK)
|
||
|
|
console.log('☁️ Initializing production storage...\n')
|
||
|
|
|
||
|
|
const brain = new Brainy({
|
||
|
|
storage: {
|
||
|
|
type: 'gcs-native', // Native GCS SDK (recommended)
|
||
|
|
gcsNativeStorage: {
|
||
|
|
bucketName: 'my-brainy-production',
|
||
|
|
// ADC (Application Default Credentials) - zero config in Cloud Run/GCE!
|
||
|
|
// Or provide credentials:
|
||
|
|
// keyFilename: '/path/to/service-account.json'
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
// Performance tuning
|
||
|
|
cache: {
|
||
|
|
maxSize: 10000, // Cache up to 10K entities
|
||
|
|
ttl: 600000 // 10 minute TTL
|
||
|
|
},
|
||
|
|
|
||
|
|
// Monitoring
|
||
|
|
verbose: process.env.NODE_ENV === 'development'
|
||
|
|
})
|
||
|
|
|
||
|
|
await brain.init()
|
||
|
|
console.log('✅ Brainy initialized with GCS Native storage\n')
|
||
|
|
|
||
|
|
// 2. BATCH IMPORT - CSV File
|
||
|
|
console.log('📊 Importing CSV data...\n')
|
||
|
|
|
||
|
|
const csvResult = await brain.import('./data/customers-1000.csv', {
|
||
|
|
vfsPath: '/imports/customers.csv', // Store in VFS
|
||
|
|
createEntities: true, // Create knowledge entities
|
||
|
|
batchSize: 100, // Process in batches of 100
|
||
|
|
onProgress: (done, total) => {
|
||
|
|
console.log(` Progress: ${done}/${total} (${(done/total*100).toFixed(1)}%)`)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log('\n📊 Import Results:')
|
||
|
|
console.log(` Entities created: ${csvResult.stats.graphNodesCreated}`)
|
||
|
|
console.log(` VFS files created: ${csvResult.stats.vfsFilesCreated}`)
|
||
|
|
console.log(` Duration: ${csvResult.stats.duration}ms`)
|
||
|
|
|
||
|
|
// 3. METADATA QUERY OPTIMIZATION
|
||
|
|
console.log('\n\n🔍 Metadata Query Optimization:\n')
|
||
|
|
|
||
|
|
// Discover what fields are available
|
||
|
|
const fields = await brain.getAvailableFields()
|
||
|
|
console.log(`Available metadata fields: ${fields.length}`)
|
||
|
|
console.log(` Top fields: ${fields.slice(0, 10).join(', ')}`)
|
||
|
|
|
||
|
|
// Get field statistics (cardinality, types)
|
||
|
|
const fieldStats = await brain.getFieldStatistics()
|
||
|
|
console.log(`\nField statistics:`)
|
||
|
|
const topFields = Array.from(fieldStats.entries()).slice(0, 5)
|
||
|
|
topFields.forEach(([field, stats]) => {
|
||
|
|
console.log(` ${field}: ${stats.cardinality} unique values`)
|
||
|
|
})
|
||
|
|
|
||
|
|
// Get optimal query plan
|
||
|
|
const queryPlan = await brain.getOptimalQueryPlan({
|
||
|
|
status: 'active',
|
||
|
|
year: 2024
|
||
|
|
})
|
||
|
|
console.log(`\nQuery plan:`)
|
||
|
|
console.log(` Estimated results: ${queryPlan.estimatedResults}`)
|
||
|
|
console.log(` Index usage: ${queryPlan.indexUsage.join(', ')}`)
|
||
|
|
console.log(` Execution time: ~${queryPlan.estimatedMs}ms`)
|
||
|
|
|
||
|
|
// 4. LARGE-SCALE BATCH OPERATIONS
|
||
|
|
console.log('\n\n📦 Large-Scale Batch Operations:\n')
|
||
|
|
|
||
|
|
// Generate test data
|
||
|
|
const testItems = Array.from({ length: 1000 }, (_, i) => ({
|
||
|
|
data: `Test entity ${i} - Machine learning and artificial intelligence`,
|
||
|
|
type: NounType.Document,
|
||
|
|
metadata: {
|
||
|
|
index: i,
|
||
|
|
category: i % 5 === 0 ? 'AI' : 'General',
|
||
|
|
priority: Math.random() > 0.5 ? 'high' : 'normal',
|
||
|
|
year: 2024
|
||
|
|
}
|
||
|
|
}))
|
||
|
|
|
||
|
|
console.log(`Adding 1000 entities...`)
|
||
|
|
const startTime = Date.now()
|
||
|
|
|
||
|
|
const batchResult = await brain.addMany({
|
||
|
|
items: testItems,
|
||
|
|
parallel: true,
|
||
|
|
chunkSize: 100,
|
||
|
|
onProgress: (done, total) => {
|
||
|
|
if (done % 200 === 0) console.log(` ${done}/${total}`)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
const duration = Date.now() - startTime
|
||
|
|
console.log(`\n✅ Batch add complete:`)
|
||
|
|
console.log(` Success: ${batchResult.successful.length}`)
|
||
|
|
console.log(` Failed: ${batchResult.failed.length}`)
|
||
|
|
console.log(` Duration: ${duration}ms`)
|
||
|
|
console.log(` Throughput: ${(batchResult.successful.length / (duration / 1000)).toFixed(0)} entities/sec`)
|
||
|
|
|
||
|
|
// 5. ADVANCED CLUSTERING (Large Dataset)
|
||
|
|
console.log('\n\n🤖 Clustering 1000 entities...\n')
|
||
|
|
|
||
|
|
const neural = brain.neural()
|
||
|
|
|
||
|
|
// Use fast clustering for large datasets
|
||
|
|
const clusters = await neural.clusterFast({
|
||
|
|
maxClusters: 5
|
||
|
|
})
|
||
|
|
|
||
|
|
console.log(`Found ${clusters.length} clusters:`)
|
||
|
|
clusters.forEach((cluster, i) => {
|
||
|
|
console.log(`\n Cluster ${i + 1}: ${cluster.label || cluster.id}`)
|
||
|
|
console.log(` Size: ${cluster.members.length} members`)
|
||
|
|
console.log(` Density: ${(cluster.density || 0).toFixed(3)}`)
|
||
|
|
if (cluster.metadata?.keywords) {
|
||
|
|
console.log(` Keywords: ${cluster.metadata.keywords.slice(0, 5).join(', ')}`)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
// 6. PRODUCTION STATISTICS
|
||
|
|
console.log('\n\n📊 Production Statistics:\n')
|
||
|
|
|
||
|
|
const stats = brain.getStats()
|
||
|
|
console.log(`Total Entities: ${stats.entities.total.toLocaleString()}`)
|
||
|
|
console.log(`Total Relationships: ${stats.relationships.totalRelationships.toLocaleString()}`)
|
||
|
|
console.log(`Graph Density: ${stats.density.toFixed(4)}`)
|
||
|
|
|
||
|
|
console.log(`\nEntities by Type:`)
|
||
|
|
Object.entries(stats.entities.byType)
|
||
|
|
.sort(([, a], [, b]) => (b as number) - (a as number))
|
||
|
|
.slice(0, 5)
|
||
|
|
.forEach(([type, count]) => {
|
||
|
|
console.log(` ${type}: ${(count as number).toLocaleString()}`)
|
||
|
|
})
|
||
|
|
|
||
|
|
// 7. QUERY PERFORMANCE MONITORING
|
||
|
|
console.log('\n\n⚡ Query Performance:\n')
|
||
|
|
|
||
|
|
const perfStart = Date.now()
|
||
|
|
const searchResults = await brain.find({
|
||
|
|
query: 'artificial intelligence machine learning',
|
||
|
|
where: { category: 'AI' },
|
||
|
|
limit: 100,
|
||
|
|
explain: true
|
||
|
|
})
|
||
|
|
const perfDuration = Date.now() - perfStart
|
||
|
|
|
||
|
|
console.log(`Query completed in ${perfDuration}ms`)
|
||
|
|
console.log(` Results: ${searchResults.length}`)
|
||
|
|
console.log(` Avg score: ${(searchResults.reduce((sum, r) => sum + r.score, 0) / searchResults.length).toFixed(3)}`)
|
||
|
|
|
||
|
|
// Show top result explanation
|
||
|
|
if (searchResults[0]?.explanation) {
|
||
|
|
console.log(`\n Top result score breakdown:`)
|
||
|
|
console.log(` Vector: ${searchResults[0].explanation.vector?.toFixed(3) || 'N/A'}`)
|
||
|
|
console.log(` Metadata: ${searchResults[0].explanation.metadata?.toFixed(3) || 'N/A'}`)
|
||
|
|
console.log(` Graph: ${searchResults[0].explanation.graph?.toFixed(3) || 'N/A'}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 8. CLEANUP & BEST PRACTICES
|
||
|
|
console.log('\n\n🧹 Production Best Practices:\n')
|
||
|
|
|
||
|
|
// Always flush before shutdown
|
||
|
|
await brain.flush()
|
||
|
|
console.log('✅ Flushed all data to storage')
|
||
|
|
|
||
|
|
// Get final stats
|
||
|
|
const finalStats = brain.getStats()
|
||
|
|
console.log(`✅ Final entity count: ${finalStats.entities.total.toLocaleString()}`)
|
||
|
|
|
||
|
|
// Clean shutdown
|
||
|
|
await brain.close()
|
||
|
|
console.log('✅ Brain closed cleanly')
|
||
|
|
|
||
|
|
console.log('\n\n🎓 Production Deployment Complete!')
|
||
|
|
console.log('\n📚 Key Production Learnings:')
|
||
|
|
console.log(' 1. Use native cloud storage (GCS, S3, R2) for persistence')
|
||
|
|
console.log(' 2. Batch operations = 100x faster than individual ops')
|
||
|
|
console.log(' 3. Metadata query optimization for complex filters')
|
||
|
|
console.log(' 4. Monitor query performance with explain: true')
|
||
|
|
console.log(' 5. Always flush() before shutdown')
|
||
|
|
console.log(' 6. Use getStats() for O(1) counts (no expensive scans)')
|
||
|
|
console.log(' 7. Stream large imports with progress callbacks')
|
||
|
|
```
|
||
|
|
|
||
|
|
### Key Concepts
|
||
|
|
|
||
|
|
#### 1. **Storage Options Comparison**
|
||
|
|
|
||
|
|
| Storage | Use Case | Performance | Cost | Setup |
|
||
|
|
|---------|----------|-------------|------|-------|
|
||
|
|
| Memory | Dev/testing | Fastest | Free | Zero config |
|
||
|
|
| Filesystem | Local prod | Fast | Free | Local path |
|
||
|
|
| GCS Native | GCP prod | Fast | $$$ | Service account |
|
||
|
|
| S3 | AWS prod | Fast | $$$ | Access keys |
|
||
|
|
| R2 | Cloudflare | Fast | $ | Access keys |
|
||
|
|
|
||
|
|
#### 2. **GCS Native vs S3-Compatible**
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// ✅ RECOMMENDED: GCS Native SDK
|
||
|
|
{
|
||
|
|
storage: {
|
||
|
|
type: 'gcs-native',
|
||
|
|
gcsNativeStorage: {
|
||
|
|
bucketName: 'my-bucket'
|
||
|
|
// ADC handles auth automatically in Cloud Run/GCE!
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ⚠️ LEGACY: S3-compatible mode
|
||
|
|
{
|
||
|
|
storage: {
|
||
|
|
type: 'gcs',
|
||
|
|
gcsStorage: {
|
||
|
|
bucketName: 'my-bucket',
|
||
|
|
accessKeyId: process.env.GCS_ACCESS_KEY,
|
||
|
|
secretAccessKey: process.env.GCS_SECRET_KEY
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 3. **Performance Optimization**
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// 1. Use batch operations
|
||
|
|
await brain.addMany({ items, parallel: true, chunkSize: 100 })
|
||
|
|
|
||
|
|
// 2. Enable caching
|
||
|
|
const brain = new Brainy({
|
||
|
|
cache: { maxSize: 10000, ttl: 600000 }
|
||
|
|
})
|
||
|
|
|
||
|
|
// 3. Use metadata indexes for filtering
|
||
|
|
await brain.find({
|
||
|
|
where: { status: 'active' }, // Uses MetadataIndexManager
|
||
|
|
limit: 100
|
||
|
|
})
|
||
|
|
|
||
|
|
// 4. Optimize query plans
|
||
|
|
const plan = await brain.getOptimalQueryPlan(filters)
|
||
|
|
// Use plan to choose best query strategy
|
||
|
|
|
||
|
|
// 5. Use writeOnly for bulk imports
|
||
|
|
await brain.add({
|
||
|
|
data,
|
||
|
|
type,
|
||
|
|
writeOnly: true // Skip validation for speed
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 4. **Import Strategies**
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// Small files (<10MB) - Direct import
|
||
|
|
await brain.import('./data.csv')
|
||
|
|
|
||
|
|
// Large files (>10MB) - Stream with progress
|
||
|
|
await brain.import('./large-data.csv', {
|
||
|
|
batchSize: 1000,
|
||
|
|
onProgress: (done, total) => {
|
||
|
|
console.log(`${(done/total*100).toFixed(1)}%`)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
// Very large files (>100MB) - External pipeline
|
||
|
|
// Use streaming pipeline API for max control
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 5. **Monitoring & Observability**
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// 1. Track query performance
|
||
|
|
const start = Date.now()
|
||
|
|
const results = await brain.find({ query, explain: true })
|
||
|
|
const duration = Date.now() - start
|
||
|
|
console.log(`Query: ${duration}ms, Results: ${results.length}`)
|
||
|
|
|
||
|
|
// 2. Monitor graph statistics
|
||
|
|
const stats = brain.getStats()
|
||
|
|
console.log(`Density: ${stats.density}`) // Relationships per entity
|
||
|
|
|
||
|
|
// 3. Track field cardinality
|
||
|
|
const fieldStats = await brain.getFieldStatistics()
|
||
|
|
// High cardinality fields = good for filtering
|
||
|
|
|
||
|
|
// 4. Enable verbose logging in dev
|
||
|
|
const brain = new Brainy({ verbose: true })
|
||
|
|
```
|
||
|
|
|
||
|
|
### Production Checklist
|
||
|
|
|
||
|
|
#### Before Deployment
|
||
|
|
|
||
|
|
- [ ] Choose cloud storage (GCS/S3/R2)
|
||
|
|
- [ ] Set up authentication (service account/access keys)
|
||
|
|
- [ ] Configure caching
|
||
|
|
- [ ] Test batch operations
|
||
|
|
- [ ] Benchmark query performance
|
||
|
|
- [ ] Set up monitoring
|
||
|
|
|
||
|
|
#### During Operation
|
||
|
|
|
||
|
|
- [ ] Monitor query latency
|
||
|
|
- [ ] Track entity/relationship counts
|
||
|
|
- [ ] Watch for outliers
|
||
|
|
- [ ] Optimize slow queries
|
||
|
|
- [ ] Regular backups
|
||
|
|
|
||
|
|
#### Scaling Considerations
|
||
|
|
|
||
|
|
- [ ] Shard data by service/tenant
|
||
|
|
- [ ] Use read replicas for queries
|
||
|
|
- [ ] Implement rate limiting
|
||
|
|
- [ ] Monitor storage costs
|
||
|
|
- [ ] Plan for growth
|
||
|
|
|
||
|
|
### Practice Exercises
|
||
|
|
|
||
|
|
1. Deploy Brainy with GCS Native storage
|
||
|
|
2. Import a 10,000 row CSV file
|
||
|
|
3. Measure query performance for different filters
|
||
|
|
4. Optimize a slow query using getOptimalQueryPlan()
|
||
|
|
5. Set up monitoring dashboard
|
||
|
|
6. Create backup/restore scripts
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 🎓 Graduation: You're a Brainy Expert!
|
||
|
|
|
||
|
|
### What You've Mastered
|
||
|
|
|
||
|
|
✅ **Level 1**: Basic operations (add, find, search)
|
||
|
|
✅ **Level 2**: Relationships & batch operations
|
||
|
|
✅ **Level 3**: Triple Intelligence & Neural AI
|
||
|
|
✅ **Level 4**: Virtual Filesystem
|
||
|
|
✅ **Level 5**: Production deployment
|
||
|
|
|
||
|
|
### Next Steps
|
||
|
|
|
||
|
|
#### Advanced Topics
|
||
|
|
|
||
|
|
- **Distributed Systems**: Sharding, replication, coordination
|
||
|
|
- **Custom Augmentations**: Extend Brainy with plugins
|
||
|
|
- **Streaming Pipelines**: Real-time data ingestion
|
||
|
|
- **Security**: Encryption, access control, audit logs
|
||
|
|
- **Framework Integration**: React, Vue, Next.js, Nuxt
|
||
|
|
|
||
|
|
#### Resources
|
||
|
|
|
||
|
|
- 📚 [API Reference](../API_REFERENCE.md) - Complete API documentation
|
||
|
|
- 📁 [VFS Guide](../vfs/VFS_API_GUIDE.md) - Virtual Filesystem deep dive
|
||
|
|
- 🤖 [Neural API](../guides/neural-api.md) - Advanced neural operations
|
||
|
|
- 🌐 [Distributed Guide](../guides/distributed-system.md) - Planet-scale architecture
|
||
|
|
- 💬 [Discord Community](https://discord.gg/brainy) - Get help, share projects
|
||
|
|
|
||
|
|
#### Share Your Success
|
||
|
|
|
||
|
|
Built something cool with Brainy? Share it with the community!
|
||
|
|
|
||
|
|
- GitHub: https://github.com/soulcraft/brainy
|
||
|
|
- Twitter: @brainydb
|
||
|
|
- Discord: https://discord.gg/brainy
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
**Congratulations! You're now a Brainy expert ready to build production neural database applications! 🎉**
|