Skip to content

Commit b6f6dcb

Browse files
kamalaboulhosncopybara-github
authored andcommitted
feat: Add a handwritten tool for Cloud Pub/Sub
Merge #3865 COPYBARA_INTEGRATE_REVIEW=#3865 from kamalaboulhosn:main 37a38a4 PiperOrigin-RevId: 845345128
1 parent 7e6ef71 commit b6f6dcb

File tree

16 files changed

+1504
-0
lines changed

16 files changed

+1504
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Pub/Sub Tools Sample
2+
3+
## Introduction
4+
5+
This sample agent demonstrates the Pub/Sub first-party tools in ADK,
6+
distributed via the `google.adk.tools.pubsub` module. These tools include:
7+
8+
1. `publish_message`
9+
10+
Publishes a message to a Pub/Sub topic.
11+
12+
2. `pull_messages`
13+
14+
Pulls messages from a Pub/Sub subscription.
15+
16+
3. `acknowledge_messages`
17+
18+
Acknowledges messages on a Pub/Sub subscription.
19+
20+
## How to use
21+
22+
Set up environment variables in your `.env` file for using
23+
[Google AI Studio](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-ai-studio)
24+
or
25+
[Google Cloud Vertex AI](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-cloud-vertex-ai)
26+
for the LLM service for your agent. For example, for using Google AI Studio you
27+
would set:
28+
29+
* GOOGLE_GENAI_USE_VERTEXAI=FALSE
30+
* GOOGLE_API_KEY={your api key}
31+
32+
### With Application Default Credentials
33+
34+
This mode is useful for quick development when the agent builder is the only
35+
user interacting with the agent. The tools are run with these credentials.
36+
37+
1. Create application default credentials on the machine where the agent would
38+
be running by following https://cloud.google.com/docs/authentication/provide-credentials-adc.
39+
40+
1. Set `CREDENTIALS_TYPE=None` in `agent.py`
41+
42+
1. Run the agent
43+
44+
### With Service Account Keys
45+
46+
This mode is useful for quick development when the agent builder wants to run
47+
the agent with service account credentials. The tools are run with these
48+
credentials.
49+
50+
1. Create service account key by following https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys.
51+
52+
1. Set `CREDENTIALS_TYPE=AuthCredentialTypes.SERVICE_ACCOUNT` in `agent.py`
53+
54+
1. Download the key file and replace `"service_account_key.json"` with the path
55+
56+
1. Run the agent
57+
58+
### With Interactive OAuth
59+
60+
1. Follow
61+
https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name.
62+
to get your client id and client secret. Be sure to choose "web" as your client
63+
type.
64+
65+
1. Follow https://developers.google.com/workspace/guides/configure-oauth-consent to add scope "https://www.googleapis.com/auth/pubsub".
66+
67+
1. Follow https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred to add http://localhost/dev-ui/ to "Authorized redirect URIs".
68+
69+
Note: localhost here is just a hostname that you use to access the dev ui,
70+
replace it with the actual hostname you use to access the dev ui.
71+
72+
1. For 1st run, allow popup for localhost in Chrome.
73+
74+
1. Configure your `.env` file to add two more variables before running the agent:
75+
76+
* OAUTH_CLIENT_ID={your client id}
77+
* OAUTH_CLIENT_SECRET={your client secret}
78+
79+
Note: don't create a separate .env, instead put it to the same .env file that
80+
stores your Vertex AI or Dev ML credentials
81+
82+
1. Set `CREDENTIALS_TYPE=AuthCredentialTypes.OAUTH2` in `agent.py` and run the agent
83+
84+
## Sample prompts
85+
86+
* publish 'Hello World' to 'my-topic'
87+
* pull messages from 'my-subscription'
88+
* acknowledge message 'ack-id' from 'my-subscription'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import textwrap
17+
18+
from google.adk.agents.llm_agent import LlmAgent
19+
from google.adk.auth.auth_credential import AuthCredentialTypes
20+
from google.adk.tools.pubsub.config import PubSubToolConfig
21+
from google.adk.tools.pubsub.pubsub_credentials import PubSubCredentialsConfig
22+
from google.adk.tools.pubsub.pubsub_toolset import PubSubToolset
23+
import google.auth
24+
25+
# Define the desired credential type.
26+
# By default use Application Default Credentials (ADC) from the local
27+
# environment, which can be set up by following
28+
# https://cloud.google.com/docs/authentication/provide-credentials-adc.
29+
CREDENTIALS_TYPE = None
30+
31+
# Define an appropriate application name
32+
PUBSUB_AGENT_NAME = "adk_sample_pubsub_agent"
33+
34+
35+
# Define Pub/Sub tool config.
36+
# You can optionally set the project_id here, or let the agent infer it from context/user input.
37+
tool_config = PubSubToolConfig(project_id=os.getenv("GOOGLE_CLOUD_PROJECT"))
38+
39+
if CREDENTIALS_TYPE == AuthCredentialTypes.OAUTH2:
40+
# Initialize the tools to do interactive OAuth
41+
# The environment variables OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET
42+
# must be set
43+
credentials_config = PubSubCredentialsConfig(
44+
client_id=os.getenv("OAUTH_CLIENT_ID"),
45+
client_secret=os.getenv("OAUTH_CLIENT_SECRET"),
46+
)
47+
elif CREDENTIALS_TYPE == AuthCredentialTypes.SERVICE_ACCOUNT:
48+
# Initialize the tools to use the credentials in the service account key.
49+
# If this flow is enabled, make sure to replace the file path with your own
50+
# service account key file
51+
# https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys
52+
creds, _ = google.auth.load_credentials_from_file("service_account_key.json")
53+
credentials_config = PubSubCredentialsConfig(credentials=creds)
54+
else:
55+
# Initialize the tools to use the application default credentials.
56+
# https://cloud.google.com/docs/authentication/provide-credentials-adc
57+
application_default_credentials, _ = google.auth.default()
58+
credentials_config = PubSubCredentialsConfig(
59+
credentials=application_default_credentials
60+
)
61+
62+
pubsub_toolset = PubSubToolset(
63+
credentials_config=credentials_config, pubsub_tool_config=tool_config
64+
)
65+
66+
# The variable name `root_agent` determines what your root agent is for the
67+
# debug CLI
68+
root_agent = LlmAgent(
69+
model="gemini-2.5-flash",
70+
name=PUBSUB_AGENT_NAME,
71+
description=(
72+
"Agent to publish, pull, and acknowledge messages from Google Cloud"
73+
" Pub/Sub."
74+
),
75+
instruction=textwrap.dedent("""\
76+
You are a cloud engineer agent with access to Google Cloud Pub/Sub tools.
77+
You can publish messages to topics, pull messages from subscriptions, and acknowledge messages.
78+
"""),
79+
tools=[pubsub_toolset],
80+
)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies = [
3737
"google-cloud-bigquery>=2.2.0",
3838
"google-cloud-bigtable>=2.32.0", # For Bigtable database
3939
"google-cloud-discoveryengine>=0.13.12, <0.14.0", # For Discovery Engine Search Tool
40+
"google-cloud-pubsub>=2.0.0, <3.0.0", # For Pub/Sub Tool
4041
"google-cloud-secret-manager>=2.22.0, <3.0.0", # Fetching secrets in RestAPI Tool
4142
"google-cloud-spanner>=3.56.0, <4.0.0", # For Spanner database
4243
"google-cloud-speech>=2.30.0, <3.0.0", # For Audio Transcription

src/google/adk/features/_feature_registry.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class FeatureName(str, Enum):
3232
GOOGLE_TOOL = "GOOGLE_TOOL"
3333
JSON_SCHEMA_FOR_FUNC_DECL = "JSON_SCHEMA_FOR_FUNC_DECL"
3434
PROGRESSIVE_SSE_STREAMING = "PROGRESSIVE_SSE_STREAMING"
35+
PUBSUB_TOOLSET = "PUBSUB_TOOLSET"
3536
SPANNER_TOOLSET = "SPANNER_TOOLSET"
3637
SPANNER_TOOL_SETTINGS = "SPANNER_TOOL_SETTINGS"
3738

@@ -90,6 +91,9 @@ class FeatureConfig:
9091
FeatureName.PROGRESSIVE_SSE_STREAMING: FeatureConfig(
9192
FeatureStage.WIP, default_on=False
9293
),
94+
FeatureName.PUBSUB_TOOLSET: FeatureConfig(
95+
FeatureStage.EXPERIMENTAL, default_on=True
96+
),
9397
FeatureName.SPANNER_TOOLSET: FeatureConfig(
9498
FeatureStage.EXPERIMENTAL, default_on=True
9599
),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Pub/Sub Tools (Experimental).
16+
17+
Pub/Sub Tools under this module are hand crafted and customized while the tools
18+
under google.adk.tools.google_api_tool are auto generated based on API
19+
definition. The rationales to have customized tool are:
20+
21+
1. Better handling of base64 encoding for published messages.
22+
2. A richer subscribe-side API that reflects how users may want to pull/ack
23+
messages.
24+
"""
25+
26+
from .config import PubSubToolConfig
27+
from .pubsub_credentials import PubSubCredentialsConfig
28+
from .pubsub_toolset import PubSubToolset
29+
30+
__all__ = ["PubSubCredentialsConfig", "PubSubToolConfig", "PubSubToolset"]

0 commit comments

Comments
 (0)