Gemini Embedder

The GeminiEmbedder class is used to embed text data into vectors using the Gemini API. You can get one from here.

Usage

1from kern.knowledge.knowledge import Knowledge
2from kern.vectordb.pgvector import PgVector
3from kern.knowledge.embedder.google import GeminiEmbedder
4
5# Embed sentence in database
6embeddings = GeminiEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")
7
8# Print the embeddings and their dimensions
9print(f"Embeddings: {embeddings[:5]}")
10print(f"Dimensions: {len(embeddings)}")
11
12# Use an embedder in a knowledge base
13knowledge = Knowledge(
14 vector_db=PgVector(
15 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
16 table_name="gemini_embeddings",
17 embedder=GeminiEmbedder(),
18 ),
19 max_results=2,
20)

Params

ParameterTypeDefaultDescription
dimensionsint768The dimensionality of the generated embeddings
modelstrmodels/text-embedding-004The name of the Gemini model to use
task_typestr-The type of task for which embeddings are being generated
titlestr-Optional title for the embedding task
api_keystr-The API key used for authenticating requests.
request_paramsOptional[Dict[str, Any]]-Optional dictionary of parameters for the embedding request
client_paramsOptional[Dict[str, Any]]-Optional dictionary of parameters for the Gemini client
gemini_clientOptional[Client]-Optional pre-configured Gemini client instance
enable_batchboolFalseEnable batch processing to reduce API calls and avoid rate limits
batch_sizeint100Number of texts to process in each API call for batch operations.

Developer Resources