Lance Vector Store
LanceVectorStoreクラスは、Lance列形式上に構築された組み込みベクトルデータベースであるLanceDB を使用してベクトル検索を提供します。ローカル開発と本番デプロイメントの両方において、効率的なストレージと高速な類似性検索を提供します。
Factory Method
LanceVectorStoreは作成にファクトリーパターンを使用します。コンストラクタを直接使用するのではなく、静的なcreate()
メソッドを使用する必要があります。
uri:
string
LanceDBデータベースへのパスまたはクラウドデプロイメント用のURI
options?:
ConnectionOptions
LanceDBの追加接続オプション
コンストラクタの例
静的なcreateメソッドを使用してLanceVectorStore
インスタンスを作成できます:
import { LanceVectorStore } from "@mastra/lance";
// ローカルデータベースに接続
const vectorStore = await LanceVectorStore.create("/path/to/db");
// LanceDBクラウドデータベースに接続
const cloudStore = await LanceVectorStore.create("db://host:port");
// オプション付きでクラウドデータベースに接続
const s3Store = await LanceVectorStore.create("s3://bucket/db", {
storageOptions: { timeout: '60s' }
});
メソッド
createIndex()
tableName:
string
インデックスを作成するテーブルの名前
indexName:
string
作成するインデックス(カラム名)の名前
dimension:
number
ベクトルの次元数(埋め込みモデルと一致する必要があります)
metric?:
'cosine' | 'euclidean' | 'dotproduct'
= cosine
類似度検索の距離メトリック
indexConfig?:
LanceIndexConfig
= { type: 'hnsw' }
インデックス設定
LanceIndexConfig
type:
'ivfflat' | 'hnsw'
= hnsw
インデックスタイプ
string
ivfflat:
ivfflat
近似検索のためにベクトルをリストにクラスタリングします。
hnsw:
hnsw
高速な検索時間と高い再現率を提供するグラフベースのインデックス。
numPartitions?:
number
= 128
IVFインデックスのパーティション数
numSubVectors?:
number
= 16
積量子化のサブベクトル数
hnsw?:
HNSWConfig
HNSW設定
object
m?:
number
ノードあたりの最大接続数(デフォルト: 16)
efConstruction?:
number
構築時の複雑度(デフォルト: 100)
createTable()
tableName:
string
作成するテーブルの名前
data:
Record<string, unknown>[] | TableLike
テーブルの初期データ
options?:
Partial<CreateTableOptions>
追加のテーブル作成オプション
upsert()
tableName:
string
ベクトルをアップサートするテーブルの名前
vectors:
number[][]
埋め込みベクトルの配列
metadata?:
Record<string, any>[]
各ベクトルのメタデータ
ids?:
string[]
オプションのベクトルID(提供されない場合は自動生成)
query()
tableName:
string
クエリするテーブルの名前
queryVector:
number[]
クエリベクトル
topK?:
number
= 10
返す結果の数
filter?:
Record<string, any>
メタデータフィルター
includeVector?:
boolean
= false
結果にベクトルを含めるかどうか
columns?:
string[]
= []
結果に含める特定の列
includeAllColumns?:
boolean
= false
結果にすべての列を含めるかどうか
listTables()
テーブル名の配列を文字列として返します。
const tables = await vectorStore.listTables();
// ['my_vectors', 'embeddings', 'documents']
getTableSchema()
tableName:
string
記述するテーブルの名前
指定されたテーブルのスキーマを返します。
deleteTable()
tableName:
string
削除するテーブルの名前
deleteAllTables()
データベース内のすべてのテーブルを削除します。
listIndexes()
インデックス名の配列を文字列として返します。
describeIndex()
indexName:
string
記述するインデックスの名前
インデックスに関する情報を返します:
interface IndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
type: "ivfflat" | "hnsw";
config: {
m?: number;
efConstruction?: number;
numPartitions?: number;
numSubVectors?: number;
};
}
deleteIndex()
indexName:
string
削除するインデックスの名前
updateVector()
indexName:
string
ベクトルを含むインデックスの名前
id:
string
更新するベクトルのID
update:
object
更新パラメータ
object
vector?:
number[]
新しいベクトル値
metadata?:
Record<string, any>
新しいメタデータ値
deleteVector()
indexName:
string
ベクトルを含むインデックスの名前
id:
string
削除するベクトルのID
close()
データベース接続を閉じます。
レスポンスタイプ
クエリ結果は以下の形式で返されます:
interface QueryResult {
id: string;
score: number;
metadata: Record<string, any>;
vector?: number[]; // Only included if includeVector is true
document?: string; // Document text if available
}
エラーハンドリング
ストアは型付きエラーをスローし、それをキャッチできます:
try {
await store.query({
tableName: "my_vectors",
queryVector: queryVector,
});
} catch (error) {
if (error instanceof Error) {
console.log(error.message);
}
}
ベストプラクティス
- 用途に適したインデックスタイプを使用してください:
- メモリに制約がない場合は、より良いリコールとパフォーマンスのためにHNSW
- 大規模データセットでのメモリ効率を重視する場合はIVF
- 大規模データセットで最適なパフォーマンスを得るには、
numPartitions
とnumSubVectors
の値の調整を検討してください - データベースの使用が完了したら、
close()
メソッドを使用して適切に接続を閉じてください - フィルタリング操作を簡素化するため、一貫したスキーマでメタデータを保存してください