2525import com .google .genai .types .VertexAISearch ;
2626import com .google .genai .types .VertexAISearchDataStoreSpec ;
2727import io .reactivex .rxjava3 .core .Completable ;
28- import java .util .ArrayList ;
2928import java .util .List ;
3029import java .util .Optional ;
3130
3938public abstract class VertexAiSearchTool extends BaseTool {
4039 public abstract Optional <String > dataStoreId ();
4140
42- public abstract Optional < List < VertexAISearchDataStoreSpec > > dataStoreSpecs ();
41+ public abstract ImmutableList < VertexAISearchDataStoreSpec > dataStoreSpecs ();
4342
4443 public abstract Optional <String > searchEngineId ();
4544
@@ -54,7 +53,7 @@ public abstract class VertexAiSearchTool extends BaseTool {
5453 public abstract Optional <String > dataStore ();
5554
5655 public static Builder builder () {
57- return new AutoValue_VertexAiSearchTool .Builder ();
56+ return new AutoValue_VertexAiSearchTool .Builder (). dataStoreSpecs ( ImmutableList . of ()) ;
5857 }
5958
6059 VertexAiSearchTool () {
@@ -80,24 +79,29 @@ public Completable processLlmRequest(
8079 searchEngineId ().ifPresent (vertexAiSearchBuilder ::engine );
8180 filter ().ifPresent (vertexAiSearchBuilder ::filter );
8281 maxResults ().ifPresent (vertexAiSearchBuilder ::maxResults );
83- dataStoreSpecs ().ifPresent (vertexAiSearchBuilder ::dataStoreSpecs );
82+ if (!dataStoreSpecs ().isEmpty ()) {
83+ vertexAiSearchBuilder .dataStoreSpecs (dataStoreSpecs ());
84+ }
8485
8586 Tool retrievalTool =
8687 Tool .builder ()
8788 .retrieval (Retrieval .builder ().vertexAiSearch (vertexAiSearchBuilder .build ()).build ())
8889 .build ();
89-
90- List <Tool > currentTools =
91- new ArrayList <>(
92- llmRequest .config ().flatMap (GenerateContentConfig ::tools ).orElse (ImmutableList .of ()));
93- currentTools .add (retrievalTool );
94-
90+ ImmutableList <Tool > tools =
91+ ImmutableList .<Tool >builder ()
92+ .addAll (
93+ llmRequest
94+ .config ()
95+ .flatMap (GenerateContentConfig ::tools )
96+ .orElse (ImmutableList .of ()))
97+ .add (retrievalTool )
98+ .build ();
9599 GenerateContentConfig newConfig =
96100 llmRequest
97101 .config ()
98102 .map (GenerateContentConfig ::toBuilder )
99103 .orElse (GenerateContentConfig .builder ())
100- .tools (ImmutableList . copyOf ( currentTools ) )
104+ .tools (tools )
101105 .build ();
102106 llmRequestBuilder .config (newConfig );
103107 return Completable .complete ();
@@ -126,14 +130,18 @@ public abstract static class Builder {
126130
127131 public final VertexAiSearchTool build () {
128132 VertexAiSearchTool tool = autoBuild ();
129- if ((tool .dataStoreId ().isEmpty () && tool .searchEngineId ().isEmpty ())
130- || (tool .dataStoreId ().isPresent () && tool .searchEngineId ().isPresent ())) {
133+ boolean hasDataStoreId =
134+ tool .dataStoreId ().isPresent () && !tool .dataStoreId ().get ().isEmpty ();
135+ boolean hasSearchEngineId =
136+ tool .searchEngineId ().isPresent () && !tool .searchEngineId ().get ().isEmpty ();
137+ if (hasDataStoreId == hasSearchEngineId ) {
131138 throw new IllegalArgumentException (
132- "Either dataStoreId or searchEngineId must be specified ." );
139+ "One and only one of dataStoreId or searchEngineId must not be empty ." );
133140 }
134- if (tool .dataStoreSpecs ().isPresent () && tool .searchEngineId ().isEmpty ()) {
141+ boolean hasDataStoreSpecs = !tool .dataStoreSpecs ().isEmpty ();
142+ if (hasDataStoreSpecs && !hasSearchEngineId ) {
135143 throw new IllegalArgumentException (
136- "searchEngineId must be specified if dataStoreSpecs is specified ." );
144+ "searchEngineId must not be empty if dataStoreSpecs is not empty ." );
137145 }
138146 return tool ;
139147 }
0 commit comments