Pinecone Vector Database
Vector Database Pinecone Embedding Similarity Search RAG Semantic Search AI Application Developer Experience
| Vector DB | Type | Free Tier | DX Score | Best For |
|---|---|---|---|---|
| Pinecone | Managed SaaS | 100K vectors | ดีมาก | Production RAG |
| Weaviate | Open Source + Cloud | Self-host free | ดี | Multi-modal |
| Qdrant | Open Source + Cloud | Self-host free | ดี | Performance |
| ChromaDB | Open Source | Free (local) | ง่ายมาก | Prototyping |
| pgvector | PostgreSQL Extension | Free (self-host) | ปานกลาง | Existing Postgres |
| Milvus | Open Source | Self-host free | ปานกลาง | Large Scale |
Pinecone Setup & Usage
# === Pinecone Quick Start ===
# pip install pinecone-client openai
# from pinecone import Pinecone, ServerlessSpec
# import openai
#
# # Initialize
# pc = Pinecone(api_key="YOUR_API_KEY")
#
# # Create Index
# pc.create_index(
# name="knowledge-base",
# dimension=1536, # OpenAI ada-002
# metric="cosine",
# spec=ServerlessSpec(cloud="aws", region="us-east-1")
# )
#
# index = pc.Index("knowledge-base")
#
# # Create Embedding
# def get_embedding(text):
# response = openai.embeddings.create(
# model="text-embedding-ada-002",
# input=text
# )
# return response.data[0].embedding
#
# # Upsert Vectors
# vectors = [
# {"id": "doc-1", "values": get_embedding("Python programming guide"),
# "metadata": {"source": "docs", "category": "programming"}},
# {"id": "doc-2", "values": get_embedding("Machine learning tutorial"),
# "metadata": {"source": "blog", "category": "ml"}},
# ]
# index.upsert(vectors=vectors, namespace="articles")
#
# # Query
# query_embedding = get_embedding("How to learn Python")
# results = index.query(
# vector=query_embedding,
# top_k=5,
# namespace="articles",
# include_metadata=True,
# filter={"category": {"$eq": "programming"}}
# )
from dataclasses import dataclass
@dataclass
class PineconeFeature:
feature: str
description: str
code_example: str
dx_benefit: str
features = [
PineconeFeature("Serverless Index",
"ไม่ต้อง Manage Infrastructure Scale อัตโนมัติ",
"ServerlessSpec(cloud='aws', region='us-east-1')",
"สร้าง Index 1 บรรทัด ไม่ต้อง Config Server"),
PineconeFeature("Namespace",
"แยกข้อมูลใน Index เดียวกัน ไม่ต้องสร้างหลาย Index",
"index.upsert(vectors, namespace='articles')",
"จัดระเบียบข้อมูลง่าย ลด Cost"),
PineconeFeature("Metadata Filtering",
"กรองผลลัพธ์ด้วย Metadata ไม่ใช่แค่ Vector Similarity",
"filter={'category': {'$eq': 'ml'}}",
"ผลลัพธ์แม่นยำขึ้น ลด Noise"),
PineconeFeature("Sparse-Dense Hybrid",
"ผสม Keyword Search กับ Semantic Search",
"index.query(vector=dense, sparse_vector=sparse)",
"ค้นหาได้ทั้ง Exact Match และ Semantic"),
]
print("=== Pinecone Features ===")
for f in features:
print(f" [{f.feature}] {f.description}")
print(f" Code: {f.code_example}")
print(f" DX: {f.dx_benefit}")
RAG Pipeline
# === RAG with Pinecone + LangChain ===
# from langchain_openai import OpenAIEmbeddings, ChatOpenAI
# from langchain_pinecone import PineconeVectorStore
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain.chains import RetrievalQA
#
# # 1. Indexing
# text_splitter = RecursiveCharacterTextSplitter(
# chunk_size=1000,
# chunk_overlap=200,
# )
# chunks = text_splitter.split_documents(documents)
#
# embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
# vectorstore = PineconeVectorStore.from_documents(
# chunks, embeddings, index_name="knowledge-base"
# )
#
# # 2. Retrieval + Generation
# llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# qa_chain = RetrievalQA.from_chain_type(
# llm=llm,
# retriever=vectorstore.as_retriever(
# search_kwargs={"k": 5}
# ),
# )
#
# answer = qa_chain.invoke("How to optimize Python code?")
@dataclass
class RAGStep:
step: str
tool: str
config: str
optimization: str
steps = [
RAGStep("Document Loading",
"LangChain DocumentLoader",
"PDF, Web, Database, API ทุก Source",
"Pre-process ลบ Noise Header Footer"),
RAGStep("Chunking",
"RecursiveCharacterTextSplitter",
"chunk_size=1000, overlap=200",
"ปรับ Size ตาม Content Type ลอง 500-1500"),
RAGStep("Embedding",
"OpenAI ada-002 / sentence-transformers",
"dimension=1536 (ada-002) หรือ 768 (sbert)",
"Batch Embedding ลด API Call"),
RAGStep("Indexing",
"Pinecone Upsert",
"Namespace per Collection, Metadata Tags",
"Upsert Batch 100 vectors ต่อครั้ง"),
RAGStep("Retrieval",
"Pinecone Query + Metadata Filter",
"top_k=5, filter by source/date/category",
"Re-ranking ด้วย Cross-encoder เพิ่ม Precision"),
RAGStep("Generation",
"GPT-4o-mini / Llama / Mistral",
"temperature=0 สำหรับ Factual Answer",
"ใส่ System Prompt กำหนด Format"),
]
print("=== RAG Pipeline ===")
for s in steps:
print(f" [{s.step}] Tool: {s.tool}")
print(f" Config: {s.config}")
print(f" Optimize: {s.optimization}")
Production Operations
# === Production Monitoring ===
@dataclass
class ProdMetric:
metric: str
target: str
monitor: str
alert: str
metrics = [
ProdMetric("Query Latency p99",
"< 100ms",
"Pinecone Console + Custom Prometheus",
"> 200ms → Warning, > 500ms → Critical"),
ProdMetric("Query Accuracy (Relevance)",
"> 80% relevant in top-5",
"Human evaluation sample weekly",
"< 70% → Review Chunking + Embedding"),
ProdMetric("Index Freshness",
"< 1 hour behind source",
"Custom metric: last_upsert_time",
"> 2hr → Warning (stale data)"),
ProdMetric("Vector Count",
"ตาม Plan limit",
"Pinecone Console",
"> 80% limit → Plan upgrade"),
ProdMetric("Error Rate",
"< 0.1%",
"API response status codes",
"> 1% → Critical"),
]
print("=== Production Metrics ===")
for m in metrics:
print(f" [{m.metric}] Target: {m.target}")
print(f" Monitor: {m.monitor}")
print(f" Alert: {m.alert}")
เคล็ดลับ
- Serverless: ใช้ Serverless Index ราคาถูก Scale อัตโนมัติ
- Namespace: ใช้ Namespace แยกข้อมูลแทนสร้างหลาย Index
- Chunk: ทดลอง Chunk Size 500-1500 ดูผลลัพธ์ก่อนตัดสินใจ
- Metadata: ใส่ Metadata ทุก Vector กรองผลลัพธ์ได้แม่นยำ
- Re-rank: ใช้ Cross-encoder Re-ranking เพิ่ม Precision
Best Practices สำหรับนักพัฒนา
การเขียนโค้ดที่ดีไม่ใช่แค่ทำให้โปรแกรมทำงานได้ แต่ต้องเขียนให้อ่านง่าย ดูแลรักษาง่าย และ Scale ได้ หลัก SOLID Principles เป็นพื้นฐานสำคัญที่นักพัฒนาทุกู้คืนควรเข้าใจ ได้แก่ Single Responsibility ที่แต่ละ Class ทำหน้าที่เดียว Open-Closed ที่เปิดให้ขยายแต่ปิดการแก้ไข Liskov Substitution ที่ Subclass ต้องใช้แทน Parent ได้ Interface Segregation ที่แยก Interface ให้เล็ก และ Dependency Inversion ที่พึ่งพา Abstraction ไม่ใช่ Implementation
เรื่อง Testing ก็ขาดไม่ได้ ควรเขียน Unit Test ครอบคลุมอย่างน้อย 80% ของ Code Base ใช้ Integration Test ทดสอบการทำงานร่วมกันของ Module ต่างๆ และ E2E Test สำหรับ Critical User Flow เครื่องมือยอดนิยมเช่น Jest, Pytest, JUnit ช่วยให้การเขียน Test เป็นเรื่องง่าย
เรื่อง Version Control ด้วย Git ใช้ Branch Strategy ที่เหมาะกับทีม เช่น Git Flow สำหรับโปรเจคใหญ่ หรือ Trunk-Based Development สำหรับทีมที่ Deploy บ่อย ทำ Code Review ทุก Pull Request และใช้ CI/CD Pipeline ทำ Automated Testing และ Deployment
Vector Database คืออะไร
เก็บ Vector Embedding Similarity Search RAG Semantic Search Recommendation Image Search Pinecone Managed Serverless Free Tier
Pinecone DX ดีอย่างไร
SDK 3 บรรทัด Serverless Console Dashboard Documentation Free Tier LangChain LlamaIndex Auto-scaling Namespace Metadata Hybrid Search
ตั้งค่าอย่างไร
pinecone.io สมัคร create_index dimension 1536 pip install pinecone-client API Key Upsert Query Namespace Metadata Filter Serverless
ใช้กับ RAG อย่างไร
Indexing Chunk Embedding Upsert Retrieval Query top_k Metadata Filter Generation LLM LangChain LlamaIndex Chunk Size 1000 Overlap 200 Re-ranking
สรุป
Vector Database Pinecone Developer Experience Embedding RAG Pipeline Serverless Namespace Metadata LangChain Similarity Search Production AI
