DatabaseConfig
使用向量查詢 Tool 時,DatabaseConfig 類型可讓你指定資料庫專屬設定。透過這些設定,你可以運用不同向量儲存區提供的功能與最佳化選項。
類型定義「類型定義」的直接連結
export type DatabaseConfig = {
pinecone?: PineconeConfig
pgvector?: PgVectorConfig
chroma?: ChromaConfig
turbopuffer?: TurbopufferConfig
[key: string]: any // Extensible for future databases
}
資料庫專屬類型「資料庫專屬類型」的直接連結
PineconeConfig「pineconeconfig」的直接連結
Pinecone 向量儲存區專用的設定選項。
namespace?:
string
Pinecone namespace,用於在同一索引內組織及隔離向量。適合用於多租戶或環境隔離。
sparseVector?:
{ indices: number[]; values: number[]; }
用於混合搜尋的稀疏向量,可結合密集與稀疏嵌入,提升關鍵字查詢的搜尋品質。indices 與 values 陣列的長度必須相同。
object
indices:
number[]
稀疏向量元件的索引陣列
values:
number[]
與索引相對應的值陣列
使用情境:
- 多租戶應用程式(每個租戶使用不同的 namespace)
- 環境隔離(開發/預備/正式環境各自使用不同的 namespace)
- 結合語意與關鍵字比對的混合搜尋
PgVectorConfig「pgvectorconfig」的直接連結
安裝 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 之間的值
使用情境:
- 為高負載情境進行效能最佳化
- 透過品質篩選移除不相關的結果
- 微調搜尋準確度與速度之間的取捨
ChromaConfig「chromaconfig」的直接連結
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" }
使用情境:
- 進階中繼資料篩選
- 依內容篩選文件
- 複雜的查詢組合
TurbopufferConfig「turbopufferconfig」的直接連結
Turbopuffer 向量儲存區專用的設定選項。
consistency?:
'strong' | 'eventual'
查詢的一致性層級。"strong"(預設)可保證查詢看得到查詢開始前寫入的所有資料,但延遲較高。"eventual" 的延遲較低,但近期寫入的資料可能尚不可見。
使用情境:
- 可接受資料稍微過時、但對延遲敏感的查詢(
eventual) - 必須看見最新資料的「讀到自己的寫入」工作流程(
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'
+ }
+ }
});