DatabaseConfig
DatabaseConfig 类型允许你在使用向量查询工具时指定特定于数据库的配置。这些配置使你能够利用不同向量存储提供的独特功能和优化。
🌐 The DatabaseConfig type allows you to specify database-specific configurations when using vector query tools. These configurations enable you to leverage unique features and optimizations offered by different vector stores.
类型定义Direct link to 类型定义
🌐 Type Definition
export type DatabaseConfig = {
pinecone?: PineconeConfig;
pgvector?: PgVectorConfig;
chroma?: ChromaConfig;
[key: string]: any; // Extensible for future databases
};
数据库特定类型Direct link to 数据库特定类型
🌐 Database-Specific Types
PineconeConfigDirect link to PineconeConfig
Pinecone 向量存储特有的配置选项。
🌐 Configuration options specific to Pinecone vector store.
namespace?:
sparseVector?:
indices:
values:
使用案例:
- 多租户应用(每个租户独立命名空间)
- 环境隔离(开发/测试/生产命名空间)
- 结合语义和关键词匹配的混合搜索
PgVectorConfigDirect link to PgVectorConfig
针对带有 pgvector 扩展的 PostgreSQL 的特定配置选项。
🌐 Configuration options specific to PostgreSQL with pgvector extension.
minScore?:
ef?:
probes?:
性能指南:
- ef:从 2-4 倍你的 topK 值开始,增加以获得更好的准确性
- 探测器:从 1-10 开始,增加以获得更好的回想
- minScore:根据你的质量要求使用 0.5-0.9 之间的值
使用案例:
- 高负载场景的性能优化
- 质量筛选以去除无关结果
- 微调搜索精度与速度的权衡
ChromaConfigDirect link to ChromaConfig
Chroma 向量存储特有的配置选项。
🌐 Configuration options specific to Chroma vector store.
where?:
whereDocument?:
过滤器语法示例:
// Simple equality
where: { "category": "technical" }
// Operators
where: { "price": { "$gt": 100 } }
// Multiple conditions
where: {
"category": "electronics",
"inStock": true
}
// Document content filtering
whereDocument: { "$contains": "API documentation" }
使用案例:
- 高级元数据过滤
- 基于内容的文档过滤
- 复杂查询组合
使用示例Direct link to 使用示例
🌐 Usage Examples
- Basic Usage
- Runtime Override
- Multi-Database
- Performance Tuning
基本数据库配置Direct link to 基本数据库配置
import { createVectorQueryTool } from '@mastra/rag';
const vectorTool = createVectorQueryTool({
vectorStoreName: 'pinecone',
indexName: 'documents',
model: embedModel,
databaseConfig: {
pinecone: {
namespace: 'production'
}
}
});
运行时配置覆盖Direct link to 运行时配置覆盖
import { RequestContext } from '@mastra/core/request-context';
// Initial configuration
const vectorTool = createVectorQueryTool({
vectorStoreName: 'pinecone',
indexName: 'documents',
model: embedModel,
databaseConfig: {
pinecone: {
namespace: 'development'
}
}
});
// Override at runtime
const requestContext = new RequestContext();
requestContext.set('databaseConfig', {
pinecone: {
namespace: 'production'
}
});
await vectorTool.execute(
{ queryText: 'search query' },
{ mastra, requestContext }
);
多数据库配置Direct link to 多数据库配置
const vectorTool = createVectorQueryTool({
vectorStoreName: 'dynamic', // Will be determined at runtime
indexName: 'documents',
model: embedModel,
databaseConfig: {
pinecone: {
namespace: 'default'
},
pgvector: {
minScore: 0.8,
ef: 150
},
chroma: {
where: { 'type': 'documentation' }
}
}
});
多数据库支持:当你配置多个数据库时,只有与实际使用的向量存储匹配的配置会被应用。
性能调优Direct link to 性能调优
// High accuracy configuration
const highAccuracyTool = createVectorQueryTool({
vectorStoreName: 'postgres',
indexName: 'embeddings',
model: embedModel,
databaseConfig: {
pgvector: {
ef: 400, // High accuracy
probes: 20, // High recall
minScore: 0.85 // High quality threshold
}
}
});
// High speed configuration
const highSpeedTool = createVectorQueryTool({
vectorStoreName: 'postgres',
indexName: 'embeddings',
model: embedModel,
databaseConfig: {
pgvector: {
ef: 50, // Lower accuracy, faster
probes: 3, // Lower recall, faster
minScore: 0.6 // Lower quality threshold
}
}
});
可扩展性Direct link to 可扩展性
🌐 Extensibility
DatabaseConfig 类型被设计为可扩展的。要添加对新的向量数据库的支持:
🌐 The DatabaseConfig type is designed to be extensible. To add support for a new vector database:
// 1. Define the configuration interface
export interface NewDatabaseConfig {
customParam1?: string;
customParam2?: number;
}
// 2. Extend DatabaseConfig type
export type DatabaseConfig = {
pinecone?: PineconeConfig;
pgvector?: PgVectorConfig;
chroma?: ChromaConfig;
newdatabase?: NewDatabaseConfig;
[key: string]: any;
};
// 3. Use in vector query tool
const vectorTool = createVectorQueryTool({
vectorStoreName: "newdatabase",
indexName: "documents",
model: embedModel,
databaseConfig: {
newdatabase: {
customParam1: "value",
customParam2: 42,
},
},
});
最佳实践Direct link to 最佳实践
🌐 Best Practices
- 环境配置:为不同的环境使用不同的命名空间或配置
- 性能调优:从默认值开始,根据你的具体需求进行调整
- 质量过滤:使用 minScore 筛选低质量结果
- 运行时灵活性:在运行时覆盖配置以应对动态场景
- 文档:为团队成员记录你特定的配置选择
迁移指南Direct link to 迁移指南
🌐 Migration Guide
现有的向量查询工具无需更改即可继续使用。要添加数据库配置:
🌐 Existing vector query tools continue to work without changes. To add database configurations:
const vectorTool = createVectorQueryTool({
vectorStoreName: 'pinecone',
indexName: 'documents',
model: embedModel,
+ databaseConfig: {
+ pinecone: {
+ namespace: 'production'
+ }
+ }
});
相关Direct link to 相关
🌐 Related