并行批量 upsert
在本练习中,您将练习并行向 Pinecone 的 'datacamp-index' 索引写入向量。您需要连接到该索引,异步按批次 upsert 向量,并检查 'datacamp-index' 的更新后指标。
您之前创建的辅助函数 chunks() 仍可使用:
def chunks(iterable, batch_size=100):
"""A helper function to break an iterable into chunks of size batch_size."""
it = iter(iterable)
chunk = tuple(itertools.islice(it, batch_size))
while chunk:
yield chunk
chunk = tuple(itertools.islice(it, batch_size))
本练习是课程的一部分
使用 Pinecone 构建 AI 应用
练习说明
- 初始化 Pinecone 客户端,使其允许 20 个并发请求。
- 将
vectors按每次请求 200 个向量的批次进行 upsert,并以异步方式执行,配置20个并发请求。 - 打印 Pinecone 索引
'datacamp-index'的最新指标。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Initialize the client
pc = Pinecone(api_key="____", ____)
index = pc.Index('datacamp-index')
# Upsert vectors in batches of 200 vectors
with pc.Index('datacamp-index', ____) as index:
async_results = [____(vectors=chunk, ____) for chunk in chunks(vectors, batch_size=____)]
[async_result.get() for async_result in async_results]
# Retrieve statistics of the connected Pinecone index
print(____)