メインコンテンツまでスキップ

Momento Vector Index APIを使う

Momento Vector Index(MVI)は、AIを活用したアプリケーションで使用するベクトルデータをリアルタイムで保存・検索するために設計された、スケーラブルで開発者に優しいベクトルインデックスサービスです。

Vector Index Client

Momento Vector Indexes を操作するには、VectorIndexClient を使用する必要があります。

new PreviewVectorIndexClient({
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
configuration: VectorIndexConfigurations.Laptop.latest(),
});

Vector Index methods

Create Index

vector indexを作成

名前タイプ詳細
indexNameStringvector indexの名前
numDimensionsStringNumber of dimensions per vector.
similarityMetricStringMetric used to quantify the distance between vectors. Can be cosine similarity, inner product, or euclidean similarity. Defaults to cosine similarity.
Method response object
  • Success
  • AlreadyExists
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await vectorClient.createIndex('test-index', 2);
if (result instanceof CreateVectorIndex.Success) {
console.log("Index 'test-index' created");
} else if (result instanceof CreateVectorIndex.AlreadyExists) {
console.log("Index 'test-index' already exists");
} else if (result instanceof CreateVectorIndex.Error) {
throw new Error(
`An error occurred while attempting to create index 'test-index': ${result.errorCode()}: ${result.toString()}`
);
}

Delete Index

vector indexの削除

名前タイプ詳細
indexNameStringvector indexの名前
Method response object
  • Success
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await vectorClient.deleteIndex('test-index');
if (result instanceof DeleteVectorIndex.Success) {
console.log("Index 'test-index' deleted");
} else if (result instanceof DeleteVectorIndex.Error) {
throw new Error(
`An error occurred while attempting to delete index 'test-index': ${result.errorCode()}: ${result.toString()}`
);
}

List Indexes

vector indexesの全てのリスト

Method response object
  • Success
    • getIndexes(): VectorIndexInfo[]
      • VectorIndexInfo:
        • name: String
        • numDimensions: number
        • similarityMetric: String
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await vectorClient.listIndexes();
if (result instanceof ListVectorIndexes.Success) {
console.log(
`Indexes:\n${result
.getIndexes()
.map(index => index.toString())
.join('\n')}\n\n`
);
} else if (result instanceof ListVectorIndexes.Error) {
throw new Error(`An error occurred while attempting to list caches: ${result.errorCode()}: ${result.toString()}`);
}

Upsert Item Batch

アイテムを一括でvector indexにUpsertsします。

名前タイプ詳細
indexNameStringvector indexの名前
itemsVectorIndexItemindexにアップサートする項目。
Method response object
  • Success
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await vectorClient.upsertItemBatch('test-index', [
{
id: 'example_item_1',
vector: [1.0, 2.0],
metadata: {key1: 'value1'},
},
{
id: 'example_item_2',
vector: [3.0, 4.0],
metadata: {key2: 'value2'},
},
]);
if (result instanceof VectorUpsertItemBatch.Success) {
console.log('Successfully added items');
} else if (result instanceof VectorUpsertItemBatch.Error) {
throw new Error(`An error occurred while adding items to index: ${result.errorCode()}: ${result.toString()}`);
}

クエリのベクトルに最も近いベクトルを持つアイテムを検索する。

名前タイプ詳細
indexNameStringvector indexの名前
queryVectornumber[]検索するベクトル
topKnumber返す結果の数。デフォルトは10
metadataFieldsString[] or ALL_VECTOR_METADATA各結果とともに返すメタデータ・フィールドのリスト、またはすべてのメタデータを返すことを示す値です。指定しない場合は、メタデータは返されません。デフォルトは None
Method response object
  • Success
    • hits(): SearchHit[]
      • SearchHit:
        • id: string
        • score: number
        • metadata: Map<string, string>
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await vectorClient.search('test-index', [1.0, 2.0], {topK: 3, metadataFields: ALL_VECTOR_METADATA});
if (result instanceof VectorSearch.Success) {
console.log(`Found ${result.hits().length} matches`);
} else if (result instanceof VectorSearch.Error) {
throw new Error(`An error occurred searching index test-index: ${result.errorCode()}: ${result.toString()}`);
}

Search And Fetch Vectors

クエリのベクトルに最も近いベクトルを持つアイテムを検索します。検索ヒットしたベクトルに対応するベクトルも返します。

名前タイプ詳細
indexNameStringvector indexの名前
queryVectornumber[]検索するベクトル
topKnumber返す結果の数。デフォルトは10。
metadataFieldsString[] or ALL_VECTOR_METADATA各結果とともに返すメタデータ・フィールドのリスト、またはすべてのメタデータを返すことを示す値です。指定しない場合は、メタデータは返されません。デフォルトは None
Method response object
  • Success
    • hits(): SearchAndFetchVectorsHit[]
      • SearchAndFetchVectorsHit:
        • id: string
        • score: number
        • metadata: Map<string, string>
        • vector: number[]
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await vectorClient.searchAndFetchVectors('test-index', [1.0, 2.0], {
topK: 3,
metadataFields: ALL_VECTOR_METADATA,
});
if (result instanceof VectorSearchAndFetchVectors.Success) {
console.log(`Found ${result.hits().length} matches`);
} else if (result instanceof VectorSearchAndFetchVectors.Error) {
throw new Error(`An error occurred searching index test-index: ${result.errorCode()}: ${result.toString()}`);
}

Delete Item Batch

vector indexから項目を一括削除します。

名前タイプ詳細
indexNameStringvector indexの名前
itemsString[]IDs of the items to be deleted.
Method response object
  • Success
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await vectorClient.deleteItemBatch('test-index', ['example_item_1', 'example_item_2']);
if (result instanceof VectorUpsertItemBatch.Success) {
console.log('Successfully deleted items');
} else if (result instanceof VectorUpsertItemBatch.Error) {
throw new Error(`An error occurred while deleting items: ${result.errorCode()}: ${result.toString()}`);
}