|
| 1 | +# Copyright 2026 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 import Agent |
| 18 | +from google.adk.auth.auth_credential import AuthCredentialTypes |
| 19 | +from google.adk.tools.data_agent.config import DataAgentToolConfig |
| 20 | +from google.adk.tools.data_agent.credentials import DataAgentCredentialsConfig |
| 21 | +from google.adk.tools.data_agent.data_agent_toolset import DataAgentToolset |
| 22 | +import google.auth |
| 23 | +import google.auth.transport.requests |
| 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 | +if CREDENTIALS_TYPE == AuthCredentialTypes.OAUTH2: |
| 32 | + # Initiaze the tools to do interactive OAuth |
| 33 | + # The environment variables OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET |
| 34 | + # must be set |
| 35 | + credentials_config = DataAgentCredentialsConfig( |
| 36 | + client_id=os.getenv("OAUTH_CLIENT_ID"), |
| 37 | + client_secret=os.getenv("OAUTH_CLIENT_SECRET"), |
| 38 | + ) |
| 39 | +elif CREDENTIALS_TYPE == AuthCredentialTypes.SERVICE_ACCOUNT: |
| 40 | + # Initialize the tools to use the credentials in the service account key. |
| 41 | + # If this flow is enabled, make sure to replace the file path with your own |
| 42 | + # service account key file |
| 43 | + # https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys |
| 44 | + creds, _ = google.auth.load_credentials_from_file( |
| 45 | + "service_account_key.json", |
| 46 | + scopes=["https://www.googleapis.com/auth/cloud-platform"], |
| 47 | + ) |
| 48 | + creds.refresh(google.auth.transport.requests.Request()) |
| 49 | + credentials_config = DataAgentCredentialsConfig(credentials=creds) |
| 50 | +else: |
| 51 | + # Initialize the tools to use the application default credentials. |
| 52 | + # https://cloud.google.com/docs/authentication/provide-credentials-adc |
| 53 | + application_default_credentials, _ = google.auth.default() |
| 54 | + credentials_config = DataAgentCredentialsConfig( |
| 55 | + credentials=application_default_credentials |
| 56 | + ) |
| 57 | + |
| 58 | +tool_config = DataAgentToolConfig( |
| 59 | + max_query_result_rows=100, |
| 60 | +) |
| 61 | +da_toolset = DataAgentToolset( |
| 62 | + credentials_config=credentials_config, |
| 63 | + data_agent_tool_config=tool_config, |
| 64 | + tool_filter=[ |
| 65 | + "list_accessible_data_agents", |
| 66 | + "get_data_agent_info", |
| 67 | + "ask_data_agent", |
| 68 | + ], |
| 69 | +) |
| 70 | + |
| 71 | +root_agent = Agent( |
| 72 | + name="data_agent", |
| 73 | + model="gemini-2.0-flash", |
| 74 | + description="Agent to answer user questions using Data Agents.", |
| 75 | + instruction=( |
| 76 | + "## Persona\nYou are a helpful assistant that uses Data Agents" |
| 77 | + " to answer user questions about their data.\n\n## Tools\n- You can" |
| 78 | + " list available data agents using `list_accessible_data_agents`.\n-" |
| 79 | + " You can get information about a specific data agent using" |
| 80 | + " `get_data_agent_info`.\n- You can chat with a specific data" |
| 81 | + " agent using `ask_data_agent`.\n" |
| 82 | + ), |
| 83 | + tools=[da_toolset], |
| 84 | +) |
0 commit comments