You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `SpannerToolset` provides a set of tools for interacting with Spanner databases. The main tool in this toolset is `similarity_search`.
159
+
160
+
***`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.
161
+
162
+
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.
163
+
164
+
```py
165
+
from google.adk.tools.spanner import SpannerToolset
166
+
167
+
# Initialize the Spanner toolset
168
+
spanner_toolset = SpannerToolset(
169
+
project_id="my-project",
170
+
instance_id="my-instance",
171
+
database_id="my-database",
172
+
)
173
+
174
+
# Example of using similarity_search in an agent
175
+
from google.adk.agents import LlmAgent
176
+
177
+
spanner_agent = LlmAgent(
178
+
name="spanner_agent",
179
+
model="gemini-2.0-flash",
180
+
instruction="You are an expert in Spanner similarity search.",
181
+
tools=[spanner_toolset],
182
+
)
183
+
184
+
# The agent can now use the similarity_search tool
185
+
# to answer questions that require vector search.
186
+
# For example:
187
+
# "Find products similar to 'eco-friendly cleaning supplies'."
188
+
```
189
+
190
+
The `similarity_search` tool takes the following parameters:
191
+
192
+
*`table_name`: The name of the table to search.
193
+
*`query`: The text query to search for.
194
+
*`embedding_column_to_search`: The name of the column containing the embeddings.
195
+
*`columns`: A list of other columns to return in the results.
196
+
*`embedding_options`: A dictionary specifying the embedding model to use.
197
+
*`spanner_embedding_model_name`: For GoogleSQL dialect databases, the name of the embedding model registered in Spanner.
198
+
*`vertex_ai_embedding_model_endpoint`: For PostgreSQL dialect databases, the endpoint of the Vertex AI embedding model.
199
+
*`additional_filter` (optional): A SQL `WHERE` clause to further filter the results.
200
+
*`search_options` (optional): A dictionary for search customization:
201
+
*`top_k`: The number of results to return (default: 4).
202
+
*`distance_type`: The distance metric to use (e.g., `COSINE_DISTANCE`, `EUCLIDEAN_DISTANCE`).
203
+
*`nearest_neighbors_algorithm`: The search algorithm to use (`EXACT_NEAREST_NEIGHBORS` or `APPROXIMATE_NEAREST_NEIGHBORS`).
204
+
*`num_leaves_to_search`: For approximate search, the number of leaves to search in the vector index.
205
+
156
206
## Use Built-in tools with other tools
157
207
158
208
The following code sample demonstrates how to use multiple built-in tools or how
0 commit comments