Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

ADK Redis Session Service Example

This project contains a sample agent built with ADK Python that demonstrates how to use Redis for session state management.

This sample agent shows how you can integrate a custom session service with the ADK to persist conversation state in a Redis database. This allows for scalable and persistent user sessions.


Directory Structure

The project is organized as follows:

./redis-session-service
├── adk_cli.py
├── README.md
├── notebooks
│   └── get_started_with_adk_redis_session_service.ipynb
└── redis_session_service
    ├── __init__.py
    ├── agent.py
    ├── .env.example
    ├── log_tools.py
    └── requirements.txt
  • adk_cli.py: A custom command-line interface script that registers the Redis session service with the ADK.
  • redis_session_service/: The main application directory containing the agent definition and supporting files.
  • redis_session_service/agent.py: Defines the simple QA agent.
  • redis_session_service/requirements.txt: Lists the Python dependencies for the project.
  • notebooks/: Contains a Jupyter notebook for an interactive walkthrough of the service.

Prerequisites

To run this agent, you will need:

  • A terminal
  • Git
  • Python 3.10+
  • uv
  • A running Redis instance.

Run Locally

  1. Clone this repository and navigate to this subdirectory.

  2. Set up environment variables:

    Copy the .env.example file to .env and fill in your Google Cloud project details.

    cp redis_session_service/.env.example redis_session_service/.env

    Your redis_session_service/.env file should look like this:

    GOOGLE_GENAI_USE_VERTEXAI=1
    GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
    GOOGLE_CLOUD_LOCATION="your-gcp-location (e.g., us-central1)"
    
  3. Install dependencies:

    Create a virtual environment and install the required packages.

    uv venv
    source .venv/bin/activate
    uv pip install -r redis_session_service/requirements.txt
  4. Run the ADK web UI:

    Start the ADK web server, pointing to the agent directory and specifying the Redis session service URI. The adk_cli.py script ensures that the redis:// protocol is recognized.

    uv run python adk_cli.py web --session_service_uri="redis://localhost:6379" redis_session_service

    Navigate to 127.0.0.1:8000 in your browser to interact with the agent.

Inspecting Session Data

As you interact with the agent, its session state will be stored in your Redis database. You can inspect this data using the redis-cli.

  1. Connect to Redis:

    redis-cli
  2. List all keys to find the session IDs:

    KEYS "session:*"
    
  3. Get the data for a specific session: Replace <session_id> with an ID from the previous step.

    GET "session:<session_id>"
    

This will return a JSON object containing the session state, including the conversation history.

Deploying to Cloud Run

You can deploy the agent to Cloud Run for a scalable setup. This requires a Redis instance accessible from your Cloud Run service.

1. Prepare Redis

Before deploying, you need a Redis instance. For production, it's recommended to use a managed service like Google Cloud Memorystore for Redis.

  • Create a Memorystore for Redis instance within the same VPC network you plan to use for Cloud Run.
  • Once created, note its IP address and port. Your Redis URI will be redis://<redis_ip_address>:<redis_port>.

2. Deploy the Agent

  • Authenticate with Google Cloud:
    gcloud auth login
  • Set Environment Variables:
    export GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
    export GOOGLE_CLOUD_LOCATION="us-central1"
    # The Redis instance must be accessible from this VPC
    export VPC_NETWORK="your-vpc-network"
    export VPC_SUBNET="your-private-subnet-for-redis"
    # The Redis URI from the previous step
    export REDIS_URI="redis://<your-redis-ip>:<your-redis-port>"
  • Run the Deployment Script: From the project root directory (./my-adk-python-samples), run the following command. The script handles containerization and deployment. The --run-command argument is used to start the ADK server with the correct Redis session URI.
    python deploy_to_cloud_run.py --agent-folder=agent-memory/redis-session-service/redis_session_service \
        --project=$GOOGLE_CLOUD_PROJECT \
        --region=$GOOGLE_CLOUD_LOCATION \
        --service-name="redis-session-agent" \
        --vpc-egress="all-traffic" \
        --network=$VPC_NETWORK \
        --subnet=$VPC_SUBNET \
        --with-ui \
        --allow-unauthenticated \
        --log-level DEBUG \
        --adk-cli-path=agent-memory/redis-session-service/adk_cli.py \
        --session-service-uri="$REDIS_URI"
  • The --vpc-egress flag configures the necessary Serverless VPC Access Connector so the agent can communicate with the Redis instance on your VPC.
  • Once complete, the script will provide a public URL to access your agent.

References