0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Vertex AI Agent Engineの上のセッションをAPI経由で操作する

0
Posted at

はじめに

Vertex AI Agent Engine はAIエージェントをデプロイ・実行できるフルマネージドなインフラサービスです。
Agent EngineにデプロイされたエージェントはHTTP API経由で呼び出すことができ、会話のコンテキスト(セッション)もプラットフォーム側で自動的に管理されます。

セッションの一覧はUI上から確認できるのですが、会話を再開したり、不要になったセッションを削除したり、プログラム上からも操作できるようにしたいです。
この記事ではVertex AI Agent Engine上のセッションを操作する方法をまとめます。

Vertex AI Agent Engineのセッション関連の操作

セッション関連の操作は以下のドキュメントに一通りありました。
今回はRESTAPIでの操作を試します。

セッション一覧取得

まずは一覧取得です。
リクエストで取得対象のAgent EngineのIDを指定することで取得できます。

セッション一覧取得 リクエスト
curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://asia-northeast1-aiplatform.googleapis.com/v1/projects/hogeProject/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions"

レスポンスです。
nameの最後にセッションIDがついています。こちらを用いて会話履歴の取得やセッションの削除ができます。

セッション一覧取得 レスポンス
{
  "sessions": [
    {
      "name": "projects/111111111111/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000001",
      "createTime": "2026-03-14T05:28:14.439337Z",
      "updateTime": "2026-03-14T05:28:14.548871Z",
      "userId": "test_user_001",
      "expireTime": "2027-03-14T05:28:14.410447Z"
    },
    {
      "name": "projects/111111111111/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000002",
      "createTime": "2026-01-26T15:22:05.484284Z",
      "updateTime": "2026-01-26T15:22:11.706769Z",
      "userId": "test_user_002"
    }
  ]
}

セッションのイベント取得

続いてセッションイベントの取得です。
セッションイベントにはリクエストやレスポンスの情報などが詰められており、ここから会話履歴を取得することが可能です。

セッションのイベント取得 リクエスト
curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://asia-northeast1-aiplatform.googleapis.com/v1/projects/hogeProject/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000001/events"

レスポンスはこちら。

セッションのイベント取得 レスポンス
{
  "sessionEvents": [
    {
      "name": "projects/111111111111/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000001/events/2000000000000000001",
      "author": "user",
      "content": {
        "role": "user",
        "parts": [
          {
            "text": "FlowのコマンドControllerの作り方"
          }
        ]
      },
      "invocationId": "e-11111111-1111-1111-1111-111111111111",
      "actions": {},
      "timestamp": "2026-01-26T15:11:13.174294Z",
      "eventMetadata": {}
    },
    {
      "name": "projects/111111111111/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000001/events/2000000000000000002",
      "author": "flow_rag_agent",
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "FlowのコマンドControllerは、通常コマンドラインインターフェースを介して呼び出されます。"
          }
        ]
      },
      "invocationId": "e-11111111-1111-1111-1111-11111111111",
      "actions": {},
      "timestamp": "2026-01-26T15:11:13.330595Z",
      "eventMetadata": {
        "groundingMetadata": {
        }
      }
    }
  ]
}

セッションの削除

セッションの削除 リクエスト
curl -X DELETE \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://asia-northeast1-aiplatform.googleapis.com/v1/projects/hogeProject/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000001"

割愛していますが、レスポンスはこんな感じです。

{
  "done": true
}

セッションへのイベント追加

セッションのイベントに履歴を追加することができます。
ユーザとAIエージェントの会話に割り込むイメージでしょうか。

セッションへのイベント追加 リクエスト
curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json; charset=utf-8" \
     "https://asia-northeast1-aiplatform.googleapis.com/v1/projects/hogeProject/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000001:appendEvent" -d '{
  "author": "Hi",
  "timestamp": "2026-03-14T08:19:18.782406Z",
  "invocationId": "test",
  "content": {
    "role": "aaa",
    "parts": [{"text": "hello"}]
  }
}' | jq

レスポンスは簡素。

セッションへのイベント追加 レスポンス
{}

イベント一覧取得で確認すると、一覧の最後に追加されていました。

セッションへのイベント追加 確認
{
  "sessionEvents": [
      {
      "name": "projects/111111111111/locations/asia-northeast1/reasoningEngines/000000000000000000/sessions/100000000000000001/events/2000000000000000001"
      "author": "Hi",
      "content": {
        "role": "aaa",
        "parts": [
          {
            "text": "hello"
          }
        ]
      },
      "invocationId": "test",
      "actions": {},
      "timestamp": "2026-03-14T08:19:18.782406Z"
    }
  ]
}

おわりに

今回はVertex AI Agent Engineでのセッション関連の操作を調べました。
セッションイベントへの追加ができるとは知りませんでしたし、これは結構便利なことができそうな予感です。
また何かあればまとめようと思います。

ここまでご覧いただきありがとうございました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?