Skip to content

Commit 4218fed

Browse files
committed
Update ADK doc according to issue #63 - 2
1 parent 7773037 commit 4218fed

File tree

2 files changed

+54
-98
lines changed

2 files changed

+54
-98
lines changed

docs/tools/built-in-tools.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,63 @@ These are a set of tools aimed to provide integration with BigQuery, namely:
145145
* **`execute_sql`**: Runs a SQL query in BigQuery and fetch the result.
146146
* **`ask_data_insights`**: Answers questions about data in BigQuery tables using natural language.
147147

148-
They are packaged in the toolset `BigQueryToolset`.
149-
148+
They are packaged in the toolset `BigQueryToolset`. You can also specify the `location` for the BigQuery data.
150149

151150

152151
```py
153152
--8<-- "examples/python/snippets/tools/built-in-tools/bigquery.py"
154153
```
155154

155+
### Spanner
156+
157+
The `SpannerToolset` provides a set of tools for interacting with Spanner databases. The main tool in this toolset is `similarity_search`.
158+
159+
* **`similarity_search`**: Performs a vector similarity search in Spanner. This tool embeds a text query, then uses the embedding vector to find similar items in a Spanner table.
160+
161+
To use the `similarity_search` tool, your Spanner table must contain a column with pre-calculated embeddings. The tool uses a configured embedding service to convert the input text query into a vector for the search.
162+
163+
```py
164+
from google.adk.tools.spanner import SpannerToolset
165+
166+
# Initialize the Spanner toolset
167+
spanner_toolset = SpannerToolset(
168+
project_id="my-project",
169+
instance_id="my-instance",
170+
database_id="my-database",
171+
)
172+
173+
# Example of using similarity_search in an agent
174+
from google.adk.agents import LlmAgent
175+
176+
spanner_agent = LlmAgent(
177+
name="spanner_agent",
178+
model="gemini-2.0-flash",
179+
instruction="You are an expert in Spanner similarity search.",
180+
tools=[spanner_toolset],
181+
)
182+
183+
# The agent can now use the similarity_search tool
184+
# to answer questions that require vector search.
185+
# For example:
186+
# "Find products similar to 'eco-friendly cleaning supplies'."
187+
```
188+
189+
The `similarity_search` tool takes the following parameters:
190+
191+
* `table_name`: The name of the table to search.
192+
* `query`: The text query to search for.
193+
* `embedding_column_to_search`: The name of the column containing the embeddings.
194+
* `columns`: A list of other columns to return in the results.
195+
* `embedding_options`: A dictionary specifying the embedding model to use.
196+
* `spanner_embedding_model_name`: For GoogleSQL dialect databases, the name of the embedding model registered in Spanner.
197+
* `vertex_ai_embedding_model_endpoint`: For PostgreSQL dialect databases, the endpoint of the Vertex AI embedding model.
198+
* `additional_filter` (optional): A SQL `WHERE` clause to further filter the results.
199+
* `search_options` (optional): A dictionary for search customization:
200+
* `top_k`: The number of results to return (default: 4).
201+
* `distance_type`: The distance metric to use (e.g., `COSINE_DISTANCE`, `EUCLIDEAN_DISTANCE`).
202+
* `nearest_neighbors_algorithm`: The search algorithm to use (`EXACT_NEAREST_NEIGHBORS` or `APPROXIMATE_NEAREST_NEIGHBORS`).
203+
* `num_leaves_to_search`: For approximate search, the number of leaves to search in the vector index.
204+
156205
## Use Built-in tools with other tools
157206

158207
The following code sample demonstrates how to use multiple built-in tools or how
Lines changed: 3 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,6 @@
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 asyncio
16-
17-
from google.adk.agents import Agent
18-
from google.adk.runners import Runner
19-
from google.adk.sessions import InMemorySessionService
20-
from google.adk.tools.bigquery import BigQueryCredentialsConfig
211
from google.adk.tools.bigquery import BigQueryToolset
22-
from google.adk.tools.bigquery.config import BigQueryToolConfig
23-
from google.adk.tools.bigquery.config import WriteMode
24-
from google.genai import types
25-
import google.auth
26-
27-
# Define constants for this example agent
28-
AGENT_NAME = "bigquery_agent"
29-
APP_NAME = "bigquery_app"
30-
USER_ID = "user1234"
31-
SESSION_ID = "1234"
32-
GEMINI_MODEL = "gemini-2.0-flash"
33-
34-
# Define a tool configuration to block any write operations
35-
tool_config = BigQueryToolConfig(write_mode=WriteMode.BLOCKED)
36-
37-
# Define a credentials config - in this example we are using application default
38-
# credentials
39-
# https://cloud.google.com/docs/authentication/provide-credentials-adc
40-
application_default_credentials, _ = google.auth.default()
41-
credentials_config = BigQueryCredentialsConfig(
42-
credentials=application_default_credentials
43-
)
44-
45-
# Instantiate a BigQuery toolset
46-
bigquery_toolset = BigQueryToolset(
47-
credentials_config=credentials_config, bigquery_tool_config=tool_config
48-
)
49-
50-
# Agent Definition
51-
bigquery_agent = Agent(
52-
model=GEMINI_MODEL,
53-
name=AGENT_NAME,
54-
description=(
55-
"Agent to answer questions about BigQuery data and models and execute"
56-
" SQL queries."
57-
),
58-
instruction="""\
59-
You are a data science agent with access to several BigQuery tools.
60-
Make use of those tools to answer the user's questions.
61-
""",
62-
tools=[bigquery_toolset],
63-
)
64-
65-
# Session and Runner
66-
session_service = InMemorySessionService()
67-
session = asyncio.run(
68-
session_service.create_session(
69-
app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
70-
)
71-
)
72-
runner = Runner(
73-
agent=bigquery_agent, app_name=APP_NAME, session_service=session_service
74-
)
75-
76-
77-
# Agent Interaction
78-
def call_agent(query):
79-
"""
80-
Helper function to call the agent with a query.
81-
"""
82-
content = types.Content(role="user", parts=[types.Part(text=query)])
83-
events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content)
84-
85-
print("USER:", query)
86-
for event in events:
87-
if event.is_final_response():
88-
final_response = event.content.parts[0].text
89-
print("AGENT:", final_response)
902

3+
# Initialize the BigQuery toolset with a specific location
4+
bigquery_toolset = BigQueryToolset(project_id="my-project", location="US")
915

92-
call_agent("Are there any ml datasets in bigquery-public-data project?")
93-
call_agent("Tell me more about ml_datasets.")
94-
call_agent("Which all tables does it have?")
95-
call_agent("Tell me more about the census_adult_income table.")
96-
call_agent("How many rows are there per income bracket?")
97-
call_agent(
98-
"What is the statistical correlation between education_num, age, and the income_bracket?"
99-
)
6+
# The agent can now use the BigQuery tools to query datasets in the specified location.

0 commit comments

Comments
 (0)