🤔

Agent EngineにデプロイしたエージェントをSDK経由で呼び出すとAttributeErrorになってしまう

に公開

何が起きた?

Google Cloudが提供しているAgent EngineにAgent Development Kit(ADK)で開発したエージェントをSDK経由でデプロイしました。

remote_agent = agent_engines.create(
    display_name=AGENT_NAME,
    agent_engine=root_agent,
    requirements=[
        "google-cloud-aiplatform[adk,agent_engines]",
    ],
)

デプロイは問題なく完了したので、デプロイしたエージェントを呼び出そうとしました。

remote_agent = agent_engines.get(RESOURCE_NAME)

for event in remote_agent.stream_query(
    user_id="user-12345",
    session_id="session-12345",
    message="あなたの役割を教えてください。",
):
    print(event)

プログラムを実行してみると、以下のエラーとなってしまいました。

AttributeError: 'AgentEngine' object has no attribute 'stream_query'

原因

結論としては、google-cloud-aiplatformが1.110.0から1.111.0になるタイミングでAdkAppがGAになったことが起因しています。

https://github.com/googleapis/python-aiplatform/commit/a6600dd007fa4541604f05661a7f8a353f58856b#diff-a4fe849687cf4bd6c9888f37a24b57819a3784e67265c8da3f7d76ce318af2af

from vertexai.preview import reasoning_enginesのAdkAppからfrom vertexai import agent_enginesのAdkAppを使うように変更されています。

from vertexai import agent_enginesのAdkAppでは、register_operationsという利用可能なAPIを定義しているメソッドからstream_queryが削除されています。

from vertexai.preview import reasoning_enginesregister_operations
https://github.com/googleapis/python-aiplatform/blob/18a55590c5679b8ea7536c4c3c73566ba006bf36/vertexai/preview/reasoning_engines/templates/adk.py#L1185

from vertexai import agent_enginesregister_operations
https://github.com/googleapis/python-aiplatform/blob/main/vertexai/agent_engines/templates/adk.py#L892

解決策

google-cloud-aiplatformの1.111.0以降ではasync_stream_queryを使えば良さそうです。

remote_agent = agent_engines.get(RESOURCE_NAME)

async for event in remote_agent.async_stream_query(
    user_id="user-12345",
    session_id="session-12345",
    message="あなたの役割を教えてください。",
):
    print(event)

https://google.github.io/adk-docs/deploy/agent-engine/#step-4-test-your-agent-locally-optional

また、今後も同様の問題が発生しないように、"google-cloud-aiplatform[adk,agent_engines]==1.111.0"といった形でバージョンは固定しておいた方が良いですね。

まとめ

  • Agent EngineにデプロイしたエージェントをSDK経由で呼び出すとAttributeErrorになってしまう原因とその解決策についてまとめました。

Discussion