$npx -y skills add AlexAI-MCP/hermes-CCC --skill pineconeManaged vector database for production RAG — serverless and pod-based deployment, hybrid search, namespaces, and metadata filtering.
| 1 | # Pinecone — Managed Vector Database |
| 2 | |
| 3 | Fully managed vector database for production RAG. Serverless (pay-per-query) or pod-based (dedicated). |
| 4 | |
| 5 | ## Setup |
| 6 | |
| 7 | ```bash |
| 8 | pip install pinecone-client sentence-transformers openai |
| 9 | ``` |
| 10 | |
| 11 | ```python |
| 12 | from pinecone import Pinecone, ServerlessSpec |
| 13 | |
| 14 | pc = Pinecone(api_key="your-api-key") # or os.environ["PINECONE_API_KEY"] |
| 15 | ``` |
| 16 | |
| 17 | --- |
| 18 | |
| 19 | ## Create Index |
| 20 | |
| 21 | ```python |
| 22 | # Serverless (pay-per-query — cheapest to start) |
| 23 | pc.create_index( |
| 24 | name="my-index", |
| 25 | dimension=1536, # match your embedding model |
| 26 | metric="cosine", # cosine | euclidean | dotproduct |
| 27 | spec=ServerlessSpec( |
| 28 | cloud="aws", |
| 29 | region="us-east-1" |
| 30 | ) |
| 31 | ) |
| 32 | |
| 33 | # Connect to index |
| 34 | index = pc.Index("my-index") |
| 35 | ``` |
| 36 | |
| 37 | --- |
| 38 | |
| 39 | ## Upsert Vectors |
| 40 | |
| 41 | ```python |
| 42 | from sentence_transformers import SentenceTransformer |
| 43 | |
| 44 | model = SentenceTransformer("all-MiniLM-L6-v2") # dim=384 |
| 45 | |
| 46 | documents = [ |
| 47 | {"id": "doc1", "text": "Python async programming guide"}, |
| 48 | {"id": "doc2", "text": "Machine learning with PyTorch"}, |
| 49 | ] |
| 50 | |
| 51 | vectors = [] |
| 52 | for doc in documents: |
| 53 | embedding = model.encode(doc["text"]).tolist() |
| 54 | vectors.append({ |
| 55 | "id": doc["id"], |
| 56 | "values": embedding, |
| 57 | "metadata": {"text": doc["text"], "source": "manual"} |
| 58 | }) |
| 59 | |
| 60 | # Batch upsert (max 100 per call) |
| 61 | index.upsert(vectors=vectors, namespace="docs") |
| 62 | ``` |
| 63 | |
| 64 | --- |
| 65 | |
| 66 | ## Query |
| 67 | |
| 68 | ```python |
| 69 | query_text = "how to write async Python?" |
| 70 | query_vector = model.encode(query_text).tolist() |
| 71 | |
| 72 | results = index.query( |
| 73 | vector=query_vector, |
| 74 | top_k=5, |
| 75 | namespace="docs", |
| 76 | include_metadata=True, |
| 77 | ) |
| 78 | |
| 79 | for match in results["matches"]: |
| 80 | print(f"Score: {match['score']:.3f} | {match['metadata']['text']}") |
| 81 | ``` |
| 82 | |
| 83 | --- |
| 84 | |
| 85 | ## Metadata Filtering |
| 86 | |
| 87 | ```python |
| 88 | results = index.query( |
| 89 | vector=query_vector, |
| 90 | top_k=5, |
| 91 | filter={"source": {"$eq": "manual"}}, |
| 92 | include_metadata=True, |
| 93 | ) |
| 94 | |
| 95 | # Operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $and, $or |
| 96 | results = index.query( |
| 97 | vector=query_vector, |
| 98 | top_k=5, |
| 99 | filter={ |
| 100 | "$and": [ |
| 101 | {"category": {"$in": ["tech", "science"]}}, |
| 102 | {"year": {"$gte": 2023}}, |
| 103 | ] |
| 104 | }, |
| 105 | include_metadata=True, |
| 106 | ) |
| 107 | ``` |
| 108 | |
| 109 | --- |
| 110 | |
| 111 | ## Namespaces |
| 112 | |
| 113 | ```python |
| 114 | # Different namespaces = separate vector spaces (free, no extra cost) |
| 115 | index.upsert(vectors=vectors, namespace="user-123") |
| 116 | index.upsert(vectors=vectors, namespace="user-456") |
| 117 | |
| 118 | # Query specific namespace |
| 119 | results = index.query(vector=query_vector, top_k=5, namespace="user-123") |
| 120 | |
| 121 | # Delete namespace |
| 122 | index.delete(delete_all=True, namespace="user-123") |
| 123 | ``` |
| 124 | |
| 125 | --- |
| 126 | |
| 127 | ## Fetch / Delete / Update |
| 128 | |
| 129 | ```python |
| 130 | # Fetch specific vectors |
| 131 | fetched = index.fetch(ids=["doc1", "doc2"], namespace="docs") |
| 132 | |
| 133 | # Delete vectors |
| 134 | index.delete(ids=["doc1"], namespace="docs") |
| 135 | |
| 136 | # Update metadata (re-upsert with same id) |
| 137 | index.upsert(vectors=[{"id": "doc1", "values": embedding, "metadata": {"updated": True}}]) |
| 138 | ``` |
| 139 | |
| 140 | --- |
| 141 | |
| 142 | ## Index Stats |
| 143 | |
| 144 | ```python |
| 145 | stats = index.describe_index_stats() |
| 146 | print(f"Total vectors: {stats['total_vector_count']}") |
| 147 | print(f"Namespaces: {stats['namespaces']}") |
| 148 | ``` |
| 149 | |
| 150 | --- |
| 151 | |
| 152 | ## When to Use Pinecone vs Alternatives |
| 153 | |
| 154 | | | Pinecone | Qdrant | Chroma | |
| 155 | |---|---|---|---| |
| 156 | | Hosting | Managed cloud | Self/cloud | Self/cloud | |
| 157 | | Cost | Pay-per-use | Self-hosted free | Free | |
| 158 | | Scale | Billions | Millions+ | Millions | |
| 159 | | Setup | Minutes | Minutes | Seconds | |
| 160 | | Best for | Production SaaS | Production self-hosted | Local dev/RAG | |