跳到主要内容

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