平行批次 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(____)