Optional
options: PineconeConfigurationThe configuration options for the Pinecone client: PineconeConfiguration.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({
apiKey: 'my-api-key',
});
Targets a specific index for performing data operations.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone()
const index = pc.index('index-name')
If you are storing metadata alongside your vector values inside your Pinecone records, you can pass a type parameter to index()
in order to get proper TypeScript typechecking when upserting and querying data.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
type MovieMetadata = {
title: string,
runtime: numbers,
genre: 'comedy' | 'horror' | 'drama' | 'action'
}
// Specify a custom metadata type while targeting the index
const index = pc.index<MovieMetadata>('test-index');
// Now you get type errors if upserting malformed metadata
await index.upsert([{
id: '1234',
values: [
.... // embedding values
],
metadata: {
genre: 'Gone with the Wind',
runtime: 238,
genre: 'drama',
// @ts-expect-error because category property not in MovieMetadata
category: 'classic'
}
}])
const results = await index.query({
vector: [
... // query embedding
],
filter: { genre: { '$eq': 'drama' }}
})
const movie = results.matches[0];
if (movie.metadata) {
// Since we passed the MovieMetadata type parameter above,
// we can interact with metadata fields without having to
// do any typecasting.
const { title, runtime, genre } = movie.metadata;
console.log(`The best match in drama was ${title}`)
}
The type of the metadata object associated with each record.
The name of the index to target.
Optional
indexHostUrl: stringAn optional host url to use for operations against this index. If not provided, the host url will be resolved by calling describeIndex.
Optional
additionalHeaders: HTTPHeadersAn optional object containing additional headers to pass with each index request.
An Index object that can be used to perform data operations.
Configure an index
Use this method to update configuration on an existing index. For both pod-based and serverless indexes you can update the deletionProtection status of an index. For pod-based index you can also configure the number of replicas and pod type.
The name of the index to configure.
The configuration properties you would like to update
A promise that resolves to IndexModel when the request to configure the index is completed.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.configureIndex('my-index', {
deletionProtection: 'enabled',
spec:{ pod:{ replicas: 2, podType: 'p1.x2' }},
});
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
Create a new collection from an existing index
The collection configuration.
a promise that resolves to CollectionModel when the request to create the collection is completed.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const indexList = await pc.listIndexes()
const indexName = indexList.indexes[0].name;
await pc.createCollection({
name: 'my-collection',
source: indexName
})
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
Creates a new index.
The index configuration.
A promise that resolves to IndexModel when the request to create the index is completed. Note that the index is not immediately ready to use. You can use the describeIndex function to check the status of the index.
The minimum required configuration to create an index is the index name
, dimension
, and spec
.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.createIndex({ name: 'my-index', dimension: 128, spec: { serverless: { cloud: 'aws', region: 'us-west-2' }}})
The spec
object defines how the index should be deployed. For serverless indexes, you define only the cloud and region where the index should be hosted.
For pod-based indexes, you define the environment where the index should be hosted, the pod type and size to use, and other index characteristics.
In a different example, you can create a pod-based index by specifying the pod
spec object with the environment
, pods
, podType
, and metric
properties.
For more information on creating indexes, see Understanding indexes.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.createIndex({
name: 'my-index',
dimension: 1536,
metric: 'cosine',
spec: {
pod: {
environment: 'us-west-2-gcp',
pods: 1,
podType: 'p1.x1'
}
}
})
If you would like to create the index only if it does not already exist, you can use the suppressConflicts
boolean option.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.createIndex({
name: 'my-index',
dimension: 1536,
spec: {
serverless: {
cloud: 'aws',
region: 'us-west-2'
}
},
suppressConflicts: true
})
If you plan to begin upserting immediately after index creation is complete, you should use the waitUntilReady
option. Otherwise, the index may not be ready to receive data operations when you attempt to upsert.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.createIndex({
name: 'my-index',
spec: {
serverless: {
cloud: 'aws',
region: 'us-west-2'
}
},
waitUntilReady: true
});
const records = [
// PineconeRecord objects with your embedding values
]
await pc.index('my-index').upsert(records)
By default all metadata fields are indexed when records are upserted with metadata, but if you want to improve performance you can specify the specific fields you want to index. This example is showing a few hypothetical metadata fields, but the values you'd use depend on what metadata you plan to store with records in your Pinecone index.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.createIndex({
name: 'my-index',
dimension: 1536,
spec: {
serverless: {
cloud: 'aws',
region: 'us-west-2',
metadataConfig: { 'indexed' : ['productName', 'productDescription'] }
}
},
})
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeBadRequestError when index creation fails due to invalid parameters being specified or other problem such as project quotas limiting the creation of any additional indexes.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
PineconeConflictError when attempting to create an index using a name that already exists in your project.
Delete a collection by collection name
The name of the collection to delete.
A promise that resolves when the request to delete the collection is completed.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const collectionList = await pc.listCollections()
const collectionName = collectionList.collections[0].name;
await pc.deleteCollection(collectionName)
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
Deletes an index
The name of the index to delete.
A promise that resolves when the request to delete the index is completed.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.deleteIndex('my-index')
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
PineconeArgumentError when arguments passed to the method fail a runtime validation.
Describe a collection
The name of the collection to describe.
A promise that resolves to a CollectionModel.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.describeCollection('my-collection')
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
Describe a Pinecone index
The name of the index to describe.
A promise that resolves to IndexModel.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const indexModel = await pc.describeIndex('my-index')
console.log(indexModel)
// {
// name: 'sample-index-1',
// dimension: 3,
// metric: 'cosine',
// host: 'sample-index-1-1390950.svc.apw5-4e34-81fa.pinecone.io',
// spec: {
// pod: undefined,
// serverless: {
// cloud: 'aws',
// region: 'us-west-2'
// }
// },
// status: {
// ready: true,
// state: 'Ready'
// }
// }
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
The configuration object that was passed to the Pinecone constructor.
Targets a specific index for performing data operations.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone()
const index = pc.index('index-name')
If you are storing metadata alongside your vector values inside your Pinecone records, you can pass a type parameter to index()
in order to get proper TypeScript typechecking when upserting and querying data.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
type MovieMetadata = {
title: string,
runtime: numbers,
genre: 'comedy' | 'horror' | 'drama' | 'action'
}
// Specify a custom metadata type while targeting the index
const index = pc.index<MovieMetadata>('test-index');
// Now you get type errors if upserting malformed metadata
await index.upsert([{
id: '1234',
values: [
.... // embedding values
],
metadata: {
genre: 'Gone with the Wind',
runtime: 238,
genre: 'drama',
// @ts-expect-error because category property not in MovieMetadata
category: 'classic'
}
}])
const results = await index.query({
vector: [
... // query embedding
],
filter: { genre: { '$eq': 'drama' }}
})
const movie = results.matches[0];
if (movie.metadata) {
// Since we passed the MovieMetadata type parameter above,
// we can interact with metadata fields without having to
// do any typecasting.
const { title, runtime, genre } = movie.metadata;
console.log(`The best match in drama was ${title}`)
}
The type of the metadata object associated with each record.
The name of the index to target.
Optional
indexHostUrl: stringAn optional host url to use for operations against this index. If not provided, the host url will be resolved by calling describeIndex.
Optional
additionalHeaders: HTTPHeadersAn optional object containing additional headers to pass with each index request.
An Index object that can be used to perform data operations.
List all collections in a project
A promise that resolves to CollectionList.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.listCollections()
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
List all Pinecone indexes
A promise that resolves to IndexList.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const indexList = await pc.listIndexes()
console.log(indexList)
// {
// indexes: [
// {
// name: "sample-index-1",
// dimension: 3,
// metric: "cosine",
// host: "sample-index-1-1234567.svc.apw5-2e18-32fa.pinecone.io",
// spec: {
// serverless: {
// cloud: "aws",
// region: "us-west-2"
// }
// },
// status: {
// ready: true,
// state: "Ready"
// }
// },
// {
// name: "sample-index-2",
// dimension: 3,
// metric: "cosine",
// host: "sample-index-2-1234567.svc.apw2-5e76-83fa.pinecone.io",
// spec: {
// serverless: {
// cloud: "aws",
// region: "us-west-2"
// }
// },
// status: {
// ready: true,
// state: "Ready"
// }
// }
// ]
// }
PineconeArgumentError when arguments passed to the method fail a runtime validation.
PineconeConnectionError when network problems or an outage of Pinecone's APIs prevent the request from being completed.
The
Pinecone
class is the main entrypoint to this sdk. You will use instances of it to create and manage indexes as well as perform data operations on those indexes after they are created.Initializing the client
There is one piece of configuration required to use the Pinecone client: an API key. This value can be passed using environment variables or in code through a configuration object. Find your API key in the console dashboard at https://app.pinecone.io
Using environment variables
The environment variables used to configure the client are the following:
When these environment variables are set, the client constructor does not require any additional arguments.
Using a configuration object
If you prefer to pass configuration in code, the constructor accepts a config object containing the
apiKey
andenvironment
values. This could be useful if your application needs to interact with multiple projects, each with a different configuration.See PineconeConfiguration for a full description of available configuration options.