Skip to main content

向量 API

🌐 Vectors API

Vectors API 提供了在 Mastra 中处理向量嵌入以进行语义搜索和相似性匹配的方法。

🌐 The Vectors API provides methods to work with vector embeddings for semantic search and similarity matching in Mastra.

使用向量
Direct link to 使用向量

🌐 Working with Vectors

获取一个向量存储实例:

🌐 Get an instance of a vector store:

const vector = mastraClient.getVector("vector-name");

向量方法
Direct link to 向量方法

🌐 Vector Methods

获取向量索引详情
Direct link to 获取向量索引详情

🌐 Get Vector Index Details

检索有关特定向量索引的信息:

🌐 Retrieve information about a specific vector index:

const details = await vector.details("index-name");

创建向量索引
Direct link to 创建向量索引

🌐 Create Vector Index

创建新的向量索引:

🌐 Create a new vector index:

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

插入或更新向量
Direct link to 插入或更新向量

🌐 Upsert Vectors

在索引中添加或更新向量:

🌐 Add or update vectors in an index:

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
});

查询向量
Direct link to 查询向量

🌐 Query Vectors

搜索相似向量:

🌐 Search for similar vectors:

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
});

获取所有索引
Direct link to 获取所有索引

🌐 Get All Indexes

列出所有可用的索引:

🌐 List all available indexes:

const indexes = await vector.getIndexes();

删除索引
Direct link to 删除索引

🌐 Delete Index

删除向量索引:

🌐 Delete a vector index:

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