pinecone について
Pineconeは、本番AIアプリケーションを構築する開発者向けのマネージドベクトルデータベースです。ベクトルの登録・検索、メタデータフィルター、名前空間、密ベクトルと疎ベクトルのハイブリッド検索に対応します。
主な機能
- 01マネージドなベクトルインデックス管理
- 02ベクトル登録と類似検索
- 03メタデータフィルターと名前空間
- 04密・疎ベクトルのハイブリッド検索
- 05インデックス統計とベクトル削除
活用シーン
- 01本番RAGアプリケーションのベクトル保存
- 02ユーザーやテナントごとの検索データ分離
- 03メタデータによるベクトル検索の絞り込み
- 04キーワード検索と意味検索の併用
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ orchestra-research/ ai-research-skills --skill pineconeインストール後、Claude Code で/pineconeを入力して読み込まれたか確認します。
同梱ファイルreferences/deployment.md
詳細
Pinecone - マネージド・ベクトルデータベース
本番環境のAIアプリケーション向けのベクトルデータベースです。
Pineconeを使用するタイミング
使用する場合:
- マネージド型のサーバーレスベクトルデータベースが必要な場合
- 本番環境のRAGアプリケーション
- 自動スケーリングが必要な場合
- 低レイテンシが極めて重要な場合(<100ms)
- インフラストラクチャを管理したくない場合
- ハイブリッド検索(密ベクトル + 疎ベクトル)が必要な場合
指標:
- 完全マネージド型のSaaS
- 数十億ベクトルまで自動スケーリング
- p95レイテンシ <100ms
- 99.9%の稼働率SLA
代わりに以下を検討してください:
- Chroma: セルフホスト可能、オープンソース
- FAISS: オフライン、純粋な類似度検索
- Weaviate: 機能が充実したセルフホスト可能なソリューション
クイックスタート
インストール
pip install pinecone-client
基本的な使い方
from pinecone import Pinecone, ServerlessSpec
# Initialize
pc = Pinecone(api_key="your-api-key")
# Create index
pc.create_index(
name="my-index",
dimension=1536, # Must match embedding dimension
metric="cosine", # or "euclidean", "dotproduct"
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)
# Connect to index
index = pc.Index("my-index")
# Upsert vectors
index.upsert(vectors=[
{"id": "vec1", "values": [0.1, 0.2, ...], "metadata": {"category": "A"}},
{"id": "vec2", "values": [0.3, 0.4, ...], "metadata": {"category": "B"}}
])
# Query
results = index.query(
vector=[0.1, 0.2, ...],
top_k=5,
include_metadata=True
)
print(results["matches"])
主な操作
インデックスの作成
# Serverless (recommended)
pc.create_index(
name="my-index",
dimension=1536,
metric="cosine",
spec=ServerlessSpec(
cloud="aws", # or "gcp", "azure"
region="us-east-1"
)
)
# Pod-based (for consistent performance)
from pinecone import PodSpec
pc.create_index(
name="my-index",
dimension=1536,
metric="cosine",
spec=PodSpec(
environment="us-east1-gcp",
pod_type="p1.x1"
)
)
ベクトルのupsert
# Single upsert
index.upsert(vectors=[
{
"id": "doc1",
"values": [0.1, 0.2, ...], # 1536 dimensions
"metadata": {
"text": "Document content",
"category": "tutorial",
"timestamp": "2025-01-01"
}
}
])
# Batch upsert (recommended)
vectors = [
{"id": f"vec{i}", "values": embedding, "metadata": metadata}
for i, (embedding, metadata) in enumerate(zip(embeddings, metadatas))
]
index.upsert(vectors=vectors, batch_size=100)
ベクトルのクエリ
# Basic query
results = index.query(
vector=[0.1, 0.2, ...],
top_k=10,
include_metadata=True,
include_values=False
)
# With metadata filtering
results = index.query(
vector=[0.1, 0.2, ...],
top_k=5,
filter={"category": {"$eq": "tutorial"}}
)
# Namespace query
results = index.query(
vector=[0.1, 0.2, ...],
top_k=5,
namespace="production"
)
# Access results
for match in results["matches"]:
print(f"ID: {match['id']}")
print(f"Score: {match['score']}")
print(f"Metadata: {match['metadata']}")
メタデータフィルタリング
# Exact match
filter = {"category": "tutorial"}
# Comparison
filter = {"price": {"$gte": 100}} # $gt, $gte, $lt, $lte, $ne
# Logical operators
filter = {
"$and": [
{"category": "tutorial"},
{"difficulty": {"$lte": 3}}
]
} # Also: $or
# In operator
filter = {"tags": {"$in": ["python", "ml"]}}
ネームスペース
# Partition data by namespace
index.upsert(
vectors=[{"id": "vec1", "values": [...]}],
namespace="user-123"
)
# Query specific namespace
results = index.query(
vector=[...],
namespace="user-123",
top_k=5
)
# List namespaces
stats = index.describe_index_stats()
print(stats['namespaces'])
ハイブリッド検索(密ベクトル + 疎ベクトル)
# Upsert with sparse vectors
index.upsert(vectors=[
{
"id": "doc1",
"values": [0.1, 0.2, ...], # Dense vector
"sparse_values": {
"indices": [10, 45, 123], # Token IDs
"values": [0.5, 0.3, 0.8] # TF-IDF scores
},
"metadata": {"text": "..."}
}
])
# Hybrid query
results = index.query(
vector=[0.1, 0.2, ...],
sparse_vector={
"indices": [10, 45],
"values": [0.5, 0.3]
},
top_k=5,
alpha=0.5 # 0=sparse, 1=dense, 0.5=hybrid
)
LangChain 連携
from langchain_pinecone import PineconeVectorStore
from langchain_openai import OpenAIEmbeddings
# Create vector store
vectorstore = PineconeVectorStore.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(),
index_name="my-index"
)
# Query
results = vectorstore.similarity_search("query", k=5)
# With metadata filter
results = vectorstore.similarity_search(
"query",
k=5,
filter={"category": "tutorial"}
)
# As retriever
retriever = vectorstore.as_retriever(search_kwargs={"k": 10})
LlamaIndex 連携
from llama_index.vector_stores.pinecone import PineconeVectorStore
# Connect to Pinecone
pc = Pinecone(api_key="your-key")
pinecone_index = pc.Index("my-index")
# Create vector store
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
# Use in LlamaIndex
from llama_index.core import StorageContext, VectorStoreIndex
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
インデックス管理
# List indices
indexes = pc.list_indexes()
# Describe index
index_info = pc.describe_index("my-index")
print(index_info)
# Get index stats
stats = index.describe_index_stats()
print(f"Total vectors: {stats['total_vector_count']}")
print(f"Namespaces: {stats['namespaces']}")
# Delete index
pc.delete_index("my-index")
ベクトルの削除
# Delete by ID
index.delete(ids=["vec1", "vec2"])
# Delete by filter
index.delete(filter={"category": "old"})
# Delete all in namespace
index.delete(delete_all=True, namespace="test")
# Delete entire index
index.delete(delete_all=True)
ベストプラクティス
- サーバーレスを使用する - 自動スケーリングでコスト効率が高い
- upsertをバッチ処理する - より効率的(バッチあたり100〜200件)
- メタデータを追加する - フィルタリングを有効にする
- ネームスペースを使用する - ユーザーやテナントごとにデータを分離する
- 使用状況を監視する - Pineconeダッシュボードで確認する
- フィルターを最適化する - 頻繁にフィルタリングされるフィールドにインデックスを付ける
- 無料枠でテストする - インデックス1つ、10万ベクトルまで無料
- ハイブリッド検索を使用する - 品質が向上する
- 適切な次元数を設定する - 埋め込みモデルに合わせる
- 定期的にバックアップする - 重要なデータをエクスポートする
パフォーマンス
| 操作 | レイテンシ | 備考 |
|---|---|---|
| Upsert | ~50-100ms | バッチごと |
| クエリ(p50) | ~50ms | インデックスのサイズに依存 |
| クエリ(p95) | ~100ms | SLAの目標値 |
| メタデータフィルター | ~+10-20ms | 追加のオーバーヘッド |
料金(2025年時点)
サーバーレス:
- 100万読み取りユニットあたり $0.096
- 100万書き込みユニットあたり $0.06
- ストレージ 1GB/月あたり $0.06
無料枠:
- サーバーレスインデックス 1つ
- 10万ベクトル(1536次元)
- プロトタイピングに最適
リソース
- ウェブサイト: https://www.pinecone.io
- ドキュメント: https://docs.pinecone.io
- コンソール: https://app.pinecone.io
- 料金: https://www.pinecone.io/pricing
コメント
Markdown 対応