Complete Pinecone Tutorial: Vector Database for AI and Semantic Search

# Tutorial Lengkap Pinecone: Vector Database untuk AI dan Semantic Search Pinecone adalah managed vector database yang dirancang khusus untuk menyimpan, mengindeks, dan melakukan pencarian similarity...

By Ruby Abdullah · · tutorial
PineconeVector DatabaseRAGSemantic SearchPython

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:

  • Fully Managed: No need to manage infrastructure, scaling, or database maintenance
  • High Performance: Similarity search in milliseconds at massive scale
  • Serverless Architecture: Usage-based pricing model suitable for projects of any size
  • Metadata Filtering: Combine vector search with metadata filters for more precise results
  • Namespace Support: Isolate data within a single index using namespaces
  • Hybrid Search: Support for sparse-dense vector search
  • Integration Ecosystem: Compatible with LangChain, LlamaIndex, Haystack, and other AI frameworks
  • 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

  • Sign up for a free account at pinecone.io
  • After logging in, open the dashboard and copy your API Key
  • Store the API Key securely, for example in a .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")

    Related Articles

    Complete Qdrant Tutorial: Vector Database for AI Applications

    Tutorial Lengkap Qdrant: Vector Database untuk Aplikasi AI Qdrant adalah vector database performa tinggi yang dirancang ...

    Weaviate: Vector Database with Integrated AI Modules

    Weaviate: Database Vektor dengan AI Modules Terintegrasi Weaviate adalah database vektor open-source yang dirancang untu...

    Complete LlamaIndex Tutorial: Building RAG Applications with LLMs

    Tutorial Lengkap LlamaIndex: Membangun Aplikasi RAG dengan LLM LlamaIndex adalah framework data yang powerful untuk memb...

    Complete ChromaDB Tutorial: Simple Vector Database for AI

    Tutorial Lengkap ChromaDB: Vector Database Sederhana untuk AI ChromaDB adalah open-source vector database yang dirancang...