跳至主要內容

Vectors API

Vectors API 提供用於在 Mastra 中處理向量嵌入的方法,可用於語義搜尋和相似度比對。

使用向量
「使用向量」的直接連結

取得向量儲存的執行個體:

const vector = mastraClient.getVector('vector-name')

Vector 方法
「Vector 方法」的直接連結

取得向量索引詳情
「取得向量索引詳情」的直接連結

取得特定向量索引的資訊:

const details = await vector.details('index-name')

建立向量索引
「建立向量索引」的直接連結

建立新的向量索引:

const result = await vector.createIndex({
indexName: 'new-index',
dimension: 128,
metric: 'cosine', // 'cosine', 'euclidean', or 'dotproduct'
})

Upsert 向量
「Upsert 向量」的直接連結

在索引中新增或更新向量:

const ids = await vector.upsert({
indexName: 'my-index',
vectors: [
[0.1, 0.2, 0.3], // First vector
[0.4, 0.5, 0.6], // Second vector
],
metadata: [{ label: 'first' }, { label: 'second' }],
ids: ['id1', 'id2'], // Optional: Custom IDs
})

查詢向量
「查詢向量」的直接連結

搜尋相似向量:

const results = await vector.query({
indexName: 'my-index',
queryVector: [0.1, 0.2, 0.3],
topK: 10,
filter: { label: 'first' }, // Optional: Metadata filter
includeVector: true, // Optional: Include vectors in results
})

取得所有索引
「取得所有索引」的直接連結

列出所有可用索引:

const indexes = await vector.getIndexes()

刪除索引
「刪除索引」的直接連結

刪除向量索引:

const result = await vector.delete('index-name')