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 uuid
2import google.auth
3from kern.agent import Agent
4from kern.db.base import SessionType
5from kern.db.gcs_json import GcsJsonDb
6from kern.tools.hackernews import HackerNewsTools
7
8# Obtain the default credentials and project id from your gcloud CLI session.
9credentials, project_id = google.auth.default()
10
11# 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}")
15
16# 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)
23
24# 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)
31
32# Execute sample queries.
33agent1.print_response("How many people live in Canada?")
34agent1.print_response("What is their national anthem called?")
35
36# Create a new agent and make sure it pursues the conversation
37agent2 = Agent(
38 db=db,
39 session_id=agent1.session_id,
40 tools=[HackerNewsTools()],
41 add_history_to_context=True,
42 debug_mode=False,
43)
44
45agent2.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

  1. Install the Google Cloud SDK
  2. Run gcloud init to 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 login

Alternatively, 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 ddgs

Setup 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:latest
5 ports:
6 - "4443:4443"
7 command: ["-scheme", "http", "-port", "4443", "-public-host", "localhost"]
8 volumes:
9 - ./fake-gcs-data:/data

Start the fake GCS server:

1docker-compose up -d

Using 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.py

When using Fake GCS, authentication isn't enforced and the client will automatically detect the emulator endpoint.

Params

ParameterTypeDefaultDescription
idOptional[str]-The ID of the database instance. UUID by default.
bucket_namestr-Name of the GCS bucket where JSON files will be stored.
prefixOptional[str]-Path prefix for organizing files in the bucket. Defaults to "kern/".
session_tableOptional[str]-Name of the JSON file to store sessions (without .json extension).
memory_tableOptional[str]-Name of the JSON file to store user memories.
metrics_tableOptional[str]-Name of the JSON file to store metrics.
eval_tableOptional[str]-Name of the JSON file to store evaluation runs.
knowledge_tableOptional[str]-Name of the JSON file to store knowledge content.
traces_tableOptional[str]-Name of the JSON file to store traces.
spans_tableOptional[str]-Name of the JSON file to store spans.
projectOptional[str]-GCP project ID. If None, uses default project.
credentialsOptional[Any]-GCP credentials. If None, uses default credentials.