|
| 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 | +) |
0 commit comments