Skip to content

Commit dffc1c1

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: VertexAiSearchTool: Unwrap List from Optional
PiperOrigin-RevId: 860144181
1 parent 55144ac commit dffc1c1

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

core/src/main/java/com/google/adk/tools/VertexAiSearchTool.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
public abstract class VertexAiSearchTool extends BaseTool {
4040
public abstract Optional<String> dataStoreId();
4141

42-
public abstract Optional<List<VertexAISearchDataStoreSpec>> dataStoreSpecs();
42+
public abstract ImmutableList<VertexAISearchDataStoreSpec> dataStoreSpecs();
4343

4444
public abstract Optional<String> searchEngineId();
4545

@@ -54,7 +54,7 @@ public abstract class VertexAiSearchTool extends BaseTool {
5454
public abstract Optional<String> dataStore();
5555

5656
public static Builder builder() {
57-
return new AutoValue_VertexAiSearchTool.Builder();
57+
return new AutoValue_VertexAiSearchTool.Builder().dataStoreSpecs(ImmutableList.of());
5858
}
5959

6060
VertexAiSearchTool() {
@@ -80,14 +80,16 @@ public Completable processLlmRequest(
8080
searchEngineId().ifPresent(vertexAiSearchBuilder::engine);
8181
filter().ifPresent(vertexAiSearchBuilder::filter);
8282
maxResults().ifPresent(vertexAiSearchBuilder::maxResults);
83-
dataStoreSpecs().ifPresent(vertexAiSearchBuilder::dataStoreSpecs);
83+
if (!dataStoreSpecs().isEmpty()) {
84+
vertexAiSearchBuilder.dataStoreSpecs(dataStoreSpecs());
85+
}
8486

8587
Tool retrievalTool =
8688
Tool.builder()
8789
.retrieval(Retrieval.builder().vertexAiSearch(vertexAiSearchBuilder.build()).build())
8890
.build();
8991

90-
List<Tool> currentTools =
92+
ArrayList<Tool> currentTools =
9193
new ArrayList<>(
9294
llmRequest.config().flatMap(GenerateContentConfig::tools).orElse(ImmutableList.of()));
9395
currentTools.add(retrievalTool);
@@ -126,14 +128,18 @@ public abstract static class Builder {
126128

127129
public final VertexAiSearchTool build() {
128130
VertexAiSearchTool tool = autoBuild();
129-
if ((tool.dataStoreId().isEmpty() && tool.searchEngineId().isEmpty())
130-
|| (tool.dataStoreId().isPresent() && tool.searchEngineId().isPresent())) {
131+
boolean hasDataStoreId =
132+
tool.dataStoreId().isPresent() && !tool.dataStoreId().get().isEmpty();
133+
boolean hasSearchEngineId =
134+
tool.searchEngineId().isPresent() && !tool.searchEngineId().get().isEmpty();
135+
boolean hasDataStoreSpecs = !tool.dataStoreSpecs().isEmpty();
136+
if (hasDataStoreId == hasSearchEngineId) {
131137
throw new IllegalArgumentException(
132-
"Either dataStoreId or searchEngineId must be specified.");
138+
"One and only one of dataStoreId or searchEngineId must not be empty.");
133139
}
134-
if (tool.dataStoreSpecs().isPresent() && tool.searchEngineId().isEmpty()) {
140+
if (hasDataStoreSpecs && !hasSearchEngineId) {
135141
throw new IllegalArgumentException(
136-
"searchEngineId must be specified if dataStoreSpecs is specified.");
142+
"searchEngineId must not be empty if dataStoreSpecs is not empty.");
137143
}
138144
return tool;
139145
}

core/src/test/java/com/google/adk/tools/VertexAiSearchToolTest.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void build_noDataStoreIdOrSearchEngineId_throwsException() {
4141
assertThrows(IllegalArgumentException.class, () -> VertexAiSearchTool.builder().build());
4242
assertThat(exception)
4343
.hasMessageThat()
44-
.isEqualTo("Either dataStoreId or searchEngineId must be specified.");
44+
.isEqualTo("One and only one of dataStoreId or searchEngineId must not be empty.");
4545
}
4646

4747
@Test
@@ -52,22 +52,24 @@ public void build_bothDataStoreIdAndSearchEngineId_throwsException() {
5252
() -> VertexAiSearchTool.builder().dataStoreId("ds1").searchEngineId("se1").build());
5353
assertThat(exception)
5454
.hasMessageThat()
55-
.isEqualTo("Either dataStoreId or searchEngineId must be specified.");
55+
.isEqualTo("One and only one of dataStoreId or searchEngineId must not be empty.");
5656
}
5757

5858
@Test
5959
public void build_dataStoreSpecsWithoutSearchEngineId_throwsException() {
60+
VertexAISearchDataStoreSpec spec =
61+
VertexAISearchDataStoreSpec.builder().dataStore("ds1").build();
6062
IllegalArgumentException exception =
6163
assertThrows(
6264
IllegalArgumentException.class,
6365
() ->
6466
VertexAiSearchTool.builder()
6567
.dataStoreId("ds1")
66-
.dataStoreSpecs(ImmutableList.of())
68+
.dataStoreSpecs(ImmutableList.of(spec))
6769
.build());
6870
assertThat(exception)
6971
.hasMessageThat()
70-
.isEqualTo("searchEngineId must be specified if dataStoreSpecs is specified.");
72+
.isEqualTo("searchEngineId must not be empty if dataStoreSpecs is not empty.");
7173
}
7274

7375
@Test
@@ -82,6 +84,19 @@ public void build_withSearchEngineId_succeeds() {
8284
assertThat(tool.searchEngineId()).hasValue("se1");
8385
}
8486

87+
@Test
88+
public void build_withSearchEngineIdAndDataStoreSpecs_succeeds() {
89+
VertexAISearchDataStoreSpec spec =
90+
VertexAISearchDataStoreSpec.builder().dataStore("ds1").build();
91+
VertexAiSearchTool tool =
92+
VertexAiSearchTool.builder()
93+
.searchEngineId("se1")
94+
.dataStoreSpecs(ImmutableList.of(spec))
95+
.build();
96+
assertThat(tool.searchEngineId()).hasValue("se1");
97+
assertThat(tool.dataStoreSpecs()).containsExactly(spec);
98+
}
99+
85100
@Test
86101
public void processLlmRequest_addsRetrievalTool() {
87102
VertexAiSearchTool tool =
@@ -135,4 +150,18 @@ public void processLlmRequest_withDataStoreSpecs_addsRetrievalTool() {
135150
.get())
136151
.containsExactly(spec);
137152
}
153+
154+
@Test
155+
public void processLlmRequest_nonGeminiModel_throwsException() {
156+
VertexAiSearchTool tool = VertexAiSearchTool.builder().searchEngineId("se1").build();
157+
LlmRequest.Builder llmRequestBuilder = LlmRequest.builder().model("other-model");
158+
tool.processLlmRequest(llmRequestBuilder, ToolContext.builder(invocationContext).build())
159+
.test()
160+
.assertError(
161+
throwable ->
162+
throwable instanceof IllegalArgumentException
163+
&& throwable
164+
.getMessage()
165+
.equals("Vertex AI Search tool is only supported for Gemini models."));
166+
}
138167
}

0 commit comments

Comments
 (0)