Module tools (0.17.0)

API documentation for tools module.

Classes

AlloyDBIfInput

AlloyDBIfInput(*, condition: str)

Input for AlloyDBIfTool.

AlloyDBIfTool

AlloyDBIfTool(
    *,
    name: str = "alloydb_if_tool",
    description: str = "A tool that uses AlloyDB AI to evaluate a semantic condition and returns True or False. Useful for semantic routing, classification, or filtering.",
    args_schema: typing.Type[
        pydantic.main.BaseModel
    ] = langchain_google_alloydb_pg.tools.AlloyDBIfInput,
    return_direct: bool = False,
    verbose: bool = False,
    callbacks: (
        list[langchain_core.callbacks.base.BaseCallbackHandler]
        | langchain_core.callbacks.base.BaseCallbackManager
        | None
    ) = None,
    tags: list[str] | None = None,
    metadata: dict[str, typing.Any] | None = None,
    handle_tool_error: (
        bool
        | str
        | collections.abc.Callable[
            [langchain_core.tools.base.ToolException],
            str | collections.abc.Sequence[str | dict[str, typing.Any]],
        ]
        | None
    ) = False,
    handle_validation_error: (
        bool
        | str
        | collections.abc.Callable[
            [
                pydantic_core._pydantic_core.ValidationError
                | pydantic.v1.error_wrappers.ValidationError
            ],
            str,
        ]
        | None
    ) = False,
    response_format: typing.Literal["content", "content_and_artifact"] = "content",
    extras: dict[str, typing.Any] | None = None,
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    model_id: typing.Optional[str] = None
)

Evaluates a natural-language condition with google_ml.if.

Returns True or False. Requires AlloyDB with google_ml_integration. Like any LangChain tool, call it with invoke / ainvoke or give it to an agent:

tool = AlloyDBIfTool(engine=engine)
tool.invoke({"condition": "Is 2 + 2 equal to 4?"})  # True

model_id works as in AlloyDBSentimentTool. If the model's reply is neither true nor false, google_ml.if returns NULL and the tool raises AlloyDBToolError rather than returning a value.

AlloyDBSentimentTool

AlloyDBSentimentTool(
    *,
    name: str = "alloydb_sentiment_tool",
    description: str = "Analyze the sentiment of a given text. Useful for determining if text is positive, negative, or neutral.",
    args_schema: typing.Type[
        pydantic.main.BaseModel
    ] = langchain_google_alloydb_pg.tools.SentimentInput,
    return_direct: bool = False,
    verbose: bool = False,
    callbacks: (
        list[langchain_core.callbacks.base.BaseCallbackHandler]
        | langchain_core.callbacks.base.BaseCallbackManager
        | None
    ) = None,
    tags: list[str] | None = None,
    metadata: dict[str, typing.Any] | None = None,
    handle_tool_error: (
        bool
        | str
        | collections.abc.Callable[
            [langchain_core.tools.base.ToolException],
            str | collections.abc.Sequence[str | dict[str, typing.Any]],
        ]
        | None
    ) = False,
    handle_validation_error: (
        bool
        | str
        | collections.abc.Callable[
            [
                pydantic_core._pydantic_core.ValidationError
                | pydantic.v1.error_wrappers.ValidationError
            ],
            str,
        ]
        | None
    ) = False,
    response_format: typing.Literal["content", "content_and_artifact"] = "content",
    extras: dict[str, typing.Any] | None = None,
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    model_id: typing.Optional[str] = None
)

Analyzes the sentiment of text with google_ml.analyze_sentiment.

Requires AlloyDB running PostgreSQL 17 or higher with google_ml_integration. Like any LangChain tool, call it with invoke / ainvoke or give it to an agent:

tool = AlloyDBSentimentTool(engine=engine)
tool.invoke({"content": "I love this product!"})  # "positive"

model_id is set on the tool, not by the agent. If it is None, the database uses the model named by its google_ml_integration.default_llm_model flag. If the function returns NULL, the tool raises AlloyDBToolError.

AlloyDBSummaryTool

AlloyDBSummaryTool(
    *,
    name: str = "alloydb_summary_tool",
    description: str = "Summarize the given text. Useful for condensing long articles or descriptions into shorter summaries.",
    args_schema: typing.Type[
        pydantic.main.BaseModel
    ] = langchain_google_alloydb_pg.tools.SummaryInput,
    return_direct: bool = False,
    verbose: bool = False,
    callbacks: (
        list[langchain_core.callbacks.base.BaseCallbackHandler]
        | langchain_core.callbacks.base.BaseCallbackManager
        | None
    ) = None,
    tags: list[str] | None = None,
    metadata: dict[str, typing.Any] | None = None,
    handle_tool_error: (
        bool
        | str
        | collections.abc.Callable[
            [langchain_core.tools.base.ToolException],
            str | collections.abc.Sequence[str | dict[str, typing.Any]],
        ]
        | None
    ) = False,
    handle_validation_error: (
        bool
        | str
        | collections.abc.Callable[
            [
                pydantic_core._pydantic_core.ValidationError
                | pydantic.v1.error_wrappers.ValidationError
            ],
            str,
        ]
        | None
    ) = False,
    response_format: typing.Literal["content", "content_and_artifact"] = "content",
    extras: dict[str, typing.Any] | None = None,
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    model_id: typing.Optional[str] = None,
    additional_instructions: typing.Optional[str] = None
)

Summarizes text with google_ml.summarize.

Requires AlloyDB running PostgreSQL 17 or higher with google_ml_integration. Like any LangChain tool, call it with invoke / ainvoke or give it to an agent:

tool = AlloyDBSummaryTool(engine=engine)
tool.invoke(
    {"content": article, "additional_instructions": "In 3 bullet points"}
)

model_id works as in AlloyDBSentimentTool: it is set on the tool, and None uses the database's default model. additional_instructions can be set on the tool or passed per call; a per-call value replaces the tool's.

AlloyDBToolError

Raised when an AlloyDB AI function returns NULL.

That happens, for example, when the model's reply isn't one of the expected answers. invoke / ainvoke raise this error like any other. Because it is a ToolException, a tool created with handle_tool_error=True returns its message to the agent instead. Database errors (DBAPIError), such as an unknown model, always raise.

SentimentInput

SentimentInput(*, content: str)

Input for AlloyDBSentimentTool.

SummaryInput

SummaryInput(*, content: str, additional_instructions: typing.Optional[str] = None)

Input for AlloyDBSummaryTool.

Modules Functions

_afetch_scalar

_afetch_scalar(
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    query: str,
    params: dict[str, typing.Any],
    null_message: str,
) -> typing.Any

Run query and return its single value, raising if it is NULL.

_check_engine_loop

_check_engine_loop(
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
) -> None

Raise if a synchronous call would block the engine's own event loop.

_check_model_id

_check_model_id(value: typing.Optional[str]) -> typing.Optional[str]

Reject a blank model ID. None selects the database's default model.

_check_text

_check_text(value: str, name: str) -> str

Reject empty or whitespace-only text. The value is used as given.