Skip to main content

Pinecone向量存储

🌐 Pinecone Vector Store

PineconeVector 类提供了一个接口,用于访问 Pinecone 的向量数据库。它提供实时向量搜索,具有混合搜索、元数据过滤和命名空间管理等功能。

🌐 The PineconeVector class provides an interface to Pinecone's vector database. It provides real-time vector search, with features like hybrid search, metadata filtering, and namespace management.

构造函数选项
Direct link to 构造函数选项

🌐 Constructor Options

构造函数接受所有 Pinecone 配置选项 以及 Mastra 特定字段。

🌐 The constructor accepts all Pinecone configuration options plus Mastra-specific fields.

id:

string
Unique identifier for this vector store instance

apiKey:

string
Pinecone API key

controllerHostUrl?:

string
Custom Pinecone controller host URL

additionalHeaders?:

Record<string, string>
Additional HTTP headers to include in requests

sourceTag?:

string
Source tag for request tracking

cloud?:

'aws' | 'gcp' | 'azure'
= aws
Cloud provider for new index creation

region?:

string
= us-east-1
Region for new index creation

方法
Direct link to 方法

🌐 Methods

createIndex()
Direct link to createIndex()

indexName:

string
Name of the index to create

dimension:

number
Vector dimension (must match your embedding model)

metric?:

'cosine' | 'euclidean' | 'dotproduct'
= cosine
Distance metric for similarity search. Use 'dotproduct' if you plan to use hybrid search.

upsert()
Direct link to upsert()

indexName:

string
Name of your Pinecone index

vectors:

number[][]
Array of dense embedding vectors

sparseVectors?:

{ indices: number[], values: number[] }[]
Array of sparse vectors for hybrid search. Each vector must have matching indices and values arrays.

metadata?:

Record<string, any>[]
Metadata for each vector

ids?:

string[]
Optional vector IDs (auto-generated if not provided)

namespace?:

string
Optional namespace to store vectors in. Vectors in different namespaces are isolated from each other.

query()
Direct link to query()

indexName:

string
Name of the index to query

vector:

number[]
Dense query vector to find similar vectors

sparseVector?:

{ indices: number[], values: number[] }
Optional sparse vector for hybrid search. Must have matching indices and values arrays.

topK?:

number
= 10
Number of results to return

filter?:

Record<string, any>
Metadata filters for the query

includeVector?:

boolean
= false
Whether to include the vector in the result

namespace?:

string
Optional namespace to query vectors from. Only returns results from the specified namespace.

listIndexes()
Direct link to listIndexes()

返回一个由索引名称组成的字符串数组。

🌐 Returns an array of index names as strings.

describeIndex()
Direct link to describeIndex()

indexName:

string
Name of the index to describe

返回:

🌐 Returns:

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

deleteIndex()
Direct link to deleteIndex()

indexName:

string
Name of the index to delete

updateVector()
Direct link to updateVector()

通过 ID 或元数据过滤器更新单个向量。必须提供 idfilter 中的一个,但不能同时提供两者。

🌐 Update a single vector by ID or by metadata filter. Either id or filter must be provided, but not both.

indexName:

string
Name of the index containing the vector

id?:

string
ID of the vector to update (mutually exclusive with filter)

filter?:

Record<string, any>
Metadata filter to identify vector(s) to update (mutually exclusive with id)

namespace?:

string
Optional namespace for the update operation

update:

object
Update parameters

update.vector?:

number[]
New vector values to update

update.metadata?:

Record<string, any>
New metadata to update

deleteVector()
Direct link to deleteVector()

indexName:

string
Name of the index containing the vector

id:

string
ID of the vector to delete

deleteVectors()
Direct link to deleteVectors()

通过 ID 或元数据过滤器删除多个向量。必须提供 idsfilter 中的一个,但不能同时提供两者。

🌐 Delete multiple vectors by IDs or by metadata filter. Either ids or filter must be provided, but not both.

indexName:

string
Name of the index containing the vectors to delete

ids?:

string[]
Array of vector IDs to delete (mutually exclusive with filter)

filter?:

Record<string, any>
Metadata filter to identify vectors to delete (mutually exclusive with ids)

namespace?:

string
Optional namespace for the deletion operation

响应类型
Direct link to 响应类型

🌐 Response Types

查询结果以此格式返回:

🌐 Query results are returned in this format:

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

错误处理
Direct link to 错误处理

🌐 Error Handling

该存储会抛出可以被捕获的类型化错误:

🌐 The store throws typed errors that can be caught:

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
}
}

环境变量
Direct link to 环境变量

🌐 Environment Variables

所需的环境变量:

🌐 Required environment variables:

  • PINECONE_API_KEY:你的 Pinecone API 密钥

🌐 Hybrid Search

Pinecone 通过结合密集向量和稀疏向量来支持混合搜索。使用混合搜索的方法如下:

🌐 Pinecone supports hybrid search by combining dense and sparse vectors. To use hybrid search:

  1. 使用 metric: 'dotproduct' 创建索引
  2. 在执行 upsert 时,使用 sparseVectors 参数提供稀疏向量
  3. 在查询时,使用 sparseVector 参数提供一个稀疏向量

🌐 Related