1313import org .elasticsearch .xcontent .XContentBuilder ;
1414import org .elasticsearch .xcontent .XContentFactory ;
1515import org .elasticsearch .xcontent .XContentType ;
16+ import org .elasticsearch .xpack .inference .services .googlevertexai .embeddings .GoogleVertexAiEmbeddingsServiceSettings ;
1617import org .elasticsearch .xpack .inference .services .googlevertexai .embeddings .GoogleVertexAiEmbeddingsTaskSettings ;
1718
1819import java .io .IOException ;
@@ -26,7 +27,8 @@ public void testToXContent_SingleEmbeddingRequest_WritesAllFields() throws IOExc
2627 var entity = new GoogleVertexAiEmbeddingsRequestEntity (
2728 List .of ("abc" ),
2829 null ,
29- new GoogleVertexAiEmbeddingsTaskSettings (true , InputType .CLUSTERING )
30+ new GoogleVertexAiEmbeddingsTaskSettings (true , InputType .CLUSTERING ),
31+ new GoogleVertexAiEmbeddingsServiceSettings ("location" , "projectId" , "modelId" , true , null , 10 , null , null )
3032 );
3133
3234 XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
@@ -42,17 +44,19 @@ public void testToXContent_SingleEmbeddingRequest_WritesAllFields() throws IOExc
4244 }
4345 ],
4446 "parameters": {
45- "autoTruncate": true
47+ "autoTruncate": true,
48+ "outputDimensionality": 10
4649 }
4750 }
4851 """ ));
4952 }
5053
51- public void testToXContent_SingleEmbeddingRequest_DoesNotWriteAutoTruncationIfNotDefined () throws IOException {
54+ public void testToXContent_SingleEmbeddingRequest_DoesNotWriteUndefinedFields () throws IOException {
5255 var entity = new GoogleVertexAiEmbeddingsRequestEntity (
5356 List .of ("abc" ),
5457 InputType .INTERNAL_INGEST ,
55- new GoogleVertexAiEmbeddingsTaskSettings (null , null )
58+ new GoogleVertexAiEmbeddingsTaskSettings (null , null ),
59+ new GoogleVertexAiEmbeddingsServiceSettings ("location" , "projectId" , "modelId" , false , null , null , null , null )
5660 );
5761
5862 XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
@@ -66,13 +70,45 @@ public void testToXContent_SingleEmbeddingRequest_DoesNotWriteAutoTruncationIfNo
6670 "content": "abc",
6771 "task_type": "RETRIEVAL_DOCUMENT"
6872 }
69- ]
73+ ],
74+ "parameters": {
75+ }
76+ }
77+ """ ));
78+ }
79+
80+ public void testToXContent_SingleEmbeddingRequest_DoesNotWriteUndefinedFields_DimensionsSetByUserFalse () throws IOException {
81+ var entity = new GoogleVertexAiEmbeddingsRequestEntity (
82+ List .of ("abc" ),
83+ InputType .INTERNAL_INGEST ,
84+ new GoogleVertexAiEmbeddingsTaskSettings (null , null ),
85+ new GoogleVertexAiEmbeddingsServiceSettings ("location" , "projectId" , "modelId" , false , null , 10 , null , null )
86+ );
87+
88+ XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
89+ entity .toXContent (builder , null );
90+ String xContentResult = Strings .toString (builder );
91+
92+ assertThat (xContentResult , equalToIgnoringWhitespaceInJsonString ("""
93+ {
94+ "instances": [
95+ {
96+ "content": "abc",
97+ "task_type": "RETRIEVAL_DOCUMENT"
98+ }
99+ ],
100+ "parameters": {}
70101 }
71102 """ ));
72103 }
73104
74105 public void testToXContent_SingleEmbeddingRequest_DoesNotWriteInputTypeIfNotDefined () throws IOException {
75- var entity = new GoogleVertexAiEmbeddingsRequestEntity (List .of ("abc" ), null , new GoogleVertexAiEmbeddingsTaskSettings (false , null ));
106+ var entity = new GoogleVertexAiEmbeddingsRequestEntity (
107+ List .of ("abc" ),
108+ null ,
109+ new GoogleVertexAiEmbeddingsTaskSettings (false , null ),
110+ new GoogleVertexAiEmbeddingsServiceSettings ("location" , "projectId" , "modelId" , false , null , null , null , null )
111+ );
76112
77113 XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
78114 entity .toXContent (builder , null );
@@ -96,7 +132,8 @@ public void testToXContent_MultipleEmbeddingsRequest_WritesAllFields() throws IO
96132 var entity = new GoogleVertexAiEmbeddingsRequestEntity (
97133 List .of ("abc" , "def" ),
98134 InputType .INTERNAL_SEARCH ,
99- new GoogleVertexAiEmbeddingsTaskSettings (true , InputType .CLUSTERING )
135+ new GoogleVertexAiEmbeddingsTaskSettings (true , InputType .CLUSTERING ),
136+ new GoogleVertexAiEmbeddingsServiceSettings ("location" , "projectId" , "modelId" , true , null , 10 , null , null )
100137 );
101138
102139 XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
@@ -116,7 +153,8 @@ public void testToXContent_MultipleEmbeddingsRequest_WritesAllFields() throws IO
116153 }
117154 ],
118155 "parameters": {
119- "autoTruncate": true
156+ "autoTruncate": true,
157+ "outputDimensionality": 10
120158 }
121159 }
122160 """ ));
@@ -126,7 +164,8 @@ public void testToXContent_MultipleEmbeddingsRequest_DoesNotWriteInputTypeIfNotD
126164 var entity = new GoogleVertexAiEmbeddingsRequestEntity (
127165 List .of ("abc" , "def" ),
128166 null ,
129- new GoogleVertexAiEmbeddingsTaskSettings (true , null )
167+ new GoogleVertexAiEmbeddingsTaskSettings (true , null ),
168+ new GoogleVertexAiEmbeddingsServiceSettings ("location" , "projectId" , "modelId" , false , null , null , null , null )
130169 );
131170
132171 XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
@@ -154,7 +193,8 @@ public void testToXContent_MultipleEmbeddingsRequest_DoesNotWriteAutoTruncationI
154193 var entity = new GoogleVertexAiEmbeddingsRequestEntity (
155194 List .of ("abc" , "def" ),
156195 null ,
157- new GoogleVertexAiEmbeddingsTaskSettings (null , InputType .CLASSIFICATION )
196+ new GoogleVertexAiEmbeddingsTaskSettings (null , InputType .CLASSIFICATION ),
197+ new GoogleVertexAiEmbeddingsServiceSettings ("location" , "projectId" , "modelId" , false , null , null , null , null )
158198 );
159199
160200 XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
@@ -172,12 +212,14 @@ public void testToXContent_MultipleEmbeddingsRequest_DoesNotWriteAutoTruncationI
172212 "content": "def",
173213 "task_type": "CLASSIFICATION"
174214 }
175- ]
215+ ],
216+ "parameters": {
217+ }
176218 }
177219 """ ));
178220 }
179221
180222 public void testToXContent_ThrowsIfTaskSettingsIsNull () {
181- expectThrows (NullPointerException .class , () -> new GoogleVertexAiEmbeddingsRequestEntity (List .of ("abc" , "def" ), null , null ));
223+ expectThrows (NullPointerException .class , () -> new GoogleVertexAiEmbeddingsRequestEntity (List .of ("abc" , "def" ), null , null , null ));
182224 }
183225}
0 commit comments