การเพิ่มหรืออัปเดตข้อมูลใน Pinecone
เชี่ยวชาญกระบวนการแทรกและอัปเดตข้อมูลเวกเตอร์พร้อมเมทาดาทาที่เกี่ยวข้องลงในดัชนี Pinecone
การเพิ่มหรืออัปเดตข้อมูลใน 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 is Upserting Data?
In vector databases like Pinecone, upserting is a key operation. It's a blend of 'update' and 'insert'.
- If a vector with a given ID doesn't exist, it's inserted.
- If it already exists, the existing vector (and its metadata) is updated with the new data.
This single operation simplifies managing dynamic vector data without needing to check for existence first.
Connecting to Your Pinecone Index
Before we upsert, you need to connect to your Pinecone index. This involves initializing the Pinecone client and selecting your target index.
Remember to replace YOUR_API_KEY and YOUR_ENVIRONMENT with your actual credentials, typically loaded from environment variables.
import os
from pinecone import Pinecone
# Initialize Pinecone client
pc = Pinecone(
api_key=os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY"),
environment=os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
)
# Connect to your index (e.g., 'my-index')
index_name = "my-index"
index = pc.Index(index_name)
print(f"Connected to index: {index_name}")Upserting a Single Vector
To upsert a single vector, you provide a unique id and the vector data (a list of floats). Each vector needs an ID for Pinecone to identify it.
The dimension of the vector must match the dimension configured for your Pinecone index (e.g., 3 for our simple examples).
Demo: Single Vector Upsert
Here's how to insert a single vector into your index. We'll use a simple 3-dimensional vector for demonstration.
import os
from pinecone import Pinecone
pc = Pinecone(
api_key=os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY"),
environment=os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
)
index = pc.Index("my-index") # Assuming 'my-index' exists
# Upsert a single vector
index.upsert(
vectors=[
{"id": "vec1", "values": [0.1, 0.2, 0.3]}
]
)
print("Single vector 'vec1' upserted!")Efficient Batch Upserts
For better performance, it's highly recommended to upsert multiple vectors in batches rather than one by one. This reduces network overhead.
You can prepare a list of dictionaries, where each dictionary represents a vector with its id and values.
Demo: Batch Upserting Vectors
This example shows how to upsert several vectors at once. Notice the vectors parameter takes a list of vector objects.
import os
from pinecone import Pinecone
pc = Pinecone(
api_key=os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY"),
environment=os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
)
index = pc.Index("my-index") # Assuming 'my-index' exists
# Prepare multiple vectors for batch upsert
batch_vectors = [
{"id": "vec2", "values": [0.4, 0.5, 0.6]},
{"id": "vec3", "values": [0.7, 0.8, 0.9]},
{"id": "vec4", "values": [0.11, 0.12, 0.13]}
]
# Upsert the batch
index.upsert(vectors=batch_vectors)
print("Batch of vectors upserted!")Adding Metadata to Vectors
Metadata allows you to store additional key-value pairs alongside your vectors. This is incredibly useful for filtering search results later.
Metadata can include properties like a document's title, author, category, or creation date. It's stored as a dictionary within each vector object.
Demo: Upsert with Metadata
Let's add some context to our vectors using metadata. This makes them much more powerful for real-world applications.
import os
from pinecone import Pinecone
pc = Pinecone(
api_key=os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY"),
environment=os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
)
index = pc.Index("my-index") # Assuming 'my-index' exists
# Upsert a vector with metadata
index.upsert(
vectors=[
{
"id": "doc1",
"values": [0.2, 0.3, 0.4],
"metadata": {"genre": "sci-fi", "year": 2023}
},
{
"id": "doc2",
"values": [0.5, 0.6, 0.7],
"metadata": {"genre": "fantasy", "author": "J. Doe"}
}
]
)
print("Vectors 'doc1' and 'doc2' upserted with metadata!")How Upsert Handles Updates
The 'update' part of 'upsert' means that if you try to upsert a vector with an id that already exists in your index, Pinecone won't create a new entry.
Instead, it will overwrite the existing vector's values and metadata with the new data you provide. This ensures data consistency and avoids duplicates.
Upserting Knowledge Check
You've learned about upserting. Let's check your understanding!
Recap: Mastering Upserts
Great job! You've learned the essentials of upserting data into Pinecone:
- Upsert = Update + Insert: A single operation for adding new or modifying existing vectors.
- IDs are Key: Each vector requires a unique ID for identification and updates.
- Batching for Performance: Always upsert multiple vectors in batches for efficiency.
- Metadata for Context: Add key-value pairs to vectors for powerful filtering and search.
- Updates are Automatic: Upserting with an existing ID overwrites the old vector.
Next, we'll explore how to query these vectors to find similar items!
คำถามที่พบบ่อย
บทเรียน “การเพิ่มหรืออัปเดตข้อมูลใน Pinecone” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเพิ่มหรืออัปเดตข้อมูลใน Pinecone” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่มหรืออัปเดตข้อมูลใน Pinecone”
เชี่ยวชาญกระบวนการแทรกและอัปเดตข้อมูลเวกเตอร์พร้อมเมทาดาทาที่เกี่ยวข้องลงในดัชนี Pinecone คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเพิ่มหรืออัปเดตข้อมูลใน Pinecone” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม
ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างดัชนี Pinecone
- การเพิ่มหรืออัปเดตข้อมูลใน Pinecone
- การค้นข้อมูลเวกเตอร์ใน Pinecone
- ทำความเข้าใจราคาและพ็อดของ Pinecone