Complete Pinecone Tutorial: Vector Database for AI and Semantic Search
Pinecone is a managed vector database designed specifically for storing, indexing, and performing similarity searches on high-dimensional vector data. In the modern AI ecosystem, Pinecone has become one of the top choices for building Retrieval-Augmented Generation (RAG) systems, semantic search, recommendation engines, and various embedding-based applications.
Unlike traditional databases that rely on exact matching, Pinecone enables search based on semantic similarity. When you convert text, images, or other data into vector embeddings, Pinecone can find the most similar items in milliseconds, even at the scale of billions of vectors.
In this tutorial, we will learn how to use Pinecone from installation to advanced usage, including integration with popular embedding models and building a complete RAG pipeline.
Why Choose Pinecone?
Before diving into implementation, here are several reasons why Pinecone is a popular choice for vector databases:
Installation and Setup
Prerequisites
Make sure you have Python 3.8 or newer installed on your system.
Installing the Pinecone Client
pip install pinecone-client
For installation with additional useful dependencies:
pip install pinecone-client sentence-transformers openai python-dotenv
Creating an Account and API Key
.env file# .env
PINECONEAPIKEY=your-api-key-here
Initializing the Client
import os
from dotenv import loaddotenv
from pinecone import Pinecone
loaddotenv()
pc = Pinecone(apikey=os.getenv("PINECONEAPIKEY"))
Verify connection
print("Available indexes:", pc.listindexes().names())
Core Concepts
Index
An index is the primary storage unit in Pinecone, similar to a "table" in a relational database. Each index is configured with a specific vector dimension and similarity metric.
Namespace
Namespaces allow you to partition data within a single index. Each namespace is independent, so queries on one namespace will not return results from another.
Vectors and Metadata
Each record in Pinecone consists of:
- ID: A unique identifier for the vector
- Values: A numerical array (the vector embedding)
- Metadata: Additional key-value pair data that can be filtered
Similarity Metrics
Pinecone supports three metrics:
- Cosine: Measures the angle between two vectors (most common for text)
- Euclidean: Measures the geometric distance between two points
- Dot Product: Measures the projection of one vector onto another
Basic Usage
Creating an Index
from pinecone import Pinecone, ServerlessSpec
pc = Pinecone(apikey=os.getenv("PINECONEAPIKEY"))
indexname = "tutorial-index"
if indexname not in pc.listindexes().names():
pc.createindex(
name=indexname,
dimension=384, # Match your embedding model
metric="cosine",
spec=ServerlessSpec(
cloud="aws",
region="us-east-1"
)
)
print(f"Index '{indexname}' created successfully")