กำหนดฟังก์ชันสำหรับการแบ่งข้อมูลเป็นชุดย่อย
เพื่อให้การ upsert แบบ batch ทำได้อย่างสม่ำเสมอ จำเป็นต้องกำหนดฟังก์ชันสำหรับแบ่งรายการเวกเตอร์ออกเป็นชุดย่อย
โมดูล itertools ในตัวได้ถูก import ไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Vector Databases สำหรับ Embeddings ด้วย Pinecone
คำแนะนำการฝึกหัด
- แปลงอินพุต
iterableให้เป็น iterator - ใช้โมดูล
itertoolsเพื่อแบ่งitออกเป็นชุดย่อยที่มีขนาดbatch_size - Yield ชุดข้อมูลปัจจุบันออกมา
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
def chunks(iterable, batch_size=100):
"""A helper function to break an iterable into chunks of size batch_size."""
# Convert the iterable into an iterator
it = ____
# Slice the iterator into chunks of size batch_size
chunk = tuple(itertools.____(it, ____))
while chunk:
# Yield the chunk
____
chunk = tuple(itertools.islice(it, batch_size))