Convex向量存储
🌐 Convex Vector Store
ConvexVector 类使用 Convex 提供向量存储和相似性搜索。它将嵌入存储在 Convex 中,并执行余弦相似度搜索。
🌐 The ConvexVector class provides vector storage and similarity search using Convex. It stores embeddings inside Convex and performs cosine similarity search.
安装Direct link to 安装
🌐 Installation
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/convex@latest
pnpm add @mastra/convex@latest
yarn add @mastra/convex@latest
bun add @mastra/convex@latest
Convex配置Direct link to Convex配置
🌐 Convex Setup
在使用 ConvexVector 之前,你需要设置 Convex 模式和存储处理器。有关设置说明,请参阅 Convex Storage Setup。
🌐 Before using ConvexVector, you need to set up the Convex schema and storage handler. See Convex Storage Setup for setup instructions.
构造函数选项Direct link to 构造函数选项
🌐 Constructor Options
deploymentUrl:
adminAuthToken:
storageFunction?:
构造函数示例Direct link to 构造函数示例
🌐 Constructor Examples
基本配置Direct link to 基本配置
🌐 Basic Configuration
import { ConvexVector } from "@mastra/convex";
const vectorStore = new ConvexVector({
id: 'convex-vectors',
deploymentUrl: "https://your-project.convex.cloud",
adminAuthToken: "your-admin-token",
});
自定义存储功能Direct link to 自定义存储功能
🌐 Custom Storage Function
const vectorStore = new ConvexVector({
id: 'convex-vectors',
deploymentUrl: "https://your-project.convex.cloud",
adminAuthToken: "your-admin-token",
storageFunction: "custom/path:handler",
});
方法Direct link to 方法
🌐 Methods
createIndex()Direct link to createIndex()
indexName:
dimension:
metric?:
await vectorStore.createIndex({
indexName: "my_vectors",
dimension: 1536,
});
upsert()Direct link to upsert()
indexName:
vectors:
metadata?:
ids?:
await vectorStore.upsert({
indexName: "my_vectors",
vectors: [[0.1, 0.2, 0.3, ...]],
metadata: [{ label: "example" }],
ids: ["vec-1"],
});
query()Direct link to query()
indexName:
queryVector:
topK?:
filter?:
includeVector?:
const results = await vectorStore.query({
indexName: "my_vectors",
queryVector: [0.1, 0.2, 0.3, ...],
topK: 5,
filter: { category: "documents" },
});
listIndexes()Direct link to listIndexes()
返回一个由索引名称组成的字符串数组。
🌐 Returns an array of index names as strings.
const indexes = await vectorStore.listIndexes();
// ["my_vectors", "embeddings", ...]
describeIndex()Direct link to describeIndex()
indexName:
返回:
🌐 Returns:
interface IndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
}
deleteIndex()Direct link to deleteIndex()
indexName:
删除索引及其所有向量。
🌐 Deletes the index and all its vectors.
await vectorStore.deleteIndex({ indexName: "my_vectors" });
updateVector()Direct link to updateVector()
通过 ID 或元数据过滤器更新单个向量。必须提供 id 或 filter 中的一个,但不能同时提供两者。
🌐 Update a single vector by ID or by metadata filter. Either id or filter must be provided, but not both.
indexName:
id?:
filter?:
update:
// Update by ID
await vectorStore.updateVector({
indexName: "my_vectors",
id: "vector123",
update: {
vector: [0.1, 0.2, 0.3],
metadata: { label: "updated" },
},
});
// Update by filter
await vectorStore.updateVector({
indexName: "my_vectors",
filter: { category: "product" },
update: {
metadata: { status: "reviewed" },
},
});
deleteVector()Direct link to deleteVector()
indexName:
id:
await vectorStore.deleteVector({ indexName: "my_vectors", id: "vector123" });
deleteVectors()Direct link to deleteVectors()
通过 ID 或元数据过滤器删除多个向量。必须提供 ids 或 filter 中的一个,但不能同时提供两者。
🌐 Delete multiple vectors by IDs or by metadata filter. Either ids or filter must be provided, but not both.
indexName:
ids?:
filter?:
// Delete by IDs
await vectorStore.deleteVectors({
indexName: "my_vectors",
ids: ["vec1", "vec2", "vec3"],
});
// Delete by filter
await vectorStore.deleteVectors({
indexName: "my_vectors",
filter: { status: "archived" },
});
响应类型Direct link to 响应类型
🌐 Response Types
查询结果以此格式返回:
🌐 Query results are returned in this format:
interface QueryResult {
id: string;
score: number;
metadata: Record<string, any>;
vector?: number[]; // Only included if includeVector is true
}
元数据过滤Direct link to 元数据过滤
🌐 Metadata Filtering
ConvexVector 支持使用各种运算符进行元数据过滤:
🌐 ConvexVector supports metadata filtering with various operators:
// Simple equality
const results = await vectorStore.query({
indexName: "my_vectors",
queryVector: embedding,
filter: { category: "documents" },
});
// Comparison operators
const results = await vectorStore.query({
indexName: "my_vectors",
queryVector: embedding,
filter: {
price: { $gt: 100 },
status: { $in: ["active", "pending"] },
},
});
// Logical operators
const results = await vectorStore.query({
indexName: "my_vectors",
queryVector: embedding,
filter: {
$and: [
{ category: "electronics" },
{ price: { $lte: 500 } },
],
},
});
支持的过滤器操作符Direct link to 支持的过滤器操作符
🌐 Supported Filter Operators
| 运算符 | 描述 |
|---|---|
$eq | 等于 |
$ne | 不等于 |
$gt | 大于 |
$gte | 大于或等于 |
$lt | 小于 |
$lte | 小于或等于 |
$in | 在数组中 |
$nin | 不在数组中 |
$and | 逻辑与 |
$or | 逻辑或 |
架构Direct link to 架构
🌐 Architecture
ConvexVector 在 mastra_vectors 表中存储向量,结构如下:
🌐 ConvexVector stores vectors in the mastra_vectors table with the following structure:
id:唯一向量标识符indexName:索引名称embedding:向量数据(浮点数组)metadata:可选的 JSON 元数据
向量相似性搜索使用余弦相似度进行,该相似度在 Convex 函数中计算。
🌐 Vector similarity search is performed using cosine similarity, computed in the Convex function.
相关Direct link to 相关
🌐 Related