AI

Pinecone Fetch Vectors

Pinecone is a vector database that enables you to store, index, and search the 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 multiple operations.

In this tutorial, we will learn how to use the HTTP requests and various Pinecone API endpoints to create an index, insert a sample data, and search the vectors based on a given index.

Requirements:

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

  1. Basic HTTP request knowledge
  2. cURL or any installed API tool

Pinecone Fetch Operation

The fetch operation allows us to look up and return the vectors by ID from a single namespace. The request returns the matching vectors including the vector data and the associated metadata.

Query Parameters:

The API endpoint accepts the following parameters in the request:

  1. ids – The vector IDs to fetch.
  2. Namespace – It specifies the name of the namespace to which the vectors you wish to look up are stored.

Create an Index and Upsert

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

curl --request POST \

--url https://vectors/upsert \

--header 'Api-Key: 0f57b6af-ea59-4fd3-a0ce-3c7f0c1d419f' \

--header 'accept: application/json' \

--header 'content-type: application/json'

-d {

("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 command 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 the sample data, we can use the fetch operation to look up the index based on the IDs as shown in the following example:

curl --request GET \
--url 'https://book-starter.us-west1-gcp-free.pinecone.io/vectors/fetch?ids=A&ids=E&namespace=linuxhint' \
--header 'Api-Key: YOUR_API_KEY \
--header '
accept: application/json'

Where the target URL is as follows:

https:// index_name-project_id.svc.environment.pinecone.io/vectors/search

Replace the previous URL with your index name, project ID, and the environment.

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

This tutorial taught us how to use the Pinecone API endpoints and HTTP requests to change a Pinecone database and search the vectors based on a given index.

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