> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Cloudflare 向量儲存 CloudflareVector 類別使用與 Cloudflare 邊緣網路整合的向量資料庫服務 [Cloudflare Vectorize](https://developers.cloudflare.com/vectorize/) 提供向量搜尋。 ## 建構函式選項 **accountId** (`string`): Cloudflare 帳戶 ID **apiToken** (`string`): 具有 Vectorize 權限的 Cloudflare API 權杖 ## 方法 ### `createIndex()` **indexName** (`string`): 要建立的索引名稱 **dimension** (`number`): 向量維度(必須與嵌入模型相符) **metric** (`'cosine' | 'euclidean' | 'dotproduct'`): 相似度搜尋使用的距離度量(dotproduct 對應至 dot-product) (Default: `cosine`) ### `upsert()` **indexName** (`string`): 要 upsert 資料的索引名稱 **vectors** (`number[][]`): 嵌入向量陣列 **metadata** (`Record[]`): 每個向量的中繼資料 **ids** (`string[]`): 選用的向量 ID(未提供時會自動產生) ### `query()` **indexName** (`string`): 要查詢的索引名稱 **queryVector** (`number[]`): 用於尋找相似向量的查詢向量 **topK** (`number`): 要傳回的結果數量 (Default: `10`) **filter** (`Record`): 查詢的中繼資料篩選條件 **includeVector** (`boolean`): 結果是否包含向量 (Default: `false`) ### `listIndexes()` 以字串陣列傳回索引名稱。 ### `describeIndex()` **indexName** (`string`): 要描述的索引名稱 傳回: ```typescript interface IndexStats { dimension: number count: number metric: 'cosine' | 'euclidean' | 'dotproduct' } ``` ### `deleteIndex()` **indexName** (`string`): 要刪除的索引名稱 ### `createMetadataIndex()` 在中繼資料欄位上建立索引,以啟用篩選。 **indexName** (`string`): 包含中繼資料欄位的索引名稱 **propertyName** (`string`): 要建立索引的中繼資料欄位名稱 **indexType** (`'string' | 'number' | 'boolean'`): 中繼資料欄位的型別 ### `deleteMetadataIndex()` 從中繼資料欄位移除索引。 **indexName** (`string`): 包含中繼資料欄位的索引名稱 **propertyName** (`string`): 要移除索引的中繼資料欄位名稱 ### `listMetadataIndexes()` 列出某個索引的所有中繼資料欄位索引。 **indexName** (`string`): 要列出其中繼資料索引的索引名稱 ### `updateVector()` 更新索引中特定 ID 的向量或中繼資料。 **indexName** (`string`): 包含待更新 ID 的索引名稱 **id** (`string`): 要更新的向量或中繼資料不重複識別碼 **update** (`{ vector?: number[]; metadata?: Record; }`): 包含待更新向量及/或中繼資料的物件 ### `deleteVector()` 刪除索引中特定 ID 的向量及其關聯中繼資料。 **indexName** (`string`): 包含待刪除 ID 的索引名稱 **id** (`string`): 要刪除的向量與中繼資料不重複識別碼 ## 回應型別 查詢結果會以下列格式傳回: ```typescript interface QueryResult { id: string score: number metadata: Record vector?: number[] } ``` ## 錯誤處理 此儲存會擲回可攔截的具型別錯誤: ```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 } } ``` ## 環境變數 必要的環境變數: - `CLOUDFLARE_ACCOUNT_ID`:你的 Cloudflare 帳戶 ID - `CLOUDFLARE_API_TOKEN`:具有 Vectorize 權限的 Cloudflare API 權杖 ## 相關內容 - [中繼資料篩選條件](https://mastra.zisheng.pro/zh-TW/reference/rag/metadata-filters)