批次 upsert

使用 Pinecone 構建 AI 應用

James Chapman

Curriculum Manager, DataCamp

Upsert 限制

 

  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 應用

序列式批次

  • 拆分請求並依序一個接一個傳送
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...