定义用于分块的函数
为便于可复现地批量执行 upsert,您需要定义一个函数,将向量列表拆分为多个小块。
内置模块 itertools 已为您导入。
本练习是课程的一部分
使用 Pinecone 构建 AI 应用
练习说明
- 将输入
iterable转换为迭代器。 - 使用
itertools模块按batch_size大小切片it。 - 产出当前这一块。
交互式实操练习
通过完成这段示例代码来试试这个练习。
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))