|
37 | 37 | import org.springframework.ai.ollama.api.OllamaOptions;
|
38 | 38 |
|
39 | 39 | import static org.assertj.core.api.Assertions.assertThat;
|
| 40 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
40 | 41 | import static org.mockito.BDDMockito.given;
|
41 | 42 |
|
42 | 43 | /**
|
@@ -115,4 +116,143 @@ public void options() {
|
115 | 116 |
|
116 | 117 | }
|
117 | 118 |
|
| 119 | + @Test |
| 120 | + public void singleInputEmbedding() { |
| 121 | + given(this.ollamaApi.embed(this.embeddingsRequestCaptor.capture())) |
| 122 | + .willReturn(new EmbeddingsResponse("TEST_MODEL", List.of(new float[] { 0.1f, 0.2f, 0.3f }), 10L, 5L, 1)); |
| 123 | + |
| 124 | + var embeddingModel = OllamaEmbeddingModel.builder() |
| 125 | + .ollamaApi(this.ollamaApi) |
| 126 | + .defaultOptions(OllamaOptions.builder().model("TEST_MODEL").build()) |
| 127 | + .build(); |
| 128 | + |
| 129 | + EmbeddingResponse response = embeddingModel |
| 130 | + .call(new EmbeddingRequest(List.of("Single input text"), EmbeddingOptionsBuilder.builder().build())); |
| 131 | + |
| 132 | + assertThat(response.getResults()).hasSize(1); |
| 133 | + assertThat(response.getResults().get(0).getIndex()).isEqualTo(0); |
| 134 | + assertThat(response.getResults().get(0).getOutput()).isEqualTo(new float[] { 0.1f, 0.2f, 0.3f }); |
| 135 | + assertThat(response.getMetadata().getModel()).isEqualTo("TEST_MODEL"); |
| 136 | + |
| 137 | + assertThat(this.embeddingsRequestCaptor.getValue().input()).isEqualTo(List.of("Single input text")); |
| 138 | + assertThat(this.embeddingsRequestCaptor.getValue().model()).isEqualTo("TEST_MODEL"); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void embeddingWithNullOptions() { |
| 143 | + given(this.ollamaApi.embed(this.embeddingsRequestCaptor.capture())) |
| 144 | + .willReturn(new EmbeddingsResponse("NULL_OPTIONS_MODEL", List.of(new float[] { 0.5f }), 5L, 2L, 1)); |
| 145 | + |
| 146 | + var embeddingModel = OllamaEmbeddingModel.builder() |
| 147 | + .ollamaApi(this.ollamaApi) |
| 148 | + .defaultOptions(OllamaOptions.builder().model("NULL_OPTIONS_MODEL").build()) |
| 149 | + .build(); |
| 150 | + |
| 151 | + EmbeddingResponse response = embeddingModel.call(new EmbeddingRequest(List.of("Null options test"), null)); |
| 152 | + |
| 153 | + assertThat(response.getResults()).hasSize(1); |
| 154 | + assertThat(response.getMetadata().getModel()).isEqualTo("NULL_OPTIONS_MODEL"); |
| 155 | + |
| 156 | + assertThat(this.embeddingsRequestCaptor.getValue().model()).isEqualTo("NULL_OPTIONS_MODEL"); |
| 157 | + assertThat(this.embeddingsRequestCaptor.getValue().options()).isEqualTo(Map.of()); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void embeddingWithMultipleLargeInputs() { |
| 162 | + List<String> largeInputs = List.of( |
| 163 | + "This is a very long text input that might be used for document embedding scenarios", |
| 164 | + "Another substantial piece of text content that could represent a paragraph or section", |
| 165 | + "A third lengthy input to test batch processing capabilities of the embedding model"); |
| 166 | + |
| 167 | + given(this.ollamaApi.embed(this.embeddingsRequestCaptor.capture())) |
| 168 | + .willReturn(new EmbeddingsResponse( |
| 169 | + "BATCH_MODEL", List.of(new float[] { 0.1f, 0.2f, 0.3f, 0.4f }, |
| 170 | + new float[] { 0.5f, 0.6f, 0.7f, 0.8f }, new float[] { 0.9f, 1.0f, 1.1f, 1.2f }), |
| 171 | + 150L, 75L, 3)); |
| 172 | + |
| 173 | + var embeddingModel = OllamaEmbeddingModel.builder() |
| 174 | + .ollamaApi(this.ollamaApi) |
| 175 | + .defaultOptions(OllamaOptions.builder().model("BATCH_MODEL").build()) |
| 176 | + .build(); |
| 177 | + |
| 178 | + EmbeddingResponse response = embeddingModel |
| 179 | + .call(new EmbeddingRequest(largeInputs, EmbeddingOptionsBuilder.builder().build())); |
| 180 | + |
| 181 | + assertThat(response.getResults()).hasSize(3); |
| 182 | + assertThat(response.getResults().get(0).getOutput()).hasSize(4); |
| 183 | + assertThat(response.getResults().get(1).getOutput()).hasSize(4); |
| 184 | + assertThat(response.getResults().get(2).getOutput()).hasSize(4); |
| 185 | + |
| 186 | + assertThat(this.embeddingsRequestCaptor.getValue().input()).isEqualTo(largeInputs); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void embeddingWithCustomKeepAliveFormats() { |
| 191 | + given(this.ollamaApi.embed(this.embeddingsRequestCaptor.capture())) |
| 192 | + .willReturn(new EmbeddingsResponse("KEEPALIVE_MODEL", List.of(new float[] { 1.0f }), 5L, 2L, 1)); |
| 193 | + |
| 194 | + var embeddingModel = OllamaEmbeddingModel.builder() |
| 195 | + .ollamaApi(this.ollamaApi) |
| 196 | + .defaultOptions(OllamaOptions.builder().model("KEEPALIVE_MODEL").build()) |
| 197 | + .build(); |
| 198 | + |
| 199 | + // Test with seconds format |
| 200 | + var secondsOptions = OllamaOptions.builder().model("KEEPALIVE_MODEL").keepAlive("300s").build(); |
| 201 | + |
| 202 | + embeddingModel.call(new EmbeddingRequest(List.of("Keep alive seconds"), secondsOptions)); |
| 203 | + assertThat(this.embeddingsRequestCaptor.getValue().keepAlive()).isEqualTo(Duration.ofSeconds(300)); |
| 204 | + |
| 205 | + // Test with hours format |
| 206 | + var hoursOptions = OllamaOptions.builder().model("KEEPALIVE_MODEL").keepAlive("2h").build(); |
| 207 | + |
| 208 | + embeddingModel.call(new EmbeddingRequest(List.of("Keep alive hours"), hoursOptions)); |
| 209 | + assertThat(this.embeddingsRequestCaptor.getValue().keepAlive()).isEqualTo(Duration.ofHours(2)); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void embeddingResponseMetadata() { |
| 214 | + given(this.ollamaApi.embed(this.embeddingsRequestCaptor.capture())) |
| 215 | + .willReturn(new EmbeddingsResponse("METADATA_MODEL", List.of(new float[] { 0.1f, 0.2f }), 100L, 50L, 25)); |
| 216 | + |
| 217 | + var embeddingModel = OllamaEmbeddingModel.builder() |
| 218 | + .ollamaApi(this.ollamaApi) |
| 219 | + .defaultOptions(OllamaOptions.builder().model("METADATA_MODEL").build()) |
| 220 | + .build(); |
| 221 | + |
| 222 | + EmbeddingResponse response = embeddingModel |
| 223 | + .call(new EmbeddingRequest(List.of("Metadata test"), EmbeddingOptionsBuilder.builder().build())); |
| 224 | + |
| 225 | + assertThat(response.getMetadata().getModel()).isEqualTo("METADATA_MODEL"); |
| 226 | + assertThat(response.getResults()).hasSize(1); |
| 227 | + assertThat(response.getResults().get(0).getMetadata()).isEqualTo(EmbeddingResultMetadata.EMPTY); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void embeddingWithZeroLengthVectors() { |
| 232 | + given(this.ollamaApi.embed(this.embeddingsRequestCaptor.capture())) |
| 233 | + .willReturn(new EmbeddingsResponse("ZERO_MODEL", List.of(new float[] {}), 0L, 0L, 1)); |
| 234 | + |
| 235 | + var embeddingModel = OllamaEmbeddingModel.builder() |
| 236 | + .ollamaApi(this.ollamaApi) |
| 237 | + .defaultOptions(OllamaOptions.builder().model("ZERO_MODEL").build()) |
| 238 | + .build(); |
| 239 | + |
| 240 | + EmbeddingResponse response = embeddingModel |
| 241 | + .call(new EmbeddingRequest(List.of("Zero length test"), EmbeddingOptionsBuilder.builder().build())); |
| 242 | + |
| 243 | + assertThat(response.getResults()).hasSize(1); |
| 244 | + assertThat(response.getResults().get(0).getOutput()).isEmpty(); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + public void builderValidation() { |
| 249 | + // Test that builder requires ollamaApi |
| 250 | + assertThatThrownBy(() -> OllamaEmbeddingModel.builder().build()).isInstanceOf(IllegalArgumentException.class); |
| 251 | + |
| 252 | + // Test successful builder with minimal required parameters |
| 253 | + var model = OllamaEmbeddingModel.builder().ollamaApi(this.ollamaApi).build(); |
| 254 | + |
| 255 | + assertThat(model).isNotNull(); |
| 256 | + } |
| 257 | + |
118 | 258 | }
|
0 commit comments