AI
Vector Database
Pinecone
Weaviate
ChromaDB
RAG
Advanced

Vector Database สำหรับ AI: Pinecone, Weaviate, ChromaDB

เรียนรู้ Vector Database สำหรับ AI applications เปรียบเทียบ Pinecone, Weaviate, ChromaDB, Qdrant พร้อมตัวอย่างการใช้งานและ best practices

AI Unlocked Team
30/01/2568
Vector Database สำหรับ AI: Pinecone, Weaviate, ChromaDB

Vector Database สำหรับ AI: Pinecone, Weaviate, ChromaDB

Vector Database เป็นหัวใจสำคัญของ AI applications ยุคใหม่ โดยเฉพาะระบบ RAG (Retrieval-Augmented Generation) บทความนี้จะพาคุณไปเรียนรู้ Vector Database ตั้งแต่พื้นฐานไปจนถึงการเลือกใช้งานจริง

Vector Database คืออะไร?

Vector Database คือฐานข้อมูลที่ออกแบบมาเพื่อเก็บและค้นหา vectors (embeddings) อย่างมีประสิทธิภาพ ต่างจาก database ทั่วไปที่ค้นหาด้วย exact match, Vector Database ค้นหาด้วย similarity (ความคล้ายคลึง)

Traditional DB vs Vector DB

Traditional Database:
SELECT * FROM products WHERE name = "iPhone 16"
→ ค้นหาแบบ exact match

Vector Database:
query = embed("smartphone with good camera")
results = vector_db.search(query, top_k=5)
→ ค้นหาด้วย semantic similarity
→ อาจพบ "iPhone 16", "Samsung Galaxy S24", "Pixel 9"

หลักการทำงาน

┌─────────────────────────────────────────────────────────────────┐
│                 How Vector Database Works                       │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  1. Indexing (เก็บข้อมูล)                                        │
│  ┌──────────┐    ┌────────────┐    ┌────────────────┐          │
│  │ Document │───▶│ Embedding  │───▶│ Vector DB      │          │
│  │ "Hello"  │    │ Model      │    │ [0.1, 0.8,...] │          │
│  └──────────┘    └────────────┘    └────────────────┘          │
│                                                                 │
│  2. Querying (ค้นหา)                                            │
│  ┌──────────┐    ┌────────────┐    ┌────────────────┐          │
│  │ Query    │───▶│ Embedding  │───▶│ Similarity     │          │
│  │ "Hi"     │    │ Model      │    │ Search         │          │
│  └──────────┘    └────────────┘    └───────┬────────┘          │
│                                            │                    │
│                                            ▼                    │
│                                    ┌────────────────┐          │
│                                    │ Results:       │          │
│                                    │ 1. "Hello"     │          │
│                                    │ 2. "Hey there" │          │
│                                    └────────────────┘          │
└─────────────────────────────────────────────────────────────────┘

ทำไมต้องใช้ Vector Database?

  1. Semantic Search: ค้นหาด้วยความหมาย ไม่ใช่แค่ keyword
  2. Speed: ค้นหาเร็วแม้มี vectors ล้านๆ ตัว
  3. Scalability: รองรับข้อมูลขนาดใหญ่
  4. Integration: เชื่อมต่อกับ AI workflows ได้ง่าย

เปรียบเทียบ Vector Databases

Overview

FeaturePineconeWeaviateChromaDBQdrantpgvector
TypeManagedSelf/ManagedSelf-hostedSelf/ManagedExtension
Open SourceNoYesYesYesYes
Free TierYesYesYesYes-
Hybrid SearchYesYesNoYesLimited
FilteringExcellentGoodBasicExcellentGood
Max Dimensions20,000UnlimitedUnlimited65,5352,000
Best ForProductionEnterprisePrototypingPerformancePostgreSQL users

1. Pinecone

Pinecone เป็น fully managed vector database ที่เน้นความง่ายในการใช้งาน

# Installation
# 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,  # OpenAI embedding size
    metric="cosine",
    spec=ServerlessSpec(
        cloud="aws",
        region="us-east-1"
    )
)

# Get index
index = pc.Index("my-index")

# Upsert vectors
index.upsert(
    vectors=[
        {
            "id": "doc1",
            "values": [0.1, 0.2, ...],  # 1536 dimensions
            "metadata": {
                "title": "Document 1",
                "category": "tech"
            }
        },
        {
            "id": "doc2",
            "values": [0.3, 0.4, ...],
            "metadata": {
                "title": "Document 2",
                "category": "business"
            }
        }
    ],
    namespace="my-namespace"
)

# Query
results = index.query(
    namespace="my-namespace",
    vector=[0.1, 0.2, ...],
    top_k=5,
    include_metadata=True,
    filter={
        "category": {"$eq": "tech"}
    }
)

for match in results['matches']:
    print(f"{match['id']}: {match['score']}")
    print(f"Metadata: {match['metadata']}")

ข้อดี:

  • Setup ง่าย ไม่ต้อง manage infrastructure
  • Performance ดีมาก
  • Filtering ทรงพลัง

ข้อเสีย:

  • ไม่ open source
  • ราคาแพงในระดับ scale

2. Weaviate

Weaviate เป็น open source vector database ที่มี features ครบครัน

# Installation
# pip install weaviate-client

import weaviate
from weaviate.classes.init import Auth

# Connect to Weaviate Cloud
client = weaviate.connect_to_weaviate_cloud(
    cluster_url="https://your-cluster.weaviate.network",
    auth_credentials=Auth.api_key("your-api-key")
)

# Or connect to local instance
# client = weaviate.connect_to_local()

# Create collection (schema)
from weaviate.classes.config import Property, DataType

client.collections.create(
    name="Document",
    properties=[
        Property(name="title", data_type=DataType.TEXT),
        Property(name="content", data_type=DataType.TEXT),
        Property(name="category", data_type=DataType.TEXT),
    ],
    vectorizer_config=weaviate.classes.config.Configure.Vectorizer.text2vec_openai()
)

# Get collection
documents = client.collections.get("Document")

# Add objects (auto-vectorization)
documents.data.insert({
    "title": "Introduction to AI",
    "content": "Artificial Intelligence is...",
    "category": "tech"
})

# Query with near_text (semantic search)
response = documents.query.near_text(
    query="machine learning basics",
    limit=5,
    return_metadata=weaviate.classes.query.MetadataQuery(distance=True)
)

for obj in response.objects:
    print(f"Title: {obj.properties['title']}")
    print(f"Distance: {obj.metadata.distance}")

# Hybrid search (vector + keyword)
response = documents.query.hybrid(
    query="AI introduction",
    alpha=0.5,  # 0 = keyword only, 1 = vector only
    limit=5
)

client.close()

ข้อดี:

  • Open source, self-host ได้
  • Built-in vectorization
  • Hybrid search
  • GraphQL API

ข้อเสีย:

  • Learning curve สูงกว่า
  • Resource intensive

3. ChromaDB

ChromaDB เป็น lightweight vector database ที่เหมาะสำหรับ prototyping

# Installation
# pip install chromadb

import chromadb
from chromadb.config import Settings

# Persistent storage
client = chromadb.PersistentClient(path="./chroma_db")

# Or in-memory
# client = chromadb.Client()

# Create collection
collection = client.create_collection(
    name="my_documents",
    metadata={"hnsw:space": "cosine"}  # distance metric
)

# Add documents (auto-embedding with default model)
collection.add(
    documents=[
        "This is document 1 about AI",
        "This is document 2 about machine learning",
        "This is document 3 about data science"
    ],
    metadatas=[
        {"source": "wiki", "category": "ai"},
        {"source": "blog", "category": "ml"},
        {"source": "paper", "category": "ds"}
    ],
    ids=["doc1", "doc2", "doc3"]
)

# Or add with pre-computed embeddings
collection.add(
    embeddings=[[0.1, 0.2, ...], [0.3, 0.4, ...]],
    documents=["doc1 text", "doc2 text"],
    ids=["id1", "id2"]
)

# Query
results = collection.query(
    query_texts=["What is artificial intelligence?"],
    n_results=5,
    where={"category": "ai"},  # metadata filter
    include=["documents", "metadatas", "distances"]
)

print(results['documents'])
print(results['distances'])

# Update
collection.update(
    ids=["doc1"],
    documents=["Updated document 1"],
    metadatas=[{"source": "updated"}]
)

# Delete
collection.delete(ids=["doc3"])

ข้อดี:

  • ง่ายมาก เริ่มต้นได้เร็ว
  • Lightweight, ไม่ต้อง setup server
  • Built-in embedding

ข้อเสีย:

  • ไม่เหมาะสำหรับ production scale
  • Features จำกัด

4. Qdrant

Qdrant เป็น high-performance vector database ที่เขียนด้วย Rust

# Installation
# pip install qdrant-client

from qdrant_client import QdrantClient
from qdrant_client.models import (
    VectorParams, Distance, PointStruct,
    Filter, FieldCondition, MatchValue
)

# Connect to Qdrant Cloud or local
client = QdrantClient(
    url="https://your-cluster.qdrant.io",
    api_key="your-api-key"
)

# Or local
# client = QdrantClient(host="localhost", port=6333)

# Create collection
client.create_collection(
    collection_name="documents",
    vectors_config=VectorParams(
        size=1536,
        distance=Distance.COSINE
    )
)

# Upsert points
client.upsert(
    collection_name="documents",
    points=[
        PointStruct(
            id=1,
            vector=[0.1, 0.2, ...],
            payload={
                "title": "Document 1",
                "category": "tech",
                "timestamp": 1704067200
            }
        ),
        PointStruct(
            id=2,
            vector=[0.3, 0.4, ...],
            payload={
                "title": "Document 2",
                "category": "business",
                "timestamp": 1704153600
            }
        )
    ]
)

# Search
results = client.search(
    collection_name="documents",
    query_vector=[0.1, 0.2, ...],
    limit=5,
    query_filter=Filter(
        must=[
            FieldCondition(
                key="category",
                match=MatchValue(value="tech")
            )
        ]
    ),
    with_payload=True
)

for result in results:
    print(f"ID: {result.id}, Score: {result.score}")
    print(f"Payload: {result.payload}")

# Batch search
results = client.search_batch(
    collection_name="documents",
    requests=[
        {"vector": [0.1, 0.2, ...], "limit": 3},
        {"vector": [0.3, 0.4, ...], "limit": 3}
    ]
)

ข้อดี:

  • Performance สูงมาก (Rust)
  • Filtering ทรงพลัง
  • Good documentation

ข้อเสีย:

  • Self-hosting ต้อง manage เอง

5. pgvector (PostgreSQL Extension)

pgvector เหมาะสำหรับคนที่ใช้ PostgreSQL อยู่แล้ว

-- Enable extension
CREATE EXTENSION vector;

-- Create table with vector column
CREATE TABLE documents (
    id SERIAL PRIMARY KEY,
    title TEXT,
    content TEXT,
    embedding vector(1536),  -- OpenAI embedding size
    created_at TIMESTAMP DEFAULT NOW()
);

-- Create index for fast search
CREATE INDEX ON documents
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);

-- Insert with vector
INSERT INTO documents (title, content, embedding)
VALUES (
    'AI Introduction',
    'Artificial intelligence is...',
    '[0.1, 0.2, ...]'::vector
);

-- Search by similarity
SELECT id, title, content,
       1 - (embedding <=> '[0.1, 0.2, ...]'::vector) as similarity
FROM documents
ORDER BY embedding <=> '[0.1, 0.2, ...]'::vector
LIMIT 5;

-- With filter
SELECT id, title,
       1 - (embedding <=> $1::vector) as similarity
FROM documents
WHERE created_at > NOW() - INTERVAL '7 days'
ORDER BY embedding <=> $1::vector
LIMIT 5;
# Python with pgvector
import psycopg2
from pgvector.psycopg2 import register_vector

conn = psycopg2.connect("postgresql://...")
register_vector(conn)

cur = conn.cursor()

# Insert
embedding = [0.1, 0.2, ...]  # from OpenAI
cur.execute(
    "INSERT INTO documents (title, content, embedding) VALUES (%s, %s, %s)",
    ("Title", "Content", embedding)
)

# Search
query_embedding = [0.1, 0.2, ...]
cur.execute(
    """
    SELECT id, title, 1 - (embedding <=> %s::vector) as similarity
    FROM documents
    ORDER BY embedding <=> %s::vector
    LIMIT 5
    """,
    (query_embedding, query_embedding)
)

results = cur.fetchall()

ข้อดี:

  • ใช้ร่วมกับ PostgreSQL ได้เลย
  • ไม่ต้องเพิ่ม infrastructure
  • SQL ที่คุ้นเคย

ข้อเสีย:

  • Performance ต่ำกว่า dedicated vector DB
  • Features จำกัด

การเลือก Vector Database

Decision Framework

เลือก Vector Database:

1. Production & Scale สูง?
   ├─ Yes + ต้องการ managed → Pinecone
   ├─ Yes + self-host ได้ → Qdrant หรือ Weaviate
   └─ No →

2. Prototype / เริ่มต้น?
   └─ Yes → ChromaDB

3. ใช้ PostgreSQL อยู่แล้ว?
   └─ Yes → pgvector

4. ต้องการ Hybrid Search?
   └─ Yes → Weaviate หรือ Qdrant

5. Budget จำกัด?
   └─ Yes → Open source (Weaviate, Qdrant, ChromaDB)

Use Case Recommendations

Use CaseRecommendedWhy
Startup MVPChromaDBQuick setup, free
Enterprise RAGWeaviateFeatures, self-host
E-commerce SearchPineconeManaged, reliable
Real-time AppQdrantPerformance
Existing PostgreSQLpgvectorNo new infra

Building RAG with Vector Database

Complete Example with ChromaDB

import chromadb
from openai import OpenAI
from typing import List

class SimpleRAG:
    def __init__(self):
        self.chroma = chromadb.PersistentClient(path="./rag_db")
        self.collection = self.chroma.get_or_create_collection(
            name="knowledge_base"
        )
        self.openai = OpenAI()

    def add_documents(self, documents: List[str], ids: List[str]):
        """Add documents to knowledge base"""
        # Generate embeddings
        embeddings = []
        for doc in documents:
            response = self.openai.embeddings.create(
                model="text-embedding-3-small",
                input=doc
            )
            embeddings.append(response.data[0].embedding)

        # Store in ChromaDB
        self.collection.add(
            embeddings=embeddings,
            documents=documents,
            ids=ids
        )

    def query(self, question: str, top_k: int = 3) -> str:
        """Query the RAG system"""
        # Get question embedding
        response = self.openai.embeddings.create(
            model="text-embedding-3-small",
            input=question
        )
        query_embedding = response.data[0].embedding

        # Search
        results = self.collection.query(
            query_embeddings=[query_embedding],
            n_results=top_k
        )

        # Build context
        context = "\n\n".join(results['documents'][0])

        # Generate answer
        response = self.openai.chat.completions.create(
            model="gpt-4o",
            messages=[
                {
                    "role": "system",
                    "content": f"Answer based on context:\n{context}"
                },
                {"role": "user", "content": question}
            ]
        )

        return response.choices[0].message.content

# Usage
rag = SimpleRAG()

# Add documents
rag.add_documents(
    documents=[
        "AI Unlocked is a company that provides AI education.",
        "Our courses cover prompt engineering, RAG, and fine-tuning.",
        "We offer both online and in-person training."
    ],
    ids=["doc1", "doc2", "doc3"]
)

# Query
answer = rag.query("What courses does AI Unlocked offer?")
print(answer)

อ่านเพิ่มเติมเกี่ยวกับ RAG ได้ที่ RAG คืออะไร? ทำไมถึงสำคัญสำหรับ AI

Best Practices

1. Chunking Strategy

from langchain.text_splitter import RecursiveCharacterTextSplitter

# Good chunking for RAG
splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,      # Not too large
    chunk_overlap=50,    # Some overlap for context
    separators=["\n\n", "\n", ".", " "]
)

chunks = splitter.split_text(document)

2. Embedding Model Selection

# For English: OpenAI text-embedding-3-small
# For multilingual: Cohere multilingual
# For code: CodeBERT
# For cost-sensitive: sentence-transformers (free)

from sentence_transformers import SentenceTransformer

# Free alternative
model = SentenceTransformer('all-MiniLM-L6-v2')
embedding = model.encode("Hello world")

3. Index Optimization

# Pinecone: Use namespaces for organization
index.upsert(vectors=[...], namespace="category_a")

# Qdrant: Use payload indexes for filtering
client.create_payload_index(
    collection_name="docs",
    field_name="category",
    field_schema="keyword"
)

# ChromaDB: Use appropriate distance metric
collection = client.create_collection(
    name="docs",
    metadata={"hnsw:space": "cosine"}  # or "l2", "ip"
)

สรุป

Vector Database เป็นเครื่องมือสำคัญสำหรับการสร้าง AI applications ที่ต้องการ semantic search การเลือกใช้ database ที่เหมาะสมขึ้นอยู่กับ use case, scale, และงบประมาณของคุณ


อ่านบทความที่เกี่ยวข้อง


พร้อมเริ่มใช้ Vector Database แล้วหรือยัง?

ติดต่อทีม AI Unlocked สำหรับคำปรึกษาและการพัฒนา AI solution ที่ใช้ Vector Database


เขียนโดย

AI Unlocked Team