@@ -135,32 +135,30 @@ private void buildSystemInstruction(XContentBuilder builder) throws IOException
135135 }
136136
137137 builder .startObject (SYSTEM_INSTRUCTION );
138- builder . startArray ( PARTS );
139- for ( var systemMessage : systemMessages ) {
140- switch ( systemMessage . content () ) {
141- case UnifiedCompletionRequest .ContentString contentString -> {
138+ {
139+ builder . startArray ( PARTS );
140+ for ( var systemMessage : systemMessages ) {
141+ if ( systemMessage . content () instanceof UnifiedCompletionRequest .ContentString contentString ) {
142142 if (contentString .content ().isEmpty ()) {
143143 var errorMessage = "System message cannot be empty for Google Vertex AI" ;
144144 throw new ElasticsearchStatusException (errorMessage , RestStatus .BAD_REQUEST );
145145 }
146146 builder .startObject ();
147147 builder .field (TEXT , contentString .content ());
148148 builder .endObject ();
149- }
150- case UnifiedCompletionRequest .ContentObjects contentObjects -> {
149+ } else if (systemMessage .content () instanceof UnifiedCompletionRequest .ContentObjects contentObjects ) {
151150 for (var contentObject : contentObjects .contentObjects ()) {
152151 builder .startObject ();
153152 builder .field (TEXT , contentObject .text ());
154153 builder .endObject ();
155154 }
156- }
157- default -> {
155+ } else {
158156 var errorMessage = "Only text system instructions are supported for Vertex AI" ;
159157 throw new ElasticsearchStatusException (errorMessage , RestStatus .BAD_REQUEST );
160158 }
161159 }
160+ builder .endArray ();
162161 }
163- builder .endArray ();
164162 builder .endObject ();
165163
166164 }
@@ -178,33 +176,32 @@ private void buildContents(XContentBuilder builder) throws IOException {
178176 builder .startObject ();
179177 builder .field (ROLE , messageRoleToGoogleVertexAiSupportedRole (message .role ()));
180178 builder .startArray (PARTS );
181- switch (message .content ()) {
182- case UnifiedCompletionRequest .ContentString contentString -> {
183- if (contentString .content ().isEmpty ()) {
184- break ; // VertexAI API does not support empty text parts
179+ {
180+ if (message .content () instanceof UnifiedCompletionRequest .ContentString ) {
181+ UnifiedCompletionRequest .ContentString contentString = (UnifiedCompletionRequest .ContentString ) message .content ();
182+ // VertexAI does not support empty text parts
183+ if (contentString .content ().isEmpty () == false ) {
184+ builder .startObject ();
185+ builder .field (TEXT , contentString .content ());
186+ builder .endObject ();
185187 }
186- builder . startObject ();
187- builder . field ( TEXT , contentString .content () );
188- builder . endObject ( );
188+ } else if ( message . content () instanceof UnifiedCompletionRequest . ContentObjects ) {
189+ UnifiedCompletionRequest . ContentObjects contentObjects = ( UnifiedCompletionRequest . ContentObjects ) message .content ();
190+ validateAndAddContentObjectsToBuilder ( builder , contentObjects );
189191 }
190- case UnifiedCompletionRequest .ContentObjects contentObjects -> validateAndAddContentObjectsToBuilder (
191- builder ,
192- contentObjects
193- );
194- case null -> {
195- // Content can be null and that's fine. If this case is not present, Null pointer exception will be thrown
196- }
197- }
198192
199- if (message .toolCalls () != null && message .toolCalls ().isEmpty () == false ) {
200- var toolCalls = message .toolCalls ();
201- for (var toolCall : toolCalls ) {
202- builder .startObject ();
203- builder .startObject (FUNCTION_CALL );
204- builder .field (FUNCTION_CALL_NAME , toolCall .function ().name ());
205- builder .field (FUNCTION_CALL_ARGS , jsonStringToMap (toolCall .function ().arguments ()));
206- builder .endObject ();
207- builder .endObject ();
193+ if (message .toolCalls () != null && message .toolCalls ().isEmpty () == false ) {
194+ var toolCalls = message .toolCalls ();
195+ for (var toolCall : toolCalls ) {
196+ builder .startObject ();
197+ {
198+ builder .startObject (FUNCTION_CALL );
199+ builder .field (FUNCTION_CALL_NAME , toolCall .function ().name ());
200+ builder .field (FUNCTION_CALL_ARGS , jsonStringToMap (toolCall .function ().arguments ()));
201+ builder .endObject ();
202+ }
203+ builder .endObject ();
204+ }
208205 }
209206 }
210207 builder .endArray ();
@@ -222,62 +219,63 @@ private void buildTools(XContentBuilder builder) throws IOException {
222219 }
223220
224221 builder .startArray (TOOLS );
225- builder .startObject ();
226- builder .startArray (FUNCTION_DECLARATIONS );
227- for (var tool : tools ) {
228- if (FUNCTION_TYPE .equals (tool .type ()) == false ) {
229- var errorMessage = format (
230- "Tool type [%s] not supported by Google VertexAI ChatCompletion. Supported types: [%s]" ,
231- tool .type (),
232- FUNCTION_TYPE
233- );
234- throw new ElasticsearchStatusException (errorMessage , RestStatus .BAD_REQUEST );
235- }
236- var function = tool .function ();
237- if (function == null ) {
238- var errorMessage = format ("Tool of type [%s] must have a function definition" , tool .type ());
239- throw new ElasticsearchStatusException (errorMessage , RestStatus .BAD_REQUEST );
240- }
241-
222+ {
242223 builder .startObject ();
243- builder .field (FUNCTION_NAME , function .name ());
244- if (Strings .hasText (function .description ())) {
245- builder .field (FUNCTION_DESCRIPTION , function .description ());
246- }
224+ builder .startArray (FUNCTION_DECLARATIONS );
225+ for (var tool : tools ) {
226+ if (FUNCTION_TYPE .equals (tool .type ()) == false ) {
227+ var errorMessage = format (
228+ "Tool type [%s] not supported by Google VertexAI ChatCompletion. Supported types: [%s]" ,
229+ tool .type (),
230+ FUNCTION_TYPE
231+ );
232+ throw new ElasticsearchStatusException (errorMessage , RestStatus .BAD_REQUEST );
233+ }
234+ var function = tool .function ();
235+ if (function == null ) {
236+ var errorMessage = format ("Tool of type [%s] must have a function definition" , tool .type ());
237+ throw new ElasticsearchStatusException (errorMessage , RestStatus .BAD_REQUEST );
238+ }
247239
248- if (function .parameters () != null && function .parameters ().isEmpty () == false ) {
249- builder .field (FUNCTION_PARAMETERS , function .parameters ());
250- }
240+ builder .startObject ();
241+ builder .field (FUNCTION_NAME , function .name ());
242+ if (Strings .hasText (function .description ())) {
243+ builder .field (FUNCTION_DESCRIPTION , function .description ());
244+ }
251245
246+ if (function .parameters () != null && function .parameters ().isEmpty () == false ) {
247+ builder .field (FUNCTION_PARAMETERS , function .parameters ());
248+ }
249+ builder .endObject ();
250+ }
251+ builder .endArray ();
252252 builder .endObject ();
253253 }
254254 builder .endArray ();
255- builder .endObject ();
256- builder .endArray ();
257255 }
258256
259257 private void buildToolConfig (XContentBuilder builder ) throws IOException {
260258 var request = unifiedChatInput .getRequest ();
261259
262260 UnifiedCompletionRequest .ToolChoiceObject toolChoice ;
263- switch (request .toolChoice ()) {
264- case UnifiedCompletionRequest .ToolChoiceObject toolChoiceObject -> toolChoice = toolChoiceObject ;
265- case UnifiedCompletionRequest .ToolChoiceString toolChoiceString -> {
266- if (toolChoiceString .value ().equals (TOOL_MODE_AUTO )) {
267- return ;
268- }
269- throw new ElasticsearchStatusException (
270- format (
271- "Tool choice value [%s] not supported by Google VertexAI ChatCompletion. Supported values: [%s]" ,
272- toolChoiceString .value (),
273- TOOL_MODE_AUTO
274- ),
275- RestStatus .BAD_REQUEST
276- );
277- }
278- case null -> {
261+ if (request .toolChoice () instanceof UnifiedCompletionRequest .ToolChoiceObject ) {
262+ UnifiedCompletionRequest .ToolChoiceObject toolChoiceObject = (UnifiedCompletionRequest .ToolChoiceObject ) request .toolChoice ();
263+ toolChoice = toolChoiceObject ;
264+ } else if (request .toolChoice () instanceof UnifiedCompletionRequest .ToolChoiceString ) {
265+ UnifiedCompletionRequest .ToolChoiceString toolChoiceString = (UnifiedCompletionRequest .ToolChoiceString ) request .toolChoice ();
266+ if (toolChoiceString .value ().equals (TOOL_MODE_AUTO )) {
279267 return ;
280268 }
269+ throw new ElasticsearchStatusException (
270+ format (
271+ "Tool choice value [%s] not supported by Google VertexAI ChatCompletion. Supported values: [%s]" ,
272+ toolChoiceString .value (),
273+ TOOL_MODE_AUTO
274+ ),
275+ RestStatus .BAD_REQUEST
276+ );
277+ } else {
278+ return ;
281279 }
282280 if (FUNCTION_TYPE .equals (toolChoice .type ()) == false ) {
283281 var errorMessage = format (
0 commit comments