[ PROMPT_NODE_22976 ]
pinecone
[ SKILL_DOCUMENTATION ]
# Pinecone - 托管向量数据库
专为生产级 AI 应用设计的向量数据库。
## 何时使用 Pinecone
**适用场景:**
- 需要托管的无服务器向量数据库
- 生产级 RAG 应用
- 需要自动扩缩容
- 对低延迟有严格要求(<100ms)
- 不想管理基础设施
- 需要混合搜索(稠密 + 稀疏向量)
**指标:**
- 全托管 SaaS
- 自动扩缩容至数十亿向量
- **p95 延迟 <100ms**
- 99.9% 正常运行时间 SLA
**替代方案:**
- **Chroma**: 自托管,开源
- **FAISS**: 离线,纯相似度搜索
- **Weaviate**: 自托管,功能更丰富
## 快速开始
### 安装
bash
pip install pinecone-client
### 基本用法
python
from pinecone import Pinecone, ServerlessSpec
# 初始化
pc = Pinecone(api_key="your-api-key")
# 创建索引
pc.create_index(
name="my-index",
dimension=1536, # 必须与嵌入维度匹配
metric="cosine", # 或 "euclidean", "dotproduct"
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)
# 连接到索引
index = pc.Index("my-index")
# Upsert 向量
index.upsert(vectors=[
{"id": "vec1", "values": [0.1, 0.2, ...], "metadata": {"category": "A"}},
{"id": "vec2", "values": [0.3, 0.4, ...], "metadata": {"category": "B"}}
])
# 查询
results = index.query(
vector=[0.1, 0.2, ...],
top_k=5,
include_metadata=True
)
print(results["matches"])
## 核心操作
### 创建索引
python
# 无服务器模式(推荐)
pc.create_index(
name="my-index",
dimension=1536,
metric="cosine",
spec=ServerlessSpec(
cloud="aws", # 或 "gcp", "azure"
region="us-east-1"
)
)
# 基于 Pod 模式(用于一致的性能)
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 向量
python
# 单个 Upsert
index.upsert(vectors=[
{
"id": "doc1",
"values": [0.1, 0.2, ...], # 1536 维度
"metadata": {
"text": "文档内容",
"category": "tutorial",
"timestamp": "2025-01-01"
}
}
])
# 批量 Upsert(推荐)
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)
### 查询向量
python
#