warpvector 0.1.0 → 0.1.2

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/dist/index.d.ts CHANGED
@@ -1,3 +1,45 @@
1
+ import * as _prisma_client_extension from '@prisma/client/extension';
2
+ import * as _prisma_client_runtime_client from '@prisma/client/runtime/client';
3
+ import { Embeddings, EmbeddingsParams } from '@langchain/core/embeddings';
4
+
5
+ type InputVector = number[] | Float32Array;
6
+ type OutputVector = Float32Array | Int8Array | Uint8Array;
7
+ type AdapterState = Record<string, unknown> | string;
8
+ /**
9
+ * WarpVector のすべてのベクトル変換アダプターに共通するインターフェース。
10
+ * 外部の統合ライブラリ(Prisma, LangChainなど)は、このインターフェースを通じて
11
+ * さまざまなアダプター(IntentAdapter, MlpAdapter, WhiteningAdapter など)を
12
+ * 透過的に扱うことができます。
13
+ */
14
+ interface WarpAdapter {
15
+ /**
16
+ * 与えられたベクトルを変換(ワープ)します。
17
+ *
18
+ * @param vector 変換前のベクトル (number[] または Float32Array)
19
+ * @param context オプションのコンテキスト情報 (意図の名前、バージョンなど)
20
+ * @returns 変換後のベクトル (Float32Array, 量子化の場合は Int8Array や Uint8Array)
21
+ */
22
+ tune(vector: InputVector, context?: string): OutputVector;
23
+ /**
24
+ * 複数のベクトルを一括で変換します(オプション実装)
25
+ * WASMやSIMDを使用した最適化処理を提供します。
26
+ *
27
+ * @param vectors 変換前のベクトルの配列
28
+ * @param context オプションのコンテキスト情報
29
+ */
30
+ tuneBatch?(vectors: InputVector[], context?: string): OutputVector[];
31
+ /**
32
+ * 非同期での初期化処理(オプション実装)
33
+ * WASMのロードなどが必要なアダプタで実装します。
34
+ */
35
+ init?(): Promise<void>;
36
+ /**
37
+ * アダプタの状態(学習済み重みなど)をエクスポートします(オプション実装)
38
+ * importState に渡して完全に復元可能なJSON形式を返します。
39
+ */
40
+ exportState?(): AdapterState;
41
+ }
42
+
1
43
  /**
2
44
  * 渡されたベクトルのL2ノルム(ユークリッド距離)を計算し、
3
45
  * 長さが1になるように正規化(normalize)した新しい Float32Array を返します。
@@ -29,6 +71,16 @@ declare function slerp(v1: number[] | Float32Array, v2: number[] | Float32Array,
29
71
  * @throws {Error} ベクトルの次元数が異なる場合にエラーをスローします。
30
72
  */
31
73
  declare function innerProduct(v1: number[] | Float32Array, v2: number[] | Float32Array): number;
74
+ /**
75
+ * target ベクトルに対して、source ベクトルを定数倍したものを足し込みます(インプレース操作)。
76
+ * target += scale * source (BLAS の axpy 相当)
77
+ *
78
+ * @param target 更新対象のベクトル (In-place)
79
+ * @param source 足し込むベクトル
80
+ * @param scale 掛け合わせるスカラー値 (デフォルト: 1.0)
81
+ * @throws {Error} ベクトルの次元数が異なる場合にエラーをスローします。
82
+ */
83
+ declare function addScaledVector(target: Float32Array | number[], source: Float32Array | number[], scale?: number): void;
32
84
  /**
33
85
  * コサイン類似度 (Cosine Similarity)
34
86
  * 2つのベクトル間のコサイン類似度 (-1.0 〜 1.0) を計算します。
@@ -52,14 +104,14 @@ declare function cosineSimilarity(v1: number[] | Float32Array, v2: number[] | Fl
52
104
  declare function reject(baseVector: number[] | Float32Array, negativeVector: number[] | Float32Array): Float32Array;
53
105
  /**
54
106
  * 活性化関数の種類を定義します。
55
- * @typedef {"relu" | "sigmoid" | "tanh"} Activation
107
+ * @typedef {"linear" | "relu" | "sigmoid" | "tanh"} Activation
56
108
  */
57
- type Activation = "relu" | "sigmoid" | "tanh";
109
+ type Activation = "linear" | "relu" | "sigmoid" | "tanh";
58
110
  /**
59
111
  * ベクトルに対して非線形活性化関数を適用します (In-place処理)。
60
112
  *
61
113
  * @param {Float32Array} vector - 活性化関数を適用する対象のベクトル(直接変更されます)
62
- * @param {Activation} [activation] - 適用する活性化関数の種類("relu", "sigmoid", "tanh")
114
+ * @param {Activation} [activation] - 適用する活性化関数の種類("linear", "relu", "sigmoid", "tanh")
63
115
  * @returns {void}
64
116
  */
65
117
  declare function applyActivationToVector(vector: Float32Array, activation?: Activation): void;
@@ -91,6 +143,33 @@ declare function flattenMatrix(matrix: number[][], rows: number, cols: number, c
91
143
  * @throws {Error} 次元数が一致しない場合にエラーをスローします。
92
144
  */
93
145
  declare function assertDimension(vector: number[] | Float32Array, expectedDimension: number, contextName?: string): void;
146
+ /**
147
+ * 変換行列とバイアスを持つオブジェクトから、処理しやすいFloat32Arrayのセットを生成します。
148
+ *
149
+ * @param weights 行列(2Dまたは1D)とバイアスを含むオブジェクト
150
+ * @param dim 次元数
151
+ * @param contextName エラー時のコンテキスト名
152
+ * @returns 1次元化されたFloat32Arrayの行列とバイアス
153
+ */
154
+ declare function getFlatMatrixAndBias(weights: {
155
+ matrix: number[][] | Float32Array;
156
+ bias: number[] | Float32Array;
157
+ }, dim: number, contextName?: string): {
158
+ flatMatrix: Float32Array;
159
+ bias: Float32Array;
160
+ };
161
+ /**
162
+ * ベクトルに対してアフィン変換 (x' = W * x + b) を適用します。
163
+ * 次元削減/拡張 (M x N) にも対応しています。
164
+ *
165
+ * @param matrix 1次元にフラット化された変換行列 (outDim x inDim)
166
+ * @param bias バイアスベクトル (outDim)。省略可能
167
+ * @param vector 変換元の入力ベクトル (inDim)
168
+ * @param result 計算結果を格納する配列 (出力先, outDim)
169
+ * @param inDim 入力ベクトルの次元数
170
+ * @param outDim 出力ベクトルの次元数 (省略時は inDim と同じ)
171
+ */
172
+ declare function applyAffine(matrix: Float32Array, bias: Float32Array | undefined | null, vector: number[] | Float32Array, result: Float32Array, inDim: number, outDim?: number): void;
94
173
 
95
174
  /**
96
175
  * 意図(コンテキスト)ごとの変換情報を定義するインターフェース
@@ -119,7 +198,8 @@ interface IntentWeights {
119
198
  * パフォーマンスのために内部ではすべての配列を Float32Array として扱い、
120
199
  * 大規模なバッチ処理には自動的にWASM/SIMDによる最適化を利用します。
121
200
  */
122
- declare class IntentAdapter {
201
+ declare class IntentAdapter implements WarpAdapter {
202
+ private weightsMap;
123
203
  private readonly dimension;
124
204
  private readonly matrices;
125
205
  private readonly biases;
@@ -149,16 +229,6 @@ declare class IntentAdapter {
149
229
  * @returns {void}
150
230
  */
151
231
  removeIntent(intentName: string): void;
152
- /**
153
- * 行列とバイアスを用いてベクトルにアフィン変換を適用する内部関数 (x' = W * x + b)
154
- *
155
- * @param {Float32Array} matrix - フラット化された変換行列
156
- * @param {Float32Array} bias - バイアスベクトル
157
- * @param {number[] | Float32Array} vector - 変換元の入力ベクトル
158
- * @param {Float32Array} result - 計算結果を格納する配列 (出力先)
159
- * @returns {void}
160
- */
161
- private applyAffine;
162
232
  /**
163
233
  * 複数の意図を指定された重みでブレンドした一時的な行列とバイアスを計算します。
164
234
  * W_blend = Σ(w_i * W_i), b_blend = Σ(w_i * b_i)
@@ -239,6 +309,15 @@ declare class IntentAdapter {
239
309
  * @param {Uint8Array} binary - exportIntentBinary で生成されたバイナリデータ
240
310
  */
241
311
  importIntentBinary(intentName: string, binary: Uint8Array): void;
312
+ /**
313
+ * 現在の IntentAdapter の全状態(全インテント)を JSON としてシリアライズしてエクスポートします。
314
+ * (WarpPipeline 等の統合管理用)
315
+ */
316
+ exportState(): string;
317
+ /**
318
+ * エクスポートされた JSON 状態から IntentAdapter を復元します。
319
+ */
320
+ static importState(stateJson: string): IntentAdapter;
242
321
  }
243
322
 
244
323
  /**
@@ -266,7 +345,7 @@ interface LoraIntentWeights {
266
345
  * LoraIntentAdapter クラス
267
346
  * 低ランク行列(A, B)を使用して高次元ベクトルのアフィン変換をメモリ効率良く行います。
268
347
  */
269
- declare class LoraIntentAdapter {
348
+ declare class LoraIntentAdapter implements WarpAdapter {
270
349
  private readonly dimension;
271
350
  private readonly rank;
272
351
  private readonly matricesA;
@@ -310,8 +389,47 @@ declare class LoraIntentAdapter {
310
389
  * @throws {Error} ベクトルの次元数が一致しない、または指定された意図が存在しない場合にエラーをスローします。
311
390
  */
312
391
  tune(baseVector: number[] | Float32Array, intent: string): Float32Array;
392
+ /**
393
+ * 現在の LoraIntentAdapter の全状態を JSON としてエクスポートします。
394
+ */
395
+ exportState(): string;
396
+ /**
397
+ * エクスポートされた JSON 状態から LoraIntentAdapter を復元します。
398
+ */
399
+ static importState(stateJson: string): LoraIntentAdapter;
313
400
  }
314
401
 
402
+ interface RankedResult {
403
+ id: string | number;
404
+ score?: number;
405
+ rank?: number;
406
+ metadata?: Record<string, any>;
407
+ }
408
+ interface FusionResult {
409
+ id: string | number;
410
+ score: number;
411
+ metadata?: Record<string, any>;
412
+ }
413
+ /**
414
+ * Reciprocal Rank Fusion (RRF) を計算し、複数の検索結果リストを統合します。
415
+ * スコアの絶対値に依存せず、各リスト内での「順位」を用いて新しいスコアを算出します。
416
+ *
417
+ * @param resultSets 検索結果リストの配列(例: [denseResults, sparseResults])
418
+ * @param k RRFの平滑化定数(通常は60が使用されます)
419
+ * @returns RRFスコアの降順でソートされた統合結果の配列
420
+ */
421
+ declare function rrf(resultSets: RankedResult[][], k?: number): FusionResult[];
422
+
423
+ /**
424
+ * Relative Score Fusion (RSF) を計算し、複数の検索結果リストを統合します。
425
+ * 各リストのスコアをMin-Max正規化によって [0, 1] にスケーリングし、重み付け加算を行います。
426
+ *
427
+ * @param resultSets 検索結果リストの配列(例: [denseResults, sparseResults])
428
+ * @param weights 各検索結果に対する重み(例: [0.7, 0.3])。省略時は全て1.0
429
+ * @returns 重み付き正規化スコアの降順でソートされた統合結果の配列
430
+ */
431
+ declare function rsf(resultSets: RankedResult[][], weights?: number[]): FusionResult[];
432
+
315
433
  /**
316
434
  * 次元削減/拡張のための射影行列の重みを定義するインターフェース
317
435
  * @interface ProjectionWeights
@@ -333,9 +451,10 @@ interface ProjectionWeights {
333
451
  * ProjectionAdapter クラス
334
452
  * PCAやSVDなどで事前計算された射影行列を用いて、ベクトルの次元削減(または拡張)を行います。
335
453
  */
336
- declare class ProjectionAdapter {
454
+ declare class ProjectionAdapter implements WarpAdapter {
337
455
  private readonly inDimension;
338
456
  private readonly outDimension;
457
+ private wasmInstance;
339
458
  private readonly matrices;
340
459
  private readonly biases;
341
460
  /**
@@ -364,15 +483,394 @@ declare class ProjectionAdapter {
364
483
  */
365
484
  removeProjection(name: string): void;
366
485
  /**
367
- * ベクトルに射影変換を適用し、次元を変更した新しいベクトルを返します。
368
- * 数式: y = W * x
486
+ * ベクトルの次元削減(射影)を実行します。
487
+ * (WarpAdapter の実装として project の代わりに tune を提供します)
369
488
  *
370
- * @param {number[] | Float32Array} baseVector - 変換元の入力ベクトル
371
- * @param {string} projectionName - 適用する射影設定の名前
372
- * @returns {Float32Array} 射影変換適用後の新しいベクトル
373
- * @throws {Error} ベクトルの次元数が inDimension と一致しない場合、または射影設定が存在しない場合にエラーをスローします。
489
+ * @param vector 変換前のベクトル (例: 1536次元)
490
+ * @param version 適用する変換バージョンの識別子 (オプション)
491
+ * @returns 変換後のベクトル (例: 512次元)
492
+ */
493
+ tune(vector: number[] | Float32Array, version?: string): Float32Array;
494
+ /**
495
+ * 現在の射影行列の状態をシリアライズしてエクスポートします。
496
+ */
497
+ exportState(): string;
498
+ /**
499
+ * エクスポートされた状態から ProjectionAdapter を復元します。
500
+ * 注意: 保存されている matrix は既にフラット化された 1D 配列であることを前提としています。
374
501
  */
375
- project(baseVector: number[] | Float32Array, projectionName: string): Float32Array;
502
+ static importState(stateJson: string): ProjectionAdapter;
503
+ }
504
+
505
+ /**
506
+ * MLPの1層を定義するインターフェース
507
+ */
508
+ interface MlpLayer {
509
+ /** 変換行列 W */
510
+ matrix: number[][] | Float32Array;
511
+ /** バイアス b */
512
+ bias: number[] | Float32Array;
513
+ /** 活性化関数 */
514
+ activation: Activation;
515
+ }
516
+ /**
517
+ * MlpAdapter は WASM を使用して超高速に非線形な多層推論を行うラッパーです。
518
+ *
519
+ * @example
520
+ * const mlp = new MlpAdapter([{ inputDim: 1536, outputDim: 128, activation: "relu" }]);
521
+ * const output = mlp.tune(inputVector);
522
+ */
523
+ declare class MlpAdapter implements WarpAdapter {
524
+ private layers;
525
+ private wasmInstance;
526
+ private isWasmReady;
527
+ private inputDim;
528
+ private outputDim;
529
+ private numLayers;
530
+ private inputPtr;
531
+ private outputPtr;
532
+ private weightsPtr;
533
+ private layerDimsPtr;
534
+ private activationsPtr;
535
+ private bufferPtr;
536
+ private bufBPtr;
537
+ constructor(layers: MlpLayer[]);
538
+ /**
539
+ * WASMの初期化と、MLP構造をWASMメモリに書き込む準備を行います。
540
+ * インスタンス作成後に必ず呼び出してください。
541
+ */
542
+ init(): Promise<void>;
543
+ /**
544
+ * ニューラルネットワークの順伝播を実行し、結果を返します。
545
+ * (WarpAdapter の実装として、predict の代わりに tune を提供します)
546
+ *
547
+ * @param input 入力ベクトル
548
+ * @returns 推論結果ベクトル
549
+ */
550
+ tune(input: number[] | Float32Array): Float32Array;
551
+ /**
552
+ * 現在のMLP構造と重みをシリアライズして出力します。
553
+ */
554
+ exportState(): string;
555
+ /**
556
+ * シリアライズされた状態から MlpAdapter を復元します。
557
+ * 注意: 復元後、再度 `await init()` を呼び出してWASMメモリを初期化する必要があります。
558
+ */
559
+ static importState(stateJson: string): MlpAdapter;
560
+ }
561
+
562
+ interface WhiteningConfig {
563
+ /**
564
+ * 学習率 (0.0 ~ 1.0)
565
+ * 平均と主成分を更新する際の指数移動平均の重み。
566
+ * デフォルトは 0.01
567
+ */
568
+ learningRate?: number;
569
+ /**
570
+ * 除去するトップ主成分(PC)の数。
571
+ * 通常、事前学習モデルのコーン(偏り)問題を取り除くには、
572
+ * 上位 1〜2 個の主成分を除去する(All-but-the-Top)だけで十分な効果があります。
573
+ * デフォルトは 1
574
+ */
575
+ numComponents?: number;
576
+ }
577
+ /**
578
+ * WhiteningAdapter は、ストリーミングデータに対して
579
+ * Oja's Rule によるオンラインPCAを実行し、
580
+ * ベクトル空間の等方化(Whitening / Anisotropy Reduction)を行うアダプターです。
581
+ */
582
+ declare class WhiteningAdapter implements WarpAdapter {
583
+ dim: number;
584
+ mean: Float32Array;
585
+ components: Float32Array[];
586
+ private count;
587
+ private learningRate;
588
+ private numComponents;
589
+ private componentsPtr;
590
+ private xResidualPtr;
591
+ private isWasmReady;
592
+ init(): Promise<void>;
593
+ /**
594
+ * 新しい WhiteningAdapter を作成します。
595
+ * @param dim ベクトルの次元数
596
+ * @param config 設定オプション
597
+ */
598
+ constructor(dim: number, config?: WhiteningConfig);
599
+ /**
600
+ * 入力ベクトルを用いて、平均と主成分をオンライン更新します。
601
+ * (Oja's Rule + Generalized Hebbian Algorithm)
602
+ *
603
+ * @param vector 学習用の入力ベクトル
604
+ */
605
+ update(vector: number[] | Float32Array): void;
606
+ /**
607
+ * 推論 (Whitening の適用):
608
+ * 入力ベクトルから平均を引き、偏りの原因である上位主成分を除去します (All-but-the-Top)。
609
+ *
610
+ * @param vector 推論・補正対象のベクトル
611
+ * @returns 等方化・ゼロセンタリングされた新しいベクトル
612
+ */
613
+ tune(vector: number[] | Float32Array): Float32Array;
614
+ /**
615
+ * 現在の学習状態(平均ベクトル、主成分ベクトルなど)をシリアライズして出力します。
616
+ * エッジ環境でのインスタンス再構築時に役立ちます。
617
+ */
618
+ exportState(): string;
619
+ /**
620
+ * シリアライズされた学習状態から WhiteningAdapter を復元します。
621
+ */
622
+ static importState(stateJson: string): WhiteningAdapter;
623
+ }
624
+
625
+ declare class ColbertAdapter {
626
+ private wasm;
627
+ constructor();
628
+ /**
629
+ * 単一のクエリと単一のドキュメント間の Late Interaction (MaxSim) スコアを計算します。
630
+ *
631
+ * @param queryTokens クエリのトークンベクトル行列 (要素数 = queryLength * dim の平坦化された配列)
632
+ * @param documentTokens ドキュメントのトークンベクトル行列
633
+ * @param dim ベクトルの次元数
634
+ * @returns MaxSimスコア
635
+ */
636
+ score(queryTokens: Float32Array, documentTokens: Float32Array, dim: number): number;
637
+ /**
638
+ * クエリと複数のドキュメント間の MaxSim スコアを計算し、スコアの降順にソートして返します。
639
+ *
640
+ * @param queryTokens クエリのトークンベクトル行列
641
+ * @param documentTokensArray ドキュメントのトークンベクトル行列の配列
642
+ * @param dim ベクトルの次元数
643
+ * @returns スコアの降順にソートされたドキュメントのインデックスとスコアの配列
644
+ */
645
+ rank(queryTokens: Float32Array, documentTokensArray: Float32Array[], dim: number): {
646
+ index: number;
647
+ score: number;
648
+ }[];
649
+ }
650
+
651
+ interface WarpPrismaConfig {
652
+ /**
653
+ * WarpVectorのアダプター (IntentAdapter, WhiteningAdapter など)
654
+ */
655
+ adapter: WarpAdapter;
656
+ /**
657
+ * pgvector のベクトルデータが保存されているカラム名
658
+ * デフォルト: "embedding"
659
+ */
660
+ vectorField?: string;
661
+ /**
662
+ * 類似度検索に使用する距離関数
663
+ * '<->' : ユークリッド距離 (デフォルト)
664
+ * '<#>' : 内積 (マイナス)
665
+ * '<=>' : コサイン距離
666
+ */
667
+ distanceOperator?: "<->" | "<#>" | "<=>";
668
+ }
669
+ /**
670
+ * Prisma Client に対して WarpVector の推論と pgvector のベクトル検索を
671
+ * 透過的に実行するメソッド `searchByVector` を追加する拡張機能です。
672
+ */
673
+ declare const withWarpVector: (config: WarpPrismaConfig) => (client: any) => _prisma_client_extension.PrismaClientExtends<_prisma_client_runtime_client.InternalArgs<{}, {
674
+ $allModels: {
675
+ /**
676
+ * 生のベクトルを受け取り、WarpVectorで変換した後に pgvector 検索を行います。
677
+ *
678
+ * @param args.vector 生のベクトル (変換前)
679
+ * @param args.topK 取得する最大件数 (デフォルト: 10)
680
+ * @param args.where 追加のフィルタリング条件 (例: "category = 'science'")
681
+ */
682
+ searchByVector<T>(this: T, args: {
683
+ vector: number[] | Float32Array;
684
+ topK?: number;
685
+ where?: string;
686
+ }): Promise<any>;
687
+ };
688
+ }, {}, {}> & _prisma_client_runtime_client.DefaultArgs>;
689
+
690
+ /**
691
+ * WarpEmbeddings の設定オプション
692
+ */
693
+ interface WarpEmbeddingsOptions extends EmbeddingsParams {
694
+ /**
695
+ * ラップするベースの Embeddings インスタンス(例: OpenAIEmbeddings)
696
+ * 初期の密ベクトル(Dense Vector)の生成に使用されます。
697
+ */
698
+ baseEmbeddings: Embeddings;
699
+ /**
700
+ * 初期化済みの WarpVector IntentAdapter。
701
+ * 意図(インテント)の変換行列とバイアスを保持しています。
702
+ */
703
+ adapter: IntentAdapter;
704
+ /**
705
+ * (オプション) 検索クエリに使用する初期の意図(インテント)名。
706
+ * 指定しない場合、検索を実行する前に `setIntent()` で設定する必要があります。
707
+ */
708
+ intentName?: string;
709
+ /**
710
+ * (オプション) アフィン変換後に適用する非線形活性化関数('relu', 'sigmoid', 'tanh'など)。
711
+ */
712
+ activation?: Activation;
713
+ /**
714
+ * (オプション) 単一の意図を指定する代わりに、自己アテンションベースの
715
+ * 動的意図合成(Auto-blending)を使用するかどうか。
716
+ * true の場合、クエリ実行時に `intentName` は無視されます。
717
+ */
718
+ autoBlend?: boolean;
719
+ }
720
+ /**
721
+ * WarpEmbeddings は、LangChain の BaseEmbeddings インスタンスをラップするクラスです。
722
+ * `embedQuery` メソッドをインターセプトし、ベースベクトルを生成した後に、
723
+ * 現在のコンテキスト/意図に基づいて動的なアフィン変換(WarpVector)を適用します。
724
+ *
725
+ * VectorStore へ正確なセマンティクスとして保存(インデックス)できるよう、
726
+ * ドキュメントは変換されず、通常通り埋め込まれます。
727
+ * ユーザーの「検索クエリ」に対してのみワープが適用されます。
728
+ *
729
+ * @example
730
+ * ```typescript
731
+ * import { OpenAIEmbeddings } from "@langchain/openai";
732
+ * import { IntentAdapter } from "warpvector";
733
+ * import { WarpEmbeddings } from "warpvector/integrations/langchain";
734
+ *
735
+ * const baseEmbeddings = new OpenAIEmbeddings();
736
+ * const adapter = new IntentAdapter({ ... });
737
+ *
738
+ * const embeddings = new WarpEmbeddings({
739
+ * baseEmbeddings,
740
+ * adapter,
741
+ * intentName: "riskAnalysis"
742
+ * });
743
+ *
744
+ * // このラップされた embeddings インスタンスを LangChain の VectorStore に直接渡せます!
745
+ * const vectorStore = new MemoryVectorStore(embeddings);
746
+ * ```
747
+ */
748
+ declare class WarpEmbeddings extends Embeddings {
749
+ private baseEmbeddings;
750
+ private adapter;
751
+ private intentName?;
752
+ private activation?;
753
+ private autoBlend;
754
+ constructor(options: WarpEmbeddingsOptions);
755
+ /**
756
+ * 実行時にアクティブな意図(インテント)を動的に切り替えます。
757
+ */
758
+ setIntent(intentName: string, activation?: Activation): void;
759
+ /**
760
+ * 自己アテンション型の動的意図合成(Auto-blending)を有効化または無効化します。
761
+ */
762
+ setAutoBlend(enabled: boolean): void;
763
+ /**
764
+ * ベースの embeddings モデルを使用して、ドキュメントを通常通り埋め込みます。
765
+ * VectorDB には客観的な空間データを含める必要があるため、インデックスされるドキュメントはワープ(変換)しません。
766
+ */
767
+ embedDocuments(documents: string[]): Promise<number[][]>;
768
+ /**
769
+ * ユーザーのクエリを埋め込み、WarpVector によるアフィン変換を適用します。
770
+ */
771
+ embedQuery(document: string): Promise<number[]>;
772
+ }
773
+
774
+ /**
775
+ * LlamaIndex の BaseEmbedding インターフェース互換
776
+ * 依存関係を避けるため、Duck Typing 用のインターフェースを定義しています。
777
+ */
778
+ interface LlamaIndexBaseEmbedding {
779
+ getTextEmbedding(text: string): Promise<number[]>;
780
+ getQueryEmbedding(query: string): Promise<number[]>;
781
+ getTextEmbeddings?(texts: string[]): Promise<number[][]>;
782
+ }
783
+ interface WarpLlamaIndexEmbeddingsOptions {
784
+ /**
785
+ * ラップするベースの Embeddings インスタンス(LlamaIndex の BaseEmbedding 継承クラス)
786
+ * 初期の密ベクトル(Dense Vector)の生成に使用されます。
787
+ */
788
+ baseEmbeddings: LlamaIndexBaseEmbedding;
789
+ /**
790
+ * 初期化済みの WarpVector IntentAdapter。
791
+ */
792
+ adapter: IntentAdapter;
793
+ /**
794
+ * (オプション) 検索クエリに使用する初期の意図(インテント)名。
795
+ */
796
+ intentName?: string;
797
+ /**
798
+ * (オプション) アフィン変換後に適用する非線形活性化関数。
799
+ */
800
+ activation?: Activation;
801
+ /**
802
+ * (オプション) 自己アテンションベースの動的意図合成(Auto-blending)を使用するかどうか。
803
+ */
804
+ autoBlend?: boolean;
805
+ }
806
+ /**
807
+ * WarpLlamaIndexEmbeddings は、LlamaIndex の BaseEmbedding インスタンスをラップします。
808
+ * `getQueryEmbedding` メソッドをインターセプトし、ベースベクトルを生成した後に
809
+ * 現在のコンテキスト/意図に基づいて動的なアフィン変換(WarpVector)を適用します。
810
+ */
811
+ declare class WarpLlamaIndexEmbeddings implements LlamaIndexBaseEmbedding {
812
+ private baseEmbeddings;
813
+ private adapter;
814
+ private intentName?;
815
+ private activation?;
816
+ private autoBlend;
817
+ constructor(options: WarpLlamaIndexEmbeddingsOptions);
818
+ setIntent(intentName: string, activation?: Activation): void;
819
+ setAutoBlend(enabled: boolean): void;
820
+ /**
821
+ * ドキュメントの埋め込み(インデックス作成用)は変換を行いません。
822
+ */
823
+ getTextEmbedding(text: string): Promise<number[]>;
824
+ /**
825
+ * 複数ドキュメントの埋め込み
826
+ */
827
+ getTextEmbeddings(texts: string[]): Promise<number[][]>;
828
+ /**
829
+ * クエリの埋め込み(検索用)にのみ WarpVector 変換を適用します。
830
+ */
831
+ getQueryEmbedding(query: string): Promise<number[]>;
832
+ }
833
+
834
+ type QuantizationType = "int8" | "binary";
835
+ interface QuantizationConfig {
836
+ /**
837
+ * "int8": 8-bit スカラー量子化 (Int8Array を返す)
838
+ * "binary": 1-bit 二値化 (Uint8Array を返す、8次元分を1バイトにパック)
839
+ */
840
+ type: QuantizationType;
841
+ /**
842
+ * 量子化するベクトルの次元数
843
+ */
844
+ dim: number;
845
+ /**
846
+ * 動的キャリブレーション(スケール算出)を有効にするか。
847
+ * trueの場合、ベクトルの絶対値の最大値 max(abs(v)) を算出し、
848
+ * scale = 127 / max(abs(v)) を用いて量子化します。
849
+ */
850
+ dynamic?: boolean;
851
+ }
852
+ /**
853
+ * QuantizationAdapter は、Float32のベクトルを Int8 や Binary に圧縮し、
854
+ * メモリ使用量と保存コストを劇的に(1/4 〜 1/32 に)削減します。
855
+ */
856
+ declare class QuantizationAdapter implements WarpAdapter {
857
+ private type;
858
+ private dim;
859
+ private dynamic;
860
+ constructor(config: QuantizationConfig);
861
+ tune(vector: number[] | Float32Array): Int8Array | Uint8Array;
862
+ /**
863
+ * Binary量子化された2つのベクトル間のハミング距離を計算します。
864
+ * ハミング距離が小さいほど類似度が高いことを意味します。
865
+ */
866
+ static hammingDistance(a: Uint8Array, b: Uint8Array): number;
867
+ /**
868
+ * Int8量子化された2つのベクトル間のドット積(内積)を計算します。
869
+ * 動的スケーリングが埋め込まれている場合はスケールを戻して計算します。
870
+ */
871
+ static int8DotProduct(a: Int8Array, b: Int8Array): number;
872
+ exportState(): string;
873
+ static importState(stateJson: string): QuantizationAdapter;
376
874
  }
377
875
 
378
876
  /**
@@ -391,13 +889,29 @@ interface BaseTrainingOptions {
391
889
  /** trueの場合、事前に数エポックのテストランを行い、最適な学習率を自動探索します。デフォルト: false */
392
890
  autoTune?: boolean;
393
891
  }
892
+ /**
893
+ * Adam最適化のステート変数を管理する共通基底クラス
894
+ */
895
+ declare abstract class AbstractAdamTrainer {
896
+ protected t: number;
897
+ protected mW: Float32Array;
898
+ protected vW: Float32Array;
899
+ protected mb: Float32Array;
900
+ protected vb: Float32Array;
901
+ protected initAdamState(sDim: number, tDim: number): void;
902
+ /**
903
+ * アフィンレイヤー (matrix, bias) に対する Adam のパラメータ更新を適用します。
904
+ * InfoNCE や Triplet など、様々な損失関数で計算された勾配(outputGradients)を元に更新を行います。
905
+ */
906
+ protected applyAdamToAffine(matrix: Float32Array, bias: Float32Array, mMatrix: Float32Array, vMatrix: Float32Array, mBias: Float32Array, vBias: Float32Array, input: number[] | Float32Array, outputGradients: number[] | Float32Array, lr: number, reg: number, t: number): void;
907
+ }
394
908
  /**
395
909
  * 確率的勾配降下法 (SGD + Momentum) を用いた学習のための共通基底クラス。
396
910
  *
397
911
  * @template TExample 学習に用いるデータペアの型 (入力と理想の出力のペアなど)
398
912
  * @template TResult 最終的に学習結果として出力される重み (行列やバイアス) の型
399
913
  */
400
- declare abstract class BaseTrainer<TExample, TResult> {
914
+ declare abstract class BaseTrainer<TExample, TResult> extends AbstractAdamTrainer {
401
915
  /** 学習用サンプルの配列 */
402
916
  protected examples: TExample[];
403
917
  /** 入力ベクトル(ソース)の次元数 */
@@ -447,20 +961,11 @@ declare abstract class BaseTrainer<TExample, TResult> {
447
961
  */
448
962
  protected findBestLearningRate(options: BaseTrainingOptions): number;
449
963
  /**
450
- * SGD + Momentum アルゴリズムによる1ステップのパラメータ更新を実行します。
964
+ * Adam オプティマイザによる1ステップのパラメータ更新を実行します。
451
965
  * In-place (破壊的) に `matrix` と `bias` を更新します。
452
- *
453
- * @param {Float32Array} matrix 現在の変換行列 (1次元フラット配列)
454
- * @param {Float32Array} bias 現在のバイアスベクトル
455
- * @param {Float32Array} vMatrix 行列のモメンタム (速度)
456
- * @param {Float32Array} vBias バイアスのモメンタム (速度)
457
- * @param {number[] | Float32Array} x 入力ベクトル (ソース)
458
- * @param {number[] | Float32Array} y 理想の出力ベクトル (ターゲット)
459
- * @param {number} lr 学習率
460
- * @param {number} reg L2正則化係数
461
- * @param {number} momentum モメンタム係数
966
+ * WASM 版の Adam 実装ができるまではネイティブ JS で処理します。
462
967
  */
463
- protected sgdMomentumStep(matrix: Float32Array, bias: Float32Array, vMatrix: Float32Array, vBias: Float32Array, x: number[] | Float32Array, y: number[] | Float32Array, lr: number, reg: number, momentum: number): void;
968
+ protected adamStep(matrix: Float32Array, bias: Float32Array, mMatrix: Float32Array, vMatrix: Float32Array, mBias: Float32Array, vBias: Float32Array, x: number[] | Float32Array, y: number[] | Float32Array, lr: number, reg: number, t: number): void;
464
969
  }
465
970
 
466
971
  /**
@@ -475,8 +980,16 @@ interface TrainingExample {
475
980
  }
476
981
  /**
477
982
  * 学習時の最適化オプション
983
+ export interface TrainingOptions extends BaseTrainingOptions {}
984
+
985
+ /**
986
+ * IntentTrainer のオンライン学習オプション
478
987
  */
479
- interface TrainingOptions extends BaseTrainingOptions {
988
+ interface IntentOnlineOptions {
989
+ /** 1ステップの学習率 (デフォルト: 0.01) */
990
+ learningRate?: number;
991
+ /** L2正則化の強さ (デフォルト: 0.001) */
992
+ regularization?: number;
480
993
  }
481
994
  /**
482
995
  * サンプルデータから最適な `IntentWeights` (行列Wとバイアスb) を
@@ -507,13 +1020,109 @@ declare class IntentTrainer extends BaseTrainer<TrainingExample, IntentWeights>
507
1020
  * ユーザーのクリックなどの 1 回のフィードバックからリアルタイムに重みを微調整します。
508
1021
  *
509
1022
  * @param {IntentWeights} currentWeights - 現在の重み
510
- * @param {number[] | Float32Array} input - 検索されたクエリベクトル
511
- * @param {number[] | Float32Array} target - クリックされた(理想の)ドキュメントのベクトル
512
- * @param {number} learningRate - 1ステップの学習率 (デフォルト: 0.01)
513
- * @param {number} regularization - L2正則化の強さ (デフォルト: 0.001)
1023
+ * @param {TrainingExample} example - アンカー、正解を含む学習データ
1024
+ * @param {IntentOnlineOptions} [options={}] - 学習オプション
514
1025
  * @returns {IntentWeights} 微調整された新しい重み
515
1026
  */
516
- updateOnline(currentWeights: IntentWeights, input: number[] | Float32Array, target: number[] | Float32Array, learningRate?: number, regularization?: number): Promise<IntentWeights>;
1027
+ updateOnline(currentWeights: IntentWeights, example: TrainingExample, options?: IntentOnlineOptions): Promise<IntentWeights>;
1028
+ }
1029
+
1030
+ /**
1031
+ * 学習データのペア(Anchor, Positive, Negative)
1032
+ * @interface TripletExample
1033
+ */
1034
+ interface TripletExample {
1035
+ /** 基準となるベクトル(検索クエリなど) */
1036
+ anchor: number[] | Float32Array;
1037
+ /** Anchorに近づけたい正解ベクトル(クリックされた商品など) */
1038
+ positive: number[] | Float32Array;
1039
+ /** Anchorから遠ざけたい不正解ベクトル(スルーされた商品など) */
1040
+ negative: number[] | Float32Array;
1041
+ }
1042
+ /**
1043
+ * TripletTrainer のオンライン学習オプション
1044
+ */
1045
+ interface TripletOnlineOptions {
1046
+ /** 1ステップの学習率 (デフォルト: 0.01) */
1047
+ learningRate?: number;
1048
+ /** トリプレットロスのマージン (デフォルト: 0.1) */
1049
+ margin?: number;
1050
+ /** L2正則化の強さ (デフォルト: 0.001) */
1051
+ regularization?: number;
1052
+ }
1053
+ /**
1054
+ * Contrastive Learning (トリプレットロス) を用いて、相対的な距離感から
1055
+ * IntentWeights (行列W と バイアスb) を学習するトレーナークラス。
1056
+ *
1057
+ * "Anchor" を変換したベクトル A' が、"Negative" よりも "Positive" に
1058
+ * 設定されたマージン(Margin)分だけ確実により近づくように重みを更新します。
1059
+ */
1060
+ declare class TripletTrainer extends AbstractAdamTrainer {
1061
+ private dimension;
1062
+ /**
1063
+ * TripletTrainer のインスタンスを作成します。
1064
+ * @param {number} dimension ベクトルの次元数
1065
+ */
1066
+ constructor(dimension: number);
1067
+ private toWeights;
1068
+ /**
1069
+ * オンライン学習 (フィードバックループ) 用のメソッド。
1070
+ * 1つのトリプレットデータからリアルタイムに重みを微調整します。
1071
+ *
1072
+ * @param {IntentWeights} currentWeights - 現在の重み
1073
+ * @param {TripletExample} example - アンカー、正解、不正解を含むトリプレットデータ
1074
+ * @param {TripletOnlineOptions} [options={}] - 学習オプション
1075
+ * @returns {Promise<IntentWeights>} 微調整された新しい重み
1076
+ */
1077
+ updateOnline(currentWeights: IntentWeights, example: TripletExample, options?: TripletOnlineOptions): Promise<IntentWeights>;
1078
+ }
1079
+
1080
+ /**
1081
+ * 学習データのペア(Anchor, Positive, 複数のNegatives)
1082
+ * @interface InfoNCEExample
1083
+ */
1084
+ interface InfoNCEExample {
1085
+ /** 基準となるベクトル(検索クエリなど) */
1086
+ anchor: number[] | Float32Array;
1087
+ /** Anchorに近づけたい正解ベクトル(クリックされた商品など) */
1088
+ positive: number[] | Float32Array;
1089
+ /** Anchorから遠ざけたい不正解ベクトルの配列(スルーされた商品群など) */
1090
+ negatives: (number[] | Float32Array)[];
1091
+ }
1092
+ /**
1093
+ * InfoNCETrainer のオンライン学習オプション
1094
+ */
1095
+ interface InfoNCEOnlineOptions {
1096
+ /** 1ステップの学習率 (デフォルト: 0.01) */
1097
+ learningRate?: number;
1098
+ /** Softmaxの温度パラメータ (デフォルト: 0.1) */
1099
+ temperature?: number;
1100
+ /** L2正則化の強さ (デフォルト: 0.001) */
1101
+ regularization?: number;
1102
+ }
1103
+ /**
1104
+ * InfoNCE Loss (Softmax Cross Entropy) を用いて、
1105
+ * 「1つの正解を近づけ、複数の不正解を一気に遠ざける」ように
1106
+ * IntentWeights (行列W と バイアスb) を学習するトレーナークラス。
1107
+ */
1108
+ declare class InfoNCETrainer extends AbstractAdamTrainer {
1109
+ private dimension;
1110
+ /**
1111
+ * InfoNCETrainer のインスタンスを作成します。
1112
+ * @param {number} dimension ベクトルの次元数
1113
+ */
1114
+ constructor(dimension: number);
1115
+ private toWeights;
1116
+ /**
1117
+ * オンライン学習 (フィードバックループ) 用のメソッド。
1118
+ * 1つのクエリ(Anchor)、1つのクリック(Positive)、複数のスルー(Negatives)から重みを微調整します。
1119
+ *
1120
+ * @param {IntentWeights} currentWeights - 現在の重み
1121
+ * @param {InfoNCEExample} example - アンカー、正解、複数の不正解を含むデータ
1122
+ * @param {InfoNCEOnlineOptions} [options={}] - 学習オプション
1123
+ * @returns {Promise<IntentWeights>} 微調整された新しい重み
1124
+ */
1125
+ updateOnline(currentWeights: IntentWeights, example: InfoNCEExample, options?: InfoNCEOnlineOptions): Promise<IntentWeights>;
517
1126
  }
518
1127
 
519
1128
  /**
@@ -559,6 +1168,15 @@ declare class MigrationTrainer extends BaseTrainer<MigrationExample, ProjectionW
559
1168
  protected toWeights(flatMatrix: Float32Array, bias: Float32Array): ProjectionWeights;
560
1169
  }
561
1170
 
1171
+ type index_WarpEmbeddings = WarpEmbeddings;
1172
+ declare const index_WarpEmbeddings: typeof WarpEmbeddings;
1173
+ type index_WarpEmbeddingsOptions = WarpEmbeddingsOptions;
1174
+ type index_WarpPrismaConfig = WarpPrismaConfig;
1175
+ declare const index_withWarpVector: typeof withWarpVector;
1176
+ declare namespace index {
1177
+ export { index_WarpEmbeddings as WarpEmbeddings, type index_WarpEmbeddingsOptions as WarpEmbeddingsOptions, type index_WarpPrismaConfig as WarpPrismaConfig, index_withWarpVector as withWarpVector };
1178
+ }
1179
+
562
1180
  /**
563
1181
  * 外部のベクトルデータベース(pgvector, Pinecone, Redisなど)へ
564
1182
  * ワープ変換後のベクトルを保存・検索するためのアダプター/ユーティリティクラス
@@ -574,7 +1192,7 @@ declare class VectorDBAdapter {
574
1192
  * @param {number[] | Float32Array} vector ワープ変換後のベクトル
575
1193
  * @returns {string} `'[0.1, 0.2, 0.3]'` のような文字列表現
576
1194
  */
577
- static toPgvector(vector: number[] | Float32Array): string;
1195
+ static toPgvector(vector: number[] | Float32Array | Int8Array | Uint8Array): string;
578
1196
  /**
579
1197
  * Pinecone 用のクエリオブジェクトを生成します。
580
1198
  * Pinecone クライアントに直接渡せる形式のオブジェクトを返します。
@@ -588,7 +1206,7 @@ declare class VectorDBAdapter {
588
1206
  * @param {Record<string, any>} [filter] メタデータフィルタ(オプション)
589
1207
  * @returns {Record<string, any>} Pineconeのqueryメソッド用オブジェクト
590
1208
  */
591
- static toPineconeQuery(vector: number[] | Float32Array, topK?: number, filter?: Record<string, any>): Record<string, any>;
1209
+ static toPineconeQuery(vector: number[] | Float32Array | Int8Array | Uint8Array, topK?: number, filter?: Record<string, unknown>): Record<string, unknown>;
592
1210
  /**
593
1211
  * Redis (RediSearch) のベクトルフィールド用に、
594
1212
  * Float32Array をバイナリ(Uint8Array)に変換します。
@@ -601,7 +1219,251 @@ declare class VectorDBAdapter {
601
1219
  * @param {number[] | Float32Array} vector ワープ変換後のベクトル
602
1220
  * @returns {Uint8Array} バイナリデータ (Float32のバイト表現)
603
1221
  */
604
- static toRedis(vector: number[] | Float32Array): Uint8Array;
1222
+ static toRedis(vector: number[] | Float32Array | Int8Array | Uint8Array): Uint8Array;
1223
+ }
1224
+
1225
+ /**
1226
+ * ベクトル・シンボリック・アーキテクチャ (VSA) / 超次元計算アダプタ
1227
+ *
1228
+ * ベクトル同士を論理的・数学的に結合(バインド)したり束ねたり(バンドル)することで、
1229
+ * 1つの密なベクトルの中にキーと値(メタデータなど)を埋め込み、検索空間上で
1230
+ * そのまま演算を行えるようにする機能を提供します。
1231
+ */
1232
+ /**
1233
+ * VSA演算のオプション
1234
+ */
1235
+ interface VsaOptions {
1236
+ /** 結果をL2正規化するかどうか(デフォルト: true) */
1237
+ shouldNormalize?: boolean;
1238
+ }
1239
+ declare class VsaAdapter {
1240
+ /**
1241
+ * ベクトルのバンドリング (Bundling / Superposition)
1242
+ * 複数のベクトルを足し合わせ(重ね合わせ)て1つのベクトルに統合します。
1243
+ * 「A と B の両方の概念を含む」ベクトルを作成する際に使用します。
1244
+ *
1245
+ * @param vectors 束ねるベクトルの配列
1246
+ * @param options 演算オプション
1247
+ * @returns 束ねられた新しいベクトル
1248
+ */
1249
+ static bundle(vectors: (number[] | Float32Array)[], options?: VsaOptions): Float32Array;
1250
+ /**
1251
+ * ベクトルのバインディング (Binding / Hadamard Product)
1252
+ * アダマール積(要素ごとの積)を用いて、2つのベクトルを「結合」します。
1253
+ * 例: キー(ユーザーID)と値(好み)を掛け合わせ、特有の「ユーザーの好み」ベクトルを生成します。
1254
+ *
1255
+ * @param vec1 バインドするベクトル1
1256
+ * @param vec2 バインドするベクトル2
1257
+ * @param options 演算オプション
1258
+ * @returns バインドされた新しいベクトル
1259
+ */
1260
+ static bind(vec1: number[] | Float32Array, vec2: number[] | Float32Array, options?: VsaOptions): Float32Array;
1261
+ /**
1262
+ * ベクトルのアンバインディング (Unbinding)
1263
+ * バインドされたベクトルから、片方のベクトル(キー)を使って元の値(バリュー)を取り出します。
1264
+ * アダマール積によるバインディングの逆演算(要素ごとの除算)を行います。
1265
+ *
1266
+ * @param boundVec バインド済みのベクトル
1267
+ * @param keyVec 抽出に使用するキーベクトル
1268
+ * @param options 演算オプション
1269
+ * @returns アンバインドされて抽出されたベクトル
1270
+ */
1271
+ static unbind(boundVec: number[] | Float32Array, keyVec: number[] | Float32Array, options?: VsaOptions): Float32Array;
1272
+ /**
1273
+ * ---------------------------------------------------------
1274
+ * Binary VSA (バイナリベクトル・シンボリック・アーキテクチャ)
1275
+ * ---------------------------------------------------------
1276
+ * QuantizationAdapter で 1-bit (Binary) 量子化された Uint8Array ベクトルに
1277
+ * 対する超次元計算を行います。XOR 演算により、超高速・極小メモリでの処理が可能です。
1278
+ */
1279
+ /**
1280
+ * バイナリベクトルのバインディング (Binary Binding / XOR)
1281
+ * XOR (排他的論理和) を用いて2つのバイナリベクトルを結合します。
1282
+ * Binary VSA において、XOR は情報を結合するための標準的な演算です。
1283
+ *
1284
+ * @param bin1 バインドするバイナリベクトル1 (Uint8Array)
1285
+ * @param bin2 バインドするバイナリベクトル2 (Uint8Array)
1286
+ * @returns バインドされた新しいバイナリベクトル (Uint8Array)
1287
+ */
1288
+ static bindBinary(bin1: Uint8Array, bin2: Uint8Array): Uint8Array;
1289
+ /**
1290
+ * バイナリベクトルのアンバインディング (Binary Unbinding / XOR)
1291
+ * XOR の自己逆性 (A ^ B ^ B = A) を利用して、キーを用いて元の値を抽出します。
1292
+ * 内部的には bindBinary と全く同じ処理です。
1293
+ *
1294
+ * @param boundBin バインド済みのバイナリベクトル (Uint8Array)
1295
+ * @param keyBin 抽出に使用するキーバイナリベクトル (Uint8Array)
1296
+ * @returns アンバインドされて抽出されたバイナリベクトル (Uint8Array)
1297
+ */
1298
+ static unbindBinary(boundBin: Uint8Array, keyBin: Uint8Array): Uint8Array;
1299
+ /**
1300
+ * バイナリベクトルのバンドリング (Binary Bundling / Majority Vote)
1301
+ * 複数のバイナリベクトルを重ね合わせます。各ビット位置で 1 と 0 の出現回数をカウントし、
1302
+ * 多数決 (Majority Vote) で最終的なビットを決定します。
1303
+ *
1304
+ * @param bins 束ねるバイナリベクトルの配列 (Uint8Arrayの配列)
1305
+ * @returns 束ねられた新しいバイナリベクトル (Uint8Array)
1306
+ */
1307
+ static bundleBinary(bins: Uint8Array[]): Uint8Array;
1308
+ }
1309
+
1310
+ interface TaskConfig {
1311
+ /**
1312
+ * 追加するタスクの重み(学習済み IntentWeights)
1313
+ */
1314
+ weights: IntentWeights;
1315
+ /**
1316
+ * そのタスクを合成するスケール(重み付け)。
1317
+ * 1.0 が標準、0.5で半分、マイナスで逆効果。
1318
+ */
1319
+ scale: number;
1320
+ }
1321
+ /**
1322
+ * Task Arithmetic (モデルマージ) ユーティリティ
1323
+ *
1324
+ * 複数の学習済みアダプタの重み(IntentWeights)を「タスクベクトル」として扱い、
1325
+ * それらを合成(マージ)することで、推論時にオーバーヘッドのない新しい静的なアダプタ行列を生成します。
1326
+ */
1327
+ declare class TaskArithmetic {
1328
+ /**
1329
+ * 複数のタスク(IntentWeights)を合成し、新しい IntentWeights を作成します。
1330
+ * 数式: W_new = W_base + Σ scale_i * (W_i - W_base)
1331
+ *
1332
+ * @param tasks 合成するタスクのリスト(IntentWeights と スケールのペア)
1333
+ * @param baseIntent 基準となる IntentWeights。省略された場合は恒等行列(Identity)とゼロバイアスがベースになります。
1334
+ * @returns 完全にマージされた新しい IntentWeights
1335
+ */
1336
+ static merge(tasks: TaskConfig[], baseIntent?: IntentWeights): IntentWeights;
1337
+ }
1338
+
1339
+ interface PipelineStep {
1340
+ type: string;
1341
+ adapter: WarpAdapter;
1342
+ }
1343
+ interface PipelineState {
1344
+ type: string;
1345
+ state: AdapterState | null;
1346
+ }
1347
+ interface RunContext {
1348
+ intent?: string;
1349
+ version?: string;
1350
+ }
1351
+ interface FormatOptions {
1352
+ format: string;
1353
+ topK?: number;
1354
+ filter?: Record<string, unknown>;
1355
+ [key: string]: unknown;
1356
+ }
1357
+ /**
1358
+ * WarpPipeline (統一インターフェース)
1359
+ *
1360
+ * 複数の WarpAdapter を直感的なビルダーパターンで数珠つなぎ(チェーン)し、
1361
+ * データパイプラインとして一括で実行・保存・復元するためのラッパークラスです。
1362
+ */
1363
+ declare class WarpPipeline {
1364
+ inputDim: number;
1365
+ private steps;
1366
+ /**
1367
+ * アダプタの復元関数を保持するレジストリ。
1368
+ * カスタムアダプタをパイプラインで利用・復元可能にするために使用します。
1369
+ */
1370
+ private static adapterRegistry;
1371
+ /**
1372
+ * カスタムアダプタをパイプラインのレジストリに登録します。
1373
+ * これにより importState でカスタムアダプタを復元可能になります。
1374
+ *
1375
+ * @param type アダプタの識別子 (例: "MyCustomAdapter")
1376
+ * @param importFn 状態オブジェクトからアダプタインスタンスを復元する関数
1377
+ */
1378
+ static registerAdapter(type: string, importFn: (state: AdapterState) => WarpAdapter): void;
1379
+ /**
1380
+ * フォーマット変換ロジックを保持するレジストリ。
1381
+ */
1382
+ private static formatRegistry;
1383
+ /**
1384
+ * カスタムの出力フォーマットを登録します。
1385
+ * これにより、ユーザー独自のDB形式(Milvus, Weaviateなど)への変換を動的に追加できます。
1386
+ *
1387
+ * @param format フォーマット名 (例: "pgvector")
1388
+ * @param formatFn 変換を行うコールバック関数
1389
+ */
1390
+ static registerFormat(format: string, formatFn: (vector: OutputVector, options: FormatOptions) => unknown): void;
1391
+ constructor(inputDim: number);
1392
+ /**
1393
+ * IntentAdapter (意図による線形変換) をパイプラインに追加します。
1394
+ */
1395
+ addIntent(intents?: Record<string, IntentWeights>): this;
1396
+ /**
1397
+ * LoraIntentAdapter (低ランク適応による線形変換) をパイプラインに追加します。
1398
+ */
1399
+ addLoraIntent(rank: number, intents?: Record<string, LoraIntentWeights>): this;
1400
+ /**
1401
+ * WhiteningAdapter (PCAによる空間的偏りの除去) をパイプラインに追加します。
1402
+ */
1403
+ addWhitening(options?: WhiteningConfig): this;
1404
+ /**
1405
+ * ProjectionAdapter (次元圧縮) をパイプラインに追加します。
1406
+ */
1407
+ addProjection(outputDim: number, projections?: Record<string, ProjectionWeights>): this;
1408
+ /**
1409
+ * MlpAdapter (多層ニューラルネットワーク / 非線形推論) をパイプラインに追加します。
1410
+ */
1411
+ addMlp(layers: MlpLayer[]): this;
1412
+ /**
1413
+ * QuantizationAdapter (ベクトル量子化 / 圧縮) をパイプラインに追加します。
1414
+ * これは通常、パイプラインの最後のステップとして使用されます。
1415
+ */
1416
+ quantize(type: QuantizationType): this;
1417
+ /**
1418
+ * カスタムアダプタを直接パイプラインの末尾に追加します。
1419
+ * (ビルダーパターンで独自の拡張アダプタを組み込む際に使用します)
1420
+ *
1421
+ * @param type アダプタの識別子(レジストリ登録名と一致させることを推奨)
1422
+ * @param adapter WarpAdapterを実装したインスタンス
1423
+ */
1424
+ addStep(type: string, adapter: WarpAdapter): this;
1425
+ /**
1426
+ * パイプライン内に WASM などの非同期初期化を必要とするアダプタが含まれている場合、
1427
+ * それらを一括でセットアップします。
1428
+ */
1429
+ init(): Promise<void>;
1430
+ /**
1431
+ * パイプラインを順次実行し、入力ベクトルを最終的な表現に変換します。
1432
+ *
1433
+ * @param vector 変換元のベースベクトル
1434
+ * @param context インテントやバージョンなどのコンテキスト情報
1435
+ * @returns パイプラインを通過した最終的なベクトル (Float32Array または Uint8Array/Int8Array)
1436
+ */
1437
+ run(vector: InputVector, context?: RunContext): OutputVector;
1438
+ /**
1439
+ * 複数のベクトル(バッチ)を一括でパイプラインに通します。
1440
+ * 内部の tuneBatch が実装されているアダプタでは WASM/SIMD による高速処理が適用されます。
1441
+ *
1442
+ * @param vectors 変換元のベースベクトルの配列
1443
+ * @param context インテントやバージョンなどのコンテキスト情報
1444
+ * @returns 変換されたベクトルの配列
1445
+ */
1446
+ runBatch(vectors: InputVector[], context?: RunContext): OutputVector[];
1447
+ /**
1448
+ * ベクトル変換から特定データベース向けのフォーマットまでを1回の呼び出しで行います。
1449
+ *
1450
+ * @param vector 変換元のベースベクトル
1451
+ * @param dbOptions フォーマットの指定オプション
1452
+ * @param context パイプラインのコンテキスト
1453
+ * @returns 指定されたデータベース形式のオブジェクトや文字列
1454
+ */
1455
+ runAndFormat(vector: InputVector, dbOptions: FormatOptions, context?: RunContext): unknown;
1456
+ /**
1457
+ * パイプライン内の全アダプタの状態(学習済みの重みなど)を JSON 化可能な配列として出力します。
1458
+ * これにより、DBやRedis等への永続化が容易になります。
1459
+ */
1460
+ exportState(): PipelineState[];
1461
+ /**
1462
+ * エクスポートされた JSON 状態から、パイプラインを完全に復元(再構築)します。
1463
+ * @param states exportState で出力された配列
1464
+ * @returns 復元された新しい WarpPipeline インスタンス
1465
+ */
1466
+ static importState(states: PipelineState[]): WarpPipeline;
605
1467
  }
606
1468
 
607
- export { type Activation, IntentAdapter, IntentTrainer, type IntentWeights, LoraIntentAdapter, type LoraIntentWeights, type MigrationExample, type MigrationOptions, MigrationTrainer, ProjectionAdapter, type ProjectionWeights, type TrainingExample, type TrainingOptions, VectorDBAdapter, applyActivationToVector, assertDimension, cosineSimilarity, flattenMatrix, innerProduct, normalize, reject, slerp, softmax };
1469
+ export { type Activation, type AdapterState, ColbertAdapter, type FormatOptions, type FusionResult, type InfoNCEExample, type InfoNCEOnlineOptions, InfoNCETrainer, type InputVector, IntentAdapter, type IntentOnlineOptions, IntentTrainer, type IntentWeights, type LlamaIndexBaseEmbedding, LoraIntentAdapter, type LoraIntentWeights, type MigrationExample, type MigrationOptions, MigrationTrainer, MlpAdapter, type MlpLayer, type OutputVector, type PipelineState, type PipelineStep, ProjectionAdapter, type ProjectionWeights, QuantizationAdapter, type QuantizationConfig, type QuantizationType, type RankedResult, type RunContext, TaskArithmetic, type TaskConfig, type TrainingExample, type TripletExample, type TripletOnlineOptions, TripletTrainer, VectorDBAdapter, VsaAdapter, type VsaOptions, type WarpAdapter, WarpEmbeddings, type WarpEmbeddingsOptions, WarpLlamaIndexEmbeddings, type WarpLlamaIndexEmbeddingsOptions, WarpPipeline, type WarpPrismaConfig, WhiteningAdapter, type WhiteningConfig, addScaledVector, applyActivationToVector, applyAffine, assertDimension, cosineSimilarity, flattenMatrix, getFlatMatrixAndBias, innerProduct, index as integrations, normalize, reject, rrf, rsf, slerp, softmax, withWarpVector };