Class AlloyDBToolSpec (0.4.0)

AlloyDBToolSpec(
    engine: llama_index_alloydb_pg.engine.AlloyDBEngine,
    model_id: typing.Optional[str] = None,
)

Tools that run AlloyDB AI functions in the database.

AlloyDBToolSpec(engine).to_tool_list() returns one FunctionTool per function, each with a native async path, ready for an agent such as FunctionAgent. Requires AlloyDB for PostgreSQL 17 or later with the google_ml_integration extension 1.5.7 or later and the google_ml_integration.enable_ai_query_engine and google_ml_integration.enable_preview_ai_functions flags on. See https://cloud.google.com/alloydb/docs/ai/analyze-sentiment.

Parameters

Name Description
engine AlloyDBEngine

Connection pool to the AlloyDB database.

model_id Optional[str]

ID of the model the AI functions use. If None, the database uses the model named by its google_ml_integration.default_llm_model flag. Defaults to None.

Methods

aanalyze_sentiment

aanalyze_sentiment(text: str) -> str

Classify the sentiment of a text as positive, negative or neutral.

Parameter
Name Description
text str

The text to analyze, such as a review or a message.

Exceptions
Type Description
ValueError If text is blank or the model gives no answer.
Returns
Type Description
str "positive", "negative" or "neutral".

aevaluate_condition

aevaluate_condition(condition: str) -> bool

Decide whether a natural-language condition is true or false.

Parameter
Name Description
condition str

A yes or no question or a statement that includes the text it is about, for example "Does the review 'The battery died in a day' complain about the battery?".

Exceptions
Type Description
ValueError If condition is blank or the model gives no answer.
Returns
Type Description
bool True if the condition holds, otherwise False.

analyze_sentiment

analyze_sentiment(text: str) -> str

Classify the sentiment of a text as positive, negative or neutral.

Parameter
Name Description
text str

The text to analyze, such as a review or a message.

Exceptions
Type Description
ValueError If text is blank or the model gives no answer.
Returns
Type Description
str "positive", "negative" or "neutral".

asummarize

asummarize(text: str, *, instructions: typing.Optional[str] = None) -> str

Summarize a text, such as an article or a set of reviews.

Parameters
Name Description
text str

The text to summarize.

instructions Optional[str]

Extra instructions for the summary, for example "Use three bullet points". Defaults to None.

Exceptions
Type Description
ValueError If text is blank or the model gives no answer.
Returns
Type Description
str The summary, as the model returned it. It is often wrapped in double quotes.

evaluate_condition

evaluate_condition(condition: str) -> bool

Decide whether a natural-language condition is true or false.

Parameter
Name Description
condition str

A yes or no question or a statement that includes the text it is about, for example "Does the review 'The battery died in a day' complain about the battery?".

Exceptions
Type Description
ValueError If condition is blank or the model gives no answer.
Returns
Type Description
bool True if the condition holds, otherwise False.

summarize

summarize(text: str, *, instructions: typing.Optional[str] = None) -> str

Summarize a text, such as an article or a set of reviews.

Parameters
Name Description
text str

The text to summarize.

instructions Optional[str]

Extra instructions for the summary, for example "Use three bullet points". Defaults to None.

Exceptions
Type Description
ValueError If text is blank or the model gives no answer.
Returns
Type Description
str The summary, as the model returned it. It is often wrapped in double quotes.