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 assistant for performing operations.
Once an assistant is targeted, you can perform operations such as uploading files, updating instructions, and chatting.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const assistant = pc.Assistant('my-assistant');
// Upload a file to the assistant
await assistant.uploadFile({
path: 'test-file.txt',
metadata: { description: 'Sample test file' }
});
// Retrieve assistant details
const details = await assistant.describe();
console.log('Assistant details:', details);
// Update assistant instructions
await assistant.update({
instructions: 'Provide concise responses only.',
});
const chatResp = await assistant.chat({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
});
console.log(chatResp);
// {
// id: '000000000000000023e7fb015be9d0ad',
// finishReason: 'stop',
// message: {
// role: 'assistant',
// content: 'The capital of France is Paris.'
// },
// model: 'gpt-4o-2024-05-13',
// citations: [ { position: 209, references: [Array] } ],
// usage: { promptTokens: 493, completionTokens: 38, totalTokens: 531 }
// }
The name of the assistant to target.
An Assistant object that can be used to perform assistant-related operations.
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.
Targets a specific assistant for performing operations.
Once an assistant is targeted, you can perform operations such as uploading files, updating instructions, and chatting.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const assistant = pc.Assistant('my-assistant');
// Upload a file to the assistant
await assistant.uploadFile({
path: 'test-file.txt',
metadata: { description: 'Sample test file' }
});
// Retrieve assistant details
const details = await assistant.describe();
console.log('Assistant details:', details);
// Update assistant instructions
await assistant.update({
instructions: 'Provide concise responses only.',
});
const chatResp = await assistant.chat({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
});
console.log(chatResp);
// {
// id: '000000000000000023e7fb015be9d0ad',
// finishReason: 'stop',
// message: {
// role: 'assistant',
// content: 'The capital of France is Paris.'
// },
// model: 'gpt-4o-2024-05-13',
// citations: [ { position: 209, references: [Array] } ],
// usage: { promptTokens: 493, completionTokens: 38, totalTokens: 531 }
// }
The name of the assistant to target.
An Assistant object that can be used to perform assistant-related 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 and/or change any index tags. 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.
Creates a new Assistant.
A CreateAssistantOptions object containing the name
of the Assistant to be created.
Optionally, users can also specify instructions, metadata, and host region. Region must be one of "us" or "eu"
and determines where the Assistant will be hosted.
A Promise that resolves to an Assistant model.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.createAssistant({name: 'test1'});
// {
// name: 'test11',
// instructions: undefined,
// metadata: undefined,
// status: 'Initializing',
// host: 'https://prod-1-data.ke.pinecone.io',
// createdAt: 2025-01-08T22:52:49.652Z,
// updatedAt: 2025-01-08T22:52:49.652Z
// }
Error if the Assistant API is not initialized.
Error if an invalid region is provided.
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'
}
},
tags: { 'team': 'data-science' }
})
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,
tags: { 'team': 'data-science' }
})
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,
tags: { 'team': 'data-science' }
});
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'] }
}
},
tags: { 'team': 'data-science' }
})
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.
Deletes an Assistant by name.
The name of the Assistant to be deleted.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.deleteAssistant('test1');
Error if the Assistant API is not initialized.
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.
Retrieves information about an Assistant by name.
The name of the Assistant to retrieve.
A Promise that resolves to an Assistant model.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const test = await pc.describeAssistant('test1');
console.log(test);
// {
// name: 'test1',
// instructions: undefined,
// metadata: undefined,
// status: 'Ready',
// host: 'https://prod-1-data.ke.pinecone.io',
// createdAt: 2025-01-08T22:24:50.525Z,
// updatedAt: 2025-01-08T22:24:52.303Z
// }
Error if the Assistant API is not initialized.
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.
Retrieves a list of all Assistants for a given Pinecone API key.
A Promise that resolves to an object containing an array of Assistant models.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const assistants = await pc.listAssistants();
console.log(assistants);
// {
// assistants: [
// {
// name: 'test2',
// instructions: 'test-instructions',
// metadata: [Object],
// status: 'Ready',
// host: 'https://prod-1-data.ke.pinecone.io',
// createdAt: 2025-01-06T19:14:18.633Z,
// updatedAt: 2025-01-06T19:14:36.977Z
// },
// ]
// }
Error if the Assistant API is not initialized.
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.
Updates an Assistant by name.
The name of the assistant being updated.
An updateAssistant object containing the name of the assistant to be updated and optional instructions and metadata.
A Promise that resolves to an UpdateAssistant200Response object.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
await pc.updateAssistant('test1', { instructions: 'some new instructions!'});
// {
// assistantName: test1,
// instructions: 'some new instructions!',
// metadata: undefined
// }
Error if the Assistant API is not initialized.
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.