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:
- Basic HTTP request knowledge
- 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:
- ids – The vector IDs to fetch.
- 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.
--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:
--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:
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:
'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.