|
| 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 | + |
| 17 | +from google.adk.agents.llm_agent import LlmAgent |
| 18 | +from google.adk.auth.auth_credential import AuthCredentialTypes |
| 19 | +from google.adk.tools.spanner.settings import Capabilities |
| 20 | +from google.adk.tools.spanner.settings import SpannerToolSettings |
| 21 | +from google.adk.tools.spanner.spanner_credentials import SpannerCredentialsConfig |
| 22 | +from google.adk.tools.spanner.spanner_toolset import SpannerToolset |
| 23 | +import google.auth |
| 24 | + |
| 25 | +# Define an appropriate credential type |
| 26 | +CREDENTIALS_TYPE = AuthCredentialTypes.OAUTH2 |
| 27 | + |
| 28 | + |
| 29 | +# Define Spanner tool config with read capability set to allowed. |
| 30 | +tool_settings = SpannerToolSettings(capabilities=[Capabilities.DATA_READ]) |
| 31 | + |
| 32 | +if CREDENTIALS_TYPE == AuthCredentialTypes.OAUTH2: |
| 33 | + # Initiaze the tools to do interactive OAuth |
| 34 | + # The environment variables OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET |
| 35 | + # must be set |
| 36 | + credentials_config = SpannerCredentialsConfig( |
| 37 | + client_id=os.getenv("OAUTH_CLIENT_ID"), |
| 38 | + client_secret=os.getenv("OAUTH_CLIENT_SECRET"), |
| 39 | + scopes=[ |
| 40 | + "https://www.googleapis.com/auth/spanner.admin", |
| 41 | + "https://www.googleapis.com/auth/spanner.data", |
| 42 | + ], |
| 43 | + ) |
| 44 | +elif CREDENTIALS_TYPE == AuthCredentialTypes.SERVICE_ACCOUNT: |
| 45 | + # Initialize the tools to use the credentials in the service account key. |
| 46 | + # If this flow is enabled, make sure to replace the file path with your own |
| 47 | + # service account key file |
| 48 | + # https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys |
| 49 | + creds, _ = google.auth.load_credentials_from_file("service_account_key.json") |
| 50 | + credentials_config = SpannerCredentialsConfig(credentials=creds) |
| 51 | +else: |
| 52 | + # Initialize the tools to use the application default credentials. |
| 53 | + # https://cloud.google.com/docs/authentication/provide-credentials-adc |
| 54 | + application_default_credentials, _ = google.auth.default() |
| 55 | + credentials_config = SpannerCredentialsConfig( |
| 56 | + credentials=application_default_credentials |
| 57 | + ) |
| 58 | + |
| 59 | +spanner_toolset = SpannerToolset( |
| 60 | + credentials_config=credentials_config, spanner_tool_settings=tool_settings |
| 61 | +) |
| 62 | + |
| 63 | +# The variable name `root_agent` determines what your root agent is for the |
| 64 | +# debug CLI |
| 65 | +root_agent = LlmAgent( |
| 66 | + model="gemini-2.5-flash", |
| 67 | + name="spanner_agent", |
| 68 | + description=( |
| 69 | + "Agent to answer questions about Spanner database tables and" |
| 70 | + " execute SQL queries." |
| 71 | + ), |
| 72 | + instruction="""\ |
| 73 | + You are a data agent with access to several Spanner tools. |
| 74 | + Make use of those tools to answer the user's questions. |
| 75 | + """, |
| 76 | + tools=[spanner_toolset], |
| 77 | +) |
0 commit comments