การจัดการเนมสเปซ
ทำความเข้าใจวิธีแบ่งข้อมูลภายในดัชนี Pinecone เดียวด้วยเนมสเปซ เพื่อการจัดระเบียบที่ดียิ่งขึ้น
การจัดการเนมสเปซ เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vector Databases: Pinecone, Weaviate & pgvector และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are Pinecone Namespaces?
Welcome to managing namespaces in Pinecone! A namespace is like a logical partition within a single Pinecone index.
Think of it as a folder inside a main directory. It allows you to segment your data without needing to create entirely separate indexes.
Why Use Namespaces?
Namespaces are incredibly useful for organizing your vector data, especially in complex applications:
- Multi-tenancy: Isolate data for different users or customers within one index.
- A/B Testing: Store different versions of embeddings for experiments.
- Data Isolation: Keep distinct datasets separate while leveraging the same index infrastructure.
- Categorization: Group vectors by topic, source, or type.
The Default Namespace
Every Pinecone index has a default namespace. If you don't explicitly specify a namespace when upserting or querying, your operations will interact with this default namespace.
It's often represented by an empty string "" or simply by omitting the namespace parameter.
Setting Up for Namespaces
Before we work with namespaces, ensure your Pinecone client is initialized. We'll assume you have your API key and environment configured, typically via environment variables.
Here's a basic setup for demonstration:
from pinecone import Pinecone
import os
def main():
# Replace with your actual API key and environment from environment variables
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
if api_key == "YOUR_API_KEY" or environment == "YOUR_ENVIRONMENT":
print("Please set PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars.")
return
pc = Pinecone(api_key=api_key, environment=environment)
print("Pinecone client initialized.")
if __name__ == "__main__":
main()Upserting Data into a Namespace
To add vectors to a specific namespace, you simply include the namespace parameter in your index.upsert() call.
This example upserts a few vectors into a namespace called "product-recommendations".
from pinecone import Pinecone, Index
import os
def main():
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
if api_key == "YOUR_API_KEY" or environment == "YOUR_ENVIRONMENT":
print("Please set PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars.")
return
pc = Pinecone(api_key=api_key, environment=environment)
index_name = "my-product-index" # Ensure this index exists
if index_name not in pc.list_indexes():
print(f"Index '{index_name}' does not exist. Please create it first.")
return
index = pc.Index(index_name)
vectors_to_upsert = [
{"id": "item-1", "values": [0.1, 0.2, 0.3]}, # 3 dimensions for example
{"id": "item-2", "values": [0.4, 0.5, 0.6]}
]
# Upsert into a specific namespace
index.upsert(vectors=vectors_to_upsert, namespace="product-recommendations")
print("Vectors upserted into 'product-recommendations' namespace.")
if __name__ == "__main__":
main()Querying a Specific Namespace
When you want to retrieve vectors from a particular namespace, you also specify the namespace parameter in your index.query() call.
This ensures your search is confined to that segment of your index.
from pinecone import Pinecone, Index
import os
def main():
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
if api_key == "YOUR_API_KEY" or environment == "YOUR_ENVIRONMENT":
print("Please set PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars.")
return
pc = Pinecone(api_key=api_key, environment=environment)
index_name = "my-product-index"
if index_name not in pc.list_indexes():
print(f"Index '{index_name}' does not exist. Please create it first.")
return
index = pc.Index(index_name)
query_vector = [0.15, 0.25, 0.35] # Example query vector
# Query within the 'product-recommendations' namespace
results = index.query(
vector=query_vector,
top_k=2,
namespace="product-recommendations",
include_values=False
)
print("Query results from 'product-recommendations' namespace:")
for match in results.matches:
print(f"ID: {match.id}, Score: {match.score}")
if __name__ == "__main__":
main()Understanding Query Scope
It's crucial to remember that queries in Pinecone are namespace-specific by default. If you query without specifying a namespace, it will only search the default namespace.
There's no direct way to query across all namespaces with a single API call; you'd need to query each namespace individually if you wanted to combine results.
Listing Namespaces in an Index
To see which namespaces currently exist in your index and their statistics, you can use the index.describe_index_stats() method.
This returns a summary including a map of namespaces and their vector counts.
from pinecone import Pinecone, Index
import os
def main():
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
if api_key == "YOUR_API_KEY" or environment == "YOUR_ENVIRONMENT":
print("Please set PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars.")
return
pc = Pinecone(api_key=api_key, environment=environment)
index_name = "my-product-index"
if index_name not in pc.list_indexes():
print(f"Index '{index_name}' does not exist. Please create it first.")
return
index = pc.Index(index_name)
# Get index statistics
index_stats = index.describe_index_stats()
print("Namespaces in index:")
for ns, stats in index_stats.namespaces.items():
print(f"- Namespace: '{ns}', Vector Count: {stats.vector_count}")
if __name__ == "__main__":
main()Deleting Vectors from a Namespace
You can delete specific vectors from a namespace by providing their IDs along with the namespace parameter to index.delete().
This example deletes a specific item from the "product-recommendations" namespace.
from pinecone import Pinecone, Index
import os
def main():
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
if api_key == "YOUR_API_KEY" or environment == "YOUR_ENVIRONMENT":
print("Please set PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars.")
return
pc = Pinecone(api_key=api_key, environment=environment)
index_name = "my-product-index"
if index_name not in pc.list_indexes():
print(f"Index '{index_name}' does not exist. Please create it first.")
return
index = pc.Index(index_name)
# Delete a specific vector by ID within a namespace
index.delete(ids=["item-1"], namespace="product-recommendations")
print("Vector 'item-1' deleted from 'product-recommendations' namespace.")
if __name__ == "__main__":
main()Deleting an Entire Namespace
To remove all vectors within a specific namespace, effectively deleting the namespace itself, you can use index.delete() with the namespace parameter and set delete_all=True.
Be careful: This action is irreversible and deletes all data in that namespace!
from pinecone import Pinecone, Index
import os
def main():
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
if api_key == "YOUR_API_KEY" or environment == "YOUR_ENVIRONMENT":
print("Please set PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars.")
return
pc = Pinecone(api_key=api_key, environment=environment)
index_name = "my-product-index"
if index_name not in pc.list_indexes():
print(f"Index '{index_name}' does not exist. Please create it first.")
return
index = pc.Index(index_name)
# Delete all vectors within a specific namespace
# This effectively removes the 'test-namespace' and all its contents
index.delete(namespace="test-namespace", delete_all=True)
print("All vectors in 'test-namespace' deleted. Namespace effectively removed.")
if __name__ == "__main__":
main()Namespace Knowledge Check
You have an index named "my-app-index". You upsert vectors into "user-1" and "user-2" namespaces. If you then call index.query(vector=my_vec, top_k=5) without specifying a namespace, what will happen?
Recap: Organize with Namespaces
Great job! You've learned about the power of Pinecone namespaces.
- Namespaces help segment data within a single index.
- They're perfect for multi-tenancy and A/B testing.
- Operations (upsert, query, delete) are namespace-specific.
- The default namespace is used if none is specified.
- You can list existing namespaces and delete entire namespaces.
Using namespaces effectively keeps your vector data organized and manageable!
คำถามที่พบบ่อย
บทเรียน “การจัดการเนมสเปซ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการเนมสเปซ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการเนมสเปซ”
ทำความเข้าใจวิธีแบ่งข้อมูลภายในดัชนี Pinecone เดียวด้วยเนมสเปซ เพื่อการจัดระเบียบที่ดียิ่งขึ้น คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการเนมสเปซ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม
ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกรองด้วยเมทาดาทา
- การจัดการเนมสเปซ
- การอัปเดตและการลบแบบเรียลไทม์
- การค้นหาแบบผสมด้วยเวกเตอร์เบาบางและหนาแน่น