DatabaseConfig
DatabaseConfig 型を使用すると、ベクトルクエリ Tool の使用時にデータベース固有の設定を指定できます。これらの設定により、各ベクトルストアが提供する機能や最適化を利用できます。
型定義型定義への直接リンク
export type DatabaseConfig = {
pinecone?: PineconeConfig
pgvector?: PgVectorConfig
chroma?: ChromaConfig
turbopuffer?: TurbopufferConfig
[key: string]: any // Extensible for future databases
}
データベース固有の型データベース固有の型への直接リンク
PineconeConfigpineconeconfigへの直接リンク
Pinecone ベクトルストア固有の設定オプションです。
namespace?:
string
同一インデックス内でベクトルを整理し、分離するための Pinecone namespace。マルチテナントや環境の分離に役立ちます。
sparseVector?:
{ indices: number[]; values: number[]; }
dense embedding と sparse embedding を組み合わせたハイブリッド検索用の sparse vector。キーワードベースのクエリで検索品質を向上できます。indices 配列と values 配列は同じ長さである必要があります。
object
indices:
number[]
sparse vector の各要素に対応するインデックスの配列
values:
number[]
インデックスに対応する値の配列
ユースケース:
- マルチテナントアプリケーション(テナントごとに namespace を分離)
- 環境の分離(dev/staging/prod の namespace)
- セマンティックマッチングとキーワードマッチングを組み合わせたハイブリッド検索
PgVectorConfigpgvectorconfigへの直接リンク
pgvector 拡張機能を使用する PostgreSQL 固有の設定オプションです。
minScore?:
number
結果に対する類似度スコアの最小しきい値。この値を上回る類似度スコアを持つベクトルだけが返されます。
ef?:
number
検索時の動的候補リストのサイズを制御する HNSW 検索パラメータ。値を大きくすると、速度とのトレードオフで精度が向上します。通常は topK から 200 の間に設定します。
probes?:
number
検索時に調べるインデックスセル数を指定する IVFFlat probe パラメータ。値を大きくすると、速度とのトレードオフで再現率が向上します。
パフォーマンスのガイドライン:
- ef: まず topK の 2~4 倍の値を設定し、精度を高める場合は値を増やします
- probes: まず 1~10 の値を設定し、再現率を高める場合は値を増やします
- minScore: 求める品質に応じて 0.5~0.9 の値を使用します
ユースケース:
- 高負荷時のパフォーマンス最適化
- 関連性の低い結果を除外する品質フィルタリング
- 検索精度と速度のトレードオフの微調整
ChromaConfigchromaconfigへの直接リンク
Chroma ベクトルストア固有の設定オプションです。
where?:
Record<string, any>
MongoDB 形式のクエリ構文を使用するメタデータのフィルター条件。メタデータフィールドに基づいて結果を絞り込みます。
whereDocument?:
Record<string, any>
ドキュメント内容のフィルター条件。実際のドキュメントのテキスト内容に基づいて絞り込めます。
フィルター構文の例:
// Simple equality
where: { "category": "technical" }
// Operators
where: { "price": { "$gt": 100 } }
// Multiple conditions
where: {
"category": "electronics",
"inStock": true
}
// Document content filtering
whereDocument: { "$contains": "API documentation" }
ユースケース:
- 高度なメタデータフィルタリング
- 内容に基づくドキュメントフィルタリング
- 複雑なクエリの組み合わせ
TurbopufferConfigturbopufferconfigへの直接リンク
Turbopuffer ベクトルストア固有の設定オプションです。
consistency?:
'strong' | 'eventual'
クエリの整合性レベル。"strong"(デフォルト)は、クエリ開始前に書き込まれたすべてのデータがクエリから参照できることを保証しますが、レイテンシーが高くなります。"eventual" はレイテンシーを抑えられますが、直近に書き込まれたデータがまだ表示されない場合があります。
ユースケース:
- わずかに古いデータを許容できる、レイテンシー重視のクエリ(
eventual) - 最新データを参照する必要がある read-your-writes ワークフロー(
strong)
使用例使用例への直接リンク
- 基本的な使用方法
- 実行時のオーバーライド
- 複数データベース
- パフォーマンスチューニング
基本的なデータベース設定基本的なデータベース設定への直接リンク
import { createVectorQueryTool } from '@mastra/rag'
const vectorTool = createVectorQueryTool({
vectorStoreName: 'pinecone',
indexName: 'documents',
model: embedModel,
databaseConfig: {
pinecone: {
namespace: 'production',
},
},
})
実行時の設定オーバーライド実行時の設定オーバーライドへの直接リンク
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 })
複数データベースの設定複数データベースの設定への直接リンク
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' },
},
},
})
注記
複数データベースのサポート: 複数のデータベースを設定した場合、実際に使用されるベクトルストアと一致する設定だけが適用されます。
パフォーマンスチューニングパフォーマンスチューニングへの直接リンク
// 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
},
},
})
拡張性拡張性への直接リンク
DatabaseConfig 型は拡張できるように設計されています。新しいベクトルデータベースのサポートを追加するには、次のようにします。
// 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,
},
},
})
ベストプラクティスベストプラクティスへの直接リンク
- 環境設定: 環境ごとに異なる namespace または設定を使用します
- パフォーマンスチューニング: デフォルト値から始め、個別の要件に合わせて調整します
- 品質フィルタリング: minScore を使用して品質の低い結果を除外します
- 実行時の柔軟性: 実行時に決まるシナリオでは、実行時に設定をオーバーライドします
- ドキュメント化: チームメンバー向けに、採用した設定とその理由を記録します
移行ガイド移行ガイドへの直接リンク
既存のベクトルクエリ Tool は変更せずに引き続き動作します。データベース設定を追加するには、次のようにします。
const vectorTool = createVectorQueryTool({
vectorStoreName: 'pinecone',
indexName: 'documents',
model: embedModel,
+ databaseConfig: {
+ pinecone: {
+ namespace: 'production'
+ }
+ }
});