anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
---
title: "Integration"
description: "Documentation for Integration"
---

# AI Integration Guide

## Table of Contents

- [Section 1]#section-1
- [Section 2]#section-2

> **Documentation Version**: 2.0  
> **Last Updated**: 2025-05-30  
> **Status**: Active  
> **AI Label**: [AIR-3][AIS-3][BPC-3]

## 📖 Table of Contents

- [Overview]#overview
- [Related Documentation]#related-documentation
- [Architecture]#architecture
  - [Core Components]#core-components
  - [Data Flow]#data-flow
- [Getting Started]#getting-started
  - [Prerequisites]#prerequisites
  - [Installation]#installation
  - [Quick Start]#quick-start
- [Model Serving]#model-serving
  - [Deployment Options]#deployment-options
  - [Scaling]#scaling
  - [Monitoring]#monitoring
  - [Canary Deployments]#canary-deployments

## Related Documentation

For a complete understanding of the AI system, please refer to:

- [AI System Overview]OVERVIEW.md - High-level architecture and components
- [AI Architecture]ARCHITECTURE.md - Technical implementation details
- [AI Development Guide]DEVELOPMENT.md - Building and contributing to AI components
- [AI Compliance]COMPLIANCE.md - Regulatory and security requirements
- [AI Metrics]METRICS.md - Performance monitoring and metrics
  - [A/B Testing]#ab-testing
- [API Reference]#api-reference
- [Best Practices]#best-practices
- [Troubleshooting]#troubleshooting
- [Examples]#examples
- [Security Considerations]#security-considerations
- [Related Documents]#related-documents

## Overview

Anya Core provides a flexible framework for integrating AI models and services. This guide covers the end-to-end process of integrating AI capabilities into the platform, from model development to production deployment.

### Key Features

- **Unified Model Serving**: Consistent interface for deploying and managing ML models
- **Scalable Architecture**: Built-in support for horizontal scaling
- **Comprehensive Monitoring**: Real-time metrics and logging
- **Security First**: Built-in security best practices
- **Extensible**: Support for custom model types and frameworks

## Architecture

### Core Components

```mermaid
graph TD
    subgraph Anya Core
        AI[AI Module] -->|Manages| ModelRegistry[Model Registry]
        AI -->|Processes| InferenceEngine[Inference Engine]
        AI -->|Trains| TrainingService[Training Service]
        AI -->|Monitors| Monitoring[Monitoring]
        
        ModelRegistry -->|Stores| ModelMetadata[Model Metadata]
        ModelRegistry -->|Tracks| ModelVersions[Model Versions]
        
        InferenceEngine -->|Supports| RealTime[Real-time Inference]
        InferenceEngine -->|Supports| Batch[Batch Processing]
        
        TrainingService -->|Uses| TrainingData[Training Data]
        TrainingService -->|Produces| TrainedModels[Trained Models]
        
        Monitoring -->|Tracks| Metrics[Performance Metrics]
        Monitoring -->|Alerts| Anomalies[Anomaly Detection]
    end
    
    subgraph External Services
        Models[AI Models]
        Data[Data Sources]
        Storage[Model Storage]
        Clients[Client Applications]
    end
    
    AI -->|Publishes| API[API Endpoints]
    API -->|Serves| Clients
    
    ModelRegistry -->|Stores in| Storage
    InferenceEngine -->|Loads from| Models
    TrainingService -->|Reads from| Data
    TrainingService -->|Saves to| Storage
```

### Data Flow

1. **Model Registration**
   - Models are registered in the Model Registry with metadata and versioning
   - Model artifacts are stored in the configured storage backend

2. **Inference Request**
   - Client sends request to API endpoint
   - Request is routed to appropriate model version
   - Inference Engine loads model (if not cached)
   - Prediction is generated and returned

3. **Model Updates**
   - New model versions are registered
   - Traffic is gradually shifted (canary deployment)
   - Rollback to previous version if issues detected

## Getting Started

### Prerequisites

Before you begin, ensure you have the following installed:

- **Anya Core** (v1.2.0 or later)
- **Python 3.8+** or **Rust 1.60+**
- **Docker** (for containerized deployment)
- **Kubernetes** (for production deployment)
- **GPU** (recommended for production workloads)

### Installation

#### Option 1: Using pip (Python)

```bash
# Install Anya Core with AI dependencies
pip install anya-core[ai]

# Or install from source
pip install -e .[ai]
```

#### Option 2: Using Cargo (Rust)

```toml
# Cargo.toml
[dependencies]
anya-core = { version = "1.2.0", features = ["ai"] }
```

### Quick Start

1. **Start the AI Service**

   ```bash
   # Start with default settings
   anya ai serve --model gpt-4 --port 8000
   ```

2. **Make a Prediction**

   ```python
   import requests
   
   response = requests.post(
       "http://localhost:8000/predict",
       json={
           "inputs": ["Hello, world!"],
           "parameters": {
               "max_length": 100,
               "temperature": 0.7
           }
       }
   )
   print(response.json())
   ```

3. **Check Service Health**

   ```bash
   curl http://localhost:8000/health
   ```

### Configuration

Configure the AI service using environment variables or a YAML configuration file:

```yaml
# config/ai.yaml
model:
  name: gpt-4
  version: latest
  device: cuda  # or cpu
  
server:
  port: 8000
  workers: 4
  log_level: info
  
monitoring:
  enabled: true
  metrics_port: 9090
  
cache:
  enabled: true
  ttl: 3600
  max_size: 1000

security:
  auth_required: true
  allowed_origins:
    - "https://app.anya.org"
    - "http://localhost:3000"
```

## Model Serving

Anya Core provides a robust model serving infrastructure that supports various deployment patterns and scaling strategies. This section covers the key aspects of serving models in production.

### Core Features

- **Multiple Serving Endpoints**: REST, gRPC, and WebSocket APIs
- **Model Versioning**: Seamless version management
- **Dynamic Batching**: Optimize throughput with automatic request batching
- **Model Warmup**: Pre-load models to reduce cold start latency
- **Request Timeouts**: Configurable timeouts for inference requests
- **Concurrency Control**: Limit concurrent requests per model

### Deployment Options

#### 1. Standalone Server

```bash
# Start a standalone server
anya ai serve --model gpt-4 --port 8000 --workers 4
```

#### 2. Docker Compose

```yaml
# docker-compose.yml
version: '3.8'
services:
  ai-service:
    image: anya/ai:latest
    ports:
      - "8000:8000"
      - "9090:9090"
    environment:
      - MODEL=gpt-4
      - DEVICE=cuda
      - WORKERS=4
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    volumes:
      - ./models:/app/models
      - ./config:/app/config
```

#### 3. Kubernetes with Helm

```bash
# Install with Helm
helm install ai-service anya/ai \
  --set model.name=gpt-4 \
  --set replicaCount=3 \
  --set resources.limits.nvidia.com/gpu=1
```

### Scaling

#### Horizontal Pod Autoscaling

```yaml
# hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: ai-service
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: ai-service
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: External
    external:
      metric:
        name: requests_per_second
        selector:
          matchLabels:
            app: ai-service
      target:
        type: AverageValue
        averageValue: 1000
```

#### GPU Sharing

```yaml
# Enable GPU sharing with time-slicing
apiVersion: v1
kind: ConfigMap
metadata:
  name: time-slicing-config
data:
  anya-ai: |
    version: v1
    sharing:
      timeSlicing:
        resources:
        - name: nvidia.com/gpu
          replicas: 4  # Share 1 GPU among 4 models
```

### Monitoring

#### Built-in Metrics

Anya Core exposes the following metrics:

- `anya_ai_requests_total` - Total number of requests
- `anya_ai_request_duration_seconds` - Request duration histogram
- `anya_ai_model_load_count` - Model load events
- `anya_ai_batch_size` - Current batch size
- `anya_ai_queue_size` - Current queue size

#### Grafana Dashboard

Import the following dashboard for monitoring:

```json
{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": 13639,
  "graphTooltip": 0,
  "id": 2,
  "links": [],
  "panels": [
    /* Dashboard panels configuration */
  ],
  "schemaVersion": 27,
  "style": "dark",
  "tags": ["ai", "monitoring"],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "Anya AI Monitoring",
  "uid": "anya-ai-dashboard",
  "version": 1
}
```

### Canary Deployments

Gradually roll out new model versions:

```yaml
# canary-deployment.yaml
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
  name: gpt-4
  namespace: ai
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: gpt-4
  service:
    port: 8000
  analysis:
    interval: 1m
    threshold: 5
    stepWeight: 10
    metrics:
    - name: request-success-rate
      thresholdRange:
        min: 99
      interval: 1m
    - name: request-duration
      thresholdRange:
        max: 500
      interval: 1m
```

### A/B Testing

Run experiments with different model versions:

```python
from anya_ai.experiment import ABTest

# Define experiment
experiment = ABTest(
    name="model-comparison",
    variants=[
        {"name": "gpt-4", "weight": 70, "config": {"version": "1.0.0"}},
        {"name": "llama-2", "weight": 30, "config": {"model": "llama-2-70b"}}
    ],
    metrics=["accuracy", "latency"]
)

# Get variant for user
variant = experiment.get_variant(user_id="user123")
# Use variant config for inference
result = model.predict(..., **variant.config)
# Log metrics
experiment.log_metric("latency", variant.name, 0.15)
```

### Best Practices

1. **Resource Management**
   - Set appropriate CPU/Memory limits
   - Use GPU for compute-intensive models
   - Implement request timeouts

2. **Reliability**
   - Use retries with exponential backoff
   - Implement circuit breakers
   - Set up health checks

3. **Performance**
   - Enable request batching
   - Use model warmup
   - Implement caching for frequent requests

4. **Security**
   - Enable authentication
   - Validate input data
   - Monitor for abuse

For more advanced deployment strategies, see the [Model Serving Best Practices](#model-serving-best-practices) section below.

## API Reference

### REST API

#### Authentication

All API endpoints require authentication. Include your API key in the `Authorization` header:

```http
Authorization: Bearer YOUR_API_KEY
```

#### Base URL

```
https://api.anya.ai/v1
```

### Endpoints

#### 1. Model Inference

**POST** `/models/{model_id}/predict`

Make predictions using a specific model.

**Request**

```json
{
  "inputs": ["Hello, world!"],
  "parameters": {
    "max_length": 100,
    "temperature": 0.7
  }
}
```

**Response**

```json
{
  "outputs": ["Generated text response"],
  "model": "gpt-4",
  "model_version": "1.0.0",
  "metadata": {
    "tokens_generated": 20,
    "tokens_processed": 5,
    "latency_ms": 150
  }
}
```

#### 2. Model Information

**GET** `/models/{model_id}`

Get information about a specific model.

**Response**

```json
{
  "id": "gpt-4",
  "version": "1.0.0",
  "framework": "pytorch",
  "input_schema": {
    "type": "string",
    "max_length": 2048
  },
  "output_schema": {
    "type": "string"
  },
  "parameters": {
    "max_length": {
      "type": "integer",
      "default": 100,
      "min": 1,
      "max": 4096
    },
    "temperature": {
      "type": "float",
      "default": 0.7,
      "min": 0.0,
      "max": 2.0
    }
  },
  "created_at": "2025-01-01T00:00:00Z",
  "updated_at": "2025-01-01T00:00:00Z"
}
```

### Error Handling

All API errors follow the same format:

```json
{
  "error": {
    "code": "invalid_request",
    "message": "The request was invalid",
    "details": {
      "field": "temperature",
      "issue": "Value must be between 0 and 2"
    }
  }
}
```

#### Common Error Codes

| Status Code | Error Code           | Description                          |
|-------------|----------------------|--------------------------------------|
| 400         | `invalid_request`    | The request was invalid              |
| 401         | `unauthorized`       | Authentication failed                |
| 403         | `forbidden`          | Insufficient permissions             |
| 404         | `not_found`          | The requested resource was not found |
| 429         | `rate_limit_exceeded`| Rate limit exceeded                  |
| 500         | `internal_error`     | An internal server error occurred    |
| 503         | `service_unavailable`| Service is currently unavailable     |

### Rate Limiting

- **Free Tier**: 100 requests per minute
- **Pro Tier**: 1,000 requests per minute
- **Enterprise**: Custom limits available

Rate limit headers are included in all responses:

```
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 97
X-RateLimit-Reset: 1625097600
```

For a complete reference of all available endpoints, see the [API Reference](./API.md).

## Best Practices

### 1. Model Versioning

- Use semantic versioning for models
- Keep backward compatibility when possible
- Document breaking changes

### 2. Error Handling

- Use custom error types
- Provide meaningful error messages
- Log errors with context

### 3. Performance

- Use batching for inference
- Implement model quantization
- Profile and optimize critical paths

### 4. Security

- Validate all inputs
- Sanitize outputs
- Implement rate limiting

## Troubleshooting

### Common Issues

1. **Model Loading Fails**
   - Check model file paths
   - Verify model format
   - Check file permissions

2. **Inference Performance Issues**
   - Check batch size
   - Profile GPU/CPU usage
   - Check for memory leaks

3. **Training Diverges**
   - Check learning rate
   - Verify data preprocessing
   - Monitor loss curves

### Debugging

Enable debug logging:

```rust
use tracing_subscriber;

fn init_logging() {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
}
```

## Examples

### Text Generation

```rust
use anya_ai::{Model, TextGenerator};

let model = TextGenerator::load("gpt-4").await?;
let prompt = "Once upon a time";
let result = model.generate(prompt, 100).await?;
println!("Generated: {}", result);
```

### Image Classification

```rust
use anya_ai::{Model, ImageClassifier};

let model = ImageClassifier::load("resnet50").await?;
let image = load_image("cat.jpg")?;
let class = model.classify(&image).await?;
println!("Predicted class: {}", class);
```

## Security Considerations

### 1. Input Validation

- Validate all inputs
- Sanitize user-provided data
- Implement input length limits

### 2. Model Security

- Sign model files
- Verify model hashes
- Use secure model storage

### 3. API Security

- Authenticate API requests
- Implement rate limiting
- Use HTTPS

### 4. Data Privacy

- Anonymize training data
- Implement data access controls
- Log data access

## Conclusion

This guide covers the essentials of AI integration in Anya Core. For more advanced usage, refer to the [API documentation](https://docs.anya.org/api/ai) and [examples](https://github.com/anya-org/anya-core/examples).

## See Also

- [Related Document]#related-document