> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Pinecone 向量儲存 PineconeVector 類別提供 [Pinecone](https://www.pinecone.io/) 向量資料庫的介面。 它提供即時向量搜尋,以及混合搜尋、中繼資料篩選和命名空間管理等功能。 ## 建構函式選項 建構函式接受所有 [Pinecone 設定選項](https://docs.pinecone.io/reference/typescript-sdk),以及 Mastra 專用欄位。 **id** (`string`): 此向量儲存執行個體的不重複識別碼 **apiKey** (`string`): Pinecone API 金鑰 **controllerHostUrl** (`string`): 自訂 Pinecone 控制器主機 URL **additionalHeaders** (`Record`): 要求中要包含的其他 HTTP 標頭 **sourceTag** (`string`): 用於追蹤要求的來源標籤 **cloud** (`'aws' | 'gcp' | 'azure'`): 建立新索引時使用的雲端 Provider (Default: `aws`) **region** (`string`): 建立新索引時使用的區域 (Default: `us-east-1`) ## 方法 ### `createIndex()` **indexName** (`string`): 要建立的索引名稱 **dimension** (`number`): 向量維度(必須與嵌入模型相符) **metric** (`'cosine' | 'euclidean' | 'dotproduct'`): 相似度搜尋使用的距離度量。若要使用混合搜尋,請使用 'dotproduct'。 (Default: `cosine`) ### `upsert()` **indexName** (`string`): Pinecone 索引名稱 **vectors** (`number[][]`): 密集嵌入向量陣列 **sparseVectors** (`{ indices: number[], values: number[] }[]`): 混合搜尋使用的稀疏向量陣列。每個向量都必須有互相對應的 indices 和 values 陣列。 **metadata** (`Record[]`): 每個向量的中繼資料 **ids** (`string[]`): 選用的向量 ID(未提供時會自動產生) **namespace** (`string`): 儲存向量的選用命名空間。不同命名空間中的向量彼此隔離。 ### `query()` **indexName** (`string`): 要查詢的索引名稱 **queryVector** (`number[]`): 用於尋找相似向量的密集查詢向量 **sparseVector** (`{ indices: number[], values: number[] }`): 混合搜尋使用的選用稀疏向量。必須有互相對應的 indices 和 values 陣列。 **topK** (`number`): 要傳回的結果數量 (Default: `10`) **filter** (`Record`): 查詢的中繼資料篩選條件 **includeVector** (`boolean`): 結果是否包含向量 (Default: `false`) **namespace** (`string`): 查詢向量的選用命名空間。只傳回指定命名空間中的結果。 ### `listIndexes()` 以字串陣列傳回索引名稱。 ### `describeIndex()` **indexName** (`string`): 要描述的索引名稱 傳回: ```typescript interface IndexStats { dimension: number count: number metric: 'cosine' | 'euclidean' | 'dotproduct' } ``` ### `deleteIndex()` **indexName** (`string`): 要刪除的索引名稱 ### `updateVector()` 依 ID 或中繼資料篩選條件更新單一向量。必須提供 `id` 或 `filter` 其中一項,但不能同時提供兩者。 **indexName** (`string`): 包含該向量的索引名稱 **id** (`string`): 要更新的向量 ID(不可與 filter 同時使用) **filter** (`Record`): 用於識別待更新向量的中繼資料篩選條件(不可與 id 同時使用) **namespace** (`string`): 更新操作使用的選用命名空間 **update** (`object`): 更新參數 **update.vector** (`number[]`): 要更新的新向量值 **update.metadata** (`Record`): 要更新的新中繼資料 ### `deleteVector()` **indexName** (`string`): 包含該向量的索引名稱 **id** (`string`): 要刪除的向量 ID ### `deleteVectors()` 依 ID 或中繼資料篩選條件刪除多個向量。必須提供 `ids` 或 `filter` 其中一項,但不能同時提供兩者。 **indexName** (`string`): 包含待刪除向量的索引名稱 **ids** (`string[]`): 要刪除的向量 ID 陣列(不可與 filter 同時使用) **filter** (`Record`): 用於識別待刪除向量的中繼資料篩選條件(不可與 ids 同時使用) **namespace** (`string`): 刪除操作使用的選用命名空間 ## 回應型別 查詢結果會以下列格式傳回: ```typescript interface QueryResult { id: string score: number metadata: Record vector?: number[] // Only included if includeVector is true } ``` ## 錯誤處理 此儲存會擲回可攔截的具型別錯誤: ```typescript 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` 參數提供稀疏向量 ## 相關內容 - [中繼資料篩選條件](https://mastra.zisheng.pro/zh-TW/reference/rag/metadata-filters)