<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Siduo Jiang on Medium]]></title>
        <description><![CDATA[Stories by Siduo Jiang on Medium]]></description>
        <link>https://medium.com/@siduojiang?source=rss-94a523ac43db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*dyyjGEjrUEJmz7qp.jpg</url>
            <title>Stories by Siduo Jiang on Medium</title>
            <link>https://medium.com/@siduojiang?source=rss-94a523ac43db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:39:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@siduojiang/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Streamlining MLOps: Building Training Pipelines with Vertex AI Pipelines]]></title>
            <link>https://medium.com/@siduojiang/streamlining-mlops-building-training-pipelines-with-vertex-ai-pipelines-fbad7edca6c2?source=rss-94a523ac43db------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbad7edca6c2</guid>
            <category><![CDATA[machine-leanring]]></category>
            <dc:creator><![CDATA[Siduo Jiang]]></dc:creator>
            <pubDate>Tue, 04 Nov 2025 17:49:44 GMT</pubDate>
            <atom:updated>2025-11-04T17:49:44.334Z</atom:updated>
            <content:encoded><![CDATA[<p>In the fast-paced world of Machine Learning, moving from experimentation to production-grade solutions can be a complex and often messy endeavor. This is where MLOps (Machine Learning Operations) comes into play, providing a set of practices to deploy and maintain ML systems reliably and efficiently. A cornerstone of robust MLOps is the concept of a Machine Learning pipeline, and Google Cloud’s Vertex AI Pipelines offers a powerful, managed solution to orchestrate these workflows.</p><p>This post will guide you through building an end-to-end training pipeline using Vertex AI Pipelines, covering everything from synthetic data generation to model training and deployment.</p><h3>Why Vertex AI Pipelines?</h3><p>Before we dive into the “how,” let’s quickly address the “why.” Vertex AI Pipelines, built on Kubeflow Pipelines, offers several compelling advantages:</p><ul><li><strong>Managed Service:</strong> No need to manage Kubernetes clusters or Kubeflow installations. Google handles the infrastructure.</li><li><strong>Scalability:</strong> Easily scale your pipeline components to handle large datasets and complex models.</li><li><strong>Reproducibility:</strong> Define your workflows as code, ensuring every run is consistent and reproducible.</li><li><strong>Visibility &amp; Monitoring:</strong> Track pipeline runs, visualize component interactions, and monitor resource consumption through the Vertex AI console.</li><li><strong>Integration:</strong> Seamlessly integrates with other Vertex AI services like Workbench (for notebooks), Data Labeling, Feature Store, and Model Registry.</li></ul><h3>Our Use Case: A Simple Classification Problem</h3><p>To illustrate the concepts, we’ll work with a synthetic dataset for a binary classification problem. Imagine we’re trying to predict if a customer will churn based on a few simple features. Our pipeline will consist of the following steps:</p><ol><li><strong>Generate Synthetic Data:</strong> Create a CSV file with features and target labels.</li><li><strong>Preprocess Data:</strong> Load, clean, and split the data into training and testing sets.</li><li><strong>Train Model:</strong> Train a simple scikit-learn classifier.</li><li><strong>Evaluate Model:</strong> Calculate accuracy and other metrics on the test set.</li><li><strong>Register Model:</strong> Save the trained model to the Vertex AI Model Registry.</li></ol><p>Let’s get started!</p><h3>1. Project Setup and Dependencies</h3><p>First, ensure you have a Google Cloud project with the Vertex AI API enabled. You’ll also need to install the necessary Python libraries:</p><p>Bash</p><pre>pip install google-cloud-aiplatform google-cloud-storage kfp scikit-learn pandas</pre><p>For authentication, ensure your environment is set up to access Google Cloud. If you’re running this from a Vertex AI Workbench instance, authentication is usually handled automatically. Otherwise, you might need to set up gcloud auth application-default login.</p><p>We’ll define some global variables for our project:</p><pre>PROJECT_ID = os.environ.get(&quot;GOOGLE_CLOUD_PROJECT&quot;) or &quot;your-gcp-project-id&quot; # Replace with your project ID<br>REGION = &quot;us-central1&quot; # Or your preferred region<br>BUCKET_NAME = f&quot;gs://{PROJECT_ID}-vertex-ai-pipelines&quot; # Replace with your desired GCS bucket name<br>PIPELINE_ROOT = f&quot;{BUCKET_NAME}/pipeline_root&quot;<br># Ensure your GCS bucket exists and you have write permissions<br># gsutil mb gs://your-gcp-project-id-vertex-ai-pipelines</pre><h3>2. Defining Pipeline Components</h3><p>Vertex AI Pipelines uses Kubeflow Pipelines components. A component is essentially a self-contained unit of work. We’ll define each step of our pipeline as a Python function, then compile it into a Kubeflow component using the @component decorator from the kfp library.</p><p>Each component function will define its inputs and outputs using kfp.Input and kfp.Output types, which represent artifacts stored in Google Cloud Storage.</p><h4>Component 1: Generate Synthetic Data</h4><p>This component will create a CSV file with random data.</p><pre>from kfp.v2.dsl import pipeline, component, Artifact, Dataset, Model, Output, Input<br>from typing import NamedTuple<br>@component(base_image=&quot;python:3.9&quot;, packages_to_install=[&quot;pandas&quot;, &quot;numpy&quot;, &quot;scikit-learn&quot;, &quot;google-cloud-storage&quot;])<br>def generate_synthetic_data(<br>    num_samples: int,<br>    output_dataset: Output[Dataset]<br>):<br>    &quot;&quot;&quot;Generates synthetic data for a binary classification problem.&quot;&quot;&quot;<br>    import pandas as pd<br>    import numpy as np<br>    from sklearn.datasets import make_classification<br>    from google.cloud import storage<br>    print(f&quot;Generating {num_samples} samples...&quot;)<br>    X, y = make_classification(<br>        n_samples=num_samples,<br>        n_features=5,<br>        n_informative=3,<br>        n_redundant=1,<br>        n_classes=2,<br>        random_state=42<br>    )<br>    df = pd.DataFrame(X, columns=[f&#39;feature_{i}&#39; for i in range(X.shape[1])])<br>    df[&#39;target&#39;] = y<br>    # Save to a temporary local path first<br>    local_path = &quot;synthetic_data.csv&quot;<br>    df.to_csv(local_path, index=False)<br>    # Upload to GCS<br>    # The output_dataset path points to a GCS location<br>    gcs_path = output_dataset.path<br>    bucket_name = gcs_path.split(&#39;/&#39;)[2]<br>    blob_path = &#39;/&#39;.join(gcs_path.split(&#39;/&#39;)[3:])<br>    print(f&quot;Uploading data to gs://{bucket_name}/{blob_path}&quot;)<br>    client = storage.Client()<br>    bucket = client.bucket(bucket_name)<br>    blob = bucket.blob(blob_path)<br>    blob.upload_from_filename(local_path)<br>    print(f&quot;Synthetic data generated and saved to {output_dataset.path}&quot;)</pre><h4>Component 2: Preprocess Data</h4><p>This component will load the data, perform a simple train-test split, and save the resulting datasets.</p><pre>@component(base_image=&quot;python:3.9&quot;, packages_to_install=[&quot;pandas&quot;, &quot;scikit-learn&quot;, &quot;google-cloud-storage&quot;])<br>def preprocess_data(<br>    input_dataset: Input[Dataset],<br>    training_dataset: Output[Dataset],<br>    testing_dataset: Output[Dataset]<br>):<br>  </pre><h4>Component 3: Train Model</h4><p>Here, we’ll train a LogisticRegression model from scikit-learn.</p><pre>@component(base_image=&quot;python:3.9&quot;, packages_to_install=[&quot;pandas&quot;, &quot;scikit-learn&quot;, &quot;google-cloud-storage&quot;])<br>def train_model(<br>    training_dataset: Input[Dataset],<br>    metrics: Output[Artifact], # For logging metrics<br>    model: Output[Model]<br>):<br>    &quot;&quot;&quot;Trains a Logistic Regression model.&quot;&quot;&quot;<br>    import pandas as pd<br>    from sklearn.linear_model import LogisticRegression<br>    import joblib # For saving/loading models<br>    from google.cloud import storage<br>    import os<br>    gcs_path = training_dataset.path<br>    bucket_name = gcs_path.split(&#39;/&#39;)[2]<br>    blob_path = &#39;/&#39;.join(gcs_path.split(&#39;/&#39;)[3:])<br>    local_train_path = &quot;train_data.csv&quot;<br>    print(f&quot;Downloading training data from gs://{bucket_name}/{blob_path}&quot;)<br>    client = storage.Client()<br>    bucket = client.bucket(bucket_name)<br>    blob = bucket.blob(blob_path)<br>    blob.download_to_filename(local_train_path)<br>    train_df = pd.read_csv(local_train_path)<br>    X_train = train_df.drop(&#39;target&#39;, axis=1)<br>    y_train = train_df[&#39;target&#39;]<br>    print(&quot;Training Logistic Regression model...&quot;)<br>    model_instance = LogisticRegression(random_state=42)<br>    model_instance.fit(X_train, y_train)<br>    # Save model locally<br>    local_model_path = &quot;model.joblib&quot;<br>    joblib.dump(model_instance, local_model_path)<br>    # Upload model to GCS<br>    # The model.path points to a GCS location<br>    gcs_model_path = model.path<br>    bucket_name = gcs_model_path.split(&#39;/&#39;)[2]<br>    blob_path = &#39;/&#39;.join(gcs_model_path.split(&#39;/&#39;)[3:])<br>    print(f&quot;Uploading model to gs://{bucket_name}/{blob_path}&quot;)<br>    client.bucket(bucket_name).blob(blob_path).upload_from_filename(local_model_path)<br>    print(f&quot;Model trained and saved to {model.path}&quot;)<br>    # Log some dummy metrics for demonstration<br>    # In a real scenario, you might calculate and save metrics here too.<br>    with open(metrics.path, &quot;w&quot;) as f:<br>        f.write(&quot;Training completed. No specific metrics logged in this component.\n&quot;)</pre><h4>Component 4: Evaluate Model</h4><p>This component will load the trained model and test data, make predictions, and calculate evaluation metrics.</p><pre>@component(base_image=&quot;python:3.9&quot;, packages_to_install=[&quot;pandas&quot;, &quot;scikit-learn&quot;, &quot;joblib&quot;, &quot;google-cloud-storage&quot;])<br>def evaluate_model(<br>    testing_dataset: Input[Dataset],<br>    model: Input[Model],<br>    metrics: Output[Artifact],<br>    accuracy: Output[Artifact]<br>) -&gt; NamedTuple(&quot;Outputs&quot;, [(&quot;model_accuracy&quot;, float)]):<br>    &quot;&quot;&quot;Evaluates the trained model and logs metrics.&quot;&quot;&quot;<br></pre><h4>Component 5: Register Model (to Vertex AI Model Registry)</h4><p>This component will take the trained model artifact and register it in the Vertex AI Model Registry. This is a crucial step for MLOps, as it centralizes model management.</p><pre>from google.cloud import aiplatform<br>@component(base_image=&quot;python:3.9&quot;, packages_to_install=[&quot;google-cloud-aiplatform&quot;, &quot;google-cloud-storage&quot;, &quot;joblib&quot;])<br>def register_model(<br>    model: Input[Model],<br>    project: str,<br>    location: str,<br>    display_name: str,<br>    accuracy: Input[Artifact]<br>):<br>    &quot;&quot;&quot;Registers the trained model to Vertex AI Model Registry.&quot;&quot;&quot;<br>    from google.cloud import aiplatform<br>    from google.cloud import storage<br>    import os<br>    aiplatform.init(project=project, location=location)<br>    # Read accuracy from artifact<br>    with open(accuracy.path, &quot;r&quot;) as f:<br>        model_accuracy = float(f.read())<br>    print(f&quot;Registering model &#39;{display_name}&#39; with accuracy {model_accuracy}...&quot;)<br>    # The input &#39;model&#39; artifact already points to the GCS URI of our saved model.<br>    # Vertex AI Model Registry expects a GCS URI for the model artifacts.<br>    # The artifact itself provides this path.<br>    artifact_uri = model.uri<br>    # We need to tell the Model Registry what container image to use for serving.<br>    # For a scikit-learn model, we can use a pre-built container.<br>    # See: https://cloud.google.com/vertex-ai/docs/predictions/pre-built-containers<br>    serving_container_image_uri = &quot;us-docker.pkg.dev/vertex-ai/prediction/sklearn-cpu.1-0:latest&quot;<br>    # You can add custom metadata to the model version<br>    metadata = {<br>        &quot;accuracy_score&quot;: model_accuracy,<br>        &quot;framework&quot;: &quot;scikit-learn&quot;,<br>        &quot;pipeline_run_id&quot;: os.environ.get(&quot;KFP_RUN_ID&quot;, &quot;local-run&quot;) # Access pipeline run ID if available<br>    }<br>    # Upload the model to Vertex AI Model Registry<br>    # This creates a Model resource in Vertex AI<br>    # If a model with display_name already exists, it creates a new version.<br>    uploaded_model = aiplatform.Model.upload(<br>        display_name=display_name,<br>        artifact_uri=artifact_uri,<br>        serving_container_image_uri=serving_container_image_uri,<br>        description=f&quot;Logistic Regression model trained via Vertex AI Pipeline. Accuracy: {model_accuracy:.4f}&quot;,<br>        labels={&quot;pipeline_component&quot;: &quot;train&quot;, &quot;model_type&quot;: &quot;classification&quot;},<br>        metadata=metadata<br>    )<br>    print(f&quot;Model &#39;{uploaded_model.display_name}&#39; (ID: {uploaded_model.name}) registered to Vertex AI Model Registry.&quot;)<br>    print(f&quot;Model resource name: {uploaded_model.resource_name}&quot;)</pre><h3>3. Assembling the Pipeline</h3><p>Now that we have our components, we can define the overall pipeline using the @pipeline decorator. This function orchestrates the flow of data and execution between components.</p><p>Python</p><pre>@pipeline(<br>    name=&quot;vertex-ai-training-pipeline&quot;,<br>    description=&quot;An end-to-end pipeline for synthetic data generation, model training, and registration.&quot;,<br>    pipeline_root=PIPELINE_ROOT<br>)<br>def training_pipeline(<br>    num_samples: int = 1000,<br>    model_display_name: str = &quot;churn-prediction-model&quot;<br>):<br>    &quot;&quot;&quot;<br>    Defines the main training pipeline workflow.<br>    &quot;&quot;&quot;<br>    # Step 1: Generate Synthetic Data<br>    data_generation_task = generate_synthetic_data(<br>        num_samples=num_samples<br>    )<br># Step 2: Preprocess Data<br>    data_preprocessing_task = preprocess_data(<br>        input_dataset=data_generation_task.outputs[&quot;output_dataset&quot;]<br>    )<br>    # Step 3: Train Model<br>    model_training_task = train_model(<br>        training_dataset=data_preprocessing_task.outputs[&quot;training_dataset&quot;]<br>    )<br>    # Step 4: Evaluate Model<br>    model_evaluation_task = evaluate_model(<br>        testing_dataset=data_preprocessing_task.outputs[&quot;testing_dataset&quot;],<br>        model=model_training_task.outputs[&quot;model&quot;]<br>    )<br>    # Step 5: Register Model<br>    model_registration_task = register_model(<br>        model=model_training_task.outputs[&quot;model&quot;],<br>        project=PROJECT_ID,<br>        location=REGION,<br>        display_name=model_display_name,<br>        accuracy=model_evaluation_task.outputs[&quot;accuracy&quot;]<br>    )</pre><p>Once the pipeline is defined, we need to compile it into a JSON file, which Vertex AI Pipelines understands. Then, we can submit this compiled pipeline for execution.</p><pre>from kfp.v2 import compiler<br>import google.cloud.aiplatform as aiplatform<br># Initialize Vertex AI SDK<br>aiplatform.init(project=PROJECT_ID, location=REGION)<br># Compile the pipeline<br>pipeline_file = &quot;training_pipeline.json&quot;<br>compiler.Compiler().compile(<br>    pipeline_func=training_pipeline,<br>    package_path=pipeline_file<br>)<br>print(f&quot;Pipeline compiled to {pipeline_file}&quot;)<br># Run the pipeline<br>job = aiplatform.PipelineJob(<br>    display_name=&quot;churn-prediction-training-run&quot;,<br>    template_path=pipeline_file,<br>    pipeline_root=PIPELINE_ROOT,<br>    parameter_values={<br>        &quot;num_samples&quot;: 2000,<br>        &quot;model_display_name&quot;: &quot;vertex-churn-predictor-v1&quot;<br>    },<br>    enable_caching=False # Set to True for faster re-runs if inputs don&#39;t change<br>)<br>print(&quot;Submitting pipeline job...&quot;)<br>job.run()<br>print(f&quot;Pipeline job submitted. View it in the console: {job.console_uri}&quot;)</pre><p>After submitting the job, you can navigate to the Vertex AI console under the “Pipelines” section to monitor its progress, view logs for each step, and inspect the generated artifacts. Each box represents a component, and the arrows show the data flow. Clicking on a component will reveal its logs, input parameters, and output artifacts, including their GCS locations.</p><h3>5. Inspecting the Model Registry</h3><p>Once the pipeline completes successfully, you can go to the Vertex AI console, navigate to “Models,” and you should see your newly registered model, vertex-churn-predictor-v1.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbad7edca6c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Secure, Scalable, and Resilient Data Pipelines on Google Cloud]]></title>
            <link>https://medium.com/@siduojiang/secure-scalable-and-resilient-data-pipelines-on-google-cloud-1b39001a3739?source=rss-94a523ac43db------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b39001a3739</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[data-engineering]]></category>
            <dc:creator><![CDATA[Siduo Jiang]]></dc:creator>
            <pubDate>Tue, 04 Nov 2025 17:39:38 GMT</pubDate>
            <atom:updated>2025-11-04T17:39:38.793Z</atom:updated>
            <content:encoded><![CDATA[<h3>An architecture for ingesting and processing millions of streaming events using Pub/Sub, Dataflow, and Cloud Storage.</h3><p>In today’s data-driven world, the challenge is no longer just <em>collecting</em> data. It’s about building systems that can ingest, process, and store a relentless, high-velocity stream of events without falling over, losing data, or exposing sensitive information. A pipeline that works for 1,000 events per day will crumble under a load of 1,000 events per <em>second</em>.</p><p>Building this kind of system from scratch requires a massive engineering investment in managing message brokers, scaling worker fleets, and ensuring fault tolerance. Fortunately, Google Cloud provides a set of serverless, managed services that, when combined, create an elastic and robust data pipeline.</p><p>We will focus on three core pillars:</p><ol><li><strong>Scalability:</strong> How to automatically handle massive, unpredictable spikes in data volume.</li><li><strong>Resilience:</strong> How to ensure no data is lost and the pipeline recovers gracefully from failures.</li><li><strong>Security:</strong> How to protect data in transit and at rest, and manage configuration safely.</li></ol><h3>The Pipeline Architecture</h3><p>At its core, our pipeline is designed for <strong>decoupling</strong>. No single component is directly dependent on another; they are all connected by a durable, asynchronous message bus. This is the key to both scalability and resilience.</p><p>Our architecture consists of four serverless components:</p><ol><li><strong>Data Source (Cloud Function):</strong> A lightweight, serverless function (gen) acts as our data producer. It simulates a high-throughput source by generating two million synthetic headlines and publishing them as JSON messages.</li><li><strong>Ingestion Buffer (Cloud Pub/Sub):</strong> This is the “shock absorber” of our architecture. A Pub/Sub topic (gen) receives all messages from the Cloud Function. It doesn&#39;t care how fast they arrive or how slowly they&#39;re consumed.</li><li><strong>Processing Engine (Dataflow):</strong> This is the serverless, auto-scaling ETL workhorse. A streaming Dataflow job, written in Apache Beam, subscribes to the Pub/Sub topic, processes the data in real-time, and windows it for batch writes.</li><li><strong>Data Lake (Cloud Storage):</strong> The final, durable destination. Dataflow writes the processed headlines into GCS buckets, neatly batched into files.</li></ol><p>Let’s explore how this simple combination achieves our three pillars.</p><h3>Pub/Sub: The Great Decoupler</h3><p>The most critical scaling component is <strong>Cloud Pub/Sub</strong>. It decouples the data <em>producer</em> from the data <em>consumer</em>.</p><p>In our project, the Cloud Function is set to generate a massive burst of 200,000 (or even 2,000,000) messages at once. Without Pub/Sub, our Dataflow pipeline would need to be provisioned to handle this peak load 24/7, which is incredibly expensive.</p><p>With Pub/Sub, the function can publish all 2 million messages in a few seconds. Pub/Sub absorbs this spike instantly and holds the messages. Dataflow can then consume and process these messages at its <em>own</em> sustainable, cost-effective pace. If we get another 5 million messages, the pipeline doesn’t crash — the Pub/Sub backlog just gets a bit longer, and Dataflow continues to work through it.</p><h3>Dataflow: The Auto-Scaling Workhorse</h3><p>The str.py file defines our logic using Apache Beam, but the magic happens when we deploy it with --runner=DataflowRunner.</p><p>We are not telling Google Cloud how many servers to use. We are simply giving it our Beam code and a streaming data source. Dataflow automatically:</p><ul><li><strong>Provisions Workers:</strong> Starts a fleet of workers to run the pipeline.</li><li><strong>Monitors Throughput:</strong> Watches the Pub/Sub subscription’s backlog and processing latency.</li><li><strong>Auto-scales:</strong> If the backlog grows, Dataflow automatically provisions <em>more</em> workers to clear it. When the spike is over, it scales the workers back down to save costs.</li></ul><p>This serverless, auto-scaling capability means we’ve built a pipeline that can handle 10 messages per minute or 10 million, all with the same code.Securing the Pipeline End to End</p><p>Finally, the pipeline must be secure. This is achieved by managing configuration, limiting permissions, and leveraging Google’s built-in security.</p><h3>Separating Configuration from Code</h3><p>You will not find a single project ID, bucket name, or topic name hardcoded in our Python files. Instead, the streaming_pipeline.py script reads its configuration from arguments, which are supplied by environment variables.</p><pre># Get defaults from environment variables<br>default_project = os.environ.get(&#39;GCP_PROJECT&#39;)<br>default_bucket = os.environ.get(&#39;GCS_BUCKET&#39;)</pre><pre># ...<br>parser = argparse.ArgumentParser()<br>parser.add_argument(&#39;--project&#39;, default=default_project, ...)<br>parser.add_argument(&#39;--output_path&#39;, ...)</pre><p>This is a fundamental security best practice. Code should be an artifact that can be deployed to <em>any</em> environment (dev, staging, prod). The configuration for that environment is injected at runtime. The deploy.sh script is the &quot;runtime&quot; that provides this configuration.</p><h3>Principle of Least Privilege</h3><p>While not explicitly defined in the scripts, this architecture is built to use <strong>Identity and Access Management (IAM)</strong> effectively. In a production environment, we would follow the principle of least privilege:</p><ul><li><strong>Cloud Function:</strong> Runs with a service account that <em>only</em> has the pubsub.publisher role for the ge topic. It cannot read from Pub/Sub or write to GCS.</li><li><strong>Dataflow Workers:</strong> Run with a service account that <em>only</em> has pubsub.subscriber (for the gen subscription) and storage.objectCreator (for the output buckets).</li><li><strong>Developer:</strong> Has permission to deploy new jobs, but not to access the service accounts or the raw data in the buckets.</li></ul><h3>Encryption by Default</h3><p>All data moving between Google Cloud services (Cloud Function to Pub/Sub, Pub/Sub to Dataflow, Dataflow to Cloud Storage) is <strong>encrypted in transit by default</strong>. All data stored in Pub/Sub and Cloud Storage is <strong>encrypted at rest by default</strong>. No complex setup is required to achieve this baseline of security.</p><h3>Deep Dive: The Dataflow Pipeline Code</h3><p>Let’s look at the core logic in streaming_pipeline.py. This is where the business logic lives. The Apache Beam SDK provides a simple, chainable syntax.</p><pre>import apache_beam as beam<br>from apache_beam.options.pipeline_options import PipelineOptions, StandardOptions<br>from apache_beam.io import ReadFromPubSub<br>from apache_beam.io.fileio import WriteToFiles<br><br># ... (argument parsing) ...<br><br># Define the pipeline<br>with beam.Pipeline(options=pipeline_options) as p:<br>    (<br>        p<br>        | &#39;Read from Pub/Sub&#39; &gt;&gt; ReadFromPubSub(topic=topic_path)<br>        | &#39;Decode messages&#39; &gt;&gt; beam.Map(lambda x: x.decode(&#39;utf-8&#39;))<br>        | &#39;Window&#39; &gt;&gt; beam.WindowInto(beam.window.FixedWindows(10)) # 10 second windows<br>        | &#39;Write to GCS&#39; &gt;&gt; WriteToFiles(<br>            path=known_args.output_path,<br>            sink=lambda dest: beam.io.fileio.TextSink(),<br>            file_naming=beam.io.fileio.default_file_naming()<br>        )<br>    )</pre><p>This is the entire pipeline. Let’s break it down:</p><ol><li><strong>ReadFromPubSub</strong>: This connects to our gen topic and creates an &quot;unbounded PCollection&quot;—a data stream that never ends.</li><li><strong>Decode messages</strong>: The messages from Pub/Sub are raw bytes. This simple beam.Map function decodes them back into UTF-8 strings (our JSON).</li><li><strong>Window</strong>: This is the most important step for our streaming use case. beam.WindowInto(beam.window.FixedWindows(10)) tells Dataflow to stop thinking about one message at a time. Instead, it creates 10-second-long, non-overlapping buckets. All messages that arrive (based on their event timestamp) in a 10-second window are grouped together for processing.</li><li><strong>WriteToFiles</strong>: This step receives an entire 10-second window of messages at once and writes them as a single text file to our Cloud Storage output bucket. This is <em>far</em> more efficient for a data lake than writing millions of tiny, individual files.</li></ol><h3>Conclusion</h3><p>We have successfully designed a data pipeline that is more than just a script — it’s a robust, production-ready system.</p><p>By combining the serverless, managed components of Google Cloud, we’ve built an architecture that:</p><ul><li><strong>Scales</strong> automatically to handle massive, unpredictable load using Dataflow and Pub/Sub.</li><li><strong>Resists</strong> failure and prevents data loss by using Pub/Sub’s durable buffer and Dataflow’s fault-tolerant streaming engine.</li><li><strong>Isolates</strong> deployments for safe, repeatable updates using a timestamped-resource pattern.</li><li><strong>Secures</strong> our data and configuration by separating config from code and leveraging built-in encryption and IAM.</li></ul><p>This approach frees developers from managing infrastructure and allows them to focus on what truly matters: the business logic inside their beam.Map functions that transforms raw data into valuable insights.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b39001a3739" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TimesFM: Using Google’s Foundation Model for Time Series Forecasting]]></title>
            <link>https://medium.com/@siduojiang/timesfm-using-googles-foundation-model-for-time-series-forecasting-3da90d07bdd7?source=rss-94a523ac43db------2</link>
            <guid isPermaLink="false">https://medium.com/p/3da90d07bdd7</guid>
            <category><![CDATA[forecasting]]></category>
            <dc:creator><![CDATA[Siduo Jiang]]></dc:creator>
            <pubDate>Wed, 17 Sep 2025 05:02:59 GMT</pubDate>
            <atom:updated>2025-09-17T05:02:59.925Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N5wNQfRFod7WXUUlVtH2NA.png" /><figcaption>Generated with Google’s Nano Banana</figcaption></figure><p>Forecasting the future is a fundamental human endeavor. From predicting next quarter’s sales to anticipating disease outbreaks, our ability to look ahead is critical. For decades, this task has been the domain of statistical methods like ARIMA and, more recently, specialized deep learning models. However, these approaches often require significant expertise and must be trained or tuned for each new dataset.</p><p>What if we could have a single, powerful model that understands the language of time — the rhythms, seasons, and trends inherent in time series data — and could forecast for a brand-new dataset without any prior training on it?</p><p>That’s the promise of <a href="https://github.com/google-research/timesfm"><strong>TimesFM</strong></a><strong> (Time-series Foundation Model)</strong>, a new decoder-only foundation model for forecasting from Google Research. This article will serve as your guide to understanding and using this groundbreaking model. We’ll dive into the theory, explore how it was built, and walk through practical code examples for forecasting, both with and without external factors.</p><h3>What is Time Series Forecasting?</h3><p>At its core, time series forecasting is the practice of using a model to predict future values based on historical data. The data points are collected at successive, equally spaced points in time.</p><p>Think about:</p><ul><li><strong>Retail:</strong> Predicting the daily demand for a product to optimize inventory.</li><li><strong>Finance:</strong> Forecasting the stock market to make investment decisions.</li><li><strong>Healthcare:</strong> Estimating the number of weekly flu cases to allocate resources, as we’ll see in our code example.</li><li><strong>Energy:</strong> Predicting hourly electricity consumption to manage the power grid.</li></ul><p>The key challenge is to identify and extrapolate the underlying patterns in the data, such as <strong>trends</strong> (long-term increase or decrease), <strong>seasonality</strong> (predictable, repeating fluctuations), and <strong>cycles</strong> (unpredictable fluctuations).</p><h3>What is a Decoder?</h3><p>To understand TimesFM, we first need to understand its architectural foundation: the <strong>decoder</strong>. Decoders are a cornerstone of modern AI, famously powering Large Language Models (LLMs) like GPT.</p><p>Imagine you’re translating a sentence from English to French. You read the English sentence (the “context”) and then begin writing the French translation word by word.</p><p>A decoder works similarly. It’s a neural network architecture designed for <strong>generative tasks</strong> — creating new sequences based on an input context. In the case of an LLM, it takes a sequence of words (the prompt) and generates the next word. It then appends this new word to the sequence and generates the next one, and so on, in an <strong>autoregressive</strong> fashion.</p><p>TimesFM applies this exact principle to numbers instead of words. The “context” is the historical time series data. The model “generates” the next value (or a patch of values) in the sequence, which is our forecast. It learns the “grammar” of time — the intricate temporal patterns — instead of human language.</p><h3>Introducing TimesFM: The Foundation Model for Time</h3><p>TimesFM is designed to be a general-purpose, zero-shot forecaster. This means it can make surprisingly accurate predictions for datasets it has never seen before, without any fine-tuning. This is a paradigm shift from traditional models.</p><p>The key question the researchers at Google sought to answer was: <em>“Can a large model pretrained on massive amounts of time-series data learn universal temporal patterns useful for forecasting on unseen datasets?”</em></p><p>The answer is a resounding yes. Here are the key architectural choices that make TimesFM so effective:</p><p><strong>1. Decoder-Only Architecture:</strong> By using a decoder-only model, TimesFM is naturally suited for generating future sequences from past context, and it can gracefully handle inputs of varying lengths.</p><p><strong>2. Input Patching:</strong> Instead of looking at one data point at a time, TimesFM groups the input time series into contiguous, non-overlapping “patches.” Think of it as reading a sentence instead of a single word. This has two major benefits:<br><strong>Improved Performance:</strong> It allows the model to learn local patterns within a patch more effectively.<br><strong>Inference Speed:</strong> The transformer has fewer “tokens” (patches) to process, making it much faster. The paper found a patch length of 32 to be a sweet spot.</p><p><strong>3. Longer Output Patches:</strong> Unlike many LLMs that generate one token at a time, TimesFM can be configured to predict an output patch that is <em>longer</em> than its input patches. For example, it can process 32-point input patches to generate a 128-point forecast in a single step. This dramatically reduces the number of steps needed for long-horizon forecasting, improving speed and accuracy.</p><p><strong>How Well Does It Perform?</strong></p><p>This is where TimesFM truly shines. The paper evaluates the model against a suite of well-known forecasting benchmarks (Monash, Darts, Informer ETT) that it was <strong>not</strong> trained on.</p><p>The results are remarkable. In zero-shot mode, TimesFM’s performance is close to or even surpasses state-of-the-art supervised models that were specifically trained and tuned for each of those datasets. This demonstrates that the universal patterns learned during pre-training are highly effective and transferable across different domains.</p><h3>The Data Behind the Model</h3><p>A foundation model is only as good as the data it’s trained on. For TimesFM to learn universal patterns, it needed a massive and incredibly diverse dataset. The researchers compiled a corpus with over <strong>100 billion timepoints</strong> from a variety of sources:</p><p><strong>Real-World Data:</strong></p><ul><li><strong>Google Trends:</strong> Billions of points representing search interest over time for thousands of queries, capturing public interest and seasonality at various granularities (hourly, daily, weekly).</li><li><strong>Wikipedia Pageviews:</strong> Billions of points tracking the views of Wikipedia pages, reflecting cultural events and trends.</li><li><strong>Public Datasets:</strong> Well-known forecasting datasets like M4, Electricity, and Traffic were also included.</li><li><strong>Synthetic Data:</strong> To ensure the model doesn’t just memorize patterns common in real-world data (e.g., 24-hour cycles), the researchers generated a huge amount of synthetic time series data. This included various trends, seasonal patterns (sine/cosine waves), and ARMA processes, teaching the model to generalize to patterns it might not have seen in the wild.</li></ul><p>This combination of scale and diversity is what enables TimesFM to be such a powerful generalist.</p><h3>Getting Started: Zero-Shot Forecasting with TimesFM</h3><p>Now, let’s get practical. The simplest way to use TimesFM is for zero-shot forecasting, where you only need your historical data.</p><p>First, ensure you have the timesfm library installed.</p><pre>!pip install --quiet git+https://github.com/google-research/timesfm</pre><p>Now, let’s perform a forecast.</p><pre>import timesfm<br>import numpy as np<br>import pandas as pd<br>import matplotlib.pyplot as plt<br><br># --- 1. Generate Synthetic Bike Rental Data ---<br>def create_bike_rental_data():<br>    &quot;&quot;&quot;Creates a realistic, synthetic daily bike rental dataset.&quot;&quot;&quot;<br>    dates = pd.date_range(start=&quot;2020-01-01&quot;, end=&quot;2023-12-31&quot;, freq=&quot;D&quot;)<br>    n_days = len(dates)<br><br>    # Baseline trend (rentals slowly increasing over the years)<br>    trend = np.linspace(start=150, stop=350, num=n_days)<br><br>    # Yearly seasonality (more rentals in summer, less in winter)<br>    yearly_seasonality = 180 * (1 + np.sin(2 * np.pi * dates.dayofyear / 365.25 - np.pi/2))<br><br>    # Weekly seasonality (more rentals on weekends)<br>    weekly_seasonality = 120 * (dates.dayofweek &gt;= 5)<br><br>    # Noise<br>    noise = np.random.normal(0, 40, n_days)<br><br>    # Combine to create the rental count<br>    rentals = trend + yearly_seasonality + weekly_seasonality + noise<br>    rentals = np.maximum(0, rentals).astype(int) # Ensure no negative rentals<br><br>    # Create weather covariates<br>    temp = 15 + 10 * np.sin(2 * np.pi * dates.dayofyear / 365.25 - np.pi/2) + np.random.randn(n_days) * 2<br>    is_weekend = (dates.dayofweek &gt;= 5).astype(int)<br><br>    # Create the final DataFrame<br>    df = pd.DataFrame({<br>        &#39;rentals&#39;: rentals,<br>        &#39;temperature&#39;: temp,<br>        &#39;is_weekend&#39;: is_weekend<br>    }, index=dates)<br>    return df<br><br>rental_df = create_bike_rental_data()<br>time_series_data = rental_df[&#39;rentals&#39;].values<br><br># --- 2. Set Up the Forecast ---<br>horizon_len = 90  # We want to forecast the next 90 days<br>context_len = 365 # The model can see up to 365 past days<br>historical_data = time_series_data[:-horizon_len]<br>true_future_values = time_series_data[-horizon_len:]<br><br># --- 3. Initialize TimesFM and Forecast ---<br>timesfm_backend = &#39;cpu&#39; #can change to gpu for faster performance<br>tfm = timesfm.TimesFm(<br>      hparams=timesfm.TimesFmHparams(<br>          backend=timesfm_backend,<br>          per_core_batch_size=32,<br>          horizon_len=90,<br>          num_layers=50,<br>          use_positional_embedding=False,<br>          context_len=2048,<br>      ),<br>      checkpoint=timesfm.TimesFmCheckpoint(<br>          huggingface_repo_id=&quot;google/timesfm-2.0-500m-jax&quot;),<br>  )<br><br># The frequency &#39;1&#39; indicates a regular, unspecified interval (in our case, daily)<br>forecast_results, _ = tfm.forecast([historical_data], freq=[1])<br>forecast_values = forecast_results[0]<br><br># --- 4. Visualize the Results ---<br>dates = rental_df.index<br>plt.figure(figsize=(15, 7))<br>plt.plot(dates[:-horizon_len], historical_data, label=&quot;Historical Data&quot;, color=&quot;black&quot;)<br>plt.plot(dates[-horizon_len:], true_future_values, label=&quot;True Future Values&quot;, color=&quot;blue&quot;, linestyle=&#39;--&#39;)<br>plt.plot(dates[-horizon_len:], forecast_values, label=&quot;TimesFM Forecast&quot;, color=&quot;red&quot;)<br><br>plt.legend()<br>plt.title(&quot;TimesFM Zero-Shot Forecast for Daily Bike Rentals&quot;)<br>plt.xlabel(&quot;Date&quot;)<br>plt.ylabel(&quot;Number of Rentals&quot;)<br>plt.grid(True, linestyle=&#39;--&#39;, alpha=0.6)<br>plt.show()<br><br># Calculate Root Mean Squared Error (RMSE)<br>rmse = np.sqrt(np.mean((true_future_values - forecast_values)**2))<br>print(f&quot;Zero-Shot Forecast RMSE: {rmse:.2f}&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z9Y7k_xoSGg4HvTkQvlw2w.jpeg" /></figure><p>Just like that, with a few lines of code, we have a robust forecast. The model had no prior knowledge of this specific bike rental dataset but was able to identify and extrapolate the trend and seasonal patterns by leveraging its vast pre-trained knowledge.</p><h3>Leveling Up: Forecasting with Covariates</h3><p>Sometimes, the value you want to predict is influenced by other known factors. For example, ice cream sales are heavily influenced by temperature. This external factor is called a <strong>covariate</strong>.</p><p>In simple terms, this is a regression problem: Y ~ X, where you predict a target Y based on a feature X. TimesFM can incorporate these covariates to produce more accurate forecasts, provided you have the future values for those covariates.</p><pre># --- 1. Prepare Target and Covariate Data ---<br># NOTE: The covariate data MUST be available for the full period (history + horizon)<br>target_series = rental_df[&quot;rentals&quot;].values<br>temp_series = rental_df[&quot;temperature&quot;].values<br>weekend_series = rental_df[&quot;is_weekend&quot;].values<br><br># --- 2. Forecast with Covariates ---<br># The model uses historical target and historical+future covariates<br>covariate_forecast, _ = tfm.forecast_with_covariates(<br>    inputs=[target_series[:-horizon_len]],<br>    dynamic_numerical_covariates={<br>        &quot;temperature&quot;: [temp_series],<br>        &quot;is_weekend&quot;: [weekend_series],<br>    },<br>    freq=[1],<br>)<br>covariate_forecast_values = covariate_forecast[0]<br><br># --- 3. Visualize the Comparison ---<br>plt.figure(figsize=(15, 7))<br>plt.plot(dates[-horizon_len:], true_future_values, label=&quot;True Future Values&quot;, color=&quot;blue&quot;, linewidth=2.5)<br>plt.plot(dates[-horizon_len:], forecast_values, label=&quot;Zero-Shot Forecast (No Covariates)&quot;, color=&quot;red&quot;, linestyle=&#39;--&#39;)<br>plt.plot(dates[-horizon_len:], covariate_forecast_values, label=&quot;Forecast with Temp &amp; Weekend Data&quot;, color=&quot;green&quot;, linestyle=&#39;-.&#39;)<br>plt.legend()<br>plt.title(&quot;TimesFM Forecast Comparison: With and Without Covariates&quot;)<br>plt.xlabel(&quot;Date&quot;)<br>plt.ylabel(&quot;Number of Rentals&quot;)<br>plt.grid(True, linestyle=&#39;--&#39;, alpha=0.6)<br>plt.show()<br><br># Calculate RMSE for the covariate model<br>covariate_rmse = np.sqrt(np.mean((true_future_values - covariate_forecast_values)**2))<br>print(f&quot;Zero-Shot Forecast RMSE: {rmse:.2f}&quot;)<br>print(f&quot;Forecast with Covariates RMSE: {covariate_rmse:.2f}&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JmY-Zn0q9lxkukmxyvBhjQ.png" /></figure><p>Why does providing extra data like temperature and weekend information lead to a better forecast? A model that only looks at the history of bike rentals — a univariate model — is essentially working with blinders on. It can recognize that rentals spike on a seven-day cycle and rise during certain months, but it has no understanding of the underlying causes. It’s simply extrapolating past patterns. When we introduce covariates, we remove the blinders. The model is no longer just seeing a pattern; it’s learning a relationship: higher temperatures correlate with more rentals, and weekend days have a fundamentally different demand profile than weekdays. This allows for more nuanced and accurate predictions. For example, if a future Saturday has a forecast for unusually cold weather, the model can intelligently lower its rental prediction for that day, something a univariate model would struggle with. In essence, covariates transform unexplained variance (or ‘noise’) in the historical data into a predictable signal, grounding the forecast in the real-world factors that drive demand.</p><h3>For Maximum Performance: Finetuning</h3><p>While TimesFM’s zero-shot performance is its most impressive feature, you can squeeze out even more accuracy by <strong>finetuning</strong> it on your specific dataset.</p><p>Finetuning means taking the pre-trained model and training it for a few more epochs on your own data. This allows the model to adapt its generalized knowledge to the specific nuances, scale, and idiosyncrasies of your time series.</p><p>The paper demonstrates that finetuning on just 10% of a dataset can lead to state-of-the-art results, significantly outperforming models trained from scratch.</p><p>While the timesfm library doesn’t provide a simple high-level .finetune() API in its initial release, the conceptual process would look like this using a framework like JAX or PyTorch</p><pre># 1. Load the pre-trained model<br>model = load_pretrained_timesfm_model()<br>optimizer = create_optimizer(model.parameters())<br><br># 2. Prepare your specific training data<br># For each sample, you need the historical context and the true future values<br>train_data_loader = prepare_finetuning_data(my_historical_data, my_future_data)<br><br># 3. Run the training loop<br>for epoch in range(num_epochs):<br>    for context, ground_truth_horizon in train_data_loader:<br>        # Forward pass: get the model&#39;s prediction<br>        predicted_horizon = model(context)<br><br>        # Calculate loss (e.g., Mean Squared Error)<br>        loss = mse_loss(predicted_horizon, ground_truth_horizon)<br><br>        # Backward pass: update model weights<br>        optimizer.zero_grad()<br>        loss.backward()<br>        optimizer.step()<br><br>    print(f&quot;Epoch {epoch+1}, Loss: {loss.item()}&quot;)<br><br># 4. Save your newly finetuned model<br>save_model(model, &quot;my_finetuned_timesfm.chkpt&quot;)</pre><p>Finetuning is the path to take when you have a critical forecasting task and need the absolute best performance possible.</p><p>For those interested in the technical details, the <a href="https://arxiv.org/pdf/2310.10688">paper</a> by Das et. al. reveals a few more insights:</p><ul><li><strong>Scaling Laws:</strong> Just like LLMs, TimesFM benefits from scale. The researchers found a clear trend: as the model size (parameters) and the amount of computation (FLOPS) increase, the forecasting error consistently decreases. This suggests that future, even larger versions of TimesFM could be more powerful still.</li><li><strong>The Importance of Synthetic Data:</strong> An ablation study showed that training without the synthetic dataset caused a performance drop, especially on datasets with uncommon granularities (like quarterly or 10-minute intervals). This highlights the crucial role synthetic data plays in teaching the model to be a true generalist.</li><li><strong>Autoregressive Decoding Matters:</strong> The paper confirmed that for long horizons, generating a larger output patch in one go is more effective than predicting one single point at a time. This is a key design decision that differentiates TimesFM from standard autoregressive models.</li><li>The journey for time series foundation models is just beginning. The researchers outline several exciting avenues for future work:</li><li><strong>Probabilistic Forecasting:</strong> The current model provides a point forecast. A future version could predict a range of possible outcomes (quantiles), which is crucial for risk management.</li><li><strong>Advanced Covariate Handling:</strong> Pre-training the model from scratch with covariate data could make its core reasoning about external factors even more powerful.</li><li><strong>Prompting:</strong> Exploring how to “prompt” a time series model — perhaps by providing examples or instructions in-context — is a fascinating open question.</li></ul><h3>Conclusion</h3><p>TimesFM represents a significant milestone in the field of time series analysis. By successfully applying the principles of large-scale pre-training from the world of NLP, Google has created a powerful, versatile, and accessible forecasting tool.</p><p>Its strong <strong>zero-shot performance</strong> democratizes forecasting, allowing practitioners to get reliable predictions without deep domain expertise or extensive model training. Its ability to incorporate <strong>covariates</strong> and be <strong>finetuned</strong> provides a path to state-of-the-art results for mission-critical applications.</p><p>As foundation models for time series continue to evolve, they promise to unlock new capabilities and make sophisticated forecasting more accessible than ever before. TimesFM is a powerful first step into that future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3da90d07bdd7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Instruction tuning Gemma 3n for low resource languages with Google Translate and Vertex AI…]]></title>
            <link>https://medium.com/@siduojiang/instruction-tuning-gemma-3n-for-low-resource-languages-with-google-translate-and-vertex-ai-3a88fefe7997?source=rss-94a523ac43db------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a88fefe7997</guid>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Siduo Jiang]]></dc:creator>
            <pubDate>Tue, 16 Sep 2025 23:20:10 GMT</pubDate>
            <atom:updated>2025-09-16T23:20:10.157Z</atom:updated>
            <content:encoded><![CDATA[<h3>Instruction tuning Gemma 3n for low resource languages with Google Translate and Vertex AI Workbench</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5e1O1MhU6nkyBVi55-CTg.png" /></figure><p>Large Language Models (LLMs) are rapidly advancing, but their performance often varies across different languages. This is especially true for low-resource languages, which have less data available for training. In this blog post, we’ll explore how to enhance the performance of Google’s Gemma 3n model on the Global MMLU benchmark, with a focus on Swahili. We’ll cover the entire process, from data preparation using the Google Translate API to efficient model training.</p><h3>Why Performance Can Be Improved for Low-Resource Languages</h3><p>The internet is predominantly English, which means that most LLMs are trained on a massive amount of English text. This creates a “digital divide” where models perform exceptionally well in English but lag behind in other languages. For low-resource languages, this gap is even more pronounced due to the scarcity of high-quality training data. By strategically creating and using translated datasets, we can significantly improve an LLM’s understanding and generation capabilities in these languages.</p><h3>What is Fine-tuning with LoRA?</h3><p>Fine-tuning is the process of taking a pre-trained model and further training it on a smaller, task-specific dataset. This adapts the model to a particular domain or style. However, finetuning a large model can be computationally expensive. This is where Low-Rank Adaptation (LoRA) comes in. LoRA is a parameter-efficient fine-tuning (PEFT) technique that freezes the original model weights and injects trainable, low-rank matrices into the model’s layers. This dramatically reduces the number of trainable parameters, making the finetuning process faster and more memory-efficient without sacrificing performance.</p><h3>What is Instruction Tuning and How it Differs from Pretraining?</h3><p>Pretraining is the initial, unsupervised learning phase where an LLM learns general language patterns from a vast corpus of text. In contrast, instruction tuning is a form of supervised fine-tuning that teaches the model to follow instructions. It involves training the model on a dataset of instructions and their desired outputs. This process aligns the model’s behavior with human expectations, making it more helpful and capable of performing a wide range of tasks based on natural language prompts.</p><h3>Global MMLU and MMLU</h3><p>The Massive Multitask Language Understanding (<a href="https://arxiv.org/abs/2009.03300">MMLU</a>) is a benchmark designed to evaluate the knowledge and problem-solving abilities of LLMs across 57 diverse subjects, from humanities to STEM. The <a href="https://arxiv.org/abs/2412.03304">Global MMLU</a> is an extension of this benchmark, offering a multilingual version to assess model capabilities in various languages, including Swahili. This allows for a more comprehensive and equitable evaluation of LLMs across different linguistic and cultural contexts.</p><p>The MMLU training dataset consists of multiple-choice questions from a wide array of academic and professional domains. The dataset is designed to be challenging, with questions requiring deep knowledge and reasoning skills. The original MMLU dataset is in English, which presents an opportunity for us to create a high-quality translated version for our Swahili instruction tuning task.</p><h3>Using the Google Translate API for Data Preparation</h3><p>To prepare our training data, we’ll translate the English MMLU dataset into Swahili using the Google Translate API. This powerful tool allows for programmatic translation of text between numerous languages. The data preparation phase is essential for adapting the model, which involved translating the English MMLU dataset into Swahili to create a new corpus of instruction-response pairs. Each translated question and answer is structured into a conversational format using specific tokens like &lt;start_of_turn&gt;user and &lt;start_of_turn&gt;model. This templating clearly demarcated the user’s prompt from the model’s desired output, explicitly teaching the model how to recognize and correctly follow instructions within a conversational context.</p><pre>from google.cloud import translate_v2 as translate<br><br># Initialize the client<br>translate_client = translate.Client()<br><br>def prepare_and_translate_data(question_json):<br>    &quot;&quot;&quot;Formats and translates a single MMLU question.&quot;&quot;&quot;<br><br>    # Formulate the question and choices<br>    question_text = question_json[&#39;question&#39;]<br>    choices = [f&quot;{chr(65+i)}. {option}&quot; for i, option in enumerate(question_json[&#39;choices&#39;])]<br>    formatted_question = f&quot;{question_text}\n&quot; + &quot;\n&quot;.join(choices)<br><br>    # Formulate the answer<br>    answer_index = question_json[&#39;answer&#39;]<br>    correct_choice = choices[answer_index]<br>    formatted_answer = f&quot;The correct answer is {correct_choice}&quot;<br><br>    # Translate to Swahili<br>    translated_question = translate_client.translate(formatted_question, target_language=&#39;sw&#39;)[&#39;translatedText&#39;]<br>    translated_answer = translate_client.translate(formatted_answer, target_language=&#39;sw&#39;)[&#39;translatedText&#39;]<br><br>    # Create the final instruction-tuning format<br>    return f&quot;&lt;start_of_turn&gt;user\n{translated_question}&lt;end_of_turn&gt;\n&lt;start_of_turn&gt;model\n{translated_answer}&lt;end_of_turn&gt;\n&quot;<br><br># Example usage with a dummy JSON object<br>example_question = {<br>    &quot;question&quot;: &quot;What is the capital of Kenya?&quot;,<br>    &quot;choices&quot;: [&quot;Kampala&quot;, &quot;Nairobi&quot;, &quot;Dodoma&quot;, &quot;Kigali&quot;],<br>    &quot;answer&quot;: 1<br>}</pre><h3>Setting Up a Training Run with Unsloth</h3><p><a href="https://unsloth.ai/">Unsloth</a> is a library that significantly speeds up LLM fine-tuning while reducing memory usage. It seamlessly integrates with popular libraries like Hugging Face’s Transformers and TRL.</p><p>Here’s how you can set up a training run for our Swahili-tuned Gemma 3n model.</p><pre>import torch<br>from unsloth import FastLanguageModel<br>from datasets import load_dataset<br>from trl import SFTTrainer<br>from transformers import TrainingArguments, BitsAndBytesConfig<br><br># Define key constants<br>MODEL_NAME = &quot;unsloth/gemma-3n-E4B-it&quot;<br>DATASET_NAME = &quot;your_swahili_mmlu_dataset&quot;<br>MAX_SEQ_LEN = 4096<br>LORA_R = 32 # Increased LoRA rank<br>OUTPUT_DIR = &quot;outputs_v2&quot;<br><br># --- 1. Load Model and Tokenizer ---<br>model, tokenizer = FastLanguageModel.from_pretrained(<br>    max_seq_length = MAX_SEQ_LEN,<br>    model_name = MODEL_NAME,<br>    load_in_4bit = True,<br>    dtype = None, # dtype = None lets Unsloth auto-select (usually bfloat16)<br>    device_map = &quot;auto&quot;, # New: Automatically map model layers across devices<br>)<br><br># --- 2. Configure PEFT (LoRA) ---<br>model = FastLanguageModel.get_peft_model(<br>    model,<br>    r = LORA_R,<br>    lora_dropout = 0.05, # Changed from 0<br>    target_modules = [ # Module list remains the same<br>        &quot;q_proj&quot;, &quot;k_proj&quot;, &quot;v_proj&quot;, &quot;o_proj&quot;,<br>        &quot;gate_proj&quot;, &quot;up_proj&quot;, &quot;down_proj&quot;,<br>    ],<br>    lora_alpha = LORA_R * 2, # Common practice: alpha = 2 * r<br>    task_type = &quot;CAUSAL_LM&quot;, <br>    bias = &quot;none&quot;,<br>    random_state = 1337, <br>)<br><br># --- 3. Load Datasets ---<br>dataset_train = load_dataset(<br>    DATASET_NAME,<br>    split = &quot;train&quot;,<br>    streaming = True, <br>)<br>dataset_eval = load_dataset(<br>    DATASET_NAME,<br>    split = &quot;test&quot;, <br>    streaming = True,<br>)<br><br><br>def formatting_function(example):<br>    # This uses the built-in Gemma instruction template<br>    prompt = [<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: f&quot;{example[&#39;instruction&#39;]}\n{example[&#39;input&#39;]}&quot;},<br>        {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: example[&#39;output&#39;]}<br>    ]<br>    return tokenizer.apply_chat_template(prompt, tokenize=False) + tokenizer.eos_token<br><br><br># --- 4. Define Training Arguments ---<br>training_args = TrainingArguments(<br>    output_dir = OUTPUT_DIR,<br>    max_steps = 250,                   <br>    warmup_steps = 20,                 <br>    per_device_train_batch_size = 2,<br>    seed = 42,<br>    gradient_accumulation_steps = 8,   <br>    optim = &quot;adamw_bnb_8bit&quot;,          <br>    learning_rate = 1.5e-4,            <br>    logging_steps = 5,<br>    fp16 = not torch.cuda.is_bf16_supported(),<br>    bf16 = torch.cuda.is_bf16_supported(),<br>    evaluation_strategy = &quot;steps&quot;,     <br>    eval_steps = 50,                   <br>    save_strategy = &quot;steps&quot;,           <br>    save_steps = 50,                   <br>    lr_scheduler_type = &quot;cosine&quot;,     <br>    weight_decay = 0.01,            <br>    report_to = &quot;tensorboard&quot;,      <br>    per_device_eval_batch_size = 2,    <br>    save_total_limit = 3,              <br>    load_best_model_at_end = True,    <br>)<br><br><br># --- 5. Initialize the Trainer ---<br>trainer = SFTTrainer(<br>    model = model,<br>    args = training_args,<br>    train_dataset = dataset_train,<br>    tokenizer = tokenizer,<br>    max_seq_length = MAX_SEQ_LEN,<br>    <br>    eval_dataset = dataset_eval,       <br>    packing = True,                   <br>    formatting_func = formatting_function, <br>)<br><br># --- 6. Start Training ---<br>print(&quot;Starting model training...&quot;)<br>trainer.train()<br><br>print(&quot;Training complete. Saving final model.&quot;)<br>model.save_pretrained(f&quot;{OUTPUT_DIR}/final_model&quot;) # Save final adapter</pre><h3>Machine Size and Hardware Considerations</h3><p>Fine-tuning LLMs is resource-intensive, but with optimizations like LoRA and Unsloth, it’s become more accessible. For this project, a high-performance GPU like the NVIDIA A100 is recommended. While a single A100 is sufficient for this task, for even larger models or faster training, Google’s TPUs can offer a significant performance boost. For more on this, check out the <a href="https://medium.com/google-cloud/using-googles-ai-hypercomputer-b149ad3fe3e7">Medium article on “Using Google’s AI Hypercomputer”</a> by <a href="https://medium.com/u/c2acc24502d0">Brady Dice</a>.</p><h3>Evaluating Performance on the Global MMLU Swahili Set</h3><p>After fine-tuning, it’s crucial to evaluate the model’s performance on the Swahili portion of the Global MMLU test set. This will give us a clear indication of how much our instruction tuning has improved its capabilities.</p><p>Here’s a simplified code snippet for running inference.</p><pre>from transformers import TextStreamer<br><br>def run_inference(model, tokenizer, question):<br>    &quot;&quot;&quot;Generates an answer to a given question.&quot;&quot;&quot;<br><br>    messages = [<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: question}<br>    ]<br><br>    inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors=&quot;pt&quot;).to(&quot;cuda&quot;)<br><br>    streamer = TextStreamer(tokenizer, skip_prompt=True)<br><br>    _ = model.generate(inputs, streamer=streamer, max_new_tokens=128)<br><br><br># Example Swahili question from the Global MMLU test set<br>swahili_question = &quot;Ni mji mkuu gani wa Tanzania? A. Nairobi, B. Kampala, C. Dodoma, D. Kigali&quot;<br><br>run_inference(model, tokenizer, swahili_question)</pre><h3>Conclusion</h3><p>Instruction tuning LLMs like Gemma 3n for low-resource languages is a powerful way to make AI more equitable and accessible. By leveraging tools like the Google Translate API for data creation and Unsloth for efficient training, we can significantly improve model performance on benchmarks like the Global MMLU. This not only enhances the model’s capabilities in a specific language but also contributes to the broader goal of building more inclusive and culturally aware AI systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a88fefe7997" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Agentic Framework for Frontend Testing with Gemini and ADK]]></title>
            <link>https://medium.com/@siduojiang/agentic-framework-for-frontend-testing-with-gemini-and-adk-396b0add0c43?source=rss-94a523ac43db------2</link>
            <guid isPermaLink="false">https://medium.com/p/396b0add0c43</guid>
            <category><![CDATA[agents]]></category>
            <dc:creator><![CDATA[Siduo Jiang]]></dc:creator>
            <pubDate>Tue, 16 Sep 2025 21:08:10 GMT</pubDate>
            <atom:updated>2025-09-16T21:08:10.133Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XMoRPMNp9tTl-W3B1OWO8w.png" /></figure><p>In modern applications, the frontend is more than an interface; it is the entire user experience (UX). While a backend may be functionally sound, any failure in the frontend — a broken button, a non-responsive filter, or a layout error — results in a direct and immediate failure for the user.</p><p>Rigorous frontend testing ensures that data is not only processed correctly but also presented intuitively and accurately. It validates that user interactions lead to the expected outcomes, building user trust and ensuring the application’s accessibility and reliability. Failure to comprehensively test the UI risks user frustration, data integrity issues, and abandonment of the service.</p><h3>The Difficulties of Modern Frontend Testing</h3><p>Traditional automated testing is often brittle and insufficient for contemporary web applications for several key reasons:</p><ul><li><strong>Dynamic Content:</strong> Modern frameworks (like React) dynamically load content, panels, and filter options asynchronously. Elements often do not exist in the Document Object Model (DOM) until after a specific user interaction (like a click) occurs. This forces testers to write complex and unreliable “wait” conditions.</li><li><strong>Encapsulation (Shadow DOM):</strong> A primary challenge, highlighted in the demonstration video, is the <strong>Shadow DOM</strong>. This is a web standard allowing developers to encapsulate a component’s structure, style, and behavior, effectively hiding it from the main document DOM. Standard Selenium selectors (like XPath or CSS selectors) cannot pierce this “shadow boundary,” rendering encapsulated components a “black box” to traditional automation scripts.</li><li><strong>Complex State:</strong> UIs are complex state machines. A selection in one filter may dynamically change the available options in another. Manually scripting every possible permutation is exceptionally time-consuming and prone to breaking with minor UI updates.</li></ul><h3>The Role of Selenium</h3><p><strong>Selenium</strong> is the industry-standard, open-source framework for browser automation. It provides the necessary tools to programmatically control a web browser (such as Firefox, as used in the provided scripts) just as a human user would.</p><p>In this framework, Selenium functions as the “hands” of the operation. While Gemini and the agentic framework serve as the “brain” — deciding what to click and validating the outcome — Selenium executes those decisions. It performs the core actions of loading the URL, finding elements, clicking buttons, selecting options from dropdowns, and executing JavaScript.</p><h3>A Gemini-Powered, Multi-Agent ADK Framework</h3><p>This solution moves beyond linear scripts by implementing a collaborative team of specialized AI agents. This system, built using Gemini and ADK, can discover, analyze, interact with, and validate a complex UI, including components that are traditionally “untestable.”</p><p>The framework is composed of the following agents:</p><h4>Agent 1: The Scraper &amp; Initializer</h4><p>This agent is responsible for loading the target URL and performing an initial scrape of all relevant assets (HTML, JavaScript, and CSS files). This provides the initial “world state” and context for the evaluation agents. This agent also initiates critical first interactions, such as clicking shadow DOM buttons, to ensure hidden panels are exposed for analysis.</p><pre>def agent1(driver, url):<br>    &#39;&#39;&#39; Get HTML using the extract_html function &#39;&#39;&#39;<br>    session = requests.Session()<br>    session.headers[&quot;User-Agent&quot;] = &quot;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36&quot;<br><br>    # get the HTML content<br>    html = session.get(url).content<br>    driver.get(url)<br>    <br>    #replace with shadow dom detector<br>    click_shadow_button(driver)<br>    <br>    # parse HTML using beautiful soup<br>    soup = bs(html, &quot;html.parser&quot;)<br><br>    # ... [Code to scrape JS and CSS files] ...<br><br>    html = driver.page_source <br>    final_response = html + &#39;\n&#39; + &#39;\n&#39;.join(script_files) + &#39;\n&#39;.join(css_files)<br>    <br>    return html, &#39;\n&#39;.join(script_files), &#39;\n&#39;.join(css_files)</pre><h4>Agent 2: The DOM Evaluator</h4><p>This agent acts as the primary “brain.” It receives the complete scraped content from Agent 1 and uses a Gemini prompt to analyze the DOM. Its purpose is to identify all interactive elements (buttons and selects), predict their function, and determine if an interaction will (or should) result in a visual data change. This output, structured as a JSON list, serves as the test plan for the other agents.</p><pre>import json<br>from pydantic import BaseModel, Field<br>from google.adk.agents import LlmAgent<br><br># Define the expected JSON output structure using Pydantic<br>class ClickableElement(BaseModel):<br>    &quot;&quot;&quot;Defines the structure for a single clickable element.&quot;&quot;&quot;<br>    element_id: str = Field(..., description=&quot;ID of the button or selectable element.&quot;)<br>    action: str = Field(..., description=&quot;The type of interaction, such as &#39;click&#39; or &#39;change&#39;.&quot;)<br>    outcome: str = Field(..., description=&quot;The expected outcome of clicking the button or changing the selection.&quot;)<br>    data_change: str = Field(..., description=&quot;Indicates if the data shown on the page is affected (&#39;Yes&#39; or &#39;No&#39;).&quot;)<br><br>class PageAnalysis(BaseModel):<br>    &quot;&quot;&quot;Defines the root structure of the JSON output.&quot;&quot;&quot;<br>    elements: list[ClickableElement]<br><br># Create the ADK agent<br>html_analyzer_agent = LlmAgent(<br>    name=&quot;html_clickable_element_analyzer&quot;,<br>    description=&quot;Analyzes HTML to find clickable and selectable elements and their expected outcomes.&quot;,<br>    instruction=&quot;&quot;&quot;Given the following HTML, identify all clickable buttons and selectable elements.<br>    For each element, determine the expected outcome of interacting with it, and specify whether this interaction<br>    will affect the data displayed on the page.<br>    <br>    Return the results in a JSON format that adheres to the provided schema. The JSON object should have a single key &quot;elements&quot; which is a list of objects, where each object has the following keys: &quot;element_id&quot;, &quot;action&quot;, &quot;outcome&quot;, and &quot;data_change&quot;.<br>    &quot;&quot;&quot;,<br>    output_schema=PageAnalysis,<br>)<br><br>def agent2(html_content: str) -&gt; PageAnalysis:<br>    &quot;&quot;&quot;<br>    Runs the ADK agent to analyze the provided HTML content.<br>    &quot;&quot;&quot;<br>    response = runner.run(agent=html_analyzer_agent, inputs={&quot;html&quot;: html_content})<br>    return PageAnalysis(elements=[])</pre><h4>Agent 3: The Script Executor (State Changer)</h4><p>This agent handles interactions identified by Agent 2 as “non-data changing.” Its sole purpose is to execute clicks that alter the UI state, such as opening a filter panel or revealing a hidden menu. As shown in the video diagram, this agent works in an iterative loop with Agent 2, clicking elements and re-feeding the new DOM state back for evaluation until all interactive elements are exposed.</p><pre>import time<br>from pydantic import BaseModel, Field<br>from selenium import webdriver<br>from selenium.webdriver.support.ui import Select<br>from selenium.webdriver.common.by import By<br><br>from google.adk.tools import Tool<br>from google.adk.agents import LlmAgent<br><br><br>driver = webdriver.Chrome()<br><br># --- Tool Implementations ---<br>def find_options(element_id: str) -&gt; list[str]:<br>    &quot;&quot;&quot;<br>    Finds a &lt;select&gt; element by its ID and returns a list of its &lt;option&gt; values.<br>    &quot;&quot;&quot;<br>    try:<br>        select_element = Select(driver.find_element(By.ID, element_id))<br>        options = [option.get_attribute(&#39;value&#39;) for option in select_element.options]<br>        return options<br>    except Exception as e:<br>        return [f&quot;Error finding options: {e}&quot;]<br><br>def select_option(element_id: str, option_value: str) -&gt; str:<br>    &quot;&quot;&quot;<br>    Selects an option in a dropdown by its value and returns the new page source HTML.<br>    &quot;&quot;&quot;<br>    try:<br>        select_element = Select(driver.find_element(By.ID, element_id))<br>        select_element.select_by_value(option_value)<br>        # Wait for potential AJAX/dynamic content to load<br>        time.sleep(2)<br>        return driver.page_source<br>    except Exception as e:<br>        return f&quot;Error selecting option: {e}&quot;<br><br># --- ADK Tool Definitions ---<br>find_select_options_tool = Tool(<br>    name=&quot;find_select_options&quot;,<br>    description=&quot;Get all of the available &lt;option&gt; values for a given &lt;select&gt; element ID.&quot;,<br>    func=find_options,<br>)<br><br>select_option_and_get_html_tool = Tool(<br>    name=&quot;select_option_and_get_html&quot;,<br>    description=&quot;Select a specific option for a given &lt;select&gt; element and return the page&#39;s resulting HTML.&quot;,<br>    func=select_option,<br>)<br><br># --- Pydantic Schemas for Structured Output ---<br>class InteractionResult(BaseModel):<br>    &quot;&quot;&quot;Describes the outcome of selecting a single dropdown option.&quot;&quot;&quot;<br>    selected_option: str = Field(..., description=&quot;The value of the option that was selected.&quot;)<br>    change_summary: str = Field(..., description=&quot;A summary of how the page content changed after this option was selected.&quot;)<br><br>class AnalysisResult(BaseModel):<br>    &quot;&quot;&quot;The final analysis containing a list of all interaction results.&quot;&quot;&quot;<br>    results: list[InteractionResult]<br><br># --- ADK Agent Definition ---<br>web_interaction_agent = LlmAgent(<br>    name=&quot;web_data_change_analyzer&quot;,<br>    description=&quot;Interacts with a dropdown menu on a webpage, observes the changes, and reports the results.&quot;,<br>    instruction=&quot;&quot;&quot;You will be given the initial HTML of a webpage and the ID of a &#39;select&#39; element.<br>    1. First, use the `find_select_options` tool to get a list of all available options for the element.<br>    2. For each option in that list, you must use the `select_option_and_get_html` tool to apply the change and get the new page HTML.<br>    3. Compare the initial HTML with the new HTML returned by the tool. Based on this comparison, write a concise summary of what data or elements on the page changed.<br>    4. After iterating through all options, return the complete list of results using the provided JSON schema.<br>    &quot;&quot;&quot;,<br>    tools=[find_select_options_tool, select_option_and_get_html_tool],<br>    output_schema=AnalysisResult,<br>)<br><br>def agent3(initial_html: str, element_id: str, page_url: str) -&gt; AnalysisResult:<br>    response = runner.run(<br>      agent=web_interaction_agent,<br>      inputs={<br>          &quot;initial_html&quot;: initial_html,<br>          &quot;element_id&quot;: element_id<br>      }<br>    )<br>    return AnalysisResult(results=[])</pre><h4>Agent 4: The Change &amp; Validation Agent (The “Tester”)</h4><p>This is the core data validation agent. For every element Agent 2 marked as a “data_change” filter (like a dropdown menu), Agent 4 is invoked to:</p><ul><li><strong>Identify Options:</strong> Use a Gemini prompt to parse the HTML and list all available options within that filter (e.g., “2021 | 2022 | 2023”).</li><li><strong>Iterate and Select:</strong> Use Selenium to methodically select every single option.</li><li><strong>Validate Change:</strong> After each selection, it scrapes the new DOM state and calls the compare_prompts function. This validation step uses another Gemini call to semantically compare the <em>before</em> and <em>after</em> HTML, determining if the application behaved as expected for that specific filter value.</li></ul><pre>def agent4(driver: WebDriver, element_id: str, initial_html: str, model) -&gt; list[str]:<br>    all_results = []<br><br>    # 1. Deterministically find all options for the dropdown using Selenium.<br>    try:<br>        select_element = Select(driver.find_element(By.ID, element_id))<br>        options = [option.get_attribute(&#39;value&#39;) for option in select_element.options if option.get_attribute(&#39;value&#39;)]<br>        if not options:<br>            return [f&quot;Error: No options with &#39;value&#39; attributes found for element with ID &#39;{element_id}&#39;.&quot;]<br>    except NoSuchElementException:<br>        return [f&quot;Error: Could not find a &lt;select&gt; element with ID &#39;{element_id}&#39;.&quot;]<br><br>    # 2. Iterate through each option, select it, and get the resulting HTML.<br>    for option_value in options:<br>        try:<br>            # Re-find the element in each loop to avoid stale element references<br>            select_field = Select(driver.find_element(By.ID, element_id))<br>            <br>            # Select the option by its value<br>            select_field.select_by_value(option_value)<br>            <br>            # Wait for dynamic content (e.g., AJAX) to load<br>            time.sleep(2)<br>            <br>            # Get the new page source<br>            new_html = driver.page_source<br><br>            # 3. Use the AI model to compare the &#39;before&#39; and &#39;after&#39; HTML states.<br>            prompt = f&quot;&quot;&quot;<br>            Analyze the difference between the two HTML documents provided below to understand what changed.<br>            An action was performed on the page: a dropdown filter with ID &#39;{element_id}&#39; was set to the value &#39;{option_value}&#39;.<br>            <br>            Your task is to provide a concise, one-sentence summary of the change observed. <br>            For example: &quot;The list of products updated to show only electronics.&quot; or &quot;The chart was regenerated with new data.&quot;<br><br>            --- INITIAL HTML ---<br>            {initial_html}<br><br>            --- NEW HTML ---<br>            {new_html}<br>            &quot;&quot;&quot;<br>            <br>            response = model.invoke(prompt)<br>            change_summary = response.content.strip()<br>            <br>            # Append the formatted result<br>            all_results.append(f&quot;Selected value: {option_value}; RESULT: {change_summary}&quot;)<br><br>        except Exception as e:<br>            all_results.append(f&quot;Selected value: {option_value}; ERROR: Failed to process this option. Details: {e}&quot;)<br>            # Optionally, you might want to refresh the page here to recover<br>            # driver.refresh()<br>            # time.sleep(2)<br><br>    return all_results</pre><h4>Agent 5: The Shadow DOM Specialist</h4><p>This agent is purpose-built to test the encapsulated Shadow DOM elements that other agents cannot access. Instead of using standard Selenium selectors, this agent uses driver.execute_script() to inject raw JavaScript into the browser. This JavaScript can pierce the shadow boundary, query elements within the shadow root (host.shadowRoot), and programmatically trigger change events, allowing the framework to test these &quot;hidden&quot; components just like any other element.</p><pre>def agent5(driver, years):<br>    results = []<br>    ...<br>    cur_html = str(driver.page_source)<br>    for year in years:<br>        # Inject JavaScript to control elements inside the Shadow DOM<br>        res = driver.execute_script(&quot;&quot;&quot;<br>            const host = document.querySelector(&#39;custom-year-filter&#39;);<br>            const shadow = host.shadowRoot;<br>            const select = shadow.querySelector(&#39;.shadow-year-select&#39;);<br>            if (select) {<br>                select.value = arguments[0];<br>                // Trigger change event<br>                select.dispatchEvent(new Event(&#39;change&#39;, { bubbles: true }));<br>                return true;<br>            }<br>            return false;<br>        &quot;&quot;&quot;, str(year))<br>        <br>        new_source = driver.page_source<br>        # Use the same Gemini validation function to confirm the outcome<br>        res = compare_prompts(cur_html, new_source, &#39;Shadow DOM&#39;, year)<br>        <br>        results.append(f&quot;Change field selected: Shadow DOM; Selected value: {year}; RESULT: {res}&quot;)<br>    return results</pre><h4>Running the Agentic Test Framework!</h4><p>The execution flow orchestrates these specialized agents in a logical sequence. The main script block initializes a global list to store all test results, then systematically calls each agent. It begins with Agent 1 and 2 to get the test plan. It then iterates through that plan, using Agent 3 to click UI-only elements and Agent 4 to test and validate data-changing filters. A separate, robust loop is dedicated to running the Agent 5 Shadow DOM tests. Finally, all results are aggregated and passed to a final Gemini prompt to generate a comprehensive, human-readable report.</p><pre>from pydantic import BaseModel, Field<br>from google.adk.agents import LlmAgent<br><br># Define the structured output for the final report<br>class TestResult(BaseModel):<br>    test_run: str = Field(description=&quot;The name or description of the test case.&quot;)<br>    test_steps: str = Field(description=&quot;Steps taken during the test.&quot;)<br>    test_outcome: str = Field(description=&quot;Observed outcome of the test.&quot;)<br>    status: str = Field(description=&quot;Final status: &#39;Success&#39; or &#39;Fail&#39;.&quot;)<br><br>class FinalReport(BaseModel):<br>    results: list[TestResult]<br><br># Define the orchestrating agent<br>web_test_coordinator_agent = LlmAgent(<br>    name=&quot;web_test_coordinator&quot;,<br>    instruction=&quot;&quot;&quot;<br>    First, scrape the initial page state from &#39;https://ui-testing-dev.web.app&#39;.<br>    Next, create a test plan by identifying all interactive elements.<br>    Execute the plan by testing UI clicks, then data-changing elements, and finally the shadow DOM.<br>    Aggregate the results from all tests and generate a final, structured summary report.<br>    &quot;&quot;&quot;,<br>    tools=[scrape_tool, create_plan_tool, click_ui_tool, test_data_tool, test_shadow_dom_tool],<br>    output_schema=FinalReport,<br>)</pre><h3>Conclusion</h3><p>This multi-agent framework represents a significant advancement over traditional, hard-coded testing scripts. By leveraging a team of specialized AI agents powered by Gemini, the system moves from brittle execution to intelligent automation.</p><p>This framework can <strong>discover</strong> a UI, <strong>understand</strong> the functional purpose of its components, and <strong>adapt</strong> to complex interactions like dynamic panels and encapsulated Shadow DOM elements. The final Gemini evaluator aggregates these individual test results into a comprehensive summary report, validating the end-to-end user experience and enabling the delivery of more resilient, reliable, and user-friendly applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=396b0add0c43" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>