@soulcraft/brainy 6.5.0 → 6.6.1
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/assets/models/all-MiniLM-L6-v2-q8/config.json +25 -0
- package/assets/models/all-MiniLM-L6-v2-q8/model.onnx +0 -0
- package/assets/models/all-MiniLM-L6-v2-q8/tokenizer.json +30686 -0
- package/assets/models/all-MiniLM-L6-v2-q8/vocab.json +1 -0
- package/dist/brainy.js +0 -6
- package/dist/config/index.d.ts +1 -3
- package/dist/config/index.js +2 -4
- package/dist/config/modelAutoConfig.d.ts +10 -17
- package/dist/config/modelAutoConfig.js +15 -88
- package/dist/config/sharedConfigManager.d.ts +1 -2
- package/dist/config/zeroConfig.d.ts +2 -13
- package/dist/config/zeroConfig.js +7 -15
- package/dist/critical/model-guardian.d.ts +5 -22
- package/dist/critical/model-guardian.js +38 -210
- package/dist/embeddings/EmbeddingManager.d.ts +7 -17
- package/dist/embeddings/EmbeddingManager.js +28 -136
- package/dist/embeddings/wasm/AssetLoader.d.ts +67 -0
- package/dist/embeddings/wasm/AssetLoader.js +238 -0
- package/dist/embeddings/wasm/EmbeddingPostProcessor.d.ts +60 -0
- package/dist/embeddings/wasm/EmbeddingPostProcessor.js +123 -0
- package/dist/embeddings/wasm/ONNXInferenceEngine.d.ts +55 -0
- package/dist/embeddings/wasm/ONNXInferenceEngine.js +154 -0
- package/dist/embeddings/wasm/WASMEmbeddingEngine.d.ts +82 -0
- package/dist/embeddings/wasm/WASMEmbeddingEngine.js +231 -0
- package/dist/embeddings/wasm/WordPieceTokenizer.d.ts +71 -0
- package/dist/embeddings/wasm/WordPieceTokenizer.js +264 -0
- package/dist/embeddings/wasm/index.d.ts +13 -0
- package/dist/embeddings/wasm/index.js +15 -0
- package/dist/embeddings/wasm/types.d.ts +114 -0
- package/dist/embeddings/wasm/types.js +25 -0
- package/dist/setup.d.ts +11 -11
- package/dist/setup.js +17 -31
- package/dist/types/brainy.types.d.ts +0 -5
- package/dist/utils/embedding.d.ts +45 -62
- package/dist/utils/embedding.js +61 -440
- package/package.json +10 -3
- package/scripts/download-model.cjs +175 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for WASM Embedding Engine
|
|
3
|
+
*
|
|
4
|
+
* Clean, production-grade types for direct ONNX WASM embeddings.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Tokenizer configuration for WordPiece
|
|
8
|
+
*/
|
|
9
|
+
export interface TokenizerConfig {
|
|
10
|
+
/** Vocabulary mapping word → token ID */
|
|
11
|
+
vocab: Map<string, number>;
|
|
12
|
+
/** [UNK] token ID (100 for BERT-based models) */
|
|
13
|
+
unkTokenId: number;
|
|
14
|
+
/** [CLS] token ID (101 for BERT-based models) */
|
|
15
|
+
clsTokenId: number;
|
|
16
|
+
/** [SEP] token ID (102 for BERT-based models) */
|
|
17
|
+
sepTokenId: number;
|
|
18
|
+
/** [PAD] token ID (0 for BERT-based models) */
|
|
19
|
+
padTokenId: number;
|
|
20
|
+
/** Maximum sequence length (512 for all-MiniLM-L6-v2) */
|
|
21
|
+
maxLength: number;
|
|
22
|
+
/** Whether to lowercase input (true for uncased models) */
|
|
23
|
+
doLowerCase: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Result of tokenization
|
|
27
|
+
*/
|
|
28
|
+
export interface TokenizedInput {
|
|
29
|
+
/** Token IDs including [CLS] and [SEP] */
|
|
30
|
+
inputIds: number[];
|
|
31
|
+
/** Attention mask (1 for real tokens, 0 for padding) */
|
|
32
|
+
attentionMask: number[];
|
|
33
|
+
/** Token type IDs (all 0 for single sentence) */
|
|
34
|
+
tokenTypeIds: number[];
|
|
35
|
+
/** Number of tokens (excluding special tokens) */
|
|
36
|
+
tokenCount: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* ONNX inference engine configuration
|
|
40
|
+
*/
|
|
41
|
+
export interface InferenceConfig {
|
|
42
|
+
/** Path to ONNX model file */
|
|
43
|
+
modelPath: string;
|
|
44
|
+
/** Path to WASM files directory */
|
|
45
|
+
wasmPath?: string;
|
|
46
|
+
/** Number of threads (1 for universal compatibility) */
|
|
47
|
+
numThreads: number;
|
|
48
|
+
/** Enable SIMD if available */
|
|
49
|
+
enableSimd: boolean;
|
|
50
|
+
/** Enable CPU memory arena (false for memory efficiency) */
|
|
51
|
+
enableCpuMemArena: boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Embedding result with metadata
|
|
55
|
+
*/
|
|
56
|
+
export interface EmbeddingResult {
|
|
57
|
+
/** 384-dimensional embedding vector */
|
|
58
|
+
embedding: number[];
|
|
59
|
+
/** Number of tokens processed */
|
|
60
|
+
tokenCount: number;
|
|
61
|
+
/** Processing time in milliseconds */
|
|
62
|
+
processingTimeMs: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Engine statistics
|
|
66
|
+
*/
|
|
67
|
+
export interface EngineStats {
|
|
68
|
+
/** Whether the engine is initialized */
|
|
69
|
+
initialized: boolean;
|
|
70
|
+
/** Total number of embeddings generated */
|
|
71
|
+
embedCount: number;
|
|
72
|
+
/** Total processing time in milliseconds */
|
|
73
|
+
totalProcessingTimeMs: number;
|
|
74
|
+
/** Average processing time per embedding */
|
|
75
|
+
avgProcessingTimeMs: number;
|
|
76
|
+
/** Model name */
|
|
77
|
+
modelName: string;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Model configuration (from config.json)
|
|
81
|
+
*/
|
|
82
|
+
export interface ModelConfig {
|
|
83
|
+
/** Model architecture type */
|
|
84
|
+
architectures: string[];
|
|
85
|
+
/** Hidden size (384 for all-MiniLM-L6-v2) */
|
|
86
|
+
hidden_size: number;
|
|
87
|
+
/** Number of attention heads */
|
|
88
|
+
num_attention_heads: number;
|
|
89
|
+
/** Number of hidden layers */
|
|
90
|
+
num_hidden_layers: number;
|
|
91
|
+
/** Vocabulary size */
|
|
92
|
+
vocab_size: number;
|
|
93
|
+
/** Maximum position embeddings */
|
|
94
|
+
max_position_embeddings: number;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Special token IDs for BERT-based models
|
|
98
|
+
*/
|
|
99
|
+
export declare const SPECIAL_TOKENS: {
|
|
100
|
+
readonly PAD: 0;
|
|
101
|
+
readonly UNK: 100;
|
|
102
|
+
readonly CLS: 101;
|
|
103
|
+
readonly SEP: 102;
|
|
104
|
+
readonly MASK: 103;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Model constants for all-MiniLM-L6-v2
|
|
108
|
+
*/
|
|
109
|
+
export declare const MODEL_CONSTANTS: {
|
|
110
|
+
readonly HIDDEN_SIZE: 384;
|
|
111
|
+
readonly MAX_SEQUENCE_LENGTH: 512;
|
|
112
|
+
readonly VOCAB_SIZE: 30522;
|
|
113
|
+
readonly MODEL_NAME: "all-MiniLM-L6-v2";
|
|
114
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for WASM Embedding Engine
|
|
3
|
+
*
|
|
4
|
+
* Clean, production-grade types for direct ONNX WASM embeddings.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Special token IDs for BERT-based models
|
|
8
|
+
*/
|
|
9
|
+
export const SPECIAL_TOKENS = {
|
|
10
|
+
PAD: 0,
|
|
11
|
+
UNK: 100,
|
|
12
|
+
CLS: 101,
|
|
13
|
+
SEP: 102,
|
|
14
|
+
MASK: 103,
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Model constants for all-MiniLM-L6-v2
|
|
18
|
+
*/
|
|
19
|
+
export const MODEL_CONSTANTS = {
|
|
20
|
+
HIDDEN_SIZE: 384,
|
|
21
|
+
MAX_SEQUENCE_LENGTH: 512,
|
|
22
|
+
VOCAB_SIZE: 30522,
|
|
23
|
+
MODEL_NAME: 'all-MiniLM-L6-v2',
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=types.js.map
|
package/dist/setup.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* for Node.js compatibility before any other library code runs.
|
|
2
|
+
* Brainy Setup - Minimal Polyfills
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* ARCHITECTURE (v7.0.0):
|
|
5
|
+
* Brainy uses direct ONNX WASM for embeddings.
|
|
6
|
+
* No transformers.js dependency, no hacks required.
|
|
8
7
|
*
|
|
9
|
-
* This file
|
|
10
|
-
*
|
|
11
|
-
* result in errors like "TextEncoder is not a constructor" when the package
|
|
12
|
-
* is used in Node.js environments.
|
|
8
|
+
* This file provides minimal polyfills for cross-environment compatibility:
|
|
9
|
+
* - TextEncoder/TextDecoder for older environments
|
|
13
10
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
11
|
+
* BENEFITS:
|
|
12
|
+
* - Clean codebase with no workarounds
|
|
13
|
+
* - Works everywhere: Node.js, Bun, Bun --compile, browsers, Deno
|
|
14
|
+
* - No platform-specific binaries
|
|
15
|
+
* - Model bundled in package (no runtime downloads)
|
|
16
16
|
*/
|
|
17
17
|
export {};
|
package/dist/setup.js
CHANGED
|
@@ -1,45 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* for Node.js compatibility before any other library code runs.
|
|
2
|
+
* Brainy Setup - Minimal Polyfills
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* ARCHITECTURE (v7.0.0):
|
|
5
|
+
* Brainy uses direct ONNX WASM for embeddings.
|
|
6
|
+
* No transformers.js dependency, no hacks required.
|
|
8
7
|
*
|
|
9
|
-
* This file
|
|
10
|
-
*
|
|
11
|
-
* result in errors like "TextEncoder is not a constructor" when the package
|
|
12
|
-
* is used in Node.js environments.
|
|
8
|
+
* This file provides minimal polyfills for cross-environment compatibility:
|
|
9
|
+
* - TextEncoder/TextDecoder for older environments
|
|
13
10
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
11
|
+
* BENEFITS:
|
|
12
|
+
* - Clean codebase with no workarounds
|
|
13
|
+
* - Works everywhere: Node.js, Bun, Bun --compile, browsers, Deno
|
|
14
|
+
* - No platform-specific binaries
|
|
15
|
+
* - Model bundled in package (no runtime downloads)
|
|
16
16
|
*/
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (typeof global !== 'undefined')
|
|
22
|
-
return global;
|
|
23
|
-
if (typeof self !== 'undefined')
|
|
24
|
-
return self;
|
|
25
|
-
return null; // No global object available
|
|
26
|
-
})();
|
|
27
|
-
// Define TextEncoder and TextDecoder globally to make sure they're available
|
|
28
|
-
// Now works across all environments: Node.js, serverless, and other server environments
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// TextEncoder/TextDecoder Polyfills
|
|
19
|
+
// ============================================================================
|
|
20
|
+
const globalObj = globalThis ?? global ?? self;
|
|
29
21
|
if (globalObj) {
|
|
30
|
-
if (!globalObj.TextEncoder)
|
|
22
|
+
if (!globalObj.TextEncoder)
|
|
31
23
|
globalObj.TextEncoder = TextEncoder;
|
|
32
|
-
|
|
33
|
-
if (!globalObj.TextDecoder) {
|
|
24
|
+
if (!globalObj.TextDecoder)
|
|
34
25
|
globalObj.TextDecoder = TextDecoder;
|
|
35
|
-
}
|
|
36
|
-
// Create special global constructors for library compatibility
|
|
37
26
|
globalObj.__TextEncoder__ = TextEncoder;
|
|
38
27
|
globalObj.__TextDecoder__ = TextDecoder;
|
|
39
28
|
}
|
|
40
|
-
// Also import normally for ES modules environments
|
|
41
29
|
import { applyTensorFlowPatch } from './utils/textEncoding.js';
|
|
42
|
-
// Apply the TextEncoder/TextDecoder compatibility patch
|
|
43
30
|
applyTensorFlowPatch();
|
|
44
|
-
console.log('Applied TextEncoder/TextDecoder patch via ES modules in setup.ts');
|
|
45
31
|
//# sourceMappingURL=setup.js.map
|
|
@@ -518,11 +518,6 @@ export interface BrainyConfig {
|
|
|
518
518
|
options?: any;
|
|
519
519
|
branch?: string;
|
|
520
520
|
};
|
|
521
|
-
model?: {
|
|
522
|
-
type: 'fast' | 'accurate' | 'balanced' | 'custom';
|
|
523
|
-
name?: string;
|
|
524
|
-
precision?: 'q8';
|
|
525
|
-
};
|
|
526
521
|
index?: {
|
|
527
522
|
m?: number;
|
|
528
523
|
efConstruction?: number;
|
|
@@ -1,60 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Embedding functions for converting data to vectors
|
|
3
|
-
*
|
|
2
|
+
* Embedding functions for converting data to vectors
|
|
3
|
+
*
|
|
4
|
+
* Uses direct ONNX WASM for universal compatibility.
|
|
5
|
+
* No transformers.js dependency - clean, production-grade implementation.
|
|
4
6
|
*/
|
|
5
7
|
import { EmbeddingFunction, EmbeddingModel, Vector } from '../coreTypes.js';
|
|
6
8
|
/**
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
export declare function detectBestDevice(): Promise<'cpu' | 'webgpu' | 'cuda'>;
|
|
10
|
-
/**
|
|
11
|
-
* Resolve device string to actual device configuration
|
|
12
|
-
*/
|
|
13
|
-
export declare function resolveDevice(device?: string): Promise<string>;
|
|
14
|
-
/**
|
|
15
|
-
* Transformers.js Sentence Encoder embedding model
|
|
16
|
-
* Uses ONNX Runtime for fast, offline embeddings with smaller models
|
|
17
|
-
* Default model: all-MiniLM-L6-v2 (384 dimensions, ~90MB)
|
|
9
|
+
* TransformerEmbedding options (kept for backward compatibility)
|
|
18
10
|
*/
|
|
19
11
|
export interface TransformerEmbeddingOptions {
|
|
20
|
-
/** Model name
|
|
12
|
+
/** Model name - only all-MiniLM-L6-v2 is supported */
|
|
21
13
|
model?: string;
|
|
22
14
|
/** Whether to enable verbose logging */
|
|
23
15
|
verbose?: boolean;
|
|
24
|
-
/** Custom cache directory
|
|
16
|
+
/** Custom cache directory - ignored (model is bundled) */
|
|
25
17
|
cacheDir?: string;
|
|
26
|
-
/** Force local files only (
|
|
18
|
+
/** Force local files only - ignored (model is bundled) */
|
|
27
19
|
localFilesOnly?: boolean;
|
|
28
|
-
/** Model precision
|
|
20
|
+
/** Model precision - always q8 */
|
|
29
21
|
precision?: 'fp32' | 'q8';
|
|
30
|
-
/** Device
|
|
22
|
+
/** Device - always WASM */
|
|
31
23
|
device?: 'auto' | 'cpu' | 'webgpu' | 'cuda' | 'gpu';
|
|
32
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* TransformerEmbedding - Sentence embeddings using WASM ONNX
|
|
27
|
+
*
|
|
28
|
+
* This class delegates all work to EmbeddingManager which uses
|
|
29
|
+
* the direct ONNX WASM engine. Kept for backward compatibility.
|
|
30
|
+
*/
|
|
33
31
|
export declare class TransformerEmbedding implements EmbeddingModel {
|
|
34
|
-
private extractor;
|
|
35
32
|
private initialized;
|
|
36
33
|
private verbose;
|
|
37
|
-
private options;
|
|
38
|
-
/**
|
|
39
|
-
* Create a new TransformerEmbedding instance
|
|
40
|
-
*/
|
|
41
34
|
constructor(options?: TransformerEmbeddingOptions);
|
|
42
|
-
/**
|
|
43
|
-
* Get the default cache directory for models
|
|
44
|
-
*/
|
|
45
|
-
private getDefaultCacheDir;
|
|
46
|
-
/**
|
|
47
|
-
* Check if we're running in a test environment
|
|
48
|
-
*/
|
|
49
|
-
private isTestEnvironment;
|
|
50
|
-
/**
|
|
51
|
-
* Log message only if verbose mode is enabled
|
|
52
|
-
*/
|
|
53
|
-
private logger;
|
|
54
|
-
/**
|
|
55
|
-
* Generate mock embeddings for unit tests
|
|
56
|
-
*/
|
|
57
|
-
private getMockEmbedding;
|
|
58
35
|
/**
|
|
59
36
|
* Initialize the embedding model
|
|
60
37
|
*/
|
|
@@ -64,45 +41,51 @@ export declare class TransformerEmbedding implements EmbeddingModel {
|
|
|
64
41
|
*/
|
|
65
42
|
embed(data: string | string[]): Promise<Vector>;
|
|
66
43
|
/**
|
|
67
|
-
*
|
|
44
|
+
* Get the embedding function
|
|
68
45
|
*/
|
|
69
|
-
|
|
46
|
+
getEmbeddingFunction(): EmbeddingFunction;
|
|
70
47
|
/**
|
|
71
|
-
*
|
|
48
|
+
* Check if initialized
|
|
72
49
|
*/
|
|
73
|
-
|
|
50
|
+
isInitialized(): boolean;
|
|
74
51
|
/**
|
|
75
|
-
*
|
|
52
|
+
* Dispose resources (no-op for WASM engine)
|
|
76
53
|
*/
|
|
77
|
-
|
|
54
|
+
dispose(): Promise<void>;
|
|
78
55
|
}
|
|
79
|
-
export declare const UniversalSentenceEncoder: typeof TransformerEmbedding;
|
|
80
56
|
/**
|
|
81
|
-
* Create a
|
|
57
|
+
* Create a simple embedding function using the default TransformerEmbedding
|
|
58
|
+
* This is the recommended way to create an embedding function for Brainy
|
|
82
59
|
*/
|
|
83
|
-
export declare function
|
|
60
|
+
export declare function createEmbeddingFunction(options?: TransformerEmbeddingOptions): EmbeddingFunction;
|
|
84
61
|
/**
|
|
85
|
-
*
|
|
86
|
-
|
|
62
|
+
* Create a TransformerEmbedding instance (backward compatibility)
|
|
63
|
+
*/
|
|
64
|
+
export declare function createTransformerEmbedding(options?: TransformerEmbeddingOptions): TransformerEmbedding;
|
|
65
|
+
/**
|
|
66
|
+
* Convenience function to detect best device (always returns 'wasm')
|
|
67
|
+
*/
|
|
68
|
+
export declare function detectBestDevice(): Promise<'cpu' | 'webgpu' | 'cuda' | 'wasm'>;
|
|
69
|
+
/**
|
|
70
|
+
* Resolve device string (always returns 'wasm')
|
|
71
|
+
*/
|
|
72
|
+
export declare function resolveDevice(_device?: string): Promise<string>;
|
|
73
|
+
/**
|
|
74
|
+
* Default embedding function (backward compatibility)
|
|
87
75
|
*/
|
|
88
76
|
export declare const defaultEmbeddingFunction: EmbeddingFunction;
|
|
89
77
|
/**
|
|
90
|
-
*
|
|
91
|
-
* NOTE: Options are validated but the singleton EmbeddingManager is always used
|
|
78
|
+
* UniversalSentenceEncoder alias (backward compatibility)
|
|
92
79
|
*/
|
|
93
|
-
export declare
|
|
80
|
+
export declare const UniversalSentenceEncoder: typeof TransformerEmbedding;
|
|
94
81
|
/**
|
|
95
|
-
* Batch
|
|
82
|
+
* Batch embed function (backward compatibility)
|
|
96
83
|
*/
|
|
97
|
-
export declare function batchEmbed(texts: string[]
|
|
84
|
+
export declare function batchEmbed(texts: string[]): Promise<Vector[]>;
|
|
98
85
|
/**
|
|
99
|
-
* Embedding functions
|
|
86
|
+
* Embedding functions registry (backward compatibility)
|
|
100
87
|
*/
|
|
101
88
|
export declare const embeddingFunctions: {
|
|
102
|
-
|
|
103
|
-
default:
|
|
104
|
-
/** Create custom embedding function */
|
|
105
|
-
create: typeof createEmbeddingFunction;
|
|
106
|
-
/** Batch processing */
|
|
107
|
-
batch: typeof batchEmbed;
|
|
89
|
+
transformer: typeof createEmbeddingFunction;
|
|
90
|
+
default: typeof createEmbeddingFunction;
|
|
108
91
|
};
|