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
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
---
title: Warbler CDA FractalStat RAG
emoji: 🦜
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 6.0.2
app_file: app.py
pinned: false
license: mit
short_description: RAG system with 8D FractalStat and 100k documents
tags:
- rag
- semantic-search
- retrieval
- fastapi
- fractalstat
thumbnail: >-
  https://cdn-uploads.huggingface.co/production/uploads/68c705b6fc90bcc7a4f56721/8G2TJJT8enAFaBLJGTXka.png
---

# Warbler CDA - Cognitive Development Architecture RAG System

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
[![FastAPI](https://img.shields.io/badge/FastAPI-0.100+-green.svg)](https://fastapi.tiangolo.com/)
[![Docker](https://img.shields.io/badge/Docker-ready-blue.svg)](https://docker.com)

A **production-ready RAG (Retrieval-Augmented Generation) system** with **FractalStat multi-dimensional addressing** for intelligent document retrieval, semantic memory, and automatic data ingestion.

## 🌟 Features

### Core RAG System

- **Semantic Anchors**: Persistent memory with provenance tracking
- **Hierarchical Summarization**: Micro/macro distillation for efficient compression
- **Conflict Detection**: Automatic detection and resolution of contradictory information
- **Memory Pooling**: Performance-optimized object pooling for high-throughput scenarios

### FractalStat Multi-Dimensional Addressing

- **8-Dimensional Coordinates**: Realm, Lineage, Adjacency, Horizon, Luminosity, Polarity, Dimensionality, Alignment
- **Hybrid Scoring**: Combines semantic similarity with FractalStat resonance for superior retrieval
- **Entanglement Detection**: Identifies relationships across dimensional space
- **Validated System**: Comprehensive experiments (EXP-01 through EXP-10) validate uniqueness, efficiency, and narrative preservation

### Production-Ready API

- **FastAPI Service**: High-performance async API with concurrent query support
- **CLI Tools**: Command-line interface for queries, ingestion, and management
- **HuggingFace Integration**: Direct ingestion from HF datasets
- **Docker Support**: Containerized deployment ready

## πŸ“š Data Sources

The Warbler system is trained on carefully curated, MIT-licensed datasets from HuggingFace:

### Original Warbler Packs

- `warbler-pack-core` - Core narrative and reasoning patterns
- `warbler-pack-wisdom-scrolls` - Philosophical and wisdom-based content
- `warbler-pack-faction-politics` - Political and faction dynamics

### HuggingFace Datasets

- **arXiv Papers** (`nick007x/arxiv-papers`) - 2.5M+ scholarly papers covering scientific domains
  - Due to space limits, we only ingest 100k of these documents for use on HuggingFace Spaces.
- **Prompt Engineering Report** (`PromptSystematicReview/ThePromptReport`) - 83 comprehensive prompt documentation entries
  - Currently unavailable due to same reasons above.
- **Generated Novels** (`GOAT-AI/generated-novels`) - 20 narrative-rich novels for storytelling patterns
  - Currently unavailable due to same reasons above.
- **Technical Manuals** (`nlasso/anac-manuals-23`) - 52 procedural and operational documents
  - Currently unavailable due to same reasons above.
- **ChatEnv Enterprise** (`SustcZhangYX/ChatEnv`) - 112K+ software development conversations
  - Currently unavailable due to same reasons above.
- **Portuguese Education** (`Solshine/Portuguese_Language_Education_Texts`) - 21 multilingual educational texts
  - Currently unavailable due to same reasons above.
- **Educational Stories** (`MU-NLPC/Edustories-en`) - 1.5K+ case studies and learning narratives

All datasets are provided under MIT or compatible licenses. For complete attribution, see the HuggingFace Hub pages listed above.

## πŸ“¦ Installation

### From Source (Current Method)

```bash
git clone https://github.com/tiny-walnut-games/the-seed.git
cd the-seed/warbler-cda-package
pip install -e .
```

### Optional Dependencies

```bash
# OpenAI embeddings integration
pip install openai

# Development tools
pip install pytest pytest-cov
```

## πŸš€ Quick Start

### Option 1: Direct Python (Easiest)

```bash
cd warbler-cda-package

# Start the API with automatic pack loading
./run_api.ps1

# Or on Linux/Mac:
python start_server.py
```

The API automatically loads all Warbler packs on startup and serves them at **http://localhost:8000**

### Option 2: Docker Compose

```bash
cd warbler-cda-package
docker-compose up --build
```

### Option 3: Kubernetes

```bash
cd warbler-cda-package/k8s
./demo-docker-k8s.sh  # Full auto-deploy
```

## πŸ“‘ API Usage Examples

### Using the REST API

```bash
# Start the API first: ./run_api.ps1
# Then test with:

# Health check
curl http://localhost:8000/health

# Semantic search (plain English queries)
curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{
    "query_id": "semantic1",
    "semantic_query": "dancing under the moon",
    "max_results": 5
  }'

# FractalStat hybrid search (technical/science with dimensional awareness)
curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{
    "query_id": "hybrid1",
    "semantic_query": "interplanetary approach maneuvers",
    "fractalstat_hybrid": true,
    "max_results": 5
  }'

# Get metrics
curl http://localhost:8000/metrics
```

### Understanding Search Modes

The system provides two search approaches with intelligent fallback:

#### Semantic Search (Default)
- **Use for**: Plain English queries, casual search, general questions
- **Behavior**: Pure semantic similarity matching
- **Examples**: "How does gravity work?", "tell me about dancing", "operating a spaceship"
- **Results**: Always returns matches when available, best for natural language

#### FractalStat Hybrid Search
- **Use for**: Technical/scientific queries, specific terminology, multi-dimensional search
- **Behavior**: Combines semantic similarity with 8D FractalStat resonance
- **Examples**: "rotation dynamics of Saturn's moons", "quantum chromodynamics", "interplanetary approach maneuvers"
- **Results**: Superior for technical content, may filter out general results
- **Fallback**: Automatically switches to semantic search if hybrid returns no results

**Pro Tip**: When hybrid search fails (threshold below 0.3), the system automatically falls back to semantic search, ensuring you always get relevant results.

### Using Python Programmatically

```python
import requests

# Health check
response = requests.get("http://localhost:8000/health")
print(f"API Status: {response.json()['status']}")

# Query
query_data = {
    "query_id": "python_test",
    "semantic_query": "rotation dynamics of Saturn's moons",
    "max_results": 5,
    "fractalstat_hybrid": True
}

results = requests.post("http://localhost:8000/query", json=query_data).json()
print(f"Found {len(results['results'])} results")

# Show top result
if results['results']:
    top_result = results['results'][0]
    print(f"Top score: {top_result['relevance_score']:.3f}")
    print(f"Content: {top_result['content'][:100]}...")
```

### FractalStat Hybrid Scoring

```python
from warbler_cda import FractalStatRAGBridge

# Enable FractalStat hybrid scoring
fractalstat_bridge = FractalStatRAGBridge()
api = RetrievalAPI(
    semantic_anchors=semantic_anchors,
    embedding_provider=embedding_provider,
    fractalstat_bridge=fractalstat_bridge,
    config={"enable_fractalstat_hybrid": True}
)

# Query with hybrid scoring
from warbler_cda import RetrievalQuery, RetrievalMode

query = RetrievalQuery(
    query_id="hybrid_query_1",
    mode=RetrievalMode.SEMANTIC_SIMILARITY,
    semantic_query="Find wisdom about resilience",
    fractalstat_hybrid=True,
    weight_semantic=0.6,
    weight_fractalstat=0.4
)

assembly = api.retrieve_context(query)
print(f"Found {len(assembly.results)} results with quality {assembly.assembly_quality:.3f}")
```

### Running the API Service

```bash
# Start the FastAPI service
uvicorn warbler_cda.api.service:app --host 0.0.0.0 --port 8000

# Or use the CLI
warbler-api --port 8000
```

### Using the CLI

```bash
# Query the API
warbler-cli query --query-id q1 --semantic "wisdom about courage" --max-results 10

# Enable hybrid scoring
warbler-cli query --query-id q2 --semantic "narrative patterns" --hybrid

# Bulk concurrent queries
warbler-cli bulk --num-queries 10 --concurrency 5 --hybrid

# Check metrics
warbler-cli metrics
```

## πŸ“Š FractalStat Experiments

The system includes validated experiments demonstrating:

- **EXP-01**: Address uniqueness (0% collision rate across 10K+ entities)
- **EXP-02**: Retrieval efficiency (sub-millisecond at 100K scale)
- **EXP-03**: Dimension necessity (all 7 dimensions required)
- **EXP-10**: Narrative preservation under concurrent load

```python
from warbler_cda import run_all_experiments

# Run validation experiments
results = run_all_experiments(
    exp01_samples=1000,
    exp01_iterations=10,
    exp02_queries=1000,
    exp03_samples=1000
)

print(f"EXP-01 Success: {results['EXP-01']['success']}")
print(f"EXP-02 Success: {results['EXP-02']['success']}")
print(f"EXP-03 Success: {results['EXP-03']['success']}")
```

## 🎯 Use Cases

### 1. Intelligent Document Retrieval

```python
# Add documents from various sources
for doc in documents:
    api.add_document(
        doc_id=doc["id"],
        content=doc["text"],
        metadata={
            "realm_type": "knowledge",
            "realm_label": "technical_docs",
            "lifecycle_stage": "emergence"
        }
    )

# Retrieve with context awareness
results = api.query_semantic_anchors("How to optimize performance?")
```

### 2. Narrative Coherence Analysis

```python
from warbler_cda import ConflictDetector

conflict_detector = ConflictDetector(embedding_provider=embedding_provider)

# Process statements
statements = [
    {"id": "s1", "text": "The system is fast"},
    {"id": "s2", "text": "The system is slow"}
]

report = conflict_detector.process_statements(statements)
print(f"Conflicts detected: {report['conflict_summary']}")
```

### 3. HuggingFace Dataset Ingestion

```python
from warbler_cda.utils import HFWarblerIngestor

ingestor = HFWarblerIngestor()

# Transform HF dataset to Warbler format
docs = ingestor.transform_npc_dialogue("amaydle/npc-dialogue")

# Create pack
pack_path = ingestor.create_warbler_pack(docs, "warbler-pack-npc-dialogue")
```

## πŸ—οΈ Architecture

```none
warbler_cda/
β”œβ”€β”€ retrieval_api.py          # Main RAG API
β”œβ”€β”€ semantic_anchors.py        # Semantic memory system
β”œβ”€β”€ anchor_data_classes.py     # Core data structures
β”œβ”€β”€ anchor_memory_pool.py      # Performance optimization
β”œβ”€β”€ summarization_ladder.py    # Hierarchical compression
β”œβ”€β”€ conflict_detector.py       # Conflict detection
β”œβ”€β”€ castle_graph.py            # Concept extraction
β”œβ”€β”€ melt_layer.py              # Memory consolidation
β”œβ”€β”€ evaporation.py             # Content distillation
β”œβ”€β”€ fractalstat_rag_bridge.py        # FractalStat hybrid scoring
β”œβ”€β”€ fractalstat_entity.py            # FractalStat entity system
β”œβ”€β”€ fractalstat_experiments.py       # Validation experiments
β”œβ”€β”€ embeddings/                # Embedding providers
β”‚   β”œβ”€β”€ base_provider.py
β”‚   β”œβ”€β”€ local_provider.py
β”‚   β”œβ”€β”€ openai_provider.py
β”‚   └── factory.py
β”œβ”€β”€ api/                       # Production API
β”‚   β”œβ”€β”€ service.py             # FastAPI service
β”‚   └── cli.py                 # CLI interface
└── utils/                     # Utilities
    β”œβ”€β”€ load_warbler_packs.py
    └── hf_warbler_ingest.py
```

## πŸ”¬ Technical Details

### FractalStat Dimensions

1. **Realm**: Domain classification (type + label)
2. **Lineage**: Generation/version number
3. **Adjacency**: Graph connectivity (0.0-1.0)
4. **Horizon**: Lifecycle stage (logline, outline, scene, panel)
5. **Luminosity**: Clarity/activity level (0.0-1.0)
6. **Polarity**: Resonance/tension (0.0-1.0)
7. **Dimensionality**: Complexity/thread count (1-7)

### Hybrid Scoring Formula

```math
hybrid_score = (weight_semantic Γ— semantic_similarity) + (weight_fractalstat Γ— fractalstat_resonance)
```

Where:

- `semantic_similarity`: Cosine similarity of embeddings
- `fractalstat_resonance`: Multi-dimensional alignment score
- Default weights: 60% semantic, 40% FractalStat

## πŸ“š Documentation

- [API Reference](docs/api.md)
- [FractalStat Guide](docs/fractalstat.md)
- [Experiments](docs/experiments.md)
- [Deployment](docs/deployment.md)

## 🀝 Contributing

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

## πŸ“„ License

MIT License - see [LICENSE](LICENSE) for details.

## πŸ™ Acknowledgments

- Built on research from The Seed project
- FractalStat addressing system inspired by multi-dimensional data structures
- Semantic anchoring based on cognitive architecture principles

## πŸ“ž Contact

- **Project**: [The Seed](https://github.com/tiny-walnut-games/the-seed)
- **Issues**: [GitHub Issues](https://github.com/tiny-walnut-games/the-seed/issues)
- **Discussions**: [GitHub Discussions](https://github.com/tiny-walnut-games/the-seed/discussions)

---

### **Made with ❀️ by Tiny Walnut Games**