跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Pinecone vector store

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.

Constructor options
Constructor options的直接链接

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

Methods
Methods的直接链接

createIndex()
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()
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()
query的直接链接

indexName:

string
Name of the index to query

queryVector:

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()
listindexes的直接链接

Returns an array of index names as strings.

describeIndex()
describeindex的直接链接

indexName:

string
Name of the index to describe

Returns:

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

deleteIndex()
deleteindex的直接链接

indexName:

string
Name of the index to delete

updateVector()
updatevector的直接链接

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()
deletevector的直接链接

indexName:

string
Name of the index containing the vector

id:

string
ID of the vector to delete

deleteVectors()
deletevectors的直接链接

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

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

Error handling
Error handling的直接链接

The store throws MastraError instances from @mastra/core/error:

import { MastraError } from '@mastra/core/error'

try {
await store.query({
indexName: 'index_name',
queryVector: queryVector,
})
} catch (error) {
if (error instanceof MastraError) {
console.log(error.id) // 'MASTRA_VECTOR_PINECONE_QUERY_FAILED'
console.log(error.domain) // 'STORAGE'
console.log(error.category) // 'USER' | 'THIRD_PARTY'
console.log(error.details) // Additional error context
}
}

Environment variables
Environment variables的直接链接

Required environment variables:

  • PINECONE_API_KEY: Your Pinecone API key

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

  1. Create an index with metric: 'dotproduct'
  2. During upsert, provide sparse vectors using the sparseVectors parameter
  3. During query, provide a sparse vector using the sparseVector parameter