Definición de una función para fragmentar
Para poder realizar upserts por lotes de forma reproducible, necesitarás definir una función que divida tu lista de vectores en fragmentos.
El módulo integrado « itertools » ya se ha importado por ti.
Este ejercicio forma parte del curso
Bases de datos vectoriales para incrustaciones con Pinecone
Instrucciones del ejercicio
- Convierte la entrada «
iterable» en un iterador. - Corta
iten trozos del tamañobatch_sizeutilizando el móduloitertools. - Devuelve el fragmento actual.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
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))