Pinecone: Getting Started

Pinecone: Getting Started

Table of Contents

  1. Introduction
  2. Setting up Pinecone
  3. Installing Pinecone Client
  4. Connecting to Pinecone
  5. Creating a Vector Index
  6. Checking Compatibility
  7. Creating Vectors
  8. Upserting Vectors
  9. Describing Index Stats
  10. Querying the Index
  11. Deleting the Index
  12. Conclusion

Introduction

In this article, we will explore how to set up the Pinecone client and quickly Create a search service using Pinecone. Pinecone is a powerful tool that allows You to build and search vector indexes efficiently.

Setting up Pinecone

To begin, we need to set up the Pinecone client. You can find the "hello pinecone" notebook here. Follow the guide to set up Pinecone and import the necessary dependencies.

Installing Pinecone Client

To install the Pinecone client, run the following command:

pip install pinecone-client

This will install the Pinecone client library, which is required to Interact with Pinecone.

Connecting to Pinecone

After installing the Pinecone client, we need to establish a connection to Pinecone. To do this, we need an API key. If you haven't signed up for Pinecone yet, you can do so here. Once you have your API key, you can proceed to connect to Pinecone.

In your notebook, paste the API key and run the following code:

import pinecone

pinecone.init(api_key="your-api-key")

This will initialize the connection to Pinecone using the provided API key.

Creating a Vector Index

Now that We Are connected to Pinecone, let's create a vector index. In this example, we will use a three-dimensional vector, but you can use larger vectors for your own applications.

To create an index, use the following code:

pinecone.create_index(index_name="hello_pinecone", dimension=3)

Make sure to specify the index name and dimension of your vectors. Additionally, you can also set other parameters such as the metric and number of shards for the index.

Checking Compatibility

Before proceeding, let's check if the Pinecone client is compatible with the Current version of Pinecone. Run the following code to confirm:

pinecone.info()

This will display the Pinecone client version and other Relevant information.

Creating Vectors

Next, we can start creating some simple vectors for our index. These vectors can be arbitrary and have no specific meaning for this example.

To create vectors, run the following code:

vectors = [
    ("id1", [0.1, 0.2, 0.3]),
    ("id2", [0.4, 0.5, 0.6]),
    ("id3", [0.7, 0.8, 0.9]),
    ("id4", [1.0, 1.1, 1.2]),
    ("id5", [1.3, 1.4, 1.5]),
]

pinecone.upsert_vectors(index_name="hello_pinecone", vectors=vectors)

Note that each vector is represented by an ID and a list of values. You can also add metadata to each vector if needed.

Upserting Vectors

To insert or update vectors in the index, use the upsert_vectors method. This method takes the index name and a list of vectors as parameters.

Describing Index Stats

If you want to get information about the index, such as the number of vectors it contains, you can use the describe_index_stats method. Run the following code to see the statistics:

stats = pinecone.describe_index_stats(index_name="hello_pinecone")
print(stats)

This will display the number of vectors in the index.

Querying the Index

Now, let's query the index to find the most similar vectors. You can query for a single vector or multiple vectors at once.

To query for a single vector, use the following code:

query_vector = [0.1, 0.2, 0.3]
results = pinecone.query(index_name="hello_pinecone", vectors=[query_vector], top_k=2)
print(results)

This will return the top two most similar vectors to the query vector.

To query for multiple vectors, you can pass a list of vectors to the vectors parameter in the pinecone.query method.

Deleting the Index

Once you are done with the index and no longer need it, you can delete it using the delete_index method. This will remove the index and all its associated vectors.

To delete the index, run the following code:

pinecone.delete_index(index_name="hello_pinecone")

Make sure to specify the correct index name.

Conclusion

In this article, we learned how to set up the Pinecone client and create a vector index. We also explored the process of inserting vectors, querying the index for similar vectors, and deleting the index when no longer needed. Pinecone is a versatile tool that enables fast and efficient vector search. Experiment with different configurations and utilize its power to enhance your applications.

Most people like

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content