跳至主要內容

Vectors API

Vectors API 提供在 Mastra 中使用向量嵌入進行語意搜尋及相似度配對的方法。

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

取得向量儲存庫的實例:

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

向量方法
向量方法 的直接連結

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

取得指定向量索引的資料:

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

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

建立新的向量索引:

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

新增或更新向量
新增或更新向量 的直接連結

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

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')