Skip to content

Commit 0907119

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: add Spanner vector_store_similarity_search tool
The vector_store_similarity_search tool performs similarity search against data in a Spanner vector store table, using the provided Spanner tool settings for configuration. PiperOrigin-RevId: 839352057
1 parent 8da61be commit 0907119

9 files changed

Lines changed: 957 additions & 359 deletions

File tree

contributing/samples/spanner_rag_agent/README.md

Lines changed: 202 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ model endpoint.
5757
CREATE MODEL EmbeddingsModel INPUT(
5858
content STRING(MAX),
5959
) OUTPUT(
60-
embeddings STRUCT<statistics STRUCT<truncated BOOL, token_count FLOAT32>, values ARRAY<FLOAT32>>,
60+
embeddings STRUCT<values ARRAY<FLOAT32>>,
6161
) REMOTE OPTIONS (
62-
endpoint = '//aiplatform.googleapis.com/projects/<PROJECT_ID>/locations/us-central1/publishers/google/models/text-embedding-004'
62+
endpoint = '//aiplatform.googleapis.com/projects/<PROJECT_ID>/locations/<LOCATION>/publishers/google/models/text-embedding-005'
6363
);
6464
```
6565

@@ -187,40 +187,203 @@ type.
187187

188188
## Which tool to use and When?
189189

190-
There are a few options to perform similarity search (see the `agent.py` for
191-
implementation details):
192-
193-
1. Wraps the built-in `similarity_search` in the Spanner Toolset.
194-
195-
- This provides an easy and controlled way to perform similarity search.
196-
You can specify different configurations related to vector search based
197-
on your need without having to figure out all the details for a vector
198-
search query.
199-
200-
2. Wraps the built-in `execute_sql` in the Spanner Toolset.
201-
202-
- `execute_sql` is a lower-level tool that you can have more control over
203-
with. With the flexibility, you can specify a complicated (parameterized)
204-
SQL query for your need, and let the `LlmAgent` pass the parameters.
205-
206-
3. Use the Spanner Toolset (and all the tools that come with it) directly.
207-
208-
- The most flexible and generic way. Instead of fixing configurations via
209-
code, you can also specify the configurations via `instruction` to
210-
the `LlmAgent` and let LLM to decide which tool to use and what parameters
211-
to pass to different tools. It might even combine different tools together!
212-
Note that in this usage, SQL generation is powered by the LlmAgent, which
213-
can be more suitable for data analysis and assistant scenarios.
214-
- To restrict the ability of an `LlmAgent`, `SpannerToolSet` also supports
215-
`tool_filter` to explicitly specify allowed tools. As an example, the
216-
following code specifies that only `execute_sql` and `get_table_schema`
217-
are allowed:
218-
219-
```py
220-
toolset = SpannerToolset(
221-
credentials_config=credentials_config,
222-
tool_filter=["execute_sql", "get_table_schema"],
223-
spanner_tool_settings=SpannerToolSettings(),
224-
)
225-
```
226-
190+
There are a few options to perform similarity search:
191+
192+
1. Use the built-in `vector_store_similarity_search` in the Spanner Toolset with explicit `SpannerVectorStoreSettings` configuration.
193+
194+
- This provides an easy way to perform similarity search. You can specify
195+
different configurations related to vector search based on your Spanner
196+
database vector store table setup.
197+
198+
Example pseudocode (see the `agent.py` for details):
199+
200+
```py
201+
from google.adk.agents.llm_agent import LlmAgent
202+
from google.adk.tools.spanner.settings import Capabilities
203+
from google.adk.tools.spanner.settings import SpannerToolSettings
204+
from google.adk.tools.spanner.settings import SpannerVectorStoreSettings
205+
from google.adk.tools.spanner.spanner_toolset import SpannerToolset
206+
207+
# credentials_config = SpannerCredentialsConfig(...)
208+
209+
# Define Spanner tool config with the vector store settings.
210+
vector_store_settings = SpannerVectorStoreSettings(
211+
project_id="<PROJECT_ID>",
212+
instance_id="<INSTANCE_ID>",
213+
database_id="<DATABASE_ID>",
214+
table_name="products",
215+
content_column="productDescription",
216+
embedding_column="productDescriptionEmbedding",
217+
vector_length=768,
218+
vertex_ai_embedding_model_name="text-embedding-005",
219+
selected_columns=[
220+
"productId",
221+
"productName",
222+
"productDescription",
223+
],
224+
nearest_neighbors_algorithm="EXACT_NEAREST_NEIGHBORS",
225+
top_k=3,
226+
distance_type="COSINE",
227+
additional_filter="inventoryCount > 0",
228+
)
229+
230+
tool_settings = SpannerToolSettings(
231+
capabilities=[Capabilities.DATA_READ],
232+
vector_store_settings=vector_store_settings,
233+
)
234+
235+
# Get the Spanner toolset with the Spanner tool settings and credentials config.
236+
spanner_toolset = SpannerToolset(
237+
credentials_config=credentials_config,
238+
spanner_tool_settings=tool_settings,
239+
# Use `vector_store_similarity_search` only
240+
tool_filter=["vector_store_similarity_search"],
241+
)
242+
243+
root_agent = LlmAgent(
244+
model="gemini-2.5-flash",
245+
name="spanner_knowledge_base_agent",
246+
description=(
247+
"Agent to answer questions about product-specific recommendations."
248+
),
249+
instruction="""
250+
You are a helpful assistant that answers user questions about product-specific recommendations.
251+
1. Always use the `vector_store_similarity_search` tool to find relevant information.
252+
2. If no relevant information is found, say you don't know.
253+
3. Present all the relevant information naturally and well formatted in your response.
254+
""",
255+
tools=[spanner_toolset],
256+
)
257+
```
258+
259+
2. Use the built-in `similarity_search` in the Spanner Toolset.
260+
261+
- `similarity_search` is a lower-level tool, which provide the most flexible
262+
and generic way. Specify all the necessary tool's parameters is required
263+
when interacting with `LlmAgent` before performing the tool call. This is
264+
more suitable for data analysis, ad-hoc query and assistant scenarios.
265+
266+
Example pseudocode:
267+
268+
```py
269+
from google.adk.agents.llm_agent import LlmAgent
270+
from google.adk.tools.spanner.settings import Capabilities
271+
from google.adk.tools.spanner.settings import SpannerToolSettings
272+
from google.adk.tools.spanner.spanner_toolset import SpannerToolset
273+
274+
# credentials_config = SpannerCredentialsConfig(...)
275+
276+
tool_settings = SpannerToolSettings(
277+
capabilities=[Capabilities.DATA_READ],
278+
)
279+
280+
spanner_toolset = SpannerToolset(
281+
credentials_config=credentials_config,
282+
spanner_tool_settings=tool_settings,
283+
# Use `similarity_search` only
284+
tool_filter=["similarity_search"],
285+
)
286+
287+
root_agent = LlmAgent(
288+
model="gemini-2.5-flash",
289+
name="spanner_knowledge_base_agent",
290+
description=(
291+
"Agent to answer questions by retrieving relevant information "
292+
"from the Spanner database."
293+
),
294+
instruction="""
295+
You are a helpful assistant that answers user questions to find the most relavant information from a Spanner database.
296+
1. Always use the `similarity_search` tool to find relevant information.
297+
2. If no relevant information is found, say you don't know.
298+
3. Present all the relevant information naturally and well formatted in your response.
299+
""",
300+
tools=[spanner_toolset],
301+
)
302+
```
303+
304+
3. Wraps the built-in `similarity_search` in the Spanner Toolset.
305+
306+
- This provides a more controlled way to perform similarity search via code.
307+
You can extend the tool as a wrapped function tool to have customized logic.
308+
309+
Example pseudocode:
310+
311+
```py
312+
from google.adk.agents.llm_agent import LlmAgent
313+
314+
from google.adk.tools.google_tool import GoogleTool
315+
from google.adk.tools.spanner import search_tool
316+
import google.auth
317+
from google.auth.credentials import Credentials
318+
319+
# credentials_config = SpannerCredentialsConfig(...)
320+
321+
# Create a wrapped function tool for the agent on top of the built-in
322+
# similarity_search tool in the Spanner toolset.
323+
# This customized tool is used to perform a Spanner KNN vector search on a
324+
# embedded knowledge base stored in a Spanner database table.
325+
def wrapped_spanner_similarity_search(
326+
search_query: str,
327+
credentials: Credentials,
328+
) -> str:
329+
"""Perform a similarity search on the product catalog.
330+
331+
Args:
332+
search_query: The search query to find relevant content.
333+
334+
Returns:
335+
Relevant product catalog content with sources
336+
"""
337+
338+
# ... Customized logic ...
339+
340+
# Instead of fixing all parameters, you can also expose some of them for
341+
# the LLM to decide.
342+
return search_tool.similarity_search(
343+
project_id="<PROJECT_ID>",
344+
instance_id="<INSTANCE_ID>",
345+
database_id="<DATABASE_ID>",
346+
table_name="products",
347+
query=search_query,
348+
embedding_column_to_search="productDescriptionEmbedding",
349+
columns= [
350+
"productId",
351+
"productName",
352+
"productDescription",
353+
]
354+
embedding_options={
355+
"vertex_ai_embedding_model_name": "text-embedding-005",
356+
},
357+
credentials=credentials,
358+
additional_filter="inventoryCount > 0",
359+
search_options={
360+
"top_k": 3,
361+
"distance_type": "EUCLIDEAN",
362+
},
363+
)
364+
365+
# ...
366+
367+
root_agent = LlmAgent(
368+
model="gemini-2.5-flash",
369+
name="spanner_knowledge_base_agent",
370+
description=(
371+
"Agent to answer questions about product-specific recommendations."
372+
),
373+
instruction="""
374+
You are a helpful assistant that answers user questions about product-specific recommendations.
375+
1. Always use the `wrapped_spanner_similarity_search` tool to find relevant information.
376+
2. If no relevant information is found, say you don't know.
377+
3. Present all the relevant information naturally and well formatted in your response.
378+
""",
379+
tools=[
380+
# Add customized Spanner tool based on the built-in similarity_search
381+
# in the Spanner toolset.
382+
GoogleTool(
383+
func=wrapped_spanner_similarity_search,
384+
credentials_config=credentials_config,
385+
tool_settings=tool_settings,
386+
),
387+
],
388+
)
389+
```

0 commit comments

Comments
 (0)