Skip to content

Commit f06ef47

Browse files
committed
format code
1 parent dcb523b commit f06ef47

File tree

8 files changed

+106
-110
lines changed

8 files changed

+106
-110
lines changed

langchain4j-community-core/src/test/java/dev/langchain4j/community/chain/RetrievalQAChainTest.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
@ExtendWith(MockitoExtension.class)
3434
public class RetrievalQAChainTest {
3535

36-
private static final Query QUERY = Query.from("query");
37-
private static final String ANSWER = "answer";
3836
public static final PromptTemplate promptTemplate = PromptTemplate.from(
3937
"""
4038
Answer the question based only on the context provided.
@@ -47,6 +45,8 @@ public class RetrievalQAChainTest {
4745
4846
Answer:
4947
""");
48+
private static final Query QUERY = Query.from("query");
49+
private static final String ANSWER = "answer";
5050

5151
@Mock
5252
ChatModel chatModel;
@@ -83,12 +83,12 @@ void should_inject_retrieved_segments_when_content_available() {
8383
verify(chatModel).chat(messagesCaptor.capture());
8484
String expectedUserMessage =
8585
"""
86-
query
87-
88-
Answer using the following information:
89-
Segment 1
90-
91-
Segment 2""";
86+
query
87+
88+
Answer using the following information:
89+
Segment 1
90+
91+
Segment 2""";
9292
assertThat(messagesCaptor.getValue()).isEqualTo(expectedUserMessage);
9393
}
9494

@@ -100,14 +100,14 @@ void should_inject_retrieved_segments_when_custom_prompt_template_used() {
100100

101101
PromptTemplate promptTemplate = PromptTemplate.from(
102102
"""
103-
Answer the question based only on the context provided.
103+
Answer the question based only on the context provided.
104104
105-
Context: {{contents}}
105+
Context: {{contents}}
106106
107-
Question: {{userMessage}}
107+
Question: {{userMessage}}
108108
109-
Answer:
110-
""");
109+
Answer:
110+
""");
111111

112112
// -- PromptTemplate via contentInjector
113113
RetrievalQAChain chain = RetrievalQAChain.builder()
@@ -129,15 +129,15 @@ void should_inject_retrieved_segments_when_custom_prompt_template_used() {
129129
verify(chatModel).chat(messagesCaptor.capture());
130130
String expectedUserMessage =
131131
"""
132-
Answer the question based only on the context provided.
133-
Context:
134-
Segment 1
135-
Segment 2
136-
137-
Question:
138-
query
139-
Answer:
140-
""";
132+
Answer the question based only on the context provided.
133+
Context:
134+
Segment 1
135+
Segment 2
136+
137+
Question:
138+
query
139+
Answer:
140+
""";
141141
assertThat(messagesCaptor.getValue()).isEqualToIgnoringWhitespace(expectedUserMessage);
142142

143143
// -- PromptTemplate via prompt builder
@@ -191,15 +191,15 @@ void should_inject_retrieved_segments_when_custom_prompt_template_and_metadata_u
191191
verify(chatModel).chat(messagesCaptor.capture());
192192
String expectedUserMessage =
193193
"""
194-
Answer the question based only on the context provided.
195-
Context:
196-
Segment 1 with meta
197-
Segment 2 with meta
198-
199-
Question:
200-
query
201-
Answer:
202-
""";
194+
Answer the question based only on the context provided.
195+
Context:
196+
Segment 1 with meta
197+
Segment 2 with meta
198+
199+
Question:
200+
query
201+
Answer:
202+
""";
203203
assertThat(messagesCaptor.getValue()).isEqualToIgnoringWhitespace(expectedUserMessage);
204204
}
205205

langchain4j-community-core/src/test/java/dev/langchain4j/community/data/document/transformer/graph/GraphTransformerTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
class GraphTransformerTest {
1212

13-
/**
14-
* A mock {@link GraphTransformer} which extract nodes from YAML document
15-
*/
16-
static class MockGraphTransformer implements GraphTransformer {
17-
18-
@Override
19-
public GraphDocument transform(Document document) {
20-
if (document.text().contains("valid")) {
21-
return GraphDocument.from(new HashSet<>(), new HashSet<>(), document);
22-
}
23-
return null;
24-
}
25-
}
26-
2713
@Test
2814
void should_return_empty_list_when_input_is_null() {
2915
GraphTransformer graphTransformer = new MockGraphTransformer();
@@ -62,4 +48,18 @@ void should_return_transformed_documents_when_input_provided() {
6248
// then
6349
assertThat(graphDocuments).hasSize(2).allMatch(g -> g.source().text().contains("valid"));
6450
}
51+
52+
/**
53+
* A mock {@link GraphTransformer} which extract nodes from YAML document
54+
*/
55+
static class MockGraphTransformer implements GraphTransformer {
56+
57+
@Override
58+
public GraphDocument transform(Document document) {
59+
if (document.text().contains("valid")) {
60+
return GraphDocument.from(new HashSet<>(), new HashSet<>(), document);
61+
}
62+
return null;
63+
}
64+
}
6565
}

langchain4j-community-core/src/test/java/dev/langchain4j/community/rag/content/aggregator/MmrContentAggregatorTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@
4141

4242
class MmrContentAggregatorTest {
4343

44+
static Stream<Arguments> should_apply_mmr_with_auto_strategy_selection() {
45+
return Stream.<Arguments>builder()
46+
.add(Arguments.of((Function<EmbeddingModel, MmrContentAggregator>) MmrContentAggregator::new))
47+
.add(Arguments.of((Function<EmbeddingModel, MmrContentAggregator>)
48+
(embeddingModel) -> MmrContentAggregator.builder()
49+
.embeddingModel(embeddingModel)
50+
.build()))
51+
.build();
52+
}
53+
54+
private static Stream<Arguments> should_return_empty_list_when_no_content() {
55+
return Stream.<Arguments>builder()
56+
.add(Arguments.of(emptyMap()))
57+
.add(Arguments.of(singletonMap(Query.from("query"), emptyList())))
58+
.add(Arguments.of(singletonMap(Query.from("query"), singletonList(emptyList()))))
59+
.add(Arguments.of(singletonMap(Query.from("query"), asList(emptyList(), emptyList()))))
60+
.build();
61+
}
62+
4463
@ParameterizedTest
4564
@MethodSource
4665
void should_apply_mmr_with_auto_strategy_selection(
@@ -83,16 +102,6 @@ void should_apply_mmr_with_auto_strategy_selection(
83102
assertThat(aggregated.get(0)).isEqualTo(content1); // most relevant first
84103
}
85104

86-
static Stream<Arguments> should_apply_mmr_with_auto_strategy_selection() {
87-
return Stream.<Arguments>builder()
88-
.add(Arguments.of((Function<EmbeddingModel, MmrContentAggregator>) MmrContentAggregator::new))
89-
.add(Arguments.of((Function<EmbeddingModel, MmrContentAggregator>)
90-
(embeddingModel) -> MmrContentAggregator.builder()
91-
.embeddingModel(embeddingModel)
92-
.build()))
93-
.build();
94-
}
95-
96105
@Test
97106
void should_use_manual_strategy_when_provided() {
98107
// given
@@ -163,8 +172,8 @@ void should_apply_lambda_parameter_correctly() {
163172
void should_fail_when_multiple_queries_with_default_query_selector() {
164173
// given
165174
Map<Query, Collection<List<Content>>> queryToContents = new HashMap<>();
166-
queryToContents.put(Query.from("query 1"), singletonList(asList()));
167-
queryToContents.put(Query.from("query 2"), singletonList(asList()));
175+
queryToContents.put(Query.from("query 1"), singletonList(List.of()));
176+
queryToContents.put(Query.from("query 2"), singletonList(List.of()));
168177

169178
EmbeddingModel embeddingModel = mock(EmbeddingModel.class);
170179
MmrContentAggregator aggregator = new MmrContentAggregator(embeddingModel);
@@ -189,8 +198,8 @@ void should_handle_multiple_queries_with_custom_query_selector() {
189198
Content content2 = Content.from("content 2");
190199

191200
Map<Query, Collection<List<Content>>> queryToContents = new LinkedHashMap<>();
192-
queryToContents.put(query1, singletonList(asList(content1)));
193-
queryToContents.put(query2, singletonList(asList(content2)));
201+
queryToContents.put(query1, singletonList(List.of(content1)));
202+
queryToContents.put(query2, singletonList(List.of(content2)));
194203

195204
EmbeddingModel embeddingModel = mock(EmbeddingModel.class);
196205

@@ -303,22 +312,13 @@ void should_return_empty_list_when_no_content(Map<Query, Collection<List<Content
303312
verifyNoInteractions(embeddingModel);
304313
}
305314

306-
private static Stream<Arguments> should_return_empty_list_when_no_content() {
307-
return Stream.<Arguments>builder()
308-
.add(Arguments.of(emptyMap()))
309-
.add(Arguments.of(singletonMap(Query.from("query"), emptyList())))
310-
.add(Arguments.of(singletonMap(Query.from("query"), singletonList(emptyList()))))
311-
.add(Arguments.of(singletonMap(Query.from("query"), asList(emptyList(), emptyList()))))
312-
.build();
313-
}
314-
315315
@Test
316316
void should_use_force_embedding_generation() {
317317
// given
318318
Query query = Query.from("test query");
319319
Content content1 = Content.from("content 1");
320320

321-
Map<Query, Collection<List<Content>>> queryToContents = singletonMap(query, singletonList(asList(content1)));
321+
Map<Query, Collection<List<Content>>> queryToContents = singletonMap(query, singletonList(List.of(content1)));
322322

323323
EmbeddingModel embeddingModel = mock(EmbeddingModel.class);
324324

langchain4j-community-core/src/test/java/dev/langchain4j/community/rag/content/retriever/EmbeddingStoreContentRetrieverTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,14 @@
2727

2828
class EmbeddingStoreContentRetrieverTest {
2929

30-
private static EmbeddingStore<TextSegment> EMBEDDING_STORE;
31-
32-
private static EmbeddingModel EMBEDDING_MODEL;
30+
public static final double DEFAULT_MIN_SCORE = 0.0;
3331
private static final Embedding EMBEDDING = Embedding.from(asList(1f, 2f, 3f));
34-
3532
private static final Query QUERY = Query.from("query");
36-
3733
private static final int DEFAULT_MAX_RESULTS = 3;
3834
private static final int CUSTOM_MAX_RESULTS = 1;
39-
4035
private static final double CUSTOM_MIN_SCORE = 0.7;
41-
public static final double DEFAULT_MIN_SCORE = 0.0;
36+
private static EmbeddingStore<TextSegment> EMBEDDING_STORE;
37+
private static EmbeddingModel EMBEDDING_MODEL;
4238

4339
@BeforeEach
4440
void beforeEach() {

langchain4j-community-core/src/test/java/dev/langchain4j/community/store/embedding/MmrSelectorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void should_use_existing_scores_when_available() {
155155
void should_handle_single_candidate() {
156156
Embedding queryEmbedding = embedding(1.0f, 0.0f, 0.0f);
157157
List<EmbeddingMatch<TextSegment>> candidates =
158-
asList(match(0.8, "only_candidate", embedding(0.8f, 0.2f, 0.0f)));
158+
List.of(match(0.8, "only_candidate", embedding(0.8f, 0.2f, 0.0f)));
159159

160160
List<EmbeddingMatch<TextSegment>> result = MmrSelector.select(queryEmbedding, candidates, 5, 0.7);
161161

web-search-engines/langchain4j-community-web-search-engine-duckduckgo/src/main/java/dev/langchain4j/community/web/search/duckduckgo/DuckDuckGoSearchRequest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package dev.langchain4j.community.web.search.duckduckgo;
22

33
class DuckDuckGoSearchRequest {
4-
private String query;
5-
private String format;
6-
private Boolean noHtml;
7-
private Boolean skipDisambig;
8-
private String safeSearch;
9-
private String region;
10-
private Integer maxResults;
4+
private final String query;
5+
private final String format;
6+
private final Boolean noHtml;
7+
private final Boolean skipDisambig;
8+
private final String safeSearch;
9+
private final String region;
10+
private final Integer maxResults;
1111

1212
DuckDuckGoSearchRequest(
1313
String query,

web-search-engines/langchain4j-community-web-search-engine-duckduckgo/src/main/java/dev/langchain4j/community/web/search/duckduckgo/DuckDuckGoSearchResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
class DuckDuckGoSearchResult {
44

5-
private String title;
6-
private String url;
7-
private String snippet;
5+
private final String title;
6+
private final String url;
7+
private final String snippet;
88

99
public DuckDuckGoSearchResult(String title, String url, String snippet) {
1010
this.title = title;

web-search-engines/langchain4j-community-web-search-engine-duckduckgo/src/main/java/dev/langchain4j/community/web/search/duckduckgo/DuckDuckGoWebSearchEngine.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ public static Builder builder() {
3535
return new Builder();
3636
}
3737

38+
private static WebSearchOrganicResult toWebSearchOrganicResult(DuckDuckGoSearchResult result) {
39+
return WebSearchOrganicResult.from(result.getTitle(), makeURI(result.getUrl()), result.getSnippet(), null);
40+
}
41+
42+
protected static URI makeURI(String urlString) {
43+
if (urlString == null || urlString.isBlank()) {
44+
throw new IllegalArgumentException("urlString can not be null or blank");
45+
}
46+
return URI.create(urlString.replaceAll("\\s+", "%20"));
47+
}
48+
49+
private static boolean hasValue(String value) {
50+
return value != null && !value.trim().isEmpty();
51+
}
52+
53+
private static boolean includeResult(DuckDuckGoSearchResult result) {
54+
return hasValue(result.getTitle()) && hasValue(result.getUrl()) && hasValue(result.getSnippet());
55+
}
56+
57+
private static int maxResults(WebSearchRequest webSearchRequest) {
58+
return webSearchRequest.maxResults() != null ? webSearchRequest.maxResults() : Integer.MAX_VALUE;
59+
}
60+
3861
@Override
3962
public WebSearchResults search(WebSearchRequest webSearchRequest) {
4063
List<DuckDuckGoSearchResult> results = client.search(webSearchRequest);
@@ -66,29 +89,6 @@ public CompletableFuture<WebSearchResults> searchAsync(WebSearchRequest webSearc
6689
});
6790
}
6891

69-
private static WebSearchOrganicResult toWebSearchOrganicResult(DuckDuckGoSearchResult result) {
70-
return WebSearchOrganicResult.from(result.getTitle(), makeURI(result.getUrl()), result.getSnippet(), null);
71-
}
72-
73-
protected static URI makeURI(String urlString) {
74-
if (urlString == null || urlString.isBlank()) {
75-
throw new IllegalArgumentException("urlString can not be null or blank");
76-
}
77-
return URI.create(urlString.replaceAll("\\s+", "%20"));
78-
}
79-
80-
private static boolean hasValue(String value) {
81-
return value != null && !value.trim().isEmpty();
82-
}
83-
84-
private static boolean includeResult(DuckDuckGoSearchResult result) {
85-
return hasValue(result.getTitle()) && hasValue(result.getUrl()) && hasValue(result.getSnippet());
86-
}
87-
88-
private static int maxResults(WebSearchRequest webSearchRequest) {
89-
return webSearchRequest.maxResults() != null ? webSearchRequest.maxResults() : Integer.MAX_VALUE;
90-
}
91-
9292
/**
9393
* Builder for new instances of {@link DuckDuckGoWebSearchEngine}.
9494
*/

0 commit comments

Comments
 (0)