アップサートのバッチ化

Pineconeを使ったAIアプリケーション開発

James Chapman

Curriculum Manager, DataCamp

アップサートの制約

 

  1. リクエストのレート
  2. リクエストのサイズ

 

  • バッチ化: リクエストを小さなチャンクに分割

 

Pinecone のレート制限

1 https://docs.pinecone.io/reference/quotas-and-limits#rate-limits
Pineconeを使ったAIアプリケーション開発

チャンク化関数の定義

def chunks(iterable, batch_size=100):

it = iter(iterable)
chunk = tuple(itertools.islice(it, batch_size))
while chunk:
yield chunk
chunk = tuple(itertools.islice(it, batch_size))
Pineconeを使ったAIアプリケーション開発

逐次バッチ

  • リクエストを分割し、順番に1つずつ送信
pc.Pinecone(api_key="YOUR API KEY")
index = pc.Index('datacamp-index')


for chunk in chunks(vectors): index.upsert(vectors=chunk)

長所:

  • レート/サイズ制限を回避

短所:

  • とても遅い
Pineconeを使ったAIアプリケーション開発

並列バッチ

  • リクエストを分割し、並列に送信
pc = Pinecone(api_key="YOUR_API_KEY", pool_threads=30)


with pc.Index('datacamp-index', pool_threads=30) as index:
async_results = [index.upsert(vectors=chunk, async_req=True) for chunk in chunks(vectors, batch_size=100)]
[async_result.get() for async_result in async_results]
Pineconeを使ったAIアプリケーション開発

練習しましょう!

Pineconeを使ったAIアプリケーション開発

Preparing Video For Download...