@hazeljs/ml 0.2.0-beta.37 → 0.2.0-beta.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +185 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# @hazeljs/ml
|
|
2
|
+
|
|
3
|
+
Machine Learning & Model Management for HazelJS - training, prediction, model registry, and metrics.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@hazeljs/ml)
|
|
6
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Model registry** – Register and discover models by name and version
|
|
11
|
+
- **Decorators** – `@Model`, `@Train`, `@Predict` for declarative ML APIs
|
|
12
|
+
- **Training pipeline** – PipelineService for data preprocessing (normalize, filter)
|
|
13
|
+
- **Inference** – PredictorService for single and batch predictions
|
|
14
|
+
- **Metrics** – MetricsService for evaluation, A/B testing, and monitoring
|
|
15
|
+
- **Framework-agnostic** – Works with TensorFlow.js, ONNX, Transformers.js, or custom backends
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @hazeljs/ml @hazeljs/core
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Peer dependencies (optional, per use case)
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# TensorFlow.js
|
|
27
|
+
npm install @tensorflow/tfjs-node
|
|
28
|
+
|
|
29
|
+
# ONNX Runtime
|
|
30
|
+
npm install onnxruntime-node
|
|
31
|
+
|
|
32
|
+
# Hugging Face Transformers (embeddings, sentiment)
|
|
33
|
+
npm install @huggingface/transformers
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
### 1. Import MLModule
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { HazelApp } from '@hazeljs/core';
|
|
42
|
+
import { MLModule } from '@hazeljs/ml';
|
|
43
|
+
|
|
44
|
+
const app = new HazelApp({
|
|
45
|
+
imports: [
|
|
46
|
+
MLModule.forRoot({
|
|
47
|
+
models: [SentimentClassifier, SpamClassifier],
|
|
48
|
+
}),
|
|
49
|
+
],
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
app.listen(3000);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 2. Define a model with decorators
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Injectable } from '@hazeljs/core';
|
|
59
|
+
import { Model, Train, Predict, ModelRegistry } from '@hazeljs/ml';
|
|
60
|
+
|
|
61
|
+
@Model({ name: 'sentiment-classifier', version: '1.0.0', framework: 'custom' })
|
|
62
|
+
@Injectable()
|
|
63
|
+
export class SentimentClassifier {
|
|
64
|
+
private labels = ['positive', 'negative', 'neutral'];
|
|
65
|
+
private weights: Record<string, number[]> = {};
|
|
66
|
+
|
|
67
|
+
constructor(private registry: ModelRegistry) {}
|
|
68
|
+
|
|
69
|
+
@Train()
|
|
70
|
+
async train(data: { text: string; label: string }[]): Promise<void> {
|
|
71
|
+
// Your training logic – e.g. bag-of-words, embeddings, etc.
|
|
72
|
+
const vocab = this.buildVocabulary(data);
|
|
73
|
+
this.weights = this.computeWeights(data, vocab);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Predict()
|
|
77
|
+
async predict(input: { text: string }): Promise<{ sentiment: string; confidence: number }> {
|
|
78
|
+
const scores = this.score(input.text);
|
|
79
|
+
const idx = scores.indexOf(Math.max(...scores));
|
|
80
|
+
return {
|
|
81
|
+
sentiment: this.labels[idx],
|
|
82
|
+
confidence: scores[idx],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 3. Predict from a controller or service
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { Controller, Post, Body, Inject } from '@hazeljs/core';
|
|
92
|
+
import { PredictorService } from '@hazeljs/ml';
|
|
93
|
+
|
|
94
|
+
@Controller('ml')
|
|
95
|
+
export class MLController {
|
|
96
|
+
constructor(private predictor: PredictorService) {}
|
|
97
|
+
|
|
98
|
+
@Post('predict')
|
|
99
|
+
async predict(@Body() body: { text: string; model?: string }) {
|
|
100
|
+
const result = await this.predictor.predict(
|
|
101
|
+
body.model ?? 'sentiment-classifier',
|
|
102
|
+
body
|
|
103
|
+
);
|
|
104
|
+
return { result };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Model registration
|
|
110
|
+
|
|
111
|
+
Models are registered when passed to `MLModule.forRoot({ models: [...] })`. The bootstrap discovers `@Train` and `@Predict` methods via reflection.
|
|
112
|
+
|
|
113
|
+
### Manual registration
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { registerMLModel, ModelRegistry, TrainerService, PredictorService } from '@hazeljs/ml';
|
|
117
|
+
|
|
118
|
+
// When injecting ModelRegistry in a custom service:
|
|
119
|
+
registerMLModel(
|
|
120
|
+
sentimentInstance,
|
|
121
|
+
modelRegistry,
|
|
122
|
+
trainerService,
|
|
123
|
+
predictorService
|
|
124
|
+
);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Training pipeline
|
|
128
|
+
|
|
129
|
+
Preprocess data before training with `PipelineService`:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { PipelineService } from '@hazeljs/ml';
|
|
133
|
+
|
|
134
|
+
const pipeline = new PipelineService();
|
|
135
|
+
const steps = [
|
|
136
|
+
{ name: 'normalize', fn: (d: { text: string }) => ({ ...d, text: d.text.toLowerCase() }) },
|
|
137
|
+
{ name: 'filter', fn: (d: { text: string }) => d.text.length > 0 },
|
|
138
|
+
];
|
|
139
|
+
const processed = await pipeline.run(data, steps);
|
|
140
|
+
await model.train(processed);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Batch predictions
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import { BatchService } from '@hazeljs/ml';
|
|
147
|
+
|
|
148
|
+
const batchService = new BatchService(predictorService);
|
|
149
|
+
const results = await batchService.predictBatch('sentiment-classifier', items, {
|
|
150
|
+
batchSize: 32,
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Metrics and evaluation
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { MetricsService } from '@hazeljs/ml';
|
|
158
|
+
|
|
159
|
+
const metricsService = new MetricsService();
|
|
160
|
+
const evaluation = await metricsService.evaluate(modelName, testData, {
|
|
161
|
+
metrics: ['accuracy', 'f1', 'precision', 'recall'],
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## API summary
|
|
166
|
+
|
|
167
|
+
| Service | Purpose |
|
|
168
|
+
|---------|---------|
|
|
169
|
+
| `ModelRegistry` | Register and lookup models by name/version |
|
|
170
|
+
| `TrainerService` | Discover and invoke `@Train` methods |
|
|
171
|
+
| `PredictorService` | Discover and invoke `@Predict` methods |
|
|
172
|
+
| `PipelineService` | Data preprocessing pipeline |
|
|
173
|
+
| `BatchService` | Batch prediction with configurable batch size |
|
|
174
|
+
| `MetricsService` | Model evaluation and metrics tracking |
|
|
175
|
+
|
|
176
|
+
## Example
|
|
177
|
+
|
|
178
|
+
See [hazeljs-ml-starter](../../../hazeljs-ml-starter) for a full example with sentiment, spam, intent classifiers, REST API, training pipeline, and metrics.
|
|
179
|
+
|
|
180
|
+
## Links
|
|
181
|
+
|
|
182
|
+
- [Documentation](https://hazeljs.com/docs/packages/ml)
|
|
183
|
+
- [GitHub](https://github.com/hazel-js/hazeljs)
|
|
184
|
+
- [Issues](https://github.com/hazeljs/hazel-js/issues)
|
|
185
|
+
- [Homepage](https://hazeljs.com)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hazeljs/ml",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.39",
|
|
4
4
|
"description": "Machine Learning & Model Management for HazelJS framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"url": "https://github.com/hazeljs/hazel-js/issues"
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://hazeljs.com",
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "1ef15653fe25b11327020085fdfdd6d426b45763"
|
|
52
52
|
}
|