Skip to main content

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

PineconeConfig
Direct link to PineconeConfig

Pinecone 向量存储特有的配置选项。

🌐 Configuration options specific to Pinecone vector store.

namespace?:

string
Pinecone namespace for organizing and isolating vectors within the same index. Useful for multi-tenancy or environment separation.

sparseVector?:

{ indices: number[]; values: number[]; }
Sparse vector for hybrid search combining dense and sparse embeddings. Enables better search quality for keyword-based queries. The indices and values arrays must be the same length.
object

indices:

number[]
Array of indices for sparse vector components

values:

number[]
Array of values corresponding to the indices

使用案例:

  • 多租户应用(每个租户独立命名空间)
  • 环境隔离(开发/测试/生产命名空间)
  • 结合语义和关键词匹配的混合搜索

PgVectorConfig
Direct link to PgVectorConfig

针对带有 pgvector 扩展的 PostgreSQL 的特定配置选项。

🌐 Configuration options specific to PostgreSQL with pgvector extension.

minScore?:

number
Minimum similarity score threshold for results. Only vectors with similarity scores above this value will be returned.

ef?:

number
HNSW search parameter that controls the size of the dynamic candidate list during search. Higher values improve accuracy at the cost of speed. Typically set between topK and 200.

probes?:

number
IVFFlat probe parameter that specifies the number of index cells to visit during search. Higher values improve recall at the cost of speed.

性能指南:

  • ef:从 2-4 倍你的 topK 值开始,增加以获得更好的准确性
  • 探测器:从 1-10 开始,增加以获得更好的回想
  • minScore:根据你的质量要求使用 0.5-0.9 之间的值

使用案例:

  • 高负载场景的性能优化
  • 质量筛选以去除无关结果
  • 微调搜索精度与速度的权衡

ChromaConfig
Direct link to ChromaConfig

Chroma 向量存储特有的配置选项。

🌐 Configuration options specific to Chroma vector store.

where?:

Record<string, any>
Metadata filtering conditions using MongoDB-style query syntax. Filters results based on metadata fields.

whereDocument?:

Record<string, any>
Document content filtering conditions. Allows filtering based on the actual document text content.

过滤器语法示例:

// 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

基本数据库配置
Direct link to 基本数据库配置

import { createVectorQueryTool } from '@mastra/rag';

const vectorTool = createVectorQueryTool({
vectorStoreName: 'pinecone',
indexName: 'documents',
model: embedModel,
databaseConfig: {
pinecone: {
namespace: 'production'
}
}
});

可扩展性
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

  1. 环境配置:为不同的环境使用不同的命名空间或配置
  2. 性能调优:从默认值开始,根据你的具体需求进行调整
  3. 质量过滤:使用 minScore 筛选低质量结果
  4. 运行时灵活性:在运行时覆盖配置以应对动态场景
  5. 文档:为团队成员记录你特定的配置选择

迁移指南
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'
+ }
+ }
});

🌐 Related