เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การ Upsert แบบแบตช์ควบคู่กัน

ในแบบฝึกหัดนี้ จะได้ฝึกนำเข้าเวกเตอร์ลงใน Pinecone index ชื่อ 'datacamp-index' แบบขนานกัน โดยต้องเชื่อมต่อกับ index จากนั้น upsert เวกเตอร์เป็นแบตช์แบบอะซิงโครนัส และตรวจสอบเมตริกที่อัปเดตของ index 'datacamp-index'

ฟังก์ชันช่วย chunks() ที่สร้างไว้ก่อนหน้านี้ยังคงใช้งานได้:

def chunks(iterable, batch_size=100):
    """A helper function to break an iterable into chunks of size batch_size."""
    it = iter(iterable)
    chunk = tuple(itertools.islice(it, batch_size))
    while chunk:
        yield chunk
        chunk = tuple(itertools.islice(it, batch_size))

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Vector Databases สำหรับ Embeddings ด้วย Pinecone

ดูคอร์ส

คำแนะนำการฝึกหัด

  • เริ่มต้น Pinecone client โดยอนุญาตให้ส่งคำขอพร้อมกันได้ 20 รายการ
  • Upsert เวกเตอร์ใน vectors เป็นแบตช์ละ 200 เวกเตอร์ต่อคำขอ แบบอะซิงโครนัส โดยกำหนดให้ส่งคำขอพร้อมกัน 20 รายการ
  • แสดงเมตริกที่อัปเดตของ Pinecone index 'datacamp-index'

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Initialize the client
pc = Pinecone(api_key="____", ____)

index = pc.Index('datacamp-index')

# Upsert vectors in batches of 200 vectors
with pc.Index('datacamp-index', ____) as index:
    async_results = [____(vectors=chunk, ____) for chunk in chunks(vectors, batch_size=____)]
    [async_result.get() for async_result in async_results]

# Retrieve statistics of the connected Pinecone index
print(____)
แก้ไขและรันโค้ด