From 2502e1b862a0863338aceacfacd5c68e7c99ff55 Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Wed, 11 Feb 2026 13:49:50 +0530 Subject: [PATCH] add go files --- .../go.integration.cloudbuild.yaml | 57 +++++++++++++++++++ docs/en/samples/pre_post_processing/go.md | 38 +++++++++++++ .../pre_post_processing/go/adk/agent.go | 1 + 3 files changed, 96 insertions(+) create mode 100644 .ci/sample_tests/pre_post_processing/go.integration.cloudbuild.yaml create mode 100644 docs/en/samples/pre_post_processing/go.md diff --git a/.ci/sample_tests/pre_post_processing/go.integration.cloudbuild.yaml b/.ci/sample_tests/pre_post_processing/go.integration.cloudbuild.yaml new file mode 100644 index 0000000000..75528a9710 --- /dev/null +++ b/.ci/sample_tests/pre_post_processing/go.integration.cloudbuild.yaml @@ -0,0 +1,57 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +steps: + - name: "${_IMAGE}" + id: "py-pre-post-processing-test" + entrypoint: "bash" + args: + - -c + - | + set -ex + chmod +x .ci/sample_tests/run_tests.sh + .ci/sample_tests/run_tests.sh + env: + - "CLOUD_SQL_INSTANCE=${_CLOUD_SQL_INSTANCE}" + - "GCP_PROJECT=${_GCP_PROJECT}" + - "DATABASE_NAME=${_DATABASE_NAME}" + - "DB_USER=${_DB_USER}" + - "TARGET_ROOT=${_TARGET_ROOT}" + - "TARGET_LANG=${_TARGET_LANG}" + - "TABLE_NAME=${_TABLE_NAME}" + - "SQL_FILE=${_SQL_FILE}" + - "AGENT_FILE_PATTERN=${_AGENT_FILE_PATTERN}" + secretEnv: ["TOOLS_YAML_CONTENT", "GOOGLE_API_KEY", "DB_PASSWORD"] + +availableSecrets: + secretManager: + - versionName: projects/${_GCP_PROJECT}/secrets/${_TOOLS_YAML_SECRET}/versions/5 + env: "TOOLS_YAML_CONTENT" + - versionName: projects/${_GCP_PROJECT_NUMBER}/secrets/${_API_KEY_SECRET}/versions/latest + env: "GOOGLE_API_KEY" + - versionName: projects/${_GCP_PROJECT}/secrets/${_DB_PASS_SECRET}/versions/latest + env: "DB_PASSWORD" + +timeout: 1200s + +substitutions: + _TARGET_LANG: "go" + _IMAGE: "gcr.io/google.com/cloudsdktool/cloud-sdk:537.0.0" + _TARGET_ROOT: "docs/en/samples/pre_post_processing/go" + _TABLE_NAME: "hotels_go_pre_post_processing" + _SQL_FILE: ".ci/sample_tests/setup_hotels.sql" + _AGENT_FILE_PATTERN: "agent.go" + +options: + logging: CLOUD_LOGGING_ONLY diff --git a/docs/en/samples/pre_post_processing/go.md b/docs/en/samples/pre_post_processing/go.md new file mode 100644 index 0000000000..2675b4545d --- /dev/null +++ b/docs/en/samples/pre_post_processing/go.md @@ -0,0 +1,38 @@ +--- +title: "Go" +type: docs +weight: 3 +description: > + How to add pre- and post- processing to your Agents using Go. +--- + +## Prerequisites + +This tutorial assumes that you have set up Toolbox with a basic agent as described in the [local quickstart](../../getting-started/local_quickstart_go.md). + +This guide demonstrates how to implement these patterns in your Toolbox applications. + +## Implementation + +{{< tabpane persist=header >}} +{{% tab header="ADK" text=true %}} +The following example demonstrates how to use the `beforeToolCallback` and `afterToolCallback` hooks in the ADK `LlmAgent` to implement pre and post processing logic. + +```go +{{< include "go/adk/agent.go" >}} +``` + +You can also add model-level (`beforeModelCallback`, `afterModelCallback`) and agent-level (`beforeAgentCallback`, `afterAgentCallback`) hooks to intercept messages at different stages of the execution loop. + +For more information, see the [ADK Callbacks documentation](https://google.github.io/adk-docs/callbacks/types-of-callbacks/).{{% /tab %}} +{{< /tabpane >}} + +## Results + +The output should look similar to the following. Note that exact responses may vary due to the non-deterministic nature of LLMs and differences between orchestration frameworks. + +``` +AI: Booking Confirmed! You earned 500 Loyalty Points with this stay. + +AI: Error: Maximum stay duration is 14 days. +``` diff --git a/docs/en/samples/pre_post_processing/go/adk/agent.go b/docs/en/samples/pre_post_processing/go/adk/agent.go index 95be99308d..d2d59d130b 100644 --- a/docs/en/samples/pre_post_processing/go/adk/agent.go +++ b/docs/en/samples/pre_post_processing/go/adk/agent.go @@ -120,6 +120,7 @@ func main() { Description: "Agent to answer questions about hotels.", Instruction: systemPrompt, Tools: tools, + // Add pre- and post- processing hooks BeforeToolCallbacks: []llmagent.BeforeToolCallback{beforeToolCallback}, AfterToolCallbacks: []llmagent.AfterToolCallback{afterToolCallback}, })