청크로 나누는 함수 정의하기
업서트 작업을 일괄 처리(batch)하고 재현 가능하도록 하려면, 벡터 리스트를 청크로 분할하는 함수를 정의해야 해요.
기본 내장 모듈인 itertools는 이미 임포트되어 있어요.
이 연습은 강의의 일부입니다
Pinecone로 AI 애플리케이션 구축하기
연습 안내
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))