|
| 1 | +package org.springframework.ai.model.transformer; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 8 | +import org.mockito.ArgumentCaptor; |
| 9 | +import org.mockito.Captor; |
| 10 | +import org.mockito.Mock; |
| 11 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 12 | + |
| 13 | +import org.springframework.ai.chat.messages.AssistantMessage; |
| 14 | +import org.springframework.ai.chat.model.ChatModel; |
| 15 | +import org.springframework.ai.chat.model.ChatResponse; |
| 16 | +import org.springframework.ai.chat.model.Generation; |
| 17 | +import org.springframework.ai.chat.prompt.Prompt; |
| 18 | +import org.springframework.ai.chat.prompt.PromptTemplate; |
| 19 | +import org.springframework.ai.document.Document; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | +import static org.mockito.BDDMockito.given; |
| 24 | +import static org.mockito.Mockito.*; |
| 25 | +import static org.springframework.ai.model.transformer.KeywordMetadataEnricher.*; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author YunKui Lu |
| 29 | + */ |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +class KeywordMetadataEnricherTest { |
| 32 | + |
| 33 | + @Mock |
| 34 | + private ChatModel chatModel; |
| 35 | + |
| 36 | + @Captor |
| 37 | + private ArgumentCaptor<Prompt> promptCaptor; |
| 38 | + |
| 39 | + private final String CUSTOM_TEMPLATE = "Custom template: {context_str}"; |
| 40 | + |
| 41 | + @Test |
| 42 | + void testUseWithDefaultTemplate() { |
| 43 | + // 1. Prepare test data |
| 44 | + // @formatter:off |
| 45 | + List<Document> documents = List.of( |
| 46 | + new Document("content1"), |
| 47 | + new Document("content2"), |
| 48 | + new Document("content3"));// @formatter:on |
| 49 | + int keywordCount = 3; |
| 50 | + |
| 51 | + // 2. Mock |
| 52 | + given(chatModel.call(any(Prompt.class))).willReturn( |
| 53 | + new ChatResponse(List.of(new Generation(new AssistantMessage("keyword1-1, keyword1-2, keyword1-3")))), |
| 54 | + new ChatResponse(List.of(new Generation(new AssistantMessage("keyword2-1, keyword2-2, keyword2-3")))), |
| 55 | + new ChatResponse(List.of(new Generation(new AssistantMessage("keyword3-1, keyword3-2, keyword3-3"))))); |
| 56 | + |
| 57 | + // 3. Create instance |
| 58 | + KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher(chatModel, keywordCount); |
| 59 | + |
| 60 | + // 4. Apply |
| 61 | + keywordMetadataEnricher.apply(documents); |
| 62 | + |
| 63 | + // 5. Assert |
| 64 | + verify(chatModel, times(3)).call(promptCaptor.capture()); |
| 65 | + |
| 66 | + assertThat(promptCaptor.getAllValues().get(0).getUserMessage().getText()) |
| 67 | + .isEqualTo(getDefaultTemplatePromptText(keywordCount, "content1")); |
| 68 | + assertThat(promptCaptor.getAllValues().get(1).getUserMessage().getText()) |
| 69 | + .isEqualTo(getDefaultTemplatePromptText(keywordCount, "content2")); |
| 70 | + assertThat(promptCaptor.getAllValues().get(2).getUserMessage().getText()) |
| 71 | + .isEqualTo(getDefaultTemplatePromptText(keywordCount, "content3")); |
| 72 | + |
| 73 | + assertThat(documents.get(0).getMetadata()).containsEntry(EXCERPT_KEYWORDS_METADATA_KEY, |
| 74 | + "keyword1-1, keyword1-2, keyword1-3"); |
| 75 | + assertThat(documents.get(1).getMetadata()).containsEntry(EXCERPT_KEYWORDS_METADATA_KEY, |
| 76 | + "keyword2-1, keyword2-2, keyword2-3"); |
| 77 | + assertThat(documents.get(2).getMetadata()).containsEntry(EXCERPT_KEYWORDS_METADATA_KEY, |
| 78 | + "keyword3-1, keyword3-2, keyword3-3"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testUseCustomTemplate() { |
| 83 | + // 1. Prepare test data |
| 84 | + // @formatter:off |
| 85 | + List<Document> documents = List.of( |
| 86 | + new Document("content1"), |
| 87 | + new Document("content2"), |
| 88 | + new Document("content3"));// @formatter:on |
| 89 | + PromptTemplate promptTemplate = new PromptTemplate(CUSTOM_TEMPLATE); |
| 90 | + |
| 91 | + // 2. Mock |
| 92 | + given(chatModel.call(any(Prompt.class))).willReturn( |
| 93 | + new ChatResponse(List.of(new Generation(new AssistantMessage("keyword1-1, keyword1-2, keyword1-3")))), |
| 94 | + new ChatResponse(List.of(new Generation(new AssistantMessage("keyword2-1, keyword2-2, keyword2-3")))), |
| 95 | + new ChatResponse(List.of(new Generation(new AssistantMessage("keyword3-1, keyword3-2, keyword3-3"))))); |
| 96 | + |
| 97 | + // 3. Create instance |
| 98 | + KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher(this.chatModel, promptTemplate); |
| 99 | + |
| 100 | + // 4. Apply |
| 101 | + keywordMetadataEnricher.apply(documents); |
| 102 | + |
| 103 | + // 5. Assert |
| 104 | + verify(chatModel, times(documents.size())).call(promptCaptor.capture()); |
| 105 | + |
| 106 | + assertThat(promptCaptor.getAllValues().get(0).getUserMessage().getText()) |
| 107 | + .isEqualTo("Custom template: content1"); |
| 108 | + assertThat(promptCaptor.getAllValues().get(1).getUserMessage().getText()) |
| 109 | + .isEqualTo("Custom template: content2"); |
| 110 | + assertThat(promptCaptor.getAllValues().get(2).getUserMessage().getText()) |
| 111 | + .isEqualTo("Custom template: content3"); |
| 112 | + |
| 113 | + assertThat(documents.get(0).getMetadata()).containsEntry(EXCERPT_KEYWORDS_METADATA_KEY, |
| 114 | + "keyword1-1, keyword1-2, keyword1-3"); |
| 115 | + assertThat(documents.get(1).getMetadata()).containsEntry(EXCERPT_KEYWORDS_METADATA_KEY, |
| 116 | + "keyword2-1, keyword2-2, keyword2-3"); |
| 117 | + assertThat(documents.get(2).getMetadata()).containsEntry(EXCERPT_KEYWORDS_METADATA_KEY, |
| 118 | + "keyword3-1, keyword3-2, keyword3-3"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testConstructorThrowsException() { |
| 123 | + assertThrows(IllegalArgumentException.class, () -> new KeywordMetadataEnricher(null, 3), |
| 124 | + "chatModel must not be null"); |
| 125 | + |
| 126 | + assertThrows(IllegalArgumentException.class, () -> new KeywordMetadataEnricher(chatModel, 0), |
| 127 | + "keywordCount must be >= 1"); |
| 128 | + |
| 129 | + assertThrows(IllegalArgumentException.class, () -> new KeywordMetadataEnricher(chatModel, null), |
| 130 | + "keywordsTemplate must not be null"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void testBuilderThrowsException() { |
| 135 | + assertThrows(IllegalArgumentException.class, () -> KeywordMetadataEnricher.builder(null), |
| 136 | + "The chatModel must not be null"); |
| 137 | + |
| 138 | + Builder builder = builder(chatModel); |
| 139 | + assertThrows(IllegalArgumentException.class, () -> builder.keywordCount(0), "The keywordCount must be >= 1"); |
| 140 | + |
| 141 | + assertThrows(IllegalArgumentException.class, () -> builder.keywordsTemplate(null), |
| 142 | + "The keywordsTemplate must not be null"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void testBuilderWithKeywordCount() { |
| 147 | + int keywordCount = 3; |
| 148 | + KeywordMetadataEnricher enricher = builder(chatModel).keywordCount(keywordCount).build(); |
| 149 | + |
| 150 | + assertThat(enricher.getKeywordsTemplate().getTemplate()) |
| 151 | + .isEqualTo(String.format(KEYWORDS_TEMPLATE, keywordCount)); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void testBuilderWithKeywordsTemplate() { |
| 156 | + PromptTemplate template = new PromptTemplate(CUSTOM_TEMPLATE); |
| 157 | + KeywordMetadataEnricher enricher = builder(chatModel).keywordsTemplate(template).build(); |
| 158 | + |
| 159 | + assertThat(enricher).extracting("chatModel", "keywordsTemplate").containsExactly(chatModel, template); |
| 160 | + } |
| 161 | + |
| 162 | + private String getDefaultTemplatePromptText(int keywordCount, String documentContent) { |
| 163 | + PromptTemplate promptTemplate = new PromptTemplate(String.format(KEYWORDS_TEMPLATE, keywordCount)); |
| 164 | + Prompt prompt = promptTemplate.create(Map.of(CONTEXT_STR_PLACEHOLDER, documentContent)); |
| 165 | + return prompt.getContents(); |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments