|
1 | 1 | /*
|
2 |
| - * Copyright 2023-2024 the original author or authors. |
| 2 | + * Copyright 2023-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.ai.mistralai;
|
18 | 18 |
|
19 |
| -import java.util.List; |
20 |
| - |
21 | 19 | import org.junit.jupiter.api.Test;
|
22 | 20 | import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
23 |
| - |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.CsvSource; |
24 | 23 | import org.springframework.ai.embedding.EmbeddingRequest;
|
| 24 | +import org.springframework.ai.mistralai.api.MistralAiApi; |
25 | 25 | import org.springframework.beans.factory.annotation.Autowired;
|
26 | 26 | import org.springframework.boot.test.context.SpringBootTest;
|
27 | 27 |
|
| 28 | +import java.util.List; |
| 29 | + |
28 | 30 | import static org.assertj.core.api.Assertions.assertThat;
|
29 | 31 |
|
30 | 32 | @SpringBootTest(classes = MistralAiTestConfiguration.class)
|
31 | 33 | @EnabledIfEnvironmentVariable(named = "MISTRAL_AI_API_KEY", matches = ".+")
|
32 | 34 | class MistralAiEmbeddingIT {
|
33 | 35 |
|
| 36 | + private static final int MISTRAL_EMBED_DIMENSIONS = 1024; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private MistralAiApi mistralAiApi; |
| 40 | + |
34 | 41 | @Autowired
|
35 | 42 | private MistralAiEmbeddingModel mistralAiEmbeddingModel;
|
36 | 43 |
|
37 | 44 | @Test
|
38 | 45 | void defaultEmbedding() {
|
39 |
| - assertThat(this.mistralAiEmbeddingModel).isNotNull(); |
40 |
| - var embeddingResponse = this.mistralAiEmbeddingModel.embedForResponse(List.of("Hello World")); |
| 46 | + var embeddingResponse = mistralAiEmbeddingModel.embedForResponse(List.of("Hello World")); |
41 | 47 | assertThat(embeddingResponse.getResults()).hasSize(1);
|
42 | 48 | assertThat(embeddingResponse.getResults().get(0)).isNotNull();
|
43 |
| - assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1024); |
| 49 | + assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(MISTRAL_EMBED_DIMENSIONS); |
44 | 50 | assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo("mistral-embed");
|
45 | 51 | assertThat(embeddingResponse.getMetadata().getUsage().getTotalTokens()).isEqualTo(4);
|
46 | 52 | assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(4);
|
47 |
| - assertThat(this.mistralAiEmbeddingModel.dimensions()).isEqualTo(1024); |
| 53 | + assertThat(mistralAiEmbeddingModel.dimensions()).isEqualTo(MISTRAL_EMBED_DIMENSIONS); |
48 | 54 | }
|
49 | 55 |
|
50 |
| - @Test |
51 |
| - void embeddingTest() { |
52 |
| - assertThat(this.mistralAiEmbeddingModel).isNotNull(); |
53 |
| - var embeddingResponse = this.mistralAiEmbeddingModel.call(new EmbeddingRequest( |
54 |
| - List.of("Hello World", "World is big"), |
55 |
| - MistralAiEmbeddingOptions.builder().withModel("mistral-embed").withEncodingFormat("float").build())); |
| 56 | + @ParameterizedTest |
| 57 | + @CsvSource({ "mistral-embed, 1024", "codestral-embed, 1536" }) |
| 58 | + void defaultOptionsEmbedding(String model, int dimensions) { |
| 59 | + var mistralAiEmbeddingOptions = MistralAiEmbeddingOptions.builder().withModel(model).build(); |
| 60 | + var anotherMistralAiEmbeddingModel = new MistralAiEmbeddingModel(mistralAiApi, mistralAiEmbeddingOptions); |
| 61 | + var embeddingResponse = anotherMistralAiEmbeddingModel.embedForResponse(List.of("Hello World", "World is big")); |
56 | 62 | assertThat(embeddingResponse.getResults()).hasSize(2);
|
57 |
| - assertThat(embeddingResponse.getResults().get(0)).isNotNull(); |
58 |
| - assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1024); |
59 |
| - assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo("mistral-embed"); |
| 63 | + embeddingResponse.getResults().forEach(result -> { |
| 64 | + assertThat(result).isNotNull(); |
| 65 | + assertThat(result.getOutput()).hasSize(dimensions); |
| 66 | + }); |
| 67 | + assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo(model); |
60 | 68 | assertThat(embeddingResponse.getMetadata().getUsage().getTotalTokens()).isEqualTo(9);
|
61 | 69 | assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(9);
|
62 |
| - assertThat(this.mistralAiEmbeddingModel.dimensions()).isEqualTo(1024); |
| 70 | + assertThat(anotherMistralAiEmbeddingModel.dimensions()).isEqualTo(dimensions); |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @CsvSource({ "mistral-embed, 1024", "codestral-embed, 1536" }) |
| 75 | + void calledOptionsEmbedding(String model, int dimensions) { |
| 76 | + var mistralAiEmbeddingOptions = MistralAiEmbeddingOptions.builder().withModel(model).build(); |
| 77 | + var embeddingRequest = new EmbeddingRequest(List.of("Hello World", "World is big", "We are small"), |
| 78 | + mistralAiEmbeddingOptions); |
| 79 | + var embeddingResponse = mistralAiEmbeddingModel.call(embeddingRequest); |
| 80 | + assertThat(embeddingResponse.getResults()).hasSize(3); |
| 81 | + embeddingResponse.getResults().forEach(result -> { |
| 82 | + assertThat(result).isNotNull(); |
| 83 | + assertThat(result.getOutput()).hasSize(dimensions); |
| 84 | + }); |
| 85 | + assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo(model); |
| 86 | + assertThat(embeddingResponse.getMetadata().getUsage().getTotalTokens()).isEqualTo(14); |
| 87 | + assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(14); |
| 88 | + assertThat(mistralAiEmbeddingModel.dimensions()).isEqualTo(MISTRAL_EMBED_DIMENSIONS); |
63 | 89 | }
|
64 | 90 |
|
65 | 91 | }
|
0 commit comments