AI

Pinecone Node.js Query

Pinecone is a vector database that specializes in similarity search and nearest neighbor retrieval. It uses the vector embeddings to represent the data points such as documents, images, or other types of data in a high-dimensional space.

A query in Pinecone DB refers to the process of searching and retrieving the information from the Pinecone database using a specific query.

In this tutorial, we will learn how we can use the Node.js client for Pinecone DB to perform a query for the data that is stored in a given 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.

Pinecone Node.js Upsert

In the Pinecone Node.js client, we have access to the index.upsert() method which allows us to write the vectors into a namespace. As mentioned, an upsert operation combines an insert and update operation in a single query. Hence, the operation overwrites the previous value if you upsert a new value for an existing vector ID.

The method accepts the following parameters:

  1. requestParameters – This defines an upsert operation wrapper.
  2. upsertRequest – This sets the actual upsert request.

The upsert request is composed of the following values:

  1. Vectors – This is an array that contains the vectors that you wish to insert.
  2. Id – It defines the unique ID of the vector that you wish to upsert.
  3. Values – It is the vector values.
  4. Metadata – This is an object that defines the metadata of the vector.
  5. Namespace – The namespace parameter defines the namespace on which you wish to insert the data. If the provided namespace does not exist, Pinecone creates one automatically.

The method returns an integer number which denores the number of records that are upserted in the index.

The following code demonstrates the upsert() method to add a data to a given Pinecone index:

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, we connect to the index where we wish to insert the data.

Next, we define the upsert request wrapper and pass the upsert request with the vector data that we wish to insert.

We also provide the target namespace that we wish to use. Since the namespace does not exist, Pinecone creates it automatically before storing the data.

Pinecone Node.js Query

The Index.query method allows us to search a namespace using a query vector. It then retrieves the IDs of the most similar items in the namespace along with their similarity scores.

The function syntax is as follows:

index.query(requestParameters: QueryOperationRequest)

The method accepts the requestParameters which is a query operation request wrapper and the QueryOperationRequest as the parameters.

The parameters of the QueryOperationRequest are as follows:

  1. Namespace – It specifies the namespace that we wish to query.
  2. topK – The number of results to return from each query.
  3. Filter – It specifies the filter that you wish to apply to the vector metadata.
  4. includeValues – This is a Boolean value that determines whether the vector values are included in the result.
  5. includeMetadata – It specifies whether the method includes the vector metadata in the result.
  6. Vector – It specifies the query vector.
  7. Id – It specifies the unique ID of the vector that you wish to query.

Let us look at a basic example on how we can use this function. Consider the following example:

const queryResponse = await index.query({
    queryRequest: {
      namespace: "sample-namespace",
      topK: 10,
      filter: {
        active: { $eq: true},
      },
      includeValues: true,
      includeMetadata: true,
      vector: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
    },
  });
  console.log(queryResponse)

The previous code searches for the top 10 matching records where the active metadata field is equal to true. We also include the values and metadata from the function.

Conclusion

We learned how to use the Index.Query() method in the Pinecone Node.js client to gather the information about the matching vectors.

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