Pinecone provides a powerful API for creating and managing vector indexes. In this comprehensive guide, we will explore how to create a Pinecone index using Python code.
Prerequisites
- Pinecone Account: Create a Pinecone account and obtain your API key.
- Python: Ensure you have Python installed on your system.
- Pinecone Python SDK: Install the Pinecone Python SDK using pip:
pip install pinecone-client
Creating a Pinecone Index
Import Necessary Libraries:
Python
import pinecone
Initialize Pinecone:
Python
pinecone.init(
api_key=”YOUR_API_KEY”,
environment=”us-west1-gcp” # Replace with your desired environment
)
Create a Collection:
Python
collection_name = “my_collection”
dimension = 128 # Adjust the dimension based on your data
metric = “cosine” # Choose a suitable metric (e.g., cosine, euclidean)
if collection_name not in pinecone.list_collections():
pinecone.create_collection(
name=collection_name,
dimension=dimension,
metric=metric
)
Adding Vectors to the Index
Prepare Your Vectors: Create a list of vectors to add to the index.
Add Vectors:
Python
vectors = [
[0.1, 0.2, …],
[0.3, 0.4, …]]
collection = pinecone.Index(collection_name)
collection.upsert(vectors=vectors, namespace=”default”)
By following these steps, you can create a Pinecone index and add your vectors to it. This provides a powerful tool for efficient similarity search and retrieval of high-dimensional data.