An embedding wrapper is an adapter that gives an application a stable interface around an embedding provider. It isolates Google API dependencies, supports provider changes, and centralizes retrieval-specific behavior. section 12.2.4
A production-oriented wrapper handles the embedding workflow by:
- Initializing the provider client through the SDK. section 12.2.4
- Embedding document collections through a unified method with progress feedback and per-document processing. section 12.2.4
- Distinguishing document embeddings used for indexing from query embeddings used for search. section 12.2.4
Listing 12.3 Embedding API wrapper for document indexing and queries
class GeminiEmbeddings(Embeddings):
def __init__(self):
self.client = genai.Client() #1
def embed_documents(self,
texts: List[str])
-> List[List[float]]: #2
embeddings = []
for text in tqdm(texts, desc="Embedding documents"):
result = self.client.models.embed_content(
model="text-embedding-004", #3
contents=text,
config=types.EmbedContentConfig(
task_type="RETRIEVAL_DOCUMENT" #4
)
)
embeddings.append(
result.embeddings[0].values) #5
return embeddings
def embed_query(self, text: str) -> List[float]:
result = self.client.models.embed_content(
model="text-embedding-004",
contents=text,
config=types.EmbedContentConfig(
task_type="RETRIEVAL_QUERY" #6
)
)
return result.embeddings[0].values
Inspect the GeminiEmbeddings implementation, including the provider client, embedding model, document retrieval task, and returned vector values. section 12.2.4
Embedding documents for a FAISS index
A retrieval pipeline loads Markdown files, splits them into overlapping chunks, creates a FAISS index with GeminiEmbeddings, persists it, and tests similarity search with a user query.
