Class AlloyDBEmbedding (0.4.0)

AlloyDBEmbedding(engine: AlloyDBEngine, model_id: str, embed_batch_size: int = 10, num_workers: Optional[int] = 4, *, model_name: str = 'unknown', callback_manager: llama_index.core.callbacks.base.CallbackManager = <factory>, embeddings_cache: typing.Optional[typing.Any] = None, rate_limiter: typing.Optional[typing.Any] = None)

Embedding model that generates embeddings inside AlloyDB.

Texts are embedded with the AlloyDB AI embedding(model_id, content) function, so model_id must be a model registered with model endpoint management, for example text-embedding-005. Each batch of embed_batch_size texts is one SQL statement, and embedding() runs once per row, so a larger batch saves round trips but not model calls. An empty string raises ValueError. The batch methods check their whole input, because LlamaIndex passes the hooks one batch at a time, so a hook could not name the text's position.

The async batch methods embed num_workers batches at a time, each on its own pool connection. DEFAULT_NUM_WORKERS (4) fits the engine's default pool. More workers than the pool allows, or None, 0 or 1 (no limit in llama-index-core), can fail with sqlalchemy.exc.TimeoutError. The sync methods need an engine with a background event loop, such as one from AlloyDBEngine.from_instance or AlloyDBEngine.afrom_instance.

Methods

AlloyDBEmbedding

AlloyDBEmbedding(engine: AlloyDBEngine, model_id: str, embed_batch_size: int = 10, num_workers: Optional[int] = 4, *, model_name: str = 'unknown', callback_manager: llama_index.core.callbacks.base.CallbackManager = <factory>, embeddings_cache: typing.Optional[typing.Any] = None, rate_limiter: typing.Optional[typing.Any] = None)

AlloyDBEmbedding constructor.

Parameters
Name Description
engine AlloyDBEngine

AlloyDB engine with a connection pool to the database.

model_id str

The model endpoint id used to generate embeddings.

embed_batch_size int

The number of texts sent in one SQL statement. Defaults to DEFAULT_EMBED_BATCH_SIZE.

num_workers Optional[int]

The number of batches the async batch methods embed at a time. Defaults to DEFAULT_NUM_WORKERS.

\*\*kwargs Any

Passed to BaseEmbedding. model_name always follows model_id.

Exceptions
Type Description
ValueError If model_id is empty or blank.

__aembed

__aembed(texts: typing.List[str]) -> typing.List[typing.List[float]]

Embed a batch of texts with one SQL statement, in input order.

__aembed_one

__aembed_one(text: str) -> typing.List[float]

Embed one text, raising ValueError if it is empty.

__check_texts

__check_texts() -> None

Raise ValueError naming the position of the first empty text.

__sync_model_name

__sync_model_name() -> llama_index_alloydb_pg.embeddings.AlloyDBEmbedding

Keep model_name equal to model_id, on construction and assignment.

_aget_query_embedding

_aget_query_embedding(query: str) -> typing.List[float]

Embed a query asynchronously.

_aget_text_embedding

_aget_text_embedding(text: str) -> typing.List[float]

Embed a text asynchronously.

_aget_text_embeddings

_aget_text_embeddings(texts: typing.List[str]) -> typing.List[typing.List[float]]

Embed a batch of texts asynchronously.

_get_query_embedding

_get_query_embedding(query: str) -> typing.List[float]

Embed a query synchronously.

_get_text_embedding

_get_text_embedding(text: str) -> typing.List[float]

Embed a text synchronously.

_get_text_embeddings

_get_text_embeddings(texts: typing.List[str]) -> typing.List[typing.List[float]]

Embed a batch of texts synchronously.

aget_text_embedding_batch

aget_text_embedding_batch(
    texts: typing.List[str], *args: typing.Any, **kwargs: typing.Any
) -> typing.List[typing.List[float]]

Embed texts in batches, raising ValueError for an empty text.

class_name

class_name() -> str

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.

get_text_embedding_batch

get_text_embedding_batch(
    texts: typing.List[str], *args: typing.Any, **kwargs: typing.Any
) -> typing.List[typing.List[float]]

Embed texts in batches, raising ValueError for an empty text.

model_post_init

model_post_init(context: Any, /) -> None

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.