AI

Create a Collection in Pinecone Using Node.js

Pinecone DB is an advanced vector database that leverages the cutting-edge techniques including approximate nearest neighbor search, indexing structures, compression algorithms, and distributed computing to enable the efficient storage, retrieval, and similarity search of high-dimensional vectors.

What Is an Index in Pinecone?

In Pinecone, an index is a critical component of the functionality of the database. It provides a highly optimized data structure for organizing and optimizing the storage and querying of a high-dimensional vector data.

What Is a Collection in Pinecone?

In Pinecone, a collection refers to a named container that facilitates the efficient storage, indexing, and retrieval of similar data objects based on their embeddings which enables fast and accurate similarity search operations.

In this tutorial, we will learn how to use the Node.js Pinecone client to connect to a Pinecone client, create an index, add a sample data, and create a collection from the index.

Requirements:

To follow along with this post, ensure that you have the following:

  1. Installed Node.js 17 and above
  2. A configured Pinecone cluster

Installing the Pinecone Client

The first step is ensuring that the Pinecone client for Node.js is installed on the machine. We can do this by running the following command:

$ npm install @pinecone-database/pinecone

Once installed, we can proceed and learn how to configure Pinecone with Node.js.

Initializing the Client

Before interacting with the Pinecone database, we must create a client with the server configuration using the API key and the environment properties.

The following code shows how to use the “PineconeClient” and the init method:

import { PineconeClient } from "@pinecone-database/pinecone";

const pinecone = new PineconeClient();

await Pinecone.init({

  environment: "us-west1-gcp-free",

  apiKey: "0f57b6af-ea59-4fd3-a0ce-3c7f0c1d419f"

});

In this case, we initialize a new Pinecone client using the provided environment and API Key.

Create an Index in Pinecone Using Node.js

Once connected to the server, we can proceed and create an index to store the target data. The Node.js client provides us with the CreateIndex() method which enables us to quickly configure a new index as shown in the following example code:

await Pinecone.createIndex({

  createRequest: {

    name: "sample-index",

    dimension: 8,

    metric: "cosine"

}

});

In the given example, we use the createIndex() method to create an index called “sample-index” with a dimension of 8 and a cosine distance metric.

Insert a Sample Data in Pinecone Using Node.js

Once we define the index, we can proceed and add some sample data for demonstration purposes. Keep in mind that you can create a collection from an empty array.

const index = pinecone.Index("sample-index");

const upsertResponse = await index.upsert({
    upsertRequest: {
      vectors: [
        {
          id: "vec1",
          values: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
          metadata: {
            active: true,
          },
        },
        {
          id: "vec2",
          values: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
          metadata: {
            active: false,
          },
        },
      ],
      namespace: "linuxhint-namespace",
    },
  });
console.log(upsertResponse)

In the given example code, we use the Index() method to specify to which index we wish to connect. Finally, we use the index.upsert() method to perform an insert operation of a sample data into the specified index.

This should return the output as follows:

{ upsertedCount: 2 }

Create a Collection in Pinecone Using Node.js

To create a collection using the Node.js client for Pinecone, we use the “CreateCollection” method and pass the name of the collection and the source index as the parameters. Refer to an example demonstration:

await Pinecone.createCollection({

 createCollectionRequest: {

  name: "sample-collection",

  source: "sample-index",

 },

});

The given code creates a new collection called “sample-collection” using the sample index that we created earlier.

Describe a Collection in Pinecone

To log an information about the collection, we can use the “DescribeCollection” method as demonstrated in the following example:

const collectionDescription = await pinecone.describeCollection({

collectionName: "sample-collection",

});

console.log(collectionDescription)

An example output is as follows:

{ name: 'sample-collection', size: undefined, status: 'Initializing' }

In the previous output, we can see that Pinecone is still configuring the collection. Once ready, you may get an output as follows:

{ name: 'sample-collection', size: 3120531, status: 'Ready' }

In this case, we can see the collection’s name, size, and status.

Conclusion

We explored the fundamentals of working with the Node.js client for Pinecone to perform various actions such as creating indexes, connecting to an index, adding a sample data, and more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list