Embed All Chunks
Loop over every chunk in batches to stay within the free tier rate limit
💻
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
Embedding a list of chunks
The Gemini free tier allows 100 embedding requests per minute. A large PDF can produce hundreds of chunks, so you must pace your requests.
The solution is to process chunks in batches of 90, then pause for 60 seconds between batches.
import time
BATCH_SIZE = 90
embeddings = []
for i in range(0, len(chunks), BATCH_SIZE):
batch = chunks[i : i + BATCH_SIZE]
for chunk in batch:
embeddings.append(embed_text(client, chunk))
if i + BATCH_SIZE < len(chunks):
print("Rate limit pause — waiting 60 seconds...")
time.sleep(60)
return embeddingsThe index of each vector matches the index of its chunk — embeddings[3] is the vector for chunks[3].
Instructions
Complete the embed_all_chunks function. The starter code provides the signature.
- Create a variable named
BATCH_SIZE. Assign it90. - Create an empty list named
embeddings. - Create a
forloop with variableioverrange(0, len(chunks), BATCH_SIZE). - Inside the loop, create a variable named
batch. Assign itchunks[i : i + BATCH_SIZE]. - Create an inner
forloop with variablechunkoverbatch. Inside it, appendembed_text(client, chunk)toembeddings. - After the inner loop, add an
ifstatement:if i + BATCH_SIZE < len(chunks):. Inside it, print"Rate limit pause — waiting 60 seconds..."and calltime.sleep(60). - After the outer loop, return
embeddings.
import os
import time
import pypdf
from dotenv import load_dotenv
from google import genai
from google.genai import types
def extract_text(pdf_path):
reader = pypdf.PdfReader(pdf_path)
pages = [page.extract_text() for page in reader.pages]
return "\n".join(pages)
def chunk_text(text, chunk_size=500, overlap=100):
chunks = []
for i in range(0, len(text), chunk_size - overlap):
chunks.append(text[i : i + chunk_size])
return chunks
def preview_chunks(chunks):
print(f"Total chunks: {len(chunks)}")
print(f"First chunk:\n{chunks[0]}")
def create_client():
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
client = genai.Client(api_key=api_key)
return client
def embed_text(client, text):
result = client.models.embed_content(model="gemini-embedding-001", contents=text, config=types.EmbedContentConfig(task_type="RETRIEVAL_DOCUMENT"))
return result.embeddings[0].values
def embed_all_chunks(client, chunks):
# Step 1: Set BATCH_SIZE to 90
# Step 2: Create empty embeddings list
# Step 3: Loop over chunks in batches
# Step 4: Slice the current batch
# Step 5: Inner loop — embed each chunk
# Step 6: If more batches remain, pause for rate limit
# Step 7: Return embeddings
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding