Managing agents
===============

エージェントは\ ``aidb.create_agent()`` で作成された\ ``aidb.agents``
の行です。
:ref:`Invoking agents <Invoking agents>` を呼び出すまでは何も実行されません。エージェントの作成は、構成を保存するだけです。

.. code:: sql

   SELECT aidb.create_agent(
       name                 => db_helper,
       instructions         => You are a helpful assistant that answers questions about this database. Be concise.,
       model                => my_gpt,
       tools                => ARRAY[run_sql_query],  -- optional; tool names from aidb.tools. Default: NULL (no tools)
       delegates            => NULL,                    -- optional; names of other agents to delegate to. Default: NULL (no delegation)
       role                 => NULL,                    -- optional; Postgres role tool calls run as. Default: NULL (run as the calling role)
       output_type          => NULL,                    -- optional; structured output schema from aidb.output_type(). Default: NULL (plain-text answer)
       input_token_budget   => NULL,                    -- optional; input token cap per agent_converse call. Default: NULL (no limit)
       output_token_budget  => NULL,                    -- optional; output token cap per agent_converse call. Default: NULL (no limit)
       max_iterations       => NULL,                    -- optional; reasoning-round cap per call. Default: NULL (a hard ceiling of 25 always applies)
       timeout              => NULL,                    -- optional; wall-clock seconds cap per call. Default: NULL (300 seconds applied at runtime)
       budget_strategy      => attempt_complete,      -- optional; behavior when a budget is exceeded. Default: attempt_complete
       preset               => NULL                     -- optional; reserved for future use, has no effect today. Default: NULL
   );

成功した場合は行を返しません。失敗した場合たとえば、名前が既に使用されている場合、\ ``error``
列セットを含む単一の行を返します。これは発生しないため、\ ``create_agent``
呼び出しのバッチは途中で中断できません。

.. code:: sql

   SELECT * FROM aidb.create_agent(db_helper, Be helpful., my_gpt);

   __OUTPUT__
                   error
   - -----------------------------------
    agent db_helper already exists
   (1 row)

モデルの選択
------------

``model``
は、テキストを生成し、指示に従うことができる登録済みの :ref:`モデルの選択 <モデルの選択>` を指定する必要があります。埋め込み、リランキング、OCR、およびマルチモーダル埋め込みプロバイダー
``bert_local`` 、\ ``openai_embeddings`` 、\ ``nim_reranking``
、\ ``clip_local`` などはエージェントをバックすることができず、
``t5_local``
は、指示フォローまたはシステムプロンプトのサポートがないため、完全に拒否されます。
。他のすべてのテキスト生成プロバイダーは動作し、ツール呼び出しの品質によって2つの層に分割されます。

**優先**
—これらは実際のプロバイダーネイティブのツール呼び出し要求を送信します。

- ``openai_responses`` + ``openai_responses_azure``

- ``anthropic_messages`` + ``anthropic_messages_azure`` /
  ``anthropic_messages_bedrock``

- ``llamacpp_generate``

**サポートされている** –これらは動作しますが、
AIDBは、プロンプトテキストで使用可能なツールを説明し、モデルのプレーンテキストの応答からツールコールバックを解析することにより、ツール呼び出しをシミュレートします。

- ``openai_completions``

- ``completions`` 他のOpenAI-Chat-Completions互換エンドポイント

- ``nim_completions``

- ``openrouter_chat``

- ``gemini``

- ``llama_instruct_local``

優先/サポートされている区別の詳細については、
:ref:`ツール呼び出しと構造化出力 <ツール呼び出しと構造化出力>` を参照してください。 ``create_agent``
を呼び出すときにモデルはまだ存在する必要はありません。\ ``agent_converse``
のみが解決可能である必要があります。

ツールとデリゲート
------------------

``tools`` は、エージェントが呼び出すことができる :ref:`Tools reference <Tools reference>` 
``aidb.tools`` からの名前の配列です。 ``delegates`` は、
:ref:`Delegation <Delegation>` ことができる他のエージェントの名前の配列です。どちらも\ ``create_agent``
/ ``update_agent`` 時間に\ ``aidb.tools`` / ``aidb.agents``
に対して検証されます。不明な名前は、\ ``agent_converse``
内で後で表面化するのではなく、すぐに拒否されます。ツールまたはデリゲートがないエージェントの場合は、いずれかを省略します。

.. code:: sql

   SELECT aidb.create_agent(
       name         => help_desk,
       instructions => Help users with general questions, delegating data questions to a specialist.,
       model        => my_gpt,
       tools        => ARRAY[run_sql_query],
       delegates    => ARRAY[sql_specialist]
   );

アクセス許可
------------

デフォルトでは、エージェントのツールは、Postgresロールが\ ``agent_converse``
を呼び出すときにrunsを呼び出します。代わりに ``role``
を渡してエージェントを特定のロールに固定します—たとえば、呼び出したアプリケーションユーザーが持っているよりも狭いテーブル権限を与えます。呼び出し者は、既にそのロール\ ``pg_has_role(..., 'MEMBER')``
のメンバーである必要があります。 ``create_agent`` /``update_agent``
属していないロールで構成されたエージェントを拒否します。

.. code:: sql

   SELECT aidb.create_agent(
       name         => readonly_reporter,
       instructions => Answer questions about sales data. Never modify data.,
       model        => my_gpt,
       tools        => ARRAY[run_sql_query],
       role         => reporting_role
   );

構造化された出力
----------------

デフォルトでは、\ ``agent_converse``
の回答はプレーンテキストです。代わりに構造化JSONを取得するには、\ ``aidb.output_type()``
および\ ``aidb.output_field()`` を使用してスキーマをビルドします。

.. code:: sql

   SELECT aidb.create_agent(
       name         => sentiment_tagger,
       instructions => Classify the sentiment of the given text.,
       model        => my_gpt,
       output_type  => aidb.output_type(
           aidb.output_field(sentiment, TEXT, One of: positive, negative, neutral),
           aidb.output_field(confidence, FLOAT)
       )
   );

``aidb.output_field()`` の\ ``description`` 引数はオプショナル。
``agent_converse`` の呼び出し時\ ``output_type``
引数は、その1つのコールのみにエージェントの構成済みスキーマをオーバーライドします。

予算、イテレーション、およびタイムアウト
----------------------------------------

``input_token_budget`` 、\ ``output_token_budget``
、\ ``max_iterations`` 、および\ ``timeout``
秒は、単一の\ ``agent_converse``
呼び出しのコストと実行時間を制限します。 ``budget_strategy``
は、予算を超過した場合の動作を制御します。すべてはオプショナル。これはエージェントの静的構成のプロパティではなく、会話の実行方法のプロパティであるため、完全な動作については
:ref:`予算と制限 <予算と制限>` を参照してください。

.. code:: sql

   SELECT aidb.create_agent(
       name                 => cost_capped_helper,
       instructions         => Answer questions about the orders table.,
       model                => my_gpt,
       input_token_budget   => 20000,   -- INTEGER; input tokens allowed per agent_converse call
       output_token_budget  => 2000,    -- INTEGER; output tokens allowed per agent_converse call
       max_iterations       => 10,      -- INTEGER; reasoning rounds allowed per call (a hard ceiling of 25 always applies)
       timeout              => 60,      -- INTEGER; wall-clock seconds allowed per call
       budget_strategy      => error  -- TEXT; one of ignore, error, summarize, attempt_complete
   );

エージェントの更新と削除
------------------------

``aidb.update_agent()`` は\ ``create_agent``
と同じフィールドを取ります。すべてオプショナル。渡したフィールドのみが変更します。その他はすべて現在の値を維持します。

.. code:: sql

   SELECT aidb.update_agent(db_helper, model => my_new_gpt, max_iterations => 15);

専用の「list agent」ファンクションはありません。\ ``aidb.agents``
は通常のテーブルであるため、\ ``SELECT * FROM aidb.agents``
はそれらをリストします。完全な列リストについては、
:ref:`Agents reference <Agents reference>` を参照してください。

``aidb.delete_agent()`` は、名前でエージェントを削除します。

.. code:: sql

   SELECT aidb.delete_agent(db_helper);

エージェントに会話履歴がある場合、これはデフォルトで失敗します内部タスクレコードは外部キーによって参照されます。
``force => true``
を渡して、エージェントとともにその内部履歴を削除します。会話
*トランスクリプト*
自分自身は、いずれの場合も変更されず、それを生成したエージェントが削除された後でも :ref:`会話の取得 <会話の取得>` で読み取り可能なままです。

.. code:: sql

   SELECT aidb.delete_agent(db_helper, force => true);

!!!note ``create_agent`` / ``update_agent`` は、 ``preset`` 引数たとえば
``'text_to_sql'``
なども受け入れます。これはエージェントに保存されますが、現在その動作は変更しません。将来の使用のために予約されています。
