|
| 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 | + |
| 16 | +import os |
| 17 | + |
| 18 | +from google.adk.agents.llm_agent import LlmAgent |
| 19 | +from google.adk.tools.bigquery.bigquery_credentials import BigQueryCredentialsConfig |
| 20 | +from google.adk.tools.bigquery.bigquery_toolset import BigQueryToolset |
| 21 | +from google.adk.tools.bigquery.config import BigQueryToolConfig |
| 22 | +from google.adk.tools.bigquery.config import WriteMode |
| 23 | +import google.auth |
| 24 | + |
| 25 | +# Check necessary environment variables |
| 26 | +if not (google_cloud_project_id := os.getenv("GOOGLE_CLOUD_PROJECT")): |
| 27 | + raise ValueError( |
| 28 | + "GOOGLE_CLOUD_PROJECT environment variable is not set. Please set it" |
| 29 | + " to the GCP project ID where your BigQuery jobs would be run." |
| 30 | + ) |
| 31 | + |
| 32 | +# Define an appropriate application name |
| 33 | +BIGQUERY_AGENT_NAME = "adk_eval_bigquery_agent" |
| 34 | + |
| 35 | + |
| 36 | +# Define BigQuery tool config with write mode set to allowed. Note that this is |
| 37 | +# only to demonstrate the full capability of the BigQuery tools. In production |
| 38 | +# you may want to change to BLOCKED (default write mode, effectively makes the |
| 39 | +# tool read-only) or PROTECTED (only allows writes in the anonymous dataset of a |
| 40 | +# BigQuery session) write mode. |
| 41 | +tool_config = BigQueryToolConfig( |
| 42 | + write_mode=WriteMode.BLOCKED, |
| 43 | + application_name=BIGQUERY_AGENT_NAME, |
| 44 | + compute_project_id=google_cloud_project_id, |
| 45 | +) |
| 46 | + |
| 47 | +# Initialize the tools to use the application default credentials. |
| 48 | +# https://cloud.google.com/docs/authentication/provide-credentials-adc |
| 49 | +application_default_credentials, _ = google.auth.default() |
| 50 | +credentials_config = BigQueryCredentialsConfig( |
| 51 | + credentials=application_default_credentials |
| 52 | +) |
| 53 | + |
| 54 | +bigquery_toolset = BigQueryToolset( |
| 55 | + credentials_config=credentials_config, bigquery_tool_config=tool_config |
| 56 | +) |
| 57 | + |
| 58 | +# The variable name `root_agent` determines what your root agent is for the |
| 59 | +# debug CLI |
| 60 | +root_agent = LlmAgent( |
| 61 | + model="gemini-2.5-flash", |
| 62 | + name=BIGQUERY_AGENT_NAME, |
| 63 | + description=( |
| 64 | + "Agent to answer questions about BigQuery data and models and execute" |
| 65 | + " SQL queries." |
| 66 | + ), |
| 67 | + instruction=f"""\ |
| 68 | + You are a data science agent with access to several BigQuery tools. |
| 69 | + Make use of those tools to answer the user's questions. |
| 70 | +
|
| 71 | + You must use the project id {google_cloud_project_id} for running SQL |
| 72 | + queries and generating data insights |
| 73 | + """, |
| 74 | + tools=[bigquery_toolset], |
| 75 | +) |
0 commit comments