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/README.md
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
# warpvector
|
|
1
|
+
# warpvector 🌌
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/warpvector)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](#)
|
|
6
|
+
[](#)
|
|
2
7
|
|
|
3
8
|
`warpvector` は、AIモデルの再学習や重い再推論を行うことなく、検索クエリやコンテキスト(意図)に応じてベクトル空間を動的に変形させる、TypeScriptネイティブの軽量ミドルウェア・ユーティリティです。
|
|
4
9
|
|
|
@@ -10,44 +15,80 @@
|
|
|
10
15
|
|
|
11
16
|
従来のベクトル検索は静的であり、事前に生成された埋め込みベクトルの距離(類似度)に依存していました。コンテキストに応じた検索の微調整を行いたい場合、これまではメタデータのフィルタリングに頼るか、重い指示チューニング型モデルを再度動かすしかなく、リアルタイム性や柔軟性に欠けていました。
|
|
12
17
|
|
|
13
|
-
`warpvector` は、次世代のベクトルデータベースのミドルウェアとして機能します。ベースとなるベクトルデータはそのままに、検索の瞬間に数KBの軽量な「意図行列(Matrix)」と「バイアス(Bias
|
|
18
|
+
`warpvector` は、次世代のベクトルデータベースのミドルウェアとして機能します。ベースとなるベクトルデータはそのままに、検索の瞬間に数KBの軽量な「意図行列(Matrix)」と「バイアス(Bias)」を適用することで、ベクトル空間全体をインメモリで高速に変形させます。これにより、ファイルやデータ間の意味的類似性をユーザーの「真の意図」に極限まで近づけ、次世代の検索体験を提供します。
|
|
14
19
|
|
|
15
20
|
---
|
|
16
21
|
|
|
17
|
-
##
|
|
18
|
-
|
|
19
|
-
より詳しい使い方や API の詳細については、以下のドキュメントをご参照ください。
|
|
22
|
+
## 🚀 主な特徴
|
|
20
23
|
|
|
21
|
-
- [
|
|
22
|
-
-
|
|
23
|
-
- [
|
|
24
|
+
- **統一された `WarpAdapter` インターフェース [✨ NEW]:** 全てのアダプターが同一の `tune()` メソッドを備えたインターフェースを実装。設計の共通化により、Prisma拡張やLangChain拡張にどのアダプターでもシームレスに組み込めます。
|
|
25
|
+
- **次世代DBミドルウェア:** 既存のベクトルDB(Pinecone, Qdrant, DuckDBなど)とフロントエンドの間に立ち、動的なコンテキストルーティングを提供。
|
|
26
|
+
- **動的アフィン変換 & 非線形MLP [NEW]:** 単純な行列変換($W \cdot x + b$)に加え、WASMを用いた超高速な多層パーセプトロン(MLP)と非線形活性化関数(ReLU, Sigmoid, Tanh)による高度な空間変形をサポート。
|
|
27
|
+
- **オンライン等方化 (Whitening) [NEW]:** Oja's Rule を用いたオンラインPCAにより、OpenAI `ada-002` などが抱える「検索空間の極端な偏り(異方性)」をエッジ側でストリーミング補正し、検索精度を劇的に向上。
|
|
28
|
+
- **ColBERT / Late Interaction (WASM) [✨ NEW]:** 単一ベクトルの代わりに「トークン行列」を用いて検索する最高峰の手法 (ColBERT) を WASM 化。TS環境では絶望的に遅い MaxSim 演算を爆速で処理し、RAGの検索品質を極限まで引き上げます。
|
|
29
|
+
- **Vector Quantization (量子化) [✨ NEW]:** `Float32` のベクトルを `Int8` (スカラー量子化: メモリ1/4) または `Binary` (二値化: メモリ1/32) に圧縮。大規模ベクトルデータの保持コストを激減させ、ハミング距離計算で超高速検索を実現します。
|
|
30
|
+
- **Hybrid Search (RRF & RSF) [✨ NEW]:** ベクトル検索(Dense)とキーワード検索(Sparse/BM25)の結果を統合するハイブリッド検索ユーティリティ(Reciprocal Rank Fusionなど)を内蔵。
|
|
31
|
+
- **WASM/SIMDによる超高速処理:** 行列変換、PCA更新、ColBERT処理にはAssemblyScriptでコンパイルされたインラインWebAssembly(WASM)バックエンドを呼び出し、計算速度を最大化します。
|
|
32
|
+
- **InfoNCE & Triplet 学習エンジン (Adam Optimizer内蔵) [NEW]:** Pythonサーバー不要。ユーザーのフィードバックから Contrastive Learning (複数Negative対応の対照学習) をエッジワーカー上で直接オンライン学習。
|
|
33
|
+
- **LoRA (低ランク適応) アーキテクチャ:** `LoraIntentAdapter` により、超高次元ベクトル(1536次元など)でもメモリ使用量・計算量を劇的に削減。
|
|
34
|
+
- **Prisma + pgvector ネイティブ統合拡張 [NEW]:** Prisma Client Extensionとして透過的に統合。複雑なSQLを書かずに WarpVector で推論・補正されたベクトルでのデータベース検索がメソッド1つで完結。
|
|
35
|
+
- **TypeScriptネイティブ & ゼロ依存:** 外部の機械学習ライブラリは一切不要。Cloudflare Workers、Bun、Node.jsなどのモダンなサーバーレス/エッジ環境に完全対応。
|
|
24
36
|
|
|
25
37
|
---
|
|
26
38
|
|
|
27
|
-
##
|
|
39
|
+
## 基本的な使い方 (WarpPipeline)
|
|
28
40
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- **非線形空間のサポート:** 空間をさらに複雑に歪める非線形活性化関数 (`relu`, `sigmoid`, `tanh`) をネイティブサポート。
|
|
34
|
-
- **LoRA (低ランク適応) アーキテクチャ:** `LoraIntentAdapter` により、超高次元ベクトル(1536次元など)でもメモリ使用量・計算量を劇的に削減。数百万件のベクトルデータに対しても瞬時にワープ可能。
|
|
35
|
-
- **最先端の幾何学アプローチ:** 空間の幾何学構造(コサイン類似度)を破壊せずにベクトルを補間する球面線形補間(`slerp`)を標準搭載。
|
|
36
|
-
- **TypeScriptネイティブ & ゼロ依存(Zero-dependency):** Python環境や重厚な機械学習ライブラリは一切不要。`Float32Array` をベースにした極限まで無駄を削ぎ落とした行列演算ロジック。
|
|
37
|
-
- **エッジ・ローカルファースト対応:** 超軽量かつ高速に動作するため、Cloudflare Workers、Bun、Node.jsなどのモダンなサーバーレス/エッジ環境に即座に組み込み可能。
|
|
41
|
+
新しく導入された `WarpPipeline` を使うと、複雑なベクトル変換(非線形推論、インテント変換、量子化)からDBフォーマットへの出力までを数行で直感的に記述できます。
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { WarpPipeline } from 'warpvector';
|
|
38
45
|
|
|
39
|
-
|
|
46
|
+
// 1. パイプラインの構築
|
|
47
|
+
const pipeline = new WarpPipeline(1536)
|
|
48
|
+
.addMlp(layers) // 非線形変換 (WASM使用)
|
|
49
|
+
.addIntent({ "domain_x": intentWeights }) // ユーザーごとに空間を歪める
|
|
50
|
+
.quantize("int8"); // 最後に Int8 に量子化して圧縮
|
|
40
51
|
|
|
41
|
-
|
|
52
|
+
// 2. 非同期初期化 (WASMモジュールのセットアップなどを一括実行)
|
|
53
|
+
await pipeline.init();
|
|
42
54
|
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
// 3. 超高速バッチ処理 (WASM/SIMD対応)
|
|
56
|
+
const batchVectors = [[0.1, ...], [0.5, ...]]; // 大量データ
|
|
57
|
+
const results = pipeline.runBatch(batchVectors, { intent: "domain_x" });
|
|
45
58
|
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
// 4. Vector DB 用フォーマットへの直接出力
|
|
60
|
+
const pineconeQuery = pipeline.runAndFormat(
|
|
61
|
+
rawVector,
|
|
62
|
+
{ format: "pinecone", topK: 10, filter: { genre: "action" } }
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// 5. パイプライン丸ごとの永続化と復元
|
|
66
|
+
const stateJson = pipeline.exportState();
|
|
67
|
+
const restoredPipeline = WarpPipeline.importState(stateJson);
|
|
68
|
+
```
|
|
48
69
|
|
|
49
|
-
|
|
50
|
-
|
|
70
|
+
## 各機能のドキュメント (Documentation)
|
|
71
|
+
|
|
72
|
+
各機能のより詳細な仕組み、コードスニペット、ユースケースについては以下の個別ドキュメントをご参照ください。
|
|
73
|
+
|
|
74
|
+
1. **[コアアダプタ (Core Adapters)](./docs/1-core-adapters.md)**
|
|
75
|
+
- `IntentAdapter`, `ProjectionAdapter`, `LoraIntentAdapter` の基本
|
|
76
|
+
2. **[ニューラルネットワーク (Neural Networks)](./docs/2-neural-networks.md)**
|
|
77
|
+
- `MlpAdapter` を用いた多層パーセプトロンと非線形活性化関数
|
|
78
|
+
3. **[オンライン等方化・PCA (Whitening)](./docs/3-whitening-pca.md)**
|
|
79
|
+
- 空間的偏り (異方性) のストリーミング学習による除去
|
|
80
|
+
4. **[量子化と圧縮 (Quantization)](./docs/4-quantization.md)**
|
|
81
|
+
- `Int8` (1/4圧縮) および `Binary` (1/32圧縮) による高速化と省メモリ化
|
|
82
|
+
5. **[Late Interaction / ColBERT](./docs/5-colbert.md)**
|
|
83
|
+
- WASM を用いた MaxSim 演算による緻密なトークン照合
|
|
84
|
+
6. **[ハイブリッド検索フュージョン (Hybrid Search)](./docs/6-hybrid-search.md)**
|
|
85
|
+
- ベクトル検索とキーワード検索の統合 (`RRF`, `RSF`)
|
|
86
|
+
7. **[オンライン学習エンジン (Trainers)](./docs/7-trainers.md)**
|
|
87
|
+
- 対照学習によるリアルタイムな空間最適化 (`InfoNCETrainer`, `TripletTrainer`)
|
|
88
|
+
8. **[エコシステム統合 (Integrations)](./docs/8-integrations.md)**
|
|
89
|
+
- `LangChain`, `LlamaIndex`, `Prisma + pgvector` とのシームレスな連携
|
|
90
|
+
9. **[状態の永続化・シリアライズ (Serialization)](./docs/9-serialization.md)**
|
|
91
|
+
- 学習結果の JSON / バイナリ形式での保存と復元
|
|
51
92
|
|
|
52
93
|
---
|
|
53
94
|
|
|
@@ -57,124 +98,248 @@
|
|
|
57
98
|
npm install warpvector
|
|
58
99
|
# または
|
|
59
100
|
bun add warpvector
|
|
60
|
-
|
|
61
101
|
```
|
|
62
102
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## 🛠 クイックスタート
|
|
106
|
+
|
|
107
|
+
### 1. 基本的なアフィン変換 (IntentAdapter)
|
|
66
108
|
|
|
67
|
-
|
|
68
|
-
|
|
109
|
+
```typescript
|
|
110
|
+
import { IntentAdapter } from 'warpvector';
|
|
111
|
+
|
|
112
|
+
// 意図ごとの変換行列とバイアスを定義
|
|
113
|
+
const myIntents = {
|
|
69
114
|
riskAnalysis: {
|
|
70
115
|
matrix: [
|
|
71
116
|
[1.2, 0.1, -0.4],
|
|
72
117
|
[-0.1, 1.5, 0.2],
|
|
73
118
|
[0.3, -0.2, 1.1],
|
|
74
119
|
],
|
|
75
|
-
bias: [0.05, -0.1, 0.2]
|
|
76
|
-
|
|
77
|
-
},
|
|
78
|
-
economicImpact: {
|
|
79
|
-
matrix: [
|
|
80
|
-
[0.5, 0.8, 0.1],
|
|
81
|
-
[0.2, 0.6, -0.3],
|
|
82
|
-
[-0.1, 0.1, 1.4],
|
|
83
|
-
],
|
|
84
|
-
bias: [-0.2, 0.4, 0.0],
|
|
85
|
-
routingVector: [0.0, 1.0, 0.0]
|
|
86
|
-
},
|
|
120
|
+
bias: [0.05, -0.1, 0.2]
|
|
121
|
+
}
|
|
87
122
|
};
|
|
88
123
|
|
|
89
|
-
// 2. アダプターの初期化
|
|
90
|
-
// ※初期化時にWASMエンジンが自動的にマウントされます
|
|
91
124
|
const adapter = new IntentAdapter(myIntents);
|
|
92
|
-
|
|
93
|
-
// 3. AIモデルから生成された標準のベースベクトル
|
|
94
125
|
const baseVector = [0.15, -0.23, 0.88];
|
|
95
126
|
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
127
|
+
// "riskAnalysis" の意図に合わせてベクトルをワープ
|
|
128
|
+
const warpedVector = adapter.tune(baseVector, "riskAnalysis");
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### 2. 多層ニューラルネットワークの高速推論 (MlpAdapter) [✨ NEW]
|
|
132
|
+
|
|
133
|
+
WASMバックエンドにより、ブラウザやエッジ環境で重厚なフレームワークなしに多層MLPの推論が可能です。
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { MlpAdapter } from 'warpvector';
|
|
137
|
+
|
|
138
|
+
// 1536次元から128次元の中間層を経て2次元に出力する2層MLP
|
|
139
|
+
const mlp = new MlpAdapter([
|
|
140
|
+
{ inputDim: 1536, outputDim: 128, activation: "relu" },
|
|
141
|
+
{ inputDim: 128, outputDim: 2, activation: "linear" }
|
|
142
|
+
]);
|
|
143
|
+
|
|
144
|
+
mlp.setLayerWeights(0, matrix1, bias1);
|
|
145
|
+
mlp.setLayerWeights(1, matrix2, bias2);
|
|
146
|
+
|
|
147
|
+
// 超高速非線形推論 (WASM)
|
|
148
|
+
const output = mlp.tune(baseVector);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### 3. 検索空間の等方化 (Online Whitening) [✨ NEW]
|
|
152
|
+
|
|
153
|
+
事前学習済みモデル(ada-002等)特有の「全ての類似度が高く出てしまう空間の偏り」をオンラインで自動補正します。
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { WhiteningAdapter } from 'warpvector';
|
|
157
|
+
|
|
158
|
+
// トップ1つの主成分(偏り)をストリーミング学習して除去するアダプター
|
|
159
|
+
const adapter = new WhiteningAdapter(1536, { learningRate: 0.01, numComponents: 1 });
|
|
160
|
+
|
|
161
|
+
// ベクトルを受信するたびに自動で偏りの方向を学習 (Oja's Rule)
|
|
162
|
+
adapter.update(rawVector1);
|
|
163
|
+
adapter.update(rawVector2);
|
|
164
|
+
|
|
165
|
+
// 検索時に偏りを除去(検索の解像度が劇的に向上)
|
|
166
|
+
const whitenedVector = adapter.tune(searchVector);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 4. Prisma + pgvector ネイティブ統合 [✨ NEW]
|
|
170
|
+
|
|
171
|
+
WarpVector を Prisma Client Extension としてアタッチすることで、ベクトル推論とデータベース検索を統合します。
|
|
99
172
|
|
|
100
|
-
|
|
101
|
-
|
|
173
|
+
```typescript
|
|
174
|
+
import { PrismaClient } from '@prisma/client';
|
|
175
|
+
import { withWarpVector } from 'warpvector/integrations/prisma';
|
|
176
|
+
import { WhiteningAdapter } from 'warpvector';
|
|
102
177
|
|
|
103
|
-
|
|
104
|
-
// クエリベクトルの中身を分析し、最適なインテント比率を自動で推論・合成して適用します
|
|
105
|
-
const autoBlendedVector = adapter.tuneAutoBlended(baseVector);
|
|
178
|
+
const adapter = new WhiteningAdapter(1536);
|
|
106
179
|
|
|
107
|
-
//
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
180
|
+
// Prisma Client に WarpVector 拡張をアタッチ
|
|
181
|
+
const prisma = new PrismaClient().$extends(
|
|
182
|
+
withWarpVector({
|
|
183
|
+
adapter: adapter,
|
|
184
|
+
vectorField: "embedding", // DB上の pgvector 保存先カラム名
|
|
185
|
+
distanceOperator: "<=>" // コサイン距離を使用
|
|
186
|
+
})
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// 生のベクトルを渡すだけ!(内部でWarpVector推論とpgvector用SQL生成が自動で行われる)
|
|
190
|
+
const results = await prisma.document.searchByVector({
|
|
191
|
+
vector: rawSearchVector,
|
|
192
|
+
topK: 10,
|
|
193
|
+
where: "category = 'science'" // 通常のSQL WHERE条件も併用可能
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### 5. 高品質 RAG のための Late Interaction (ColBERT) [✨ NEW]
|
|
198
|
+
|
|
199
|
+
単一ベクトルでは潰れてしまう細かいニュアンスを、トークンごとの行列で保持し、WASMの超高速 MaxSim 演算によって緻密に照合します。
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { ColbertAdapter } from 'warpvector';
|
|
203
|
+
|
|
204
|
+
const adapter = new ColbertAdapter();
|
|
205
|
+
|
|
206
|
+
// queryTokens: クエリのトークン行列 (平坦化された Float32Array)
|
|
207
|
+
// documentTokensArray: 各ドキュメントのトークン行列の配列
|
|
208
|
+
const results = adapter.rank(queryTokens, [doc1Tokens, doc2Tokens, doc3Tokens], 1536);
|
|
209
|
+
|
|
210
|
+
console.log(results); // [{ index: 1, score: 1.44 }, { index: 0, score: 0.76 }, ...]
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### 6. Hybrid Search / 検索結果の統合 (RRF & RSF) [✨ NEW]
|
|
214
|
+
|
|
215
|
+
ベクトル検索結果(Dense)とキーワード検索結果(Sparse/BM25)をシームレスに統合(フュージョン)するための独立したアルゴリズムを提供します。WarpVector のコアなベクトル変形機能と組み合わせることで、最高峰の検索精度を達成できます。
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import { rrf, rsf } from 'warpvector';
|
|
219
|
+
|
|
220
|
+
const denseResults = [
|
|
221
|
+
{ id: "doc1", score: 0.95 },
|
|
222
|
+
{ id: "doc2", score: 0.88 }
|
|
111
223
|
];
|
|
112
|
-
// 行列の数が多い場合、内部で自動的にWASMメモリへ転送され高速に処理されます
|
|
113
|
-
const tunedBatch = adapter.tuneBatch(batchVectors, "riskAnalysis");
|
|
114
|
-
const normalizedVector = normalize(tunedBatch[0]); // コサイン類似度計算用などにL2正規化
|
|
115
224
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
225
|
+
const sparseResults = [
|
|
226
|
+
{ id: "doc2", score: 15.2 },
|
|
227
|
+
{ id: "doc1", score: 12.1 }
|
|
228
|
+
];
|
|
229
|
+
|
|
230
|
+
// RRF (Reciprocal Rank Fusion): スコアの絶対値に依存せず、順位(Rank)のみを使って公平に統合
|
|
231
|
+
const rrfResults = rrf([denseResults, sparseResults]);
|
|
120
232
|
|
|
121
|
-
//
|
|
122
|
-
const
|
|
233
|
+
// RSF (Relative Score Fusion): Min-Max正規化を行い、重み(Dense 70%, Sparse 30%)をつけて加算統合
|
|
234
|
+
const rsfResults = rsf([denseResults, sparseResults], [0.7, 0.3]);
|
|
123
235
|
```
|
|
124
236
|
|
|
125
|
-
|
|
237
|
+
### 7. ベクトル量子化 (Vector Quantization) [✨ NEW]
|
|
126
238
|
|
|
127
|
-
`
|
|
239
|
+
メモリ制約の厳しいエッジ環境向けに、`Float32` (32ビット) のベクトルを `Int8` (8ビットスカラー) または `Binary` (1ビット) に圧縮するアダプターを提供します。
|
|
128
240
|
|
|
129
|
-
|
|
241
|
+
```typescript
|
|
242
|
+
import { QuantizationAdapter } from 'warpvector';
|
|
130
243
|
|
|
131
|
-
|
|
244
|
+
// Int8 量子化 (メモリを 1/4 に削減)
|
|
245
|
+
const int8Adapter = new QuantizationAdapter({ type: "int8", dim: 1536 });
|
|
246
|
+
const int8Vec = int8Adapter.tune(floatVector); // Int8Array
|
|
247
|
+
const dot = QuantizationAdapter.int8DotProduct(int8Vec, int8Vec2);
|
|
132
248
|
|
|
133
|
-
|
|
249
|
+
// Binary 量子化 (メモリを 1/32 に削減)
|
|
250
|
+
const binaryAdapter = new QuantizationAdapter({ type: "binary", dim: 1536 });
|
|
251
|
+
const binVec = binaryAdapter.tune(floatVector); // Uint8Array(192バイト)
|
|
252
|
+
const dist = QuantizationAdapter.hammingDistance(binVec, binVec2); // 超高速なXORハミング距離計算
|
|
253
|
+
```
|
|
134
254
|
|
|
135
|
-
|
|
255
|
+
### 8. 動的学習エンジン (Trainers with Adam) [✨ UPGRADED]
|
|
136
256
|
|
|
137
|
-
|
|
257
|
+
Pythonサーバーを立てることなく、ユーザーのフィードバックをもとにエッジ上でベクトル空間を最適化できます。Adamオプティマイザーと InfoNCE Loss (複数Negative) に対応しました。
|
|
138
258
|
|
|
139
|
-
|
|
259
|
+
```typescript
|
|
260
|
+
import { InfoNCETrainer } from 'warpvector';
|
|
140
261
|
|
|
141
|
-
|
|
262
|
+
const trainer = new InfoNCETrainer(1536, { learningRate: 0.001 });
|
|
142
263
|
|
|
143
|
-
-
|
|
144
|
-
|
|
145
|
-
|
|
264
|
+
// 1つの正解と複数の不正解(In-batch Negatives)を同時に学習
|
|
265
|
+
const loss = trainer.updateOnline(
|
|
266
|
+
anchorVector,
|
|
267
|
+
positiveVector,
|
|
268
|
+
[negativeVector1, negativeVector2],
|
|
269
|
+
currentWeights
|
|
270
|
+
);
|
|
271
|
+
```
|
|
146
272
|
|
|
147
|
-
|
|
273
|
+
### 9. LangChain / LlamaIndex との統合 (Integrations)
|
|
148
274
|
|
|
149
|
-
|
|
275
|
+
`warpvector` は、既存の巨大エコシステムに「たった数行」で組み込むことができます。
|
|
150
276
|
|
|
151
|
-
|
|
277
|
+
#### LangChain 統合 (`WarpEmbeddings`)
|
|
278
|
+
```typescript
|
|
279
|
+
import { OpenAIEmbeddings } from "@langchain/openai";
|
|
280
|
+
import { IntentAdapter } from "warpvector";
|
|
281
|
+
import { WarpEmbeddings } from "warpvector/integrations/langchain";
|
|
152
282
|
|
|
153
|
-
|
|
283
|
+
const baseEmbeddings = new OpenAIEmbeddings();
|
|
284
|
+
const adapter = new IntentAdapter(myIntents);
|
|
154
285
|
|
|
155
|
-
|
|
286
|
+
const warpEmbeddings = new WarpEmbeddings({
|
|
287
|
+
baseEmbeddings, adapter, intentName: "riskAnalysis"
|
|
288
|
+
});
|
|
289
|
+
// 検索時のみ動的ワープが適用されます
|
|
290
|
+
const vectorStore = new MemoryVectorStore(warpEmbeddings);
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
#### LlamaIndex 統合 (`WarpLlamaIndexEmbeddings`) [✨ NEW]
|
|
294
|
+
```typescript
|
|
295
|
+
import { OpenAIEmbedding } from "llamaindex";
|
|
296
|
+
import { WarpLlamaIndexEmbeddings } from "warpvector/integrations/llama-index";
|
|
297
|
+
|
|
298
|
+
const warpLlamaIndexEmbeddings = new WarpLlamaIndexEmbeddings({
|
|
299
|
+
baseEmbeddings: new OpenAIEmbedding(),
|
|
300
|
+
adapter: intentAdapter,
|
|
301
|
+
intentName: "riskAnalysis"
|
|
302
|
+
});
|
|
303
|
+
// LlamaIndex の VectorStoreIndex などに直接渡せます
|
|
304
|
+
```
|
|
156
305
|
|
|
157
|
-
|
|
158
|
-
- **相関の結合:** 本来は独立していた2つの特徴軸(例:「気象の数値」と「テキストのネガティブ度」)を、行列の回転作用によって同じ方向へ傾け、データ間の潜在的な相関関係を強制的に浮かび上がらせます。
|
|
306
|
+
### 10. 全アダプタの状態永続化 (Universal Serialization) [✨ NEW]
|
|
159
307
|
|
|
160
|
-
|
|
308
|
+
Cloudflare Workers 等の揮発性環境でも、オンライン学習やPCAで得られたコンポーネントを即座にJSONで保存・復元できます。
|
|
161
309
|
|
|
162
|
-
|
|
310
|
+
```typescript
|
|
311
|
+
import { WhiteningAdapter } from 'warpvector';
|
|
163
312
|
|
|
164
|
-
|
|
313
|
+
const adapter = new WhiteningAdapter(1536);
|
|
314
|
+
// ... オンライン学習 (update) を実行 ...
|
|
165
315
|
|
|
166
|
-
|
|
316
|
+
// 状態をシリアライズしてRedis等に保存
|
|
317
|
+
const stateJson = adapter.exportState();
|
|
167
318
|
|
|
168
|
-
|
|
319
|
+
// 次回起動時や別インスタンスで即座に復元
|
|
320
|
+
const restoredAdapter = WhiteningAdapter.importState(stateJson);
|
|
321
|
+
```
|
|
169
322
|
|
|
170
323
|
---
|
|
171
324
|
|
|
172
|
-
|
|
325
|
+
## 📐 数学的背景:動的アフィン変換と非線形性
|
|
326
|
+
|
|
327
|
+
入力となる標準的なベースベクトル $\mathbf{x} \in \mathbb{R}^d$ に対し、`warpvector` は以下の**アフィン写像(Affine Map)**を適用し、調律された新しいベクトル $\mathbf{x}' \in \mathbb{R}^d$ を生成します。
|
|
328
|
+
|
|
329
|
+
$$\mathbf{x}' = \sigma(\mathbf{W}_I \mathbf{x} + \mathbf{b}_I)$$
|
|
330
|
+
|
|
331
|
+
- $\mathbf{W}_I \in \mathbb{R}^{d \times d}$ :**意図変換行列(Intent Matrix)**。空間の回転や特徴量の強調(歪み)を担当します。
|
|
332
|
+
- $\mathbf{b}_I \in \mathbb{R}^d$ :**意図バイアスベクトル(Intent Bias)**。空間全体を特定のコンテキストへ平行移動(シフト)させます。
|
|
333
|
+
- $\sigma$ :**非線形活性化関数(Activation Function)**。空間を曲げ込み、複雑な意味の切り分けを可能にします (`relu`, `sigmoid`, `tanh`)。
|
|
334
|
+
|
|
335
|
+
この計算複雑度はわずか $\mathcal{O}(d^2)$ (LoRAの場合は $\mathcal{O}(d \cdot r)$)であり、WASM(WebAssembly)と `Float32Array` によるメモリアライメント最適化を活用することで、**ブラウザ上やエッジ環境でも数千〜数万件の推論を数ミリ秒で完了**させることができます。
|
|
336
|
+
|
|
337
|
+
---
|
|
173
338
|
|
|
174
|
-
|
|
339
|
+
## 🤝 貢献 (Contributing)
|
|
175
340
|
|
|
176
|
-
|
|
341
|
+
本プロジェクトはオープンソースです。新機能の追加、VectorDB用の最適化アダプターの提供、パフォーマンス改善のプルリクエストを歓迎します!
|
|
177
342
|
|
|
178
|
-
|
|
343
|
+
## 📄 ライセンス
|
|
179
344
|
|
|
180
|
-
|
|
345
|
+
MIT License
|