Vector Database Pinecone
Vector Database Pinecone Embedding Semantic Search RAG LLM Cosine Similarity Remote Work Knowledge Base AI Chatbot Weaviate Qdrant Milvus Chroma
| Vector DB | Type | Free Tier | Scale | เหมาะกับ |
|---|---|---|---|---|
| Pinecone | Managed | มี | Billion | Production SaaS |
| Weaviate | Self/Cloud | มี | Billion | Hybrid Search |
| Qdrant | Self/Cloud | มี | Billion | Performance |
| Chroma | Self-hosted | ฟรี | Million | Dev/Prototype |
| pgvector | PostgreSQL ext | ฟรี | Million | Existing PG |
Pinecone Setup
# === Pinecone Vector Database ===
# pip install pinecone-client openai langchain
# import os
# from pinecone import Pinecone, ServerlessSpec
# import openai
#
# # Initialize Pinecone
# pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
#
# # Create Index
# pc.create_index(
# name="company-knowledge",
# dimension=1536, # OpenAI text-embedding-3-small
# metric="cosine",
# spec=ServerlessSpec(
# cloud="aws",
# region="us-east-1"
# )
# )
#
# index = pc.Index("company-knowledge")
#
# # Generate Embedding
# def get_embedding(text, model="text-embedding-3-small"):
# response = openai.embeddings.create(
# input=text,
# model=model
# )
# return response.data[0].embedding
#
# # Upsert Documents
# documents = [
# {"id": "doc1", "text": "วิธีขอลาพักร้อน ส่ง Email ถึง HR..."},
# {"id": "doc2", "text": "นโยบาย Remote Work ทำงาน 8 ชม/วัน..."},
# {"id": "doc3", "text": "วิธีเข้าใช้ VPN เปิด Cisco AnyConnect..."},
# ]
#
# vectors = []
# for doc in documents:
# embedding = get_embedding(doc["text"])
# vectors.append({
# "id": doc["id"],
# "values": embedding,
# "metadata": {"text": doc["text"], "source": "wiki"}
# })
#
# index.upsert(vectors=vectors, namespace="hr-docs")
#
# # Semantic Search
# query = "วิธีทำงานจากบ้าน"
# query_embedding = get_embedding(query)
# results = index.query(
# vector=query_embedding,
# top_k=3,
# namespace="hr-docs",
# include_metadata=True
# )
# for match in results["matches"]:
# print(f"Score: {match['score']:.3f} | {match['metadata']['text'][:80]}")
from dataclasses import dataclass
@dataclass
class VectorIndex:
name: str
dimension: int
vectors: int
namespace: str
queries_day: int
avg_latency_ms: int
indexes = [
VectorIndex("company-knowledge", 1536, 15000, "hr-docs", 500, 25),
VectorIndex("company-knowledge", 1536, 8000, "engineering", 1200, 20),
VectorIndex("company-knowledge", 1536, 3000, "meeting-notes", 300, 22),
VectorIndex("product-docs", 1536, 25000, "user-guides", 2000, 18),
VectorIndex("support-tickets", 1536, 50000, "resolved", 800, 30),
]
print("=== Pinecone Indexes ===")
for idx in indexes:
print(f" [{idx.name}] ns:{idx.namespace}")
print(f" Vectors: {idx.vectors:,} | Dim: {idx.dimension}")
print(f" Queries: {idx.queries_day}/day | Latency: {idx.avg_latency_ms}ms")
RAG Pipeline
# === RAG with LangChain ===
# from langchain.embeddings import OpenAIEmbeddings
# from langchain.vectorstores import Pinecone
# from langchain.chat_models import ChatOpenAI
# from langchain.chains import RetrievalQA
# from langchain.text_splitter import RecursiveCharacterTextSplitter
#
# # Setup
# embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
# vectorstore = Pinecone.from_existing_index(
# index_name="company-knowledge",
# embedding=embeddings,
# namespace="hr-docs"
# )
#
# # RAG Chain
# llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# qa_chain = RetrievalQA.from_chain_type(
# llm=llm,
# chain_type="stuff",
# retriever=vectorstore.as_retriever(
# search_kwargs={"k": 5}
# ),
# return_source_documents=True
# )
#
# # Query
# result = qa_chain.invoke({"query": "วิธีขอลาพักร้อน"})
# print(result["result"])
# for doc in result["source_documents"]:
# print(f" Source: {doc.metadata['source']}")
# Document Ingestion Pipeline
# 1. Read documents (PDF, Markdown, Notion, Confluence)
# 2. Split into chunks (500-1000 tokens)
# 3. Generate embeddings
# 4. Upsert to Pinecone with metadata
# 5. Schedule re-index weekly
@dataclass
class RAGApp:
name: str
knowledge_source: str
vectors: int
queries_day: int
accuracy: float
users: int
apps = [
RAGApp("HR Assistant", "HR Wiki + Policy Docs", 15000, 200, 0.92, 150),
RAGApp("Engineering Docs", "Confluence + GitHub Wiki", 25000, 500, 0.89, 80),
RAGApp("Customer Support", "FAQ + Ticket History", 50000, 1000, 0.95, 30),
RAGApp("Meeting Search", "Meeting Transcripts", 8000, 150, 0.85, 120),
RAGApp("Onboarding Guide", "Onboarding Docs + Videos", 5000, 50, 0.91, 20),
]
print("\n=== RAG Applications ===")
for a in apps:
print(f" [{a.name}] {a.users} users")
print(f" Source: {a.knowledge_source}")
print(f" Vectors: {a.vectors:,} | Queries: {a.queries_day}/day | Acc: {a.accuracy:.0%}")
Remote Work Integration
# === Remote Work Knowledge Platform ===
# Slack Bot Integration
# from slack_bolt import App
# app = App(token=os.environ["SLACK_BOT_TOKEN"])
#
# @app.message(re.compile(r"^ask (.+)"))
# def handle_question(message, say, context):
# question = context["matches"][0]
# result = qa_chain.invoke({"query": question})
# answer = result["result"]
# sources = [d.metadata["source"] for d in result["source_documents"]]
# say(f"*Answer:* {answer}\n_Sources: {', '.join(sources)}_")
# API Server — FastAPI
# from fastapi import FastAPI
# app = FastAPI()
#
# @app.post("/search")
# async def search(query: str, namespace: str = "all"):
# embedding = get_embedding(query)
# results = index.query(
# vector=embedding, top_k=5,
# namespace=namespace,
# include_metadata=True
# )
# return {"results": results["matches"]}
#
# @app.post("/ask")
# async def ask(question: str):
# result = qa_chain.invoke({"query": question})
# return {"answer": result["result"]}
remote_tools = {
"Slack Bot": "ถามคำถามใน Slack ได้ทันที @ask-bot",
"Web Search": "Search Portal ค้นหาเอกสารทั้งองค์กร",
"API": "FastAPI Endpoint สำหรับ Integration",
"Chrome Extension": "ค้นหาจากทุก Tab ด้วย Extension",
"Mobile App": "ค้นหาจากมือถือ Progressive Web App",
"Teams Bot": "Integration กับ Microsoft Teams",
}
print("Remote Work Tools:")
for tool, desc in remote_tools.items():
print(f" [{tool}]: {desc}")
cost_estimate = {
"Pinecone Serverless": "$0.00 (Free Tier: 100K vectors)",
"Pinecone Standard": "$70/mo (1M vectors)",
"OpenAI Embeddings": "$0.02/1M tokens (~$5/mo)",
"OpenAI GPT-4o-mini": "$0.15/1M input tokens (~$15/mo)",
"Total (Small Team)": "~$90/month",
"Total (Enterprise)": "~$500-2000/month",
}
print(f"\n\nCost Estimate:")
for item, cost in cost_estimate.items():
print(f" {item}: {cost}")
เคล็ดลับ
- Chunk: แบ่ง Document เป็น Chunk 500-1000 tokens
- Namespace: แยก Namespace ตาม Category
- Metadata: เก็บ Metadata สำหรับ Filter
- Re-index: อัพเดท Index สม่ำเสมอเมื่อ Doc เปลี่ยน
- Hybrid: ใช้ Hybrid Search (Vector + Keyword) ดีกว่า
Vector Database คืออะไร
ฐานข้อมูล Vector Embedding Semantic Search AI LLM RAG Cosine Similarity Text Image Audio Pinecone Weaviate Qdrant Milvus Chroma
Pinecone คืออะไร
Managed Vector DB ไม่ต้องจัดการ Infrastructure Billion-scale Free Tier API Python JavaScript Serverless Metadata Namespace Hybrid Search
RAG คืออะไร
Retrieval Augmented Generation เสริม LLM ข้อมูลภายนอก Vector Database Prompt แม่นยำ อัพเดท ลด Hallucination Knowledge Base FAQ Document
ใช้ Pinecone กับ Remote Work อย่างไร
Knowledge Base เอกสาร Wiki Policy Semantic Search AI Chatbot FAQ Onboarding Meeting Notes Slack Bot API Chrome Extension
สรุป
Vector Database Pinecone Embedding Semantic Search RAG LLM Remote Work Knowledge Base AI Chatbot Slack Bot LangChain OpenAI Namespace Hybrid Search Production
