LoslegenKostenlos loslegen

Defining a function for chunking

To be able to batch upserts in a reproducible way, you'll need to define a function to split your list of vectors into chunks.

The built-in itertools module has already been imported for you.

Diese Übung ist Teil des Kurses

Vector Databases for Embeddings with Pinecone

Kurs anzeigen

Anleitung zur Übung

  • Convert the iterable input into an iterator.
  • Slice it into chunks of size batch_size using the itertools module.
  • Yield the current chunk.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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))
Code bearbeiten und ausführen