Google Cloud Storage for Agent
Kern supports using Google Cloud Storage (GCS) as a storage backend for Agents using the GcsJsonDb class. This storage backend stores session data as JSON blobs in a GCS bucket.
Usage
Configure your agent with GCS storage to enable cloud-based session persistence.
1import uuid2import google.auth3from kern.agent import Agent4from kern.db.base import SessionType5from kern.db.gcs_json import GcsJsonDb6from kern.tools.hackernews import HackerNewsTools78# Obtain the default credentials and project id from your gcloud CLI session.9credentials, project_id = google.auth.default()1011# Generate a unique bucket name using a base name and a UUID4 suffix.12base_bucket_name = "example-gcs-bucket"13unique_bucket_name = f"{base_bucket_name}-{uuid.uuid4().hex[:12]}"14print(f"Using bucket: {unique_bucket_name}")1516# Initialize GCSJsonDb with explicit credentials, unique bucket name, and project.17db = GcsJsonDb(18 bucket_name=unique_bucket_name,19 prefix="agent/",20 project=project_id,21 credentials=credentials,22)2324# Initialize the Kern agent with the new storage backend and HackerNews tools.25agent1 = Agent(26 db=db,27 tools=[HackerNewsTools()],28 add_history_to_context=True,29 debug_mode=False,30)3132# Execute sample queries.33agent1.print_response("How many people live in Canada?")34agent1.print_response("What is their national anthem called?")3536# Create a new agent and make sure it pursues the conversation37agent2 = Agent(38 db=db,39 session_id=agent1.session_id,40 tools=[HackerNewsTools()],41 add_history_to_context=True,42 debug_mode=False,43)4445agent2.print_response("What's the name of the country we discussed?")46agent2.print_response("What is that country's national sport?")Prerequisites
Google Cloud SDK Setup
- Install the Google Cloud SDK
- Run
gcloud initto configure your account and project
GCS Permissions
Ensure your account has sufficient permissions (e.g., Storage Admin) to create and manage GCS buckets:
1gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \2 --member="user:YOUR_EMAIL@example.com" \3 --role="roles/storage.admin"Authentication
Use default credentials from your gcloud CLI session:
1gcloud auth application-default loginAlternatively, if using a service account, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account JSON file.
Python Dependencies
Install the required Python packages:
1pip install google-auth google-cloud-storage openai ddgsSetup with Docker
For local testing without using real GCS, you can use fake-gcs-server.
Create a docker-compose.yml file:
1version: '3.8'2services:3 fake-gcs-server:4 image: fsouza/fake-gcs-server:latest5 ports:6 - "4443:4443"7 command: ["-scheme", "http", "-port", "4443", "-public-host", "localhost"]8 volumes:9 - ./fake-gcs-data:/dataStart the fake GCS server:
1docker-compose up -dUsing Fake GCS with Docker
Set the environment variable to direct API calls to the emulator:
1export STORAGE_EMULATOR_HOST="http://localhost:4443"2python gcs_for_agent.pyWhen using Fake GCS, authentication isn't enforced and the client will automatically detect the emulator endpoint.
Params
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | The ID of the database instance. UUID by default. |
bucket_name | str | - | Name of the GCS bucket where JSON files will be stored. |
prefix | Optional[str] | - | Path prefix for organizing files in the bucket. Defaults to "kern/". |
session_table | Optional[str] | - | Name of the JSON file to store sessions (without .json extension). |
memory_table | Optional[str] | - | Name of the JSON file to store user memories. |
metrics_table | Optional[str] | - | Name of the JSON file to store metrics. |
eval_table | Optional[str] | - | Name of the JSON file to store evaluation runs. |
knowledge_table | Optional[str] | - | Name of the JSON file to store knowledge content. |
traces_table | Optional[str] | - | Name of the JSON file to store traces. |
spans_table | Optional[str] | - | Name of the JSON file to store spans. |
project | Optional[str] | - | GCP project ID. If None, uses default project. |
credentials | Optional[Any] | - | GCP credentials. If None, uses default credentials. |