跳至主要內容

Pinecone 向量儲存

PineconeVector 類別提供 Pinecone 向量資料庫的介面。 它提供即時向量搜尋,以及混合搜尋、中繼資料篩選和命名空間管理等功能。

建構函式選項
「建構函式選項」的直接連結

建構函式接受所有 Pinecone 設定選項,以及 Mastra 專用欄位。

id:

string
此向量儲存執行個體的不重複識別碼

apiKey:

string
Pinecone API 金鑰

controllerHostUrl?:

string
自訂 Pinecone 控制器主機 URL

additionalHeaders?:

Record<string, string>
要求中要包含的其他 HTTP 標頭

sourceTag?:

string
用於追蹤要求的來源標籤

cloud?:

'aws' | 'gcp' | 'azure'
= aws
建立新索引時使用的雲端 Provider

region?:

string
= us-east-1
建立新索引時使用的區域

方法
「方法」的直接連結

createIndex()
「createindex」的直接連結

indexName:

string
要建立的索引名稱

dimension:

number
向量維度(必須與嵌入模型相符)

metric?:

'cosine' | 'euclidean' | 'dotproduct'
= cosine
相似度搜尋使用的距離度量。若要使用混合搜尋,請使用 'dotproduct'。

upsert()
「upsert」的直接連結

indexName:

string
Pinecone 索引名稱

vectors:

number[][]
密集嵌入向量陣列

sparseVectors?:

{ indices: number[], values: number[] }[]
混合搜尋使用的稀疏向量陣列。每個向量都必須有互相對應的 indices 和 values 陣列。

metadata?:

Record<string, any>[]
每個向量的中繼資料

ids?:

string[]
選用的向量 ID(未提供時會自動產生)

namespace?:

string
儲存向量的選用命名空間。不同命名空間中的向量彼此隔離。

query()
「query」的直接連結

indexName:

string
要查詢的索引名稱

queryVector:

number[]
用於尋找相似向量的密集查詢向量

sparseVector?:

{ indices: number[], values: number[] }
混合搜尋使用的選用稀疏向量。必須有互相對應的 indices 和 values 陣列。

topK?:

number
= 10
要傳回的結果數量

filter?:

Record<string, any>
查詢的中繼資料篩選條件

includeVector?:

boolean
= false
結果是否包含向量

namespace?:

string
查詢向量的選用命名空間。只傳回指定命名空間中的結果。

listIndexes()
「listindexes」的直接連結

以字串陣列傳回索引名稱。

describeIndex()
「describeindex」的直接連結

indexName:

string
要描述的索引名稱

傳回:

interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}

deleteIndex()
「deleteindex」的直接連結

indexName:

string
要刪除的索引名稱

updateVector()
「updatevector」的直接連結

依 ID 或中繼資料篩選條件更新單一向量。必須提供 idfilter 其中一項,但不能同時提供兩者。

indexName:

string
包含該向量的索引名稱

id?:

string
要更新的向量 ID(不可與 filter 同時使用)

filter?:

Record<string, any>
用於識別待更新向量的中繼資料篩選條件(不可與 id 同時使用)

namespace?:

string
更新操作使用的選用命名空間

update:

object
更新參數

update.vector?:

number[]
要更新的新向量值

update.metadata?:

Record<string, any>
要更新的新中繼資料

deleteVector()
「deletevector」的直接連結

indexName:

string
包含該向量的索引名稱

id:

string
要刪除的向量 ID

deleteVectors()
「deletevectors」的直接連結

依 ID 或中繼資料篩選條件刪除多個向量。必須提供 idsfilter 其中一項,但不能同時提供兩者。

indexName:

string
包含待刪除向量的索引名稱

ids?:

string[]
要刪除的向量 ID 陣列(不可與 filter 同時使用)

filter?:

Record<string, any>
用於識別待刪除向量的中繼資料篩選條件(不可與 ids 同時使用)

namespace?:

string
刪除操作使用的選用命名空間

回應型別
「回應型別」的直接連結

查詢結果會以下列格式傳回:

interface QueryResult {
id: string
score: number
metadata: Record<string, any>
vector?: number[] // Only included if includeVector is true
}

錯誤處理
「錯誤處理」的直接連結

此儲存會擲回可攔截的具型別錯誤:

try {
await store.query({
indexName: 'index_name',
queryVector: queryVector,
})
} catch (error) {
if (error instanceof VectorStoreError) {
console.log(error.code) // 'connection_failed' | 'invalid_dimension' | etc
console.log(error.details) // Additional error context
}
}

環境變數
「環境變數」的直接連結

必要的環境變數:

  • PINECONE_API_KEY:你的 Pinecone API 金鑰

Pinecone 支援結合密集與稀疏向量的混合搜尋。若要使用混合搜尋:

  1. 使用 metric: 'dotproduct' 建立索引
  2. upsert 時,使用 sparseVectors 參數提供稀疏向量
  3. 查詢時,使用 sparseVector 參數提供稀疏向量