複数のツールを持つエージェント

複数のツールを使用するエージェントの作成方法を学習します。

RAGツールと複数の機能ツールを備えたサポート・エージェント

この例では、ナレッジ・ベース(AgenticRagToolを使用)とカスタム関数ツールのコレクション(Toolkitを使用)を備えたエージェントを示します。

開始する前に、RAGツールのナレッジ・ベースを作成します。ナレッジ・ベースの作成を参照してください。データ・ソースに対して、オブジェクト・ストレージ・ファイルを作成します。次に、ナレッジ・ベースのOCIDをコピーし、ノートパッドに貼り付けます。

product_support_agent.py

from oci.addons.adk import Agent, AgentClient
from oci.addons.adk.tool.prebuilt import AgenticRagTool
from custom_function_tools import AccountToolkit

def main():

    # Assuming the resources were already provisioned
    agent_endpoint_id = "ocid1.genaiagentendpoint..."
    knowledge_base_id = "ocid1.genaiagentknowledgebase..."

    client = AgentClient(
        auth_type="api_key",
        profile="DEFAULT",
        region="us-chicago-1"
    )

    instructions = """
    You are customer support agent.
    Use RAG tool to answer product questions.
    Use function tools to fetch user and org info by id.
    Only orgs of Enterprise plan can use Responses API.
    """

    agent = Agent(
        client=client,
        agent_endpoint_id=agent_endpoint_id,
        instructions=instructions,
        tools=[
            AgenticRagTool(knowledge_base_ids=[knowledge_base_id]),
            AccountToolkit()
        ]
    )

    agent.setup()

    # This is a context your existing code is best at producing (e.g., fetching the authenticated user id)
    client_provided_context = "[Context: The logged in user ID is: user_123] "

    # Handle the first user turn of the conversation
    input = "What is the Responses API?"
    input = client_provided_context + " " + input
    response = agent.run(input)
    response.pretty_print()

    # Handle the second user turn of the conversation
    input = "Is my user account eligible for the Responses API?"
    input = client_provided_context + " " + input
    response = agent.run(input, session_id=response.session_id)
    response.pretty_print()


if __name__ == "__main__":
    main()

情報:この例は、session_idを使用してターン間のコンテキストを維持するマルチターン会話処理も示しています。マルチターン会話処理の例の詳細を参照してください。


エージェントはカスタムAccountToolkitを使用します。Toolkitクラスのoci.addons.adkから継承することで、独自のカスタム・ツールキット・クラスを作成できます。

Toolkitクラスは、関連ツールを単一のクラスに編成するのに役立ちます。

同じツールキットを異なるエージェントで再利用できます。Toolkitクラスのインスタンス内で一部の状態を維持することもできます。ADKはインスタンス・メソッドを呼び出して、@toolデコレータを使用してメソッドの状態を使用できるようにします。

custom_function_tools.py

from typing import Dict, Any
from oci.addons.adk import Toolkit, tool

class AccountToolkit(Toolkit):

    @tool
    def get_user_info(self, user_id: str) -> Dict[str, Any]:
        """Get information about a user by user_id

        Args:
            user_id (str): The user ID to get information about

        Returns:
            Dict[str, Any]: A dictionary containing the user information
        """
        # Here is a mock implementation
        return {
            "user_id": user_id,
            "account_id": "acc_111",
            "name": "John Doe",
            "email": "john.doe@example.com",
            "org_id": "org_222",
        }

    @tool
    def get_org_info(self, org_id: str) -> Dict[str, Any]:
        """Get information about an organization by org_id

        Args:
            org_id (str): The organization ID to get information about

        Returns:
            Dict[str, Any]: A dictionary containing the organization information
        """
        # Here is a mock implementation
        return {
            "org_id": org_id,
            "name": "Acme Inc",
            "admin_email": "admin@acme.com",
            "plan": "Enterprise",
        }