セッションの削除

セッションを事前に削除して、プライバシーとスケーラビリティを向上させます。

ADKでは、セッションを使用して、マルチターン会話のサポートやローカル機能ツールの起動など、実行のコンテキストを追跡します。

セッションを明示的に削除しない場合、その存続時間(TTL)が期限切れになると、セッションは自動的に削除されます。デフォルトのTTLは24時間です。ただし、この動作はユースケースに合わない場合があります。たとえば、プライバシ上の理由から、会話が終了した直後にセッションを削除する場合があります。

また、エージェントは多数の同時ユーザーをサポートしているため、セッションを事前に削除してリソースを解放することもできます。エージェントは、デフォルト割当て当たり最大800セッションを保持できるため、この制限に達しないように、セッションが不要になったタイミングをアプリケーションが決定できる場合は、事前にセッションを削除できます。

Python

def main():

    agent = Agent(
        client=client,
        agent_endpoint_id="ocid1.genaiagentendpoint...",
        instructions="You are a helpful assistant that can perform calculations.",
        tools=[CalculatorToolkit()]
    )

    agent.setup()

    input = "What is the square root of 81?"
    response = agent.run(input)

    # You explicitly delete the session used by the last run
    agent.delete_session(response.session_id)

if __name__ == "__main__":
    main()

Java

public static void main(String[] args) throws Exception {
    Agent agent = Agent.builder()
        .client(agentClient)
        .agentEndpointId(AGENT_ENDPOINT_ID)
        .instructions("You perform calculations using tools provided.")
        .tools(Arrays.asList(new SimpleCalculatorToolkit()))
        .build();

    agent.setup()

    final String input = "What is the square root of 475695037565?";
    RunResponse response = agent.run(input);

    agent.deleteSession(response.getSessionId());
}

または、runメソッドでdelete_session=Trueを設定して、実行の完了後にセッションを削除できます。

Python

def main():

    # Ask ADK to auto delete the session after the run completes
    response = agent.run(input, delete_session=True)

if __name__ == "__main__":
    main()

Java

public static void main(String[] args) throws Exception {
    final RunOptions runOptions = RunOptions.builder().deleteSession(true).build();
    RunResponse response = agent.run(input, runOptions);
}