warpvector 0.1.0 → 0.1.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/README.md +263 -98
- package/dist/index.d.mts +895 -46
- package/dist/index.d.ts +895 -46
- package/dist/index.js +2744 -350
- package/dist/index.mjs +2744 -351
- package/package.json +16 -1
package/dist/index.d.mts
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,7 +389,46 @@ 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;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
interface RankedResult {
|
|
403
|
+
id: string | number;
|
|
404
|
+
score?: number;
|
|
405
|
+
rank?: number;
|
|
406
|
+
metadata?: Record<string, any>;
|
|
313
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[];
|
|
314
432
|
|
|
315
433
|
/**
|
|
316
434
|
* 次元削減/拡張のための射影行列の重みを定義するインターフェース
|
|
@@ -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,381 @@ declare class ProjectionAdapter {
|
|
|
364
483
|
*/
|
|
365
484
|
removeProjection(name: string): void;
|
|
366
485
|
/**
|
|
367
|
-
*
|
|
368
|
-
*
|
|
486
|
+
* ベクトルの次元削減(射影)を実行します。
|
|
487
|
+
* (WarpAdapter の実装として project の代わりに tune を提供します)
|
|
488
|
+
*
|
|
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 配列であることを前提としています。
|
|
501
|
+
*/
|
|
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
|
+
constructor(layers: MlpLayer[]);
|
|
537
|
+
/**
|
|
538
|
+
* WASMの初期化と、MLP構造をWASMメモリに書き込む準備を行います。
|
|
539
|
+
* インスタンス作成後に必ず呼び出してください。
|
|
540
|
+
*/
|
|
541
|
+
init(): Promise<void>;
|
|
542
|
+
/**
|
|
543
|
+
* ニューラルネットワークの順伝播を実行し、結果を返します。
|
|
544
|
+
* (WarpAdapter の実装として、predict の代わりに tune を提供します)
|
|
545
|
+
*
|
|
546
|
+
* @param input 入力ベクトル
|
|
547
|
+
* @returns 推論結果ベクトル
|
|
548
|
+
*/
|
|
549
|
+
tune(input: number[] | Float32Array): Float32Array;
|
|
550
|
+
/**
|
|
551
|
+
* 現在のMLP構造と重みをシリアライズして出力します。
|
|
552
|
+
*/
|
|
553
|
+
exportState(): string;
|
|
554
|
+
/**
|
|
555
|
+
* シリアライズされた状態から MlpAdapter を復元します。
|
|
556
|
+
* 注意: 復元後、再度 `await init()` を呼び出してWASMメモリを初期化する必要があります。
|
|
557
|
+
*/
|
|
558
|
+
static importState(stateJson: string): MlpAdapter;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
interface WhiteningConfig {
|
|
562
|
+
/**
|
|
563
|
+
* 学習率 (0.0 ~ 1.0)
|
|
564
|
+
* 平均と主成分を更新する際の指数移動平均の重み。
|
|
565
|
+
* デフォルトは 0.01
|
|
566
|
+
*/
|
|
567
|
+
learningRate?: number;
|
|
568
|
+
/**
|
|
569
|
+
* 除去するトップ主成分(PC)の数。
|
|
570
|
+
* 通常、事前学習モデルのコーン(偏り)問題を取り除くには、
|
|
571
|
+
* 上位 1〜2 個の主成分を除去する(All-but-the-Top)だけで十分な効果があります。
|
|
572
|
+
* デフォルトは 1
|
|
573
|
+
*/
|
|
574
|
+
numComponents?: number;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* WhiteningAdapter は、ストリーミングデータに対して
|
|
578
|
+
* Oja's Rule によるオンラインPCAを実行し、
|
|
579
|
+
* ベクトル空間の等方化(Whitening / Anisotropy Reduction)を行うアダプターです。
|
|
580
|
+
*/
|
|
581
|
+
declare class WhiteningAdapter implements WarpAdapter {
|
|
582
|
+
dim: number;
|
|
583
|
+
mean: Float32Array;
|
|
584
|
+
components: Float32Array[];
|
|
585
|
+
private count;
|
|
586
|
+
private learningRate;
|
|
587
|
+
private numComponents;
|
|
588
|
+
/**
|
|
589
|
+
* 新しい WhiteningAdapter を作成します。
|
|
590
|
+
* @param dim ベクトルの次元数
|
|
591
|
+
* @param config 設定オプション
|
|
592
|
+
*/
|
|
593
|
+
constructor(dim: number, config?: WhiteningConfig);
|
|
594
|
+
/**
|
|
595
|
+
* 入力ベクトルを用いて、平均と主成分をオンライン更新します。
|
|
596
|
+
* (Oja's Rule + Generalized Hebbian Algorithm)
|
|
597
|
+
*
|
|
598
|
+
* @param vector 学習用の入力ベクトル
|
|
599
|
+
*/
|
|
600
|
+
update(vector: number[] | Float32Array): void;
|
|
601
|
+
/**
|
|
602
|
+
* 推論 (Whitening の適用):
|
|
603
|
+
* 入力ベクトルから平均を引き、偏りの原因である上位主成分を除去します (All-but-the-Top)。
|
|
604
|
+
*
|
|
605
|
+
* @param vector 推論・補正対象のベクトル
|
|
606
|
+
* @returns 等方化・ゼロセンタリングされた新しいベクトル
|
|
607
|
+
*/
|
|
608
|
+
tune(vector: number[] | Float32Array): Float32Array;
|
|
609
|
+
/**
|
|
610
|
+
* 現在の学習状態(平均ベクトル、主成分ベクトルなど)をシリアライズして出力します。
|
|
611
|
+
* エッジ環境でのインスタンス再構築時に役立ちます。
|
|
612
|
+
*/
|
|
613
|
+
exportState(): string;
|
|
614
|
+
/**
|
|
615
|
+
* シリアライズされた学習状態から WhiteningAdapter を復元します。
|
|
616
|
+
*/
|
|
617
|
+
static importState(stateJson: string): WhiteningAdapter;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
declare class ColbertAdapter {
|
|
621
|
+
private wasm;
|
|
622
|
+
constructor();
|
|
623
|
+
/**
|
|
624
|
+
* 単一のクエリと単一のドキュメント間の Late Interaction (MaxSim) スコアを計算します。
|
|
625
|
+
*
|
|
626
|
+
* @param queryTokens クエリのトークンベクトル行列 (要素数 = queryLength * dim の平坦化された配列)
|
|
627
|
+
* @param documentTokens ドキュメントのトークンベクトル行列
|
|
628
|
+
* @param dim ベクトルの次元数
|
|
629
|
+
* @returns MaxSimスコア
|
|
630
|
+
*/
|
|
631
|
+
score(queryTokens: Float32Array, documentTokens: Float32Array, dim: number): number;
|
|
632
|
+
/**
|
|
633
|
+
* クエリと複数のドキュメント間の MaxSim スコアを計算し、スコアの降順にソートして返します。
|
|
369
634
|
*
|
|
370
|
-
* @param
|
|
371
|
-
* @param
|
|
372
|
-
* @
|
|
373
|
-
* @
|
|
635
|
+
* @param queryTokens クエリのトークンベクトル行列
|
|
636
|
+
* @param documentTokensArray ドキュメントのトークンベクトル行列の配列
|
|
637
|
+
* @param dim ベクトルの次元数
|
|
638
|
+
* @returns スコアの降順にソートされたドキュメントのインデックスとスコアの配列
|
|
639
|
+
*/
|
|
640
|
+
rank(queryTokens: Float32Array, documentTokensArray: Float32Array[], dim: number): {
|
|
641
|
+
index: number;
|
|
642
|
+
score: number;
|
|
643
|
+
}[];
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
interface WarpPrismaConfig {
|
|
647
|
+
/**
|
|
648
|
+
* WarpVectorのアダプター (IntentAdapter, WhiteningAdapter など)
|
|
649
|
+
*/
|
|
650
|
+
adapter: WarpAdapter;
|
|
651
|
+
/**
|
|
652
|
+
* pgvector のベクトルデータが保存されているカラム名
|
|
653
|
+
* デフォルト: "embedding"
|
|
654
|
+
*/
|
|
655
|
+
vectorField?: string;
|
|
656
|
+
/**
|
|
657
|
+
* 類似度検索に使用する距離関数
|
|
658
|
+
* '<->' : ユークリッド距離 (デフォルト)
|
|
659
|
+
* '<#>' : 内積 (マイナス)
|
|
660
|
+
* '<=>' : コサイン距離
|
|
661
|
+
*/
|
|
662
|
+
distanceOperator?: "<->" | "<#>" | "<=>";
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Prisma Client に対して WarpVector の推論と pgvector のベクトル検索を
|
|
666
|
+
* 透過的に実行するメソッド `searchByVector` を追加する拡張機能です。
|
|
667
|
+
*/
|
|
668
|
+
declare const withWarpVector: (config: WarpPrismaConfig) => (client: any) => _prisma_client_extension.PrismaClientExtends<_prisma_client_runtime_client.InternalArgs<{}, {
|
|
669
|
+
$allModels: {
|
|
670
|
+
/**
|
|
671
|
+
* 生のベクトルを受け取り、WarpVectorで変換した後に pgvector 検索を行います。
|
|
672
|
+
*
|
|
673
|
+
* @param args.vector 生のベクトル (変換前)
|
|
674
|
+
* @param args.topK 取得する最大件数 (デフォルト: 10)
|
|
675
|
+
* @param args.where 追加のフィルタリング条件 (例: "category = 'science'")
|
|
676
|
+
*/
|
|
677
|
+
searchByVector<T>(this: T, args: {
|
|
678
|
+
vector: number[] | Float32Array;
|
|
679
|
+
topK?: number;
|
|
680
|
+
where?: string;
|
|
681
|
+
}): Promise<any>;
|
|
682
|
+
};
|
|
683
|
+
}, {}, {}> & _prisma_client_runtime_client.DefaultArgs>;
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* WarpEmbeddings の設定オプション
|
|
687
|
+
*/
|
|
688
|
+
interface WarpEmbeddingsOptions extends EmbeddingsParams {
|
|
689
|
+
/**
|
|
690
|
+
* ラップするベースの Embeddings インスタンス(例: OpenAIEmbeddings)
|
|
691
|
+
* 初期の密ベクトル(Dense Vector)の生成に使用されます。
|
|
692
|
+
*/
|
|
693
|
+
baseEmbeddings: Embeddings;
|
|
694
|
+
/**
|
|
695
|
+
* 初期化済みの WarpVector IntentAdapter。
|
|
696
|
+
* 意図(インテント)の変換行列とバイアスを保持しています。
|
|
697
|
+
*/
|
|
698
|
+
adapter: IntentAdapter;
|
|
699
|
+
/**
|
|
700
|
+
* (オプション) 検索クエリに使用する初期の意図(インテント)名。
|
|
701
|
+
* 指定しない場合、検索を実行する前に `setIntent()` で設定する必要があります。
|
|
702
|
+
*/
|
|
703
|
+
intentName?: string;
|
|
704
|
+
/**
|
|
705
|
+
* (オプション) アフィン変換後に適用する非線形活性化関数('relu', 'sigmoid', 'tanh'など)。
|
|
374
706
|
*/
|
|
375
|
-
|
|
707
|
+
activation?: Activation;
|
|
708
|
+
/**
|
|
709
|
+
* (オプション) 単一の意図を指定する代わりに、自己アテンションベースの
|
|
710
|
+
* 動的意図合成(Auto-blending)を使用するかどうか。
|
|
711
|
+
* true の場合、クエリ実行時に `intentName` は無視されます。
|
|
712
|
+
*/
|
|
713
|
+
autoBlend?: boolean;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* WarpEmbeddings は、LangChain の BaseEmbeddings インスタンスをラップするクラスです。
|
|
717
|
+
* `embedQuery` メソッドをインターセプトし、ベースベクトルを生成した後に、
|
|
718
|
+
* 現在のコンテキスト/意図に基づいて動的なアフィン変換(WarpVector)を適用します。
|
|
719
|
+
*
|
|
720
|
+
* VectorStore へ正確なセマンティクスとして保存(インデックス)できるよう、
|
|
721
|
+
* ドキュメントは変換されず、通常通り埋め込まれます。
|
|
722
|
+
* ユーザーの「検索クエリ」に対してのみワープが適用されます。
|
|
723
|
+
*
|
|
724
|
+
* @example
|
|
725
|
+
* ```typescript
|
|
726
|
+
* import { OpenAIEmbeddings } from "@langchain/openai";
|
|
727
|
+
* import { IntentAdapter } from "warpvector";
|
|
728
|
+
* import { WarpEmbeddings } from "warpvector/integrations/langchain";
|
|
729
|
+
*
|
|
730
|
+
* const baseEmbeddings = new OpenAIEmbeddings();
|
|
731
|
+
* const adapter = new IntentAdapter({ ... });
|
|
732
|
+
*
|
|
733
|
+
* const embeddings = new WarpEmbeddings({
|
|
734
|
+
* baseEmbeddings,
|
|
735
|
+
* adapter,
|
|
736
|
+
* intentName: "riskAnalysis"
|
|
737
|
+
* });
|
|
738
|
+
*
|
|
739
|
+
* // このラップされた embeddings インスタンスを LangChain の VectorStore に直接渡せます!
|
|
740
|
+
* const vectorStore = new MemoryVectorStore(embeddings);
|
|
741
|
+
* ```
|
|
742
|
+
*/
|
|
743
|
+
declare class WarpEmbeddings extends Embeddings {
|
|
744
|
+
private baseEmbeddings;
|
|
745
|
+
private adapter;
|
|
746
|
+
private intentName?;
|
|
747
|
+
private activation?;
|
|
748
|
+
private autoBlend;
|
|
749
|
+
constructor(options: WarpEmbeddingsOptions);
|
|
750
|
+
/**
|
|
751
|
+
* 実行時にアクティブな意図(インテント)を動的に切り替えます。
|
|
752
|
+
*/
|
|
753
|
+
setIntent(intentName: string, activation?: Activation): void;
|
|
754
|
+
/**
|
|
755
|
+
* 自己アテンション型の動的意図合成(Auto-blending)を有効化または無効化します。
|
|
756
|
+
*/
|
|
757
|
+
setAutoBlend(enabled: boolean): void;
|
|
758
|
+
/**
|
|
759
|
+
* ベースの embeddings モデルを使用して、ドキュメントを通常通り埋め込みます。
|
|
760
|
+
* VectorDB には客観的な空間データを含める必要があるため、インデックスされるドキュメントはワープ(変換)しません。
|
|
761
|
+
*/
|
|
762
|
+
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
763
|
+
/**
|
|
764
|
+
* ユーザーのクエリを埋め込み、WarpVector によるアフィン変換を適用します。
|
|
765
|
+
*/
|
|
766
|
+
embedQuery(document: string): Promise<number[]>;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* LlamaIndex の BaseEmbedding インターフェース互換
|
|
771
|
+
* 依存関係を避けるため、Duck Typing 用のインターフェースを定義しています。
|
|
772
|
+
*/
|
|
773
|
+
interface LlamaIndexBaseEmbedding {
|
|
774
|
+
getTextEmbedding(text: string): Promise<number[]>;
|
|
775
|
+
getQueryEmbedding(query: string): Promise<number[]>;
|
|
776
|
+
getTextEmbeddings?(texts: string[]): Promise<number[][]>;
|
|
777
|
+
}
|
|
778
|
+
interface WarpLlamaIndexEmbeddingsOptions {
|
|
779
|
+
/**
|
|
780
|
+
* ラップするベースの Embeddings インスタンス(LlamaIndex の BaseEmbedding 継承クラス)
|
|
781
|
+
* 初期の密ベクトル(Dense Vector)の生成に使用されます。
|
|
782
|
+
*/
|
|
783
|
+
baseEmbeddings: LlamaIndexBaseEmbedding;
|
|
784
|
+
/**
|
|
785
|
+
* 初期化済みの WarpVector IntentAdapter。
|
|
786
|
+
*/
|
|
787
|
+
adapter: IntentAdapter;
|
|
788
|
+
/**
|
|
789
|
+
* (オプション) 検索クエリに使用する初期の意図(インテント)名。
|
|
790
|
+
*/
|
|
791
|
+
intentName?: string;
|
|
792
|
+
/**
|
|
793
|
+
* (オプション) アフィン変換後に適用する非線形活性化関数。
|
|
794
|
+
*/
|
|
795
|
+
activation?: Activation;
|
|
796
|
+
/**
|
|
797
|
+
* (オプション) 自己アテンションベースの動的意図合成(Auto-blending)を使用するかどうか。
|
|
798
|
+
*/
|
|
799
|
+
autoBlend?: boolean;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* WarpLlamaIndexEmbeddings は、LlamaIndex の BaseEmbedding インスタンスをラップします。
|
|
803
|
+
* `getQueryEmbedding` メソッドをインターセプトし、ベースベクトルを生成した後に
|
|
804
|
+
* 現在のコンテキスト/意図に基づいて動的なアフィン変換(WarpVector)を適用します。
|
|
805
|
+
*/
|
|
806
|
+
declare class WarpLlamaIndexEmbeddings implements LlamaIndexBaseEmbedding {
|
|
807
|
+
private baseEmbeddings;
|
|
808
|
+
private adapter;
|
|
809
|
+
private intentName?;
|
|
810
|
+
private activation?;
|
|
811
|
+
private autoBlend;
|
|
812
|
+
constructor(options: WarpLlamaIndexEmbeddingsOptions);
|
|
813
|
+
setIntent(intentName: string, activation?: Activation): void;
|
|
814
|
+
setAutoBlend(enabled: boolean): void;
|
|
815
|
+
/**
|
|
816
|
+
* ドキュメントの埋め込み(インデックス作成用)は変換を行いません。
|
|
817
|
+
*/
|
|
818
|
+
getTextEmbedding(text: string): Promise<number[]>;
|
|
819
|
+
/**
|
|
820
|
+
* 複数ドキュメントの埋め込み
|
|
821
|
+
*/
|
|
822
|
+
getTextEmbeddings(texts: string[]): Promise<number[][]>;
|
|
823
|
+
/**
|
|
824
|
+
* クエリの埋め込み(検索用)にのみ WarpVector 変換を適用します。
|
|
825
|
+
*/
|
|
826
|
+
getQueryEmbedding(query: string): Promise<number[]>;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
type QuantizationType = "int8" | "binary";
|
|
830
|
+
interface QuantizationConfig {
|
|
831
|
+
/**
|
|
832
|
+
* "int8": 8-bit スカラー量子化 (Int8Array を返す)
|
|
833
|
+
* "binary": 1-bit 二値化 (Uint8Array を返す、8次元分を1バイトにパック)
|
|
834
|
+
*/
|
|
835
|
+
type: QuantizationType;
|
|
836
|
+
/**
|
|
837
|
+
* 量子化するベクトルの次元数
|
|
838
|
+
*/
|
|
839
|
+
dim: number;
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* QuantizationAdapter は、Float32のベクトルを Int8 や Binary に圧縮し、
|
|
843
|
+
* メモリ使用量と保存コストを劇的に(1/4 〜 1/32 に)削減します。
|
|
844
|
+
*/
|
|
845
|
+
declare class QuantizationAdapter implements WarpAdapter {
|
|
846
|
+
private type;
|
|
847
|
+
private dim;
|
|
848
|
+
constructor(config: QuantizationConfig);
|
|
849
|
+
tune(vector: number[] | Float32Array): Int8Array | Uint8Array;
|
|
850
|
+
/**
|
|
851
|
+
* Binary量子化された2つのベクトル間のハミング距離を計算します。
|
|
852
|
+
* ハミング距離が小さいほど類似度が高いことを意味します。
|
|
853
|
+
*/
|
|
854
|
+
static hammingDistance(a: Uint8Array, b: Uint8Array): number;
|
|
855
|
+
/**
|
|
856
|
+
* Int8量子化された2つのベクトル間のドット積(内積)を計算します。
|
|
857
|
+
*/
|
|
858
|
+
static int8DotProduct(a: Int8Array, b: Int8Array): number;
|
|
859
|
+
exportState(): string;
|
|
860
|
+
static importState(stateJson: string): QuantizationAdapter;
|
|
376
861
|
}
|
|
377
862
|
|
|
378
863
|
/**
|
|
@@ -391,13 +876,29 @@ interface BaseTrainingOptions {
|
|
|
391
876
|
/** trueの場合、事前に数エポックのテストランを行い、最適な学習率を自動探索します。デフォルト: false */
|
|
392
877
|
autoTune?: boolean;
|
|
393
878
|
}
|
|
879
|
+
/**
|
|
880
|
+
* Adam最適化のステート変数を管理する共通基底クラス
|
|
881
|
+
*/
|
|
882
|
+
declare abstract class AbstractAdamTrainer {
|
|
883
|
+
protected t: number;
|
|
884
|
+
protected mW: Float32Array;
|
|
885
|
+
protected vW: Float32Array;
|
|
886
|
+
protected mb: Float32Array;
|
|
887
|
+
protected vb: Float32Array;
|
|
888
|
+
protected initAdamState(sDim: number, tDim: number): void;
|
|
889
|
+
/**
|
|
890
|
+
* アフィンレイヤー (matrix, bias) に対する Adam のパラメータ更新を適用します。
|
|
891
|
+
* InfoNCE や Triplet など、様々な損失関数で計算された勾配(outputGradients)を元に更新を行います。
|
|
892
|
+
*/
|
|
893
|
+
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;
|
|
894
|
+
}
|
|
394
895
|
/**
|
|
395
896
|
* 確率的勾配降下法 (SGD + Momentum) を用いた学習のための共通基底クラス。
|
|
396
897
|
*
|
|
397
898
|
* @template TExample 学習に用いるデータペアの型 (入力と理想の出力のペアなど)
|
|
398
899
|
* @template TResult 最終的に学習結果として出力される重み (行列やバイアス) の型
|
|
399
900
|
*/
|
|
400
|
-
declare abstract class BaseTrainer<TExample, TResult> {
|
|
901
|
+
declare abstract class BaseTrainer<TExample, TResult> extends AbstractAdamTrainer {
|
|
401
902
|
/** 学習用サンプルの配列 */
|
|
402
903
|
protected examples: TExample[];
|
|
403
904
|
/** 入力ベクトル(ソース)の次元数 */
|
|
@@ -447,20 +948,11 @@ declare abstract class BaseTrainer<TExample, TResult> {
|
|
|
447
948
|
*/
|
|
448
949
|
protected findBestLearningRate(options: BaseTrainingOptions): number;
|
|
449
950
|
/**
|
|
450
|
-
*
|
|
951
|
+
* Adam オプティマイザによる1ステップのパラメータ更新を実行します。
|
|
451
952
|
* 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 モメンタム係数
|
|
953
|
+
* WASM 版の Adam 実装ができるまではネイティブ JS で処理します。
|
|
462
954
|
*/
|
|
463
|
-
protected
|
|
955
|
+
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
956
|
}
|
|
465
957
|
|
|
466
958
|
/**
|
|
@@ -475,8 +967,16 @@ interface TrainingExample {
|
|
|
475
967
|
}
|
|
476
968
|
/**
|
|
477
969
|
* 学習時の最適化オプション
|
|
970
|
+
export interface TrainingOptions extends BaseTrainingOptions {}
|
|
971
|
+
|
|
972
|
+
/**
|
|
973
|
+
* IntentTrainer のオンライン学習オプション
|
|
478
974
|
*/
|
|
479
|
-
interface
|
|
975
|
+
interface IntentOnlineOptions {
|
|
976
|
+
/** 1ステップの学習率 (デフォルト: 0.01) */
|
|
977
|
+
learningRate?: number;
|
|
978
|
+
/** L2正則化の強さ (デフォルト: 0.001) */
|
|
979
|
+
regularization?: number;
|
|
480
980
|
}
|
|
481
981
|
/**
|
|
482
982
|
* サンプルデータから最適な `IntentWeights` (行列Wとバイアスb) を
|
|
@@ -507,13 +1007,109 @@ declare class IntentTrainer extends BaseTrainer<TrainingExample, IntentWeights>
|
|
|
507
1007
|
* ユーザーのクリックなどの 1 回のフィードバックからリアルタイムに重みを微調整します。
|
|
508
1008
|
*
|
|
509
1009
|
* @param {IntentWeights} currentWeights - 現在の重み
|
|
510
|
-
* @param {
|
|
511
|
-
* @param {
|
|
512
|
-
* @param {number} learningRate - 1ステップの学習率 (デフォルト: 0.01)
|
|
513
|
-
* @param {number} regularization - L2正則化の強さ (デフォルト: 0.001)
|
|
1010
|
+
* @param {TrainingExample} example - アンカー、正解を含む学習データ
|
|
1011
|
+
* @param {IntentOnlineOptions} [options={}] - 学習オプション
|
|
514
1012
|
* @returns {IntentWeights} 微調整された新しい重み
|
|
515
1013
|
*/
|
|
516
|
-
updateOnline(currentWeights: IntentWeights,
|
|
1014
|
+
updateOnline(currentWeights: IntentWeights, example: TrainingExample, options?: IntentOnlineOptions): Promise<IntentWeights>;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* 学習データのペア(Anchor, Positive, Negative)
|
|
1019
|
+
* @interface TripletExample
|
|
1020
|
+
*/
|
|
1021
|
+
interface TripletExample {
|
|
1022
|
+
/** 基準となるベクトル(検索クエリなど) */
|
|
1023
|
+
anchor: number[] | Float32Array;
|
|
1024
|
+
/** Anchorに近づけたい正解ベクトル(クリックされた商品など) */
|
|
1025
|
+
positive: number[] | Float32Array;
|
|
1026
|
+
/** Anchorから遠ざけたい不正解ベクトル(スルーされた商品など) */
|
|
1027
|
+
negative: number[] | Float32Array;
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* TripletTrainer のオンライン学習オプション
|
|
1031
|
+
*/
|
|
1032
|
+
interface TripletOnlineOptions {
|
|
1033
|
+
/** 1ステップの学習率 (デフォルト: 0.01) */
|
|
1034
|
+
learningRate?: number;
|
|
1035
|
+
/** トリプレットロスのマージン (デフォルト: 0.1) */
|
|
1036
|
+
margin?: number;
|
|
1037
|
+
/** L2正則化の強さ (デフォルト: 0.001) */
|
|
1038
|
+
regularization?: number;
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* Contrastive Learning (トリプレットロス) を用いて、相対的な距離感から
|
|
1042
|
+
* IntentWeights (行列W と バイアスb) を学習するトレーナークラス。
|
|
1043
|
+
*
|
|
1044
|
+
* "Anchor" を変換したベクトル A' が、"Negative" よりも "Positive" に
|
|
1045
|
+
* 設定されたマージン(Margin)分だけ確実により近づくように重みを更新します。
|
|
1046
|
+
*/
|
|
1047
|
+
declare class TripletTrainer extends AbstractAdamTrainer {
|
|
1048
|
+
private dimension;
|
|
1049
|
+
/**
|
|
1050
|
+
* TripletTrainer のインスタンスを作成します。
|
|
1051
|
+
* @param {number} dimension ベクトルの次元数
|
|
1052
|
+
*/
|
|
1053
|
+
constructor(dimension: number);
|
|
1054
|
+
private toWeights;
|
|
1055
|
+
/**
|
|
1056
|
+
* オンライン学習 (フィードバックループ) 用のメソッド。
|
|
1057
|
+
* 1つのトリプレットデータからリアルタイムに重みを微調整します。
|
|
1058
|
+
*
|
|
1059
|
+
* @param {IntentWeights} currentWeights - 現在の重み
|
|
1060
|
+
* @param {TripletExample} example - アンカー、正解、不正解を含むトリプレットデータ
|
|
1061
|
+
* @param {TripletOnlineOptions} [options={}] - 学習オプション
|
|
1062
|
+
* @returns {Promise<IntentWeights>} 微調整された新しい重み
|
|
1063
|
+
*/
|
|
1064
|
+
updateOnline(currentWeights: IntentWeights, example: TripletExample, options?: TripletOnlineOptions): Promise<IntentWeights>;
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/**
|
|
1068
|
+
* 学習データのペア(Anchor, Positive, 複数のNegatives)
|
|
1069
|
+
* @interface InfoNCEExample
|
|
1070
|
+
*/
|
|
1071
|
+
interface InfoNCEExample {
|
|
1072
|
+
/** 基準となるベクトル(検索クエリなど) */
|
|
1073
|
+
anchor: number[] | Float32Array;
|
|
1074
|
+
/** Anchorに近づけたい正解ベクトル(クリックされた商品など) */
|
|
1075
|
+
positive: number[] | Float32Array;
|
|
1076
|
+
/** Anchorから遠ざけたい不正解ベクトルの配列(スルーされた商品群など) */
|
|
1077
|
+
negatives: (number[] | Float32Array)[];
|
|
1078
|
+
}
|
|
1079
|
+
/**
|
|
1080
|
+
* InfoNCETrainer のオンライン学習オプション
|
|
1081
|
+
*/
|
|
1082
|
+
interface InfoNCEOnlineOptions {
|
|
1083
|
+
/** 1ステップの学習率 (デフォルト: 0.01) */
|
|
1084
|
+
learningRate?: number;
|
|
1085
|
+
/** Softmaxの温度パラメータ (デフォルト: 0.1) */
|
|
1086
|
+
temperature?: number;
|
|
1087
|
+
/** L2正則化の強さ (デフォルト: 0.001) */
|
|
1088
|
+
regularization?: number;
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* InfoNCE Loss (Softmax Cross Entropy) を用いて、
|
|
1092
|
+
* 「1つの正解を近づけ、複数の不正解を一気に遠ざける」ように
|
|
1093
|
+
* IntentWeights (行列W と バイアスb) を学習するトレーナークラス。
|
|
1094
|
+
*/
|
|
1095
|
+
declare class InfoNCETrainer extends AbstractAdamTrainer {
|
|
1096
|
+
private dimension;
|
|
1097
|
+
/**
|
|
1098
|
+
* InfoNCETrainer のインスタンスを作成します。
|
|
1099
|
+
* @param {number} dimension ベクトルの次元数
|
|
1100
|
+
*/
|
|
1101
|
+
constructor(dimension: number);
|
|
1102
|
+
private toWeights;
|
|
1103
|
+
/**
|
|
1104
|
+
* オンライン学習 (フィードバックループ) 用のメソッド。
|
|
1105
|
+
* 1つのクエリ(Anchor)、1つのクリック(Positive)、複数のスルー(Negatives)から重みを微調整します。
|
|
1106
|
+
*
|
|
1107
|
+
* @param {IntentWeights} currentWeights - 現在の重み
|
|
1108
|
+
* @param {InfoNCEExample} example - アンカー、正解、複数の不正解を含むデータ
|
|
1109
|
+
* @param {InfoNCEOnlineOptions} [options={}] - 学習オプション
|
|
1110
|
+
* @returns {Promise<IntentWeights>} 微調整された新しい重み
|
|
1111
|
+
*/
|
|
1112
|
+
updateOnline(currentWeights: IntentWeights, example: InfoNCEExample, options?: InfoNCEOnlineOptions): Promise<IntentWeights>;
|
|
517
1113
|
}
|
|
518
1114
|
|
|
519
1115
|
/**
|
|
@@ -559,6 +1155,15 @@ declare class MigrationTrainer extends BaseTrainer<MigrationExample, ProjectionW
|
|
|
559
1155
|
protected toWeights(flatMatrix: Float32Array, bias: Float32Array): ProjectionWeights;
|
|
560
1156
|
}
|
|
561
1157
|
|
|
1158
|
+
type index_WarpEmbeddings = WarpEmbeddings;
|
|
1159
|
+
declare const index_WarpEmbeddings: typeof WarpEmbeddings;
|
|
1160
|
+
type index_WarpEmbeddingsOptions = WarpEmbeddingsOptions;
|
|
1161
|
+
type index_WarpPrismaConfig = WarpPrismaConfig;
|
|
1162
|
+
declare const index_withWarpVector: typeof withWarpVector;
|
|
1163
|
+
declare namespace index {
|
|
1164
|
+
export { index_WarpEmbeddings as WarpEmbeddings, type index_WarpEmbeddingsOptions as WarpEmbeddingsOptions, type index_WarpPrismaConfig as WarpPrismaConfig, index_withWarpVector as withWarpVector };
|
|
1165
|
+
}
|
|
1166
|
+
|
|
562
1167
|
/**
|
|
563
1168
|
* 外部のベクトルデータベース(pgvector, Pinecone, Redisなど)へ
|
|
564
1169
|
* ワープ変換後のベクトルを保存・検索するためのアダプター/ユーティリティクラス
|
|
@@ -574,7 +1179,7 @@ declare class VectorDBAdapter {
|
|
|
574
1179
|
* @param {number[] | Float32Array} vector ワープ変換後のベクトル
|
|
575
1180
|
* @returns {string} `'[0.1, 0.2, 0.3]'` のような文字列表現
|
|
576
1181
|
*/
|
|
577
|
-
static toPgvector(vector: number[] | Float32Array): string;
|
|
1182
|
+
static toPgvector(vector: number[] | Float32Array | Int8Array | Uint8Array): string;
|
|
578
1183
|
/**
|
|
579
1184
|
* Pinecone 用のクエリオブジェクトを生成します。
|
|
580
1185
|
* Pinecone クライアントに直接渡せる形式のオブジェクトを返します。
|
|
@@ -588,7 +1193,7 @@ declare class VectorDBAdapter {
|
|
|
588
1193
|
* @param {Record<string, any>} [filter] メタデータフィルタ(オプション)
|
|
589
1194
|
* @returns {Record<string, any>} Pineconeのqueryメソッド用オブジェクト
|
|
590
1195
|
*/
|
|
591
|
-
static toPineconeQuery(vector: number[] | Float32Array, topK?: number, filter?: Record<string,
|
|
1196
|
+
static toPineconeQuery(vector: number[] | Float32Array | Int8Array | Uint8Array, topK?: number, filter?: Record<string, unknown>): Record<string, unknown>;
|
|
592
1197
|
/**
|
|
593
1198
|
* Redis (RediSearch) のベクトルフィールド用に、
|
|
594
1199
|
* Float32Array をバイナリ(Uint8Array)に変換します。
|
|
@@ -601,7 +1206,251 @@ declare class VectorDBAdapter {
|
|
|
601
1206
|
* @param {number[] | Float32Array} vector ワープ変換後のベクトル
|
|
602
1207
|
* @returns {Uint8Array} バイナリデータ (Float32のバイト表現)
|
|
603
1208
|
*/
|
|
604
|
-
static toRedis(vector: number[] | Float32Array): Uint8Array;
|
|
1209
|
+
static toRedis(vector: number[] | Float32Array | Int8Array | Uint8Array): Uint8Array;
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* ベクトル・シンボリック・アーキテクチャ (VSA) / 超次元計算アダプタ
|
|
1214
|
+
*
|
|
1215
|
+
* ベクトル同士を論理的・数学的に結合(バインド)したり束ねたり(バンドル)することで、
|
|
1216
|
+
* 1つの密なベクトルの中にキーと値(メタデータなど)を埋め込み、検索空間上で
|
|
1217
|
+
* そのまま演算を行えるようにする機能を提供します。
|
|
1218
|
+
*/
|
|
1219
|
+
/**
|
|
1220
|
+
* VSA演算のオプション
|
|
1221
|
+
*/
|
|
1222
|
+
interface VsaOptions {
|
|
1223
|
+
/** 結果をL2正規化するかどうか(デフォルト: true) */
|
|
1224
|
+
shouldNormalize?: boolean;
|
|
1225
|
+
}
|
|
1226
|
+
declare class VsaAdapter {
|
|
1227
|
+
/**
|
|
1228
|
+
* ベクトルのバンドリング (Bundling / Superposition)
|
|
1229
|
+
* 複数のベクトルを足し合わせ(重ね合わせ)て1つのベクトルに統合します。
|
|
1230
|
+
* 「A と B の両方の概念を含む」ベクトルを作成する際に使用します。
|
|
1231
|
+
*
|
|
1232
|
+
* @param vectors 束ねるベクトルの配列
|
|
1233
|
+
* @param options 演算オプション
|
|
1234
|
+
* @returns 束ねられた新しいベクトル
|
|
1235
|
+
*/
|
|
1236
|
+
static bundle(vectors: (number[] | Float32Array)[], options?: VsaOptions): Float32Array;
|
|
1237
|
+
/**
|
|
1238
|
+
* ベクトルのバインディング (Binding / Hadamard Product)
|
|
1239
|
+
* アダマール積(要素ごとの積)を用いて、2つのベクトルを「結合」します。
|
|
1240
|
+
* 例: キー(ユーザーID)と値(好み)を掛け合わせ、特有の「ユーザーの好み」ベクトルを生成します。
|
|
1241
|
+
*
|
|
1242
|
+
* @param vec1 バインドするベクトル1
|
|
1243
|
+
* @param vec2 バインドするベクトル2
|
|
1244
|
+
* @param options 演算オプション
|
|
1245
|
+
* @returns バインドされた新しいベクトル
|
|
1246
|
+
*/
|
|
1247
|
+
static bind(vec1: number[] | Float32Array, vec2: number[] | Float32Array, options?: VsaOptions): Float32Array;
|
|
1248
|
+
/**
|
|
1249
|
+
* ベクトルのアンバインディング (Unbinding)
|
|
1250
|
+
* バインドされたベクトルから、片方のベクトル(キー)を使って元の値(バリュー)を取り出します。
|
|
1251
|
+
* アダマール積によるバインディングの逆演算(要素ごとの除算)を行います。
|
|
1252
|
+
*
|
|
1253
|
+
* @param boundVec バインド済みのベクトル
|
|
1254
|
+
* @param keyVec 抽出に使用するキーベクトル
|
|
1255
|
+
* @param options 演算オプション
|
|
1256
|
+
* @returns アンバインドされて抽出されたベクトル
|
|
1257
|
+
*/
|
|
1258
|
+
static unbind(boundVec: number[] | Float32Array, keyVec: number[] | Float32Array, options?: VsaOptions): Float32Array;
|
|
1259
|
+
/**
|
|
1260
|
+
* ---------------------------------------------------------
|
|
1261
|
+
* Binary VSA (バイナリベクトル・シンボリック・アーキテクチャ)
|
|
1262
|
+
* ---------------------------------------------------------
|
|
1263
|
+
* QuantizationAdapter で 1-bit (Binary) 量子化された Uint8Array ベクトルに
|
|
1264
|
+
* 対する超次元計算を行います。XOR 演算により、超高速・極小メモリでの処理が可能です。
|
|
1265
|
+
*/
|
|
1266
|
+
/**
|
|
1267
|
+
* バイナリベクトルのバインディング (Binary Binding / XOR)
|
|
1268
|
+
* XOR (排他的論理和) を用いて2つのバイナリベクトルを結合します。
|
|
1269
|
+
* Binary VSA において、XOR は情報を結合するための標準的な演算です。
|
|
1270
|
+
*
|
|
1271
|
+
* @param bin1 バインドするバイナリベクトル1 (Uint8Array)
|
|
1272
|
+
* @param bin2 バインドするバイナリベクトル2 (Uint8Array)
|
|
1273
|
+
* @returns バインドされた新しいバイナリベクトル (Uint8Array)
|
|
1274
|
+
*/
|
|
1275
|
+
static bindBinary(bin1: Uint8Array, bin2: Uint8Array): Uint8Array;
|
|
1276
|
+
/**
|
|
1277
|
+
* バイナリベクトルのアンバインディング (Binary Unbinding / XOR)
|
|
1278
|
+
* XOR の自己逆性 (A ^ B ^ B = A) を利用して、キーを用いて元の値を抽出します。
|
|
1279
|
+
* 内部的には bindBinary と全く同じ処理です。
|
|
1280
|
+
*
|
|
1281
|
+
* @param boundBin バインド済みのバイナリベクトル (Uint8Array)
|
|
1282
|
+
* @param keyBin 抽出に使用するキーバイナリベクトル (Uint8Array)
|
|
1283
|
+
* @returns アンバインドされて抽出されたバイナリベクトル (Uint8Array)
|
|
1284
|
+
*/
|
|
1285
|
+
static unbindBinary(boundBin: Uint8Array, keyBin: Uint8Array): Uint8Array;
|
|
1286
|
+
/**
|
|
1287
|
+
* バイナリベクトルのバンドリング (Binary Bundling / Majority Vote)
|
|
1288
|
+
* 複数のバイナリベクトルを重ね合わせます。各ビット位置で 1 と 0 の出現回数をカウントし、
|
|
1289
|
+
* 多数決 (Majority Vote) で最終的なビットを決定します。
|
|
1290
|
+
*
|
|
1291
|
+
* @param bins 束ねるバイナリベクトルの配列 (Uint8Arrayの配列)
|
|
1292
|
+
* @returns 束ねられた新しいバイナリベクトル (Uint8Array)
|
|
1293
|
+
*/
|
|
1294
|
+
static bundleBinary(bins: Uint8Array[]): Uint8Array;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
interface TaskConfig {
|
|
1298
|
+
/**
|
|
1299
|
+
* 追加するタスクの重み(学習済み IntentWeights)
|
|
1300
|
+
*/
|
|
1301
|
+
weights: IntentWeights;
|
|
1302
|
+
/**
|
|
1303
|
+
* そのタスクを合成するスケール(重み付け)。
|
|
1304
|
+
* 1.0 が標準、0.5で半分、マイナスで逆効果。
|
|
1305
|
+
*/
|
|
1306
|
+
scale: number;
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* Task Arithmetic (モデルマージ) ユーティリティ
|
|
1310
|
+
*
|
|
1311
|
+
* 複数の学習済みアダプタの重み(IntentWeights)を「タスクベクトル」として扱い、
|
|
1312
|
+
* それらを合成(マージ)することで、推論時にオーバーヘッドのない新しい静的なアダプタ行列を生成します。
|
|
1313
|
+
*/
|
|
1314
|
+
declare class TaskArithmetic {
|
|
1315
|
+
/**
|
|
1316
|
+
* 複数のタスク(IntentWeights)を合成し、新しい IntentWeights を作成します。
|
|
1317
|
+
* 数式: W_new = W_base + Σ scale_i * (W_i - W_base)
|
|
1318
|
+
*
|
|
1319
|
+
* @param tasks 合成するタスクのリスト(IntentWeights と スケールのペア)
|
|
1320
|
+
* @param baseIntent 基準となる IntentWeights。省略された場合は恒等行列(Identity)とゼロバイアスがベースになります。
|
|
1321
|
+
* @returns 完全にマージされた新しい IntentWeights
|
|
1322
|
+
*/
|
|
1323
|
+
static merge(tasks: TaskConfig[], baseIntent?: IntentWeights): IntentWeights;
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
interface PipelineStep {
|
|
1327
|
+
type: string;
|
|
1328
|
+
adapter: WarpAdapter;
|
|
1329
|
+
}
|
|
1330
|
+
interface PipelineState {
|
|
1331
|
+
type: string;
|
|
1332
|
+
state: AdapterState | null;
|
|
1333
|
+
}
|
|
1334
|
+
interface RunContext {
|
|
1335
|
+
intent?: string;
|
|
1336
|
+
version?: string;
|
|
1337
|
+
}
|
|
1338
|
+
interface FormatOptions {
|
|
1339
|
+
format: string;
|
|
1340
|
+
topK?: number;
|
|
1341
|
+
filter?: Record<string, unknown>;
|
|
1342
|
+
[key: string]: unknown;
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* WarpPipeline (統一インターフェース)
|
|
1346
|
+
*
|
|
1347
|
+
* 複数の WarpAdapter を直感的なビルダーパターンで数珠つなぎ(チェーン)し、
|
|
1348
|
+
* データパイプラインとして一括で実行・保存・復元するためのラッパークラスです。
|
|
1349
|
+
*/
|
|
1350
|
+
declare class WarpPipeline {
|
|
1351
|
+
inputDim: number;
|
|
1352
|
+
private steps;
|
|
1353
|
+
/**
|
|
1354
|
+
* アダプタの復元関数を保持するレジストリ。
|
|
1355
|
+
* カスタムアダプタをパイプラインで利用・復元可能にするために使用します。
|
|
1356
|
+
*/
|
|
1357
|
+
private static adapterRegistry;
|
|
1358
|
+
/**
|
|
1359
|
+
* カスタムアダプタをパイプラインのレジストリに登録します。
|
|
1360
|
+
* これにより importState でカスタムアダプタを復元可能になります。
|
|
1361
|
+
*
|
|
1362
|
+
* @param type アダプタの識別子 (例: "MyCustomAdapter")
|
|
1363
|
+
* @param importFn 状態オブジェクトからアダプタインスタンスを復元する関数
|
|
1364
|
+
*/
|
|
1365
|
+
static registerAdapter(type: string, importFn: (state: AdapterState) => WarpAdapter): void;
|
|
1366
|
+
/**
|
|
1367
|
+
* フォーマット変換ロジックを保持するレジストリ。
|
|
1368
|
+
*/
|
|
1369
|
+
private static formatRegistry;
|
|
1370
|
+
/**
|
|
1371
|
+
* カスタムの出力フォーマットを登録します。
|
|
1372
|
+
* これにより、ユーザー独自のDB形式(Milvus, Weaviateなど)への変換を動的に追加できます。
|
|
1373
|
+
*
|
|
1374
|
+
* @param format フォーマット名 (例: "pgvector")
|
|
1375
|
+
* @param formatFn 変換を行うコールバック関数
|
|
1376
|
+
*/
|
|
1377
|
+
static registerFormat(format: string, formatFn: (vector: OutputVector, options: FormatOptions) => unknown): void;
|
|
1378
|
+
constructor(inputDim: number);
|
|
1379
|
+
/**
|
|
1380
|
+
* IntentAdapter (意図による線形変換) をパイプラインに追加します。
|
|
1381
|
+
*/
|
|
1382
|
+
addIntent(intents?: Record<string, IntentWeights>): this;
|
|
1383
|
+
/**
|
|
1384
|
+
* LoraIntentAdapter (低ランク適応による線形変換) をパイプラインに追加します。
|
|
1385
|
+
*/
|
|
1386
|
+
addLoraIntent(rank: number, intents?: Record<string, LoraIntentWeights>): this;
|
|
1387
|
+
/**
|
|
1388
|
+
* WhiteningAdapter (PCAによる空間的偏りの除去) をパイプラインに追加します。
|
|
1389
|
+
*/
|
|
1390
|
+
addWhitening(options?: WhiteningConfig): this;
|
|
1391
|
+
/**
|
|
1392
|
+
* ProjectionAdapter (次元圧縮) をパイプラインに追加します。
|
|
1393
|
+
*/
|
|
1394
|
+
addProjection(outputDim: number, projections?: Record<string, ProjectionWeights>): this;
|
|
1395
|
+
/**
|
|
1396
|
+
* MlpAdapter (多層ニューラルネットワーク / 非線形推論) をパイプラインに追加します。
|
|
1397
|
+
*/
|
|
1398
|
+
addMlp(layers: MlpLayer[]): this;
|
|
1399
|
+
/**
|
|
1400
|
+
* QuantizationAdapter (ベクトル量子化 / 圧縮) をパイプラインに追加します。
|
|
1401
|
+
* これは通常、パイプラインの最後のステップとして使用されます。
|
|
1402
|
+
*/
|
|
1403
|
+
quantize(type: QuantizationType): this;
|
|
1404
|
+
/**
|
|
1405
|
+
* カスタムアダプタを直接パイプラインの末尾に追加します。
|
|
1406
|
+
* (ビルダーパターンで独自の拡張アダプタを組み込む際に使用します)
|
|
1407
|
+
*
|
|
1408
|
+
* @param type アダプタの識別子(レジストリ登録名と一致させることを推奨)
|
|
1409
|
+
* @param adapter WarpAdapterを実装したインスタンス
|
|
1410
|
+
*/
|
|
1411
|
+
addStep(type: string, adapter: WarpAdapter): this;
|
|
1412
|
+
/**
|
|
1413
|
+
* パイプライン内に WASM などの非同期初期化を必要とするアダプタが含まれている場合、
|
|
1414
|
+
* それらを一括でセットアップします。
|
|
1415
|
+
*/
|
|
1416
|
+
init(): Promise<void>;
|
|
1417
|
+
/**
|
|
1418
|
+
* パイプラインを順次実行し、入力ベクトルを最終的な表現に変換します。
|
|
1419
|
+
*
|
|
1420
|
+
* @param vector 変換元のベースベクトル
|
|
1421
|
+
* @param context インテントやバージョンなどのコンテキスト情報
|
|
1422
|
+
* @returns パイプラインを通過した最終的なベクトル (Float32Array または Uint8Array/Int8Array)
|
|
1423
|
+
*/
|
|
1424
|
+
run(vector: InputVector, context?: RunContext): OutputVector;
|
|
1425
|
+
/**
|
|
1426
|
+
* 複数のベクトル(バッチ)を一括でパイプラインに通します。
|
|
1427
|
+
* 内部の tuneBatch が実装されているアダプタでは WASM/SIMD による高速処理が適用されます。
|
|
1428
|
+
*
|
|
1429
|
+
* @param vectors 変換元のベースベクトルの配列
|
|
1430
|
+
* @param context インテントやバージョンなどのコンテキスト情報
|
|
1431
|
+
* @returns 変換されたベクトルの配列
|
|
1432
|
+
*/
|
|
1433
|
+
runBatch(vectors: InputVector[], context?: RunContext): OutputVector[];
|
|
1434
|
+
/**
|
|
1435
|
+
* ベクトル変換から特定データベース向けのフォーマットまでを1回の呼び出しで行います。
|
|
1436
|
+
*
|
|
1437
|
+
* @param vector 変換元のベースベクトル
|
|
1438
|
+
* @param dbOptions フォーマットの指定オプション
|
|
1439
|
+
* @param context パイプラインのコンテキスト
|
|
1440
|
+
* @returns 指定されたデータベース形式のオブジェクトや文字列
|
|
1441
|
+
*/
|
|
1442
|
+
runAndFormat(vector: InputVector, dbOptions: FormatOptions, context?: RunContext): unknown;
|
|
1443
|
+
/**
|
|
1444
|
+
* パイプライン内の全アダプタの状態(学習済みの重みなど)を JSON 化可能な配列として出力します。
|
|
1445
|
+
* これにより、DBやRedis等への永続化が容易になります。
|
|
1446
|
+
*/
|
|
1447
|
+
exportState(): PipelineState[];
|
|
1448
|
+
/**
|
|
1449
|
+
* エクスポートされた JSON 状態から、パイプラインを完全に復元(再構築)します。
|
|
1450
|
+
* @param states exportState で出力された配列
|
|
1451
|
+
* @returns 復元された新しい WarpPipeline インスタンス
|
|
1452
|
+
*/
|
|
1453
|
+
static importState(states: PipelineState[]): WarpPipeline;
|
|
605
1454
|
}
|
|
606
1455
|
|
|
607
|
-
export { type Activation, IntentAdapter, IntentTrainer, type IntentWeights, LoraIntentAdapter, type LoraIntentWeights, type MigrationExample, type MigrationOptions, MigrationTrainer, ProjectionAdapter, type ProjectionWeights, type TrainingExample, type
|
|
1456
|
+
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 };
|