Skip to content

Commit fef5318

Browse files
google-genai-botcopybara-github
authored andcommitted
docs: add contributing bigtable sample
PiperOrigin-RevId: 797014958
1 parent 018db79 commit fef5318

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Bigtable Tools Sample
2+
3+
## Introduction
4+
5+
This sample agent demonstrates the Bigtable first-party tools in ADK,
6+
distributed via the `google.adk.tools.bigtable` module. These tools include:
7+
8+
1. `bigtable_list_instances`
9+
10+
Fetches Bigtable instance ids in a Google Cloud project.
11+
12+
1. `bigtable_get_instance_info`
13+
14+
Fetches metadata information about a Bigtable instance.
15+
16+
1. `bigtable_list_tables`
17+
18+
Fetches table ids in a Bigtable instance.
19+
20+
1. `bigtable_get_table_info`
21+
22+
Fetches metadata information about a Bigtable table.
23+
24+
1. `bigtable_execute_sql`
25+
26+
Runs a DQL SQL query in Bigtable database.
27+
28+
## How to use
29+
30+
Set up environment variables in your `.env` file for using
31+
[Google AI Studio](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-ai-studio)
32+
or
33+
[Google Cloud Vertex AI](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-cloud-vertex-ai)
34+
for the LLM service for your agent. For example, for using Google AI Studio you
35+
would set:
36+
37+
* GOOGLE_GENAI_USE_VERTEXAI=FALSE
38+
* GOOGLE_API_KEY={your api key}
39+
40+
### With Application Default Credentials
41+
42+
This mode is useful for quick development when the agent builder is the only
43+
user interacting with the agent. The tools are run with these credentials.
44+
45+
1. Create application default credentials on the machine where the agent would
46+
be running by following https://cloud.google.com/docs/authentication/provide-credentials-adc.
47+
48+
1. Set `CREDENTIALS_TYPE=None` in `agent.py`
49+
50+
1. Run the agent
51+
52+
### With Service Account Keys
53+
54+
This mode is useful for quick development when the agent builder wants to run
55+
the agent with service account credentials. The tools are run with these
56+
credentials.
57+
58+
1. Create service account key by following https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys.
59+
60+
1. Set `CREDENTIALS_TYPE=AuthCredentialTypes.SERVICE_ACCOUNT` in `agent.py`
61+
62+
1. Download the key file and replace `"service_account_key.json"` with the path
63+
64+
1. Run the agent
65+
66+
### With Interactive OAuth
67+
68+
1. Follow
69+
https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name.
70+
to get your client id and client secret. Be sure to choose "web" as your client
71+
type.
72+
73+
1. Follow https://developers.google.com/workspace/guides/configure-oauth-consent
74+
to add scope "https://www.googleapis.com/auth/bigtable.admin" and
75+
"https://www.googleapis.com/auth/bigtable.data" as declaration, this is used
76+
for review purpose.
77+
78+
1. Follow
79+
https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred
80+
to add http://localhost/dev-ui/ to "Authorized redirect URIs".
81+
82+
Note: localhost here is just a hostname that you use to access the dev ui,
83+
replace it with the actual hostname you use to access the dev ui.
84+
85+
1. For 1st run, allow popup for localhost in Chrome.
86+
87+
1. Configure your `.env` file to add two more variables before running the
88+
agent:
89+
90+
* OAUTH_CLIENT_ID={your client id}
91+
* OAUTH_CLIENT_SECRET={your client secret}
92+
93+
Note: don't create a separate .env, instead put it to the same .env file that
94+
stores your Vertex AI or Dev ML credentials
95+
96+
1. Set `CREDENTIALS_TYPE=AuthCredentialTypes.OAUTH2` in `agent.py` and run the
97+
agent
98+
99+
## Sample prompts
100+
101+
* Show me all instances in the my-project.
102+
* Show me all tables in the my-instance instance in my-project.
103+
* Describe the schema of the my-table table in the my-instance instance in my-project.
104+
* Show me the first 10 rows of data from the my-table table in the my-instance instance in my-project.
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.bigtable.bigtable_credentials import BigtableCredentialsConfig
20+
from google.adk.tools.bigtable.bigtable_toolset import BigtableToolset
21+
from google.adk.tools.bigtable.settings import BigtableToolSettings
22+
import google.auth
23+
24+
# Define an appropriate credential type
25+
CREDENTIALS_TYPE = AuthCredentialTypes.OAUTH2
26+
27+
28+
# Define Bigtable tool config with read capability set to allowed.
29+
tool_settings = BigtableToolSettings()
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 = BigtableCredentialsConfig(
36+
client_id=os.getenv("OAUTH_CLIENT_ID"),
37+
client_secret=os.getenv("OAUTH_CLIENT_SECRET"),
38+
scopes=[
39+
"https://www.googleapis.com/auth/bigtable.admin",
40+
"https://www.googleapis.com/auth/bigtable.data",
41+
],
42+
)
43+
elif CREDENTIALS_TYPE == AuthCredentialTypes.SERVICE_ACCOUNT:
44+
# Initialize the tools to use the credentials in the service account key.
45+
# If this flow is enabled, make sure to replace the file path with your own
46+
# service account key file
47+
# https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys
48+
creds, _ = google.auth.load_credentials_from_file("service_account_key.json")
49+
credentials_config = BigtableCredentialsConfig(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 = BigtableCredentialsConfig(
55+
credentials=application_default_credentials
56+
)
57+
58+
bigtable_toolset = BigtableToolset(
59+
credentials_config=credentials_config, bigtable_tool_settings=tool_settings
60+
)
61+
62+
# The variable name `root_agent` determines what your root agent is for the
63+
# debug CLI
64+
root_agent = LlmAgent(
65+
model="gemini-1.5-flash",
66+
name="bigtable_agent",
67+
description=(
68+
"Agent to answer questions about Bigtable database tables and"
69+
" execute SQL queries."
70+
), # TODO(b/360128447): Update description
71+
instruction="""\
72+
You are a data agent with access to several Bigtable tools.
73+
Make use of those tools to answer the user's questions.
74+
""",
75+
tools=[bigtable_toolset],
76+
)

0 commit comments

Comments
 (0)