AI

Pinecone Index.Fetch()

Pinecone is a vector database that enables you to store, index, and search high-dimensional vectors such as word embeddings, image features, or audio embeddings.

Pinecone provides us with various clients for various programming languages. We can use these clients to connect to the Pinecone database and perform various operations.

The index.fetch() method is a function that is provided by the Pinecone client for Python. It allows us to look up and return the vectors by ID from a single namespace. The function returns the vectors and their associated metadata.

In this tutorial, we will walk you through how to use this method to fetch the vectors of a given index.

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.

Pinecone Index.Fetch() Method

This method allows us to look up and return the matching vectors using their index from a single namespace.

The function syntax is as follows:

Index.fetch(ids, **kwargs)

The function accepts the following parameters:

ids – This parameter represents the IDs of the vectors that you wish to look up in the index. The value cannot contain any spaces.

namespace – This specifies the namespace that contains the vectors that you want to look up.

The function returns an object that contains the matching vectors. It also returns a string that denotes the namespace on which the vectors are located.

Create an Index and Upsert

To better demonstrate how the function works, let us create a basic index and add a sample data into the index using a given namespace.

import pinecone

# init pinecone configuration

pinecone.init(api_key="YOUR_API_KEY", environment="YOUR_ENV")

# create basic index

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

# connect to the index

index = pinecone.Index("book")

index.upsert([

("A", [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], {"genre": "comedy", "year": 2020, "title": "Book A"}),

("B", [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2], {"genre": "mystery", "year": 2019, "title": "Book B"}),

("C", [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3], {"genre": "comedy", "year": 2019, "title": "Book C"}),

("D", [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4], {"genre": "drama", "title": "Book D"}),

("E", [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], {"genre": "romance", "title": "Book E"})

], namespace="linuxhint")

The previous code creates an index called “books” with the specified parameters.

We then use the upsert() function to add the vector data into the index. We also specify the vector metadata such as the genre, year, and title. We also include the namespace in which we wish to add the data. Since the namespace does not exist, Pinecone automatically creates it before adding the data.

Pinecone Fetch

Once we have a sample data, we can use the fetch function to look up the index based on the IDs as shown in the following example:

fetch_response = index.fetch(ids=['A', 'E'], namespace='linuxhint')
print(fetch_response)

This should look up the vectors with the IDs of “A” and “E”. It should then return the matching vectors as JSON as shown in the following:

{{'namespace': 'linuxhint',
'vectors': {'A': {'id': 'A',
'metadata': {'genre': 'comedy',
'title': 'Book A',
'year': 2020.0},
'values': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
'E': {'id': 'E',
'metadata': {'genre': 'romance', 'title': 'Book E'},
'values': [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]}}}

This represents the matching vectors for the specified IDs.

Conclusion

You learned how look up the vectors based on their indexes using the fetch() method in the Pinecone client for Python.

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