This is the Node.js client for Pinecone, written in Typescript. It is a wrapper around the Pinecone OpenAPI spec.
⚠️ WarningThis is a public preview ("Beta") client. Test thoroughly before using this client for production workloads. No SLAs or technical support commitments are provided for this client. Expect potential breaking changes in future releases.
npm i @pinecone-database/pinecone
Set the following environment variables:
PINECONE_API_KEY=your_api_key
PINECONE_ENVIRONMENT=your_environmentimport{PineconeClient}from"@pinecone-database/pinecone";// Create a clientconstclient=newPineconeClient();// Initialize the clientawaitclient.init({apiKey: process.env.PINECONE_API_KEY,environment: process.env.PINECONE_ENVIRONMENT,});The Pinecone control plane allows you to perform the following operations:
- Create, configure and delete indexes
- Get information about an existing indexes
- Create and delete collections
- Select an index to operate on
constcreateRequest: CreateRequest={name: indexName,dimension: dimensions,
metric,};awaitclient.createIndex({ createRequest });awaitclient.deleteIndex({ indexName });constindexDescription=awaitclient.describeIndex({ indexName });Example result:
{
"database": {
"name": "my-index",
"metric": "cosine",
"dimension": 10,
"replicas": 1,
"shards": 1,
"pods": 1,
"pod_type": "p1.x1"
},
"status": {
"waiting": [],
"crashed": [],
"host": "my-index-[project-id].svc.[environment].pinecone.io",
"port": 433,
"state": "Ready",
"ready": true
}
}constlist=awaitclient.listIndexes();Example result:
["index1", "index2"]To operate on an index, you must select it. This is done by calling the Index method on the client.
constindex=client.Index(indexName);constcreateCollectionRequest: CreateCollectionRequest={name: collection,source: indexName,};awaitclient.createCollection({ createCollectionRequest });awaitclient.deleteCollection(collection);constdescribeCollection=awaitclient.describeCollection({ collectionName });Example result:
{
"name": "my-collection",
"status": "Ready",
"size": 3059815,
"dimension": 10
}constlist=awaitclient.listCollections();Example result:
["collection1", "collection2"]The Pinecone index operations allow you to perform the following operations instances of Vector.
A Vector is defined as follows:
typeVector={id: string;values: number[];metadata?: object;sparseValues: {indices: [15,30,11];values: [0.1,0.2,0.3];};// optional sparse values};After selecting an index to operate on, you can:
constupsertRequest: UpsertRequest={
vectors,
namespace,};awaitindex.upsert({ upsertRequest });constvector=[...]// a vectorconstqueryRequest: QueryRequest={topK: 1,
vector,
namespace,includeMetadata: true,includeValues: true,}To query with a sparse vector:
constqueryRequest: QueryRequest={topK: 1,
vector,
namespace,includeMetadata: true,includeValues: true,sparseVector: {indices: [15,30,11],values: [0.1,0.2,0.3],},};To execute the query:
constqueryResponse=awaitindex.query({ queryRequest });constupdateRequest: UpdateRequest={id: vectorId,// the ID of the vector to updatevalues: [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],// the new vector valuessparseValues: {indices: [15,30,11],values: [0.1,0.2,0.3],},// optional sparse valuessetMetadata: metadata,// the new metadata
namespace,};awaitindex.update({ updateRequest });constfetchResult=awaitindex.fetch({ids: [vectorIDs],
namespace,});awaitindex.delete1({ids: [vectorIDs],
namespace,});awaitindex.delete1({deleteAll: true,
namespace,});