AI

Pinecone List_Indexes()

The Pinecone database is a highly scalable and specialized search index that is designed for vector similarity search. It leverages the advanced algorithms and data structures to efficiently store and retrieve the high-dimensional vectors which enables the rapid similarity-based querying.

Due to its advanced functionality to handle the vector data, Pinecone is a great tool for building the machine learning and AI-based applications such as recommendation systems, natural language processing, computer vision, and more. If you have an application where the vector similarity is a prevailing feature, Pinecone can handle it.

One of the most common ways of interacting with the Pinecone database is using the Python client.

In this tutorial, we will learn how to use the list_indexes() method from the Pinecone-client package to get a list of available indexes in the cluster.

Requirements:

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

  1. Installed Python 3.10 and above
  2. Basic Python programming knowledge

Installing the Pinecone Client

Before interacting with the Pinecone server using Python, we need to install the Pinecone client on our machine. Luckily, we can do this with a simple “pip” command as follows:

$ pip3 install pinecone-client

The previous command should download the latest stable version of the Pinecone client and install it in your project.

Create an Index in Pinecone

Once connected to the server, you can create an index to store the desired data. Using the Pinecone-client, we can use the create_index() method followed by the name of the index and various parameters as shown in the following example code:

import pinecone

import numpy as np

# init pinecone configuration

pinecone.init(api_key="0f57b6af-ea59-4fd3-a0ce-3c7f0c1d419f", environment="us-west1-gcp-free")

# create basic index

pinecone.create_index("sample", dimension=8, metric="euclidean", pod_type="p1", pods=2, replicas=1)

In the previous example, we use the init() method to initialize the Pinecone configuration. Finally, we use the create_index() method to create an index called “sample” with a dimension of 8, a Euclidean distance, a pod type of p1, two pods, and one replica.

Insert a Sample Data

Once we create an index, we can add the data using the upsert() function. An example is as follows:

# Create three sets of 8-dimensional vectors

vectors_a = np.random.rand(15, 8).tolist()

# Create ids

ids_a = map(str, np.arange(15).tolist())

# Insert into separate namespaces

index.upsert(vectors=zip(ids_a,vectors_a),namespace='linuxhint_namespace_a')

This query uses NumPy to generate random values and insert them into the sample index using the linuxhint_namespace_a.

List the Indexes in Pinecone

The list_indexes method in the Pinecone-client package allows us to list all the indexes in the Pinecone server.

The function syntax is as follows:

pinecone.list_indexes()

The function returns an array of strings with the names of the indexes in your server.

Example Output:

['sample-index']

Conclusion

There you have it! Using this post, you learned the fundamentals of working with indexes in Pinecone. This includes creating a new index, adding the data, and listing all the indexes in the Pinecone project.

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