Agents#

AIDBを使用すると、データベース内でエージェントを構成および実行できます。彼らはデータへの安全なローカルアクセスを持っており、モデルが実行される場所によっては、データがPostgresを離れる必要はありません。

エージェント は、名前付けの再利用可能な構成指示、モデル、および呼び出すことができるツールセットです。AIDBは、ReActスタイルの推論ループを介して実行します。モデルは考え、オプションで1つ以上のツールを呼び出し、結果を観察し、答えがあるまで繰り返します。

!!!note これは、 Agent Factory in Hybrid Manager (HM) とは異なる明確なデータベース内機能です。 AIDBのエージェントは、Postgresデータベース内でSQLファンクション呼び出しとして完全に実行されます。

エージェントは通常のPostgresオブジェクトであり、 SQLで作成および照会されます。以下の例では、小さなtext-to- SQLエージェントをエンドツーエンドで構築します。データベーススキーマを事前に知らず、 catalog discovery tools で検出し、データに対して実際のクエリーを実行することにより質問に回答します。

例 text-to- SQLエージェント#

セットアップ#

サンプルデータを作成し、モデルを登録し、エージェントを作成します。

CREATE TABLE public.foods (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    category TEXT NOT NULL,
    calories INTEGER NOT NULL,
    price_usd NUMERIC(6,2) NOT NULL,
    is_vegetarian BOOLEAN NOT NULL DEFAULT false
);

INSERT INTO public.foods (name, category, calories, price_usd, is_vegetarian) VALUES
    (Apple,                 Fruit,     95, 0.50, true),
    (Banana,                Fruit,    105, 0.30, true),
    (Grilled Chicken Breast,Meat,     165, 3.50, false),
    (Salmon Fillet,         Fish,     208, 6.00, false),
    (Broccoli,              Vegetable, 55, 1.20, true),
    (Cheddar Cheese,        Dairy,    402, 2.80, true),
    (Brown Rice,            Grain,    216, 0.80, true),
    (Bacon,                 Meat,     541, 4.20, false),
    (Almonds,               Nuts,     579, 5.50, true),
    (Dark Chocolate,        Snack,    546, 3.00, true);

SELECT aidb.create_model(
    gpt_5_6_terra_azure,
    openai_responses,
    config => aidb.openai_responses_config(
        model => gpt-5.6-terra,
        url   => https://my-ai-service.example.com/openai/v1/responses
    ),
    credentials => jsonb_build_object(
        api_key, <your-api-key>
    )
);

SELECT aidb.create_agent(
    name         => text2sql_demo,
    instructions => You are a helpful, read-only database assistant that answers questions by
                     || querying the database. You do not know this databases schema in
                     || advance -- before answering, use the catalog_list_objects tool to
                     || discover which tables exist, then use catalog_get_object_details to
                     || inspect the columns of any table you intend to query. Never guess a
                     || table or column name -- always verify it via these catalog tools first.
                     || Once you know the relevant table and columns, use the run_sql_query
                     || tool to fetch the actual data needed to answer the question -- never
                     || state a specific result as fact unless it came from a successful
                     || run_sql_query call. You only have read-only access and cannot modify
                     || any data. Once a query has given you enough information to answer,
                     || respond immediately -- do not repeat a tool call you have already made
                     || with the exact same arguments.,
    model        => gpt_5_6_terra_azure,
    tools        => ARRAY[run_sql_query, catalog_list_objects, catalog_get_object_details]
);

会話#

データに関するエージェントに質問します。バックグラウンドでは、使用可能なテーブルをリストし、foods 列を検査し、クエリーを実行し、結果から回答します。

SELECT * FROM aidb.agent_converse(text2sql_demo, Which vegetarian food item has the fewest calories?);
__OUTPUT__
                                    message                                     |           conversation_id            | error
- -------------------------------------------------------------------------------+--------------------------------------+-------
 Broccoli has the fewest calories among vegetarian items, with  **55 calories** . | 08571a03-0758-4cda-8595-607e2a69c8ce |
(1 row)

推論ループの個々のツール呼び出しと結果を監視するには、 debug => true で再実行します。 Invoking agents を参照してください。

比較#

エージェントの推論ループとツールを使用せずに、同じモデルに直接尋ねられた同じ質問には、モデルのトレーニングデータからのみ回答できます。 foods テーブルの存在を知りません。

SELECT aidb.generate_text(gpt_5_6_terra_azure, Which vegetarian food item has the fewest calories?);
__OUTPUT__
                                                           generate_text
- ----------------------------------------------------------------------------------------------------------------------------------
 Among common solid vegetarian foods,  **raw watercress**  is one of the lowest-calorie options, at about  **11 calories per 100 g** .+
                                                                                                                                  +
[...]
(1 row)

エージェントは実際のデータに基づいて回答を根拠にしました。裸のモデル呼び出しは推測することしかできませんでした。

エージェントのtools 配列は、AIDBの統合ツールカタログつまりビルトインAIDBオペレーション、登録するカスタムパラメーター化SQLクエリー、または外部MCPサーバーからインポートされたオペレーションから取得します。完全なカタログについては、 ツールとデリゲート を参照してください。

ドキュメントマップ#

Page

What it covers

Managing agents

aidb.create_agent(), updating and deleting agents, roles, structured output

Invoking agents

aidb.agent_converse(), conversations and sessions, read-only mode, debug mode, budgets and limits

Delegation

Presenting delegates to the model, continuing delegate conversations, what propagates across a handoff, delegation depth

完全なパラメーターテーブルと戻り型については、 Agents reference を参照してください。