Skip to content

Commit fb9c02f

Browse files
committed
Merge branch 'master' into SpringAIUpdate
2 parents 0629878 + fe501b4 commit fb9c02f

File tree

16 files changed

+11271
-27423
lines changed

16 files changed

+11271
-27423
lines changed

backend/src/main/java/ch/xxx/aidoclibchat/adapter/client/OpenLibraryRestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.util.List;
1818
import java.util.Optional;
19+
import java.util.function.Predicate;
1920
import java.util.stream.Collectors;
2021

2122
import org.slf4j.Logger;
@@ -41,7 +42,7 @@ public Response apply(Request request) {
4142
var titleOpt = this.createParamOpt(request.title(), "title");
4243
var subjectOpt = this.createParamOpt(request.subject(), "subject");
4344
var paramsStr = List.of(authorOpt, titleOpt, subjectOpt).stream()
44-
.filter(Optional::isPresent).map(myOpt -> myOpt.get()).collect(Collectors.joining("&"));
45+
.filter(Optional::isPresent).map(Optional::get).collect(Collectors.joining("&"));
4546
var urlStr =
4647
String.format("%s?%s", this.baseUrl, paramsStr);
4748
LOGGER.info(urlStr);
@@ -50,7 +51,7 @@ public Response apply(Request request) {
5051
}
5152

5253
private Optional<String> createParamOpt(String valueStr, String keyStr) {
53-
return Optional.ofNullable(valueStr).stream().filter(myAuthor -> !myAuthor.isBlank())
54+
return Optional.ofNullable(valueStr).stream().filter(Predicate.not(String::isBlank))
5455
.map(myAuthor -> String.format("%s=%s", keyStr, URLEncoder.encode(myAuthor, StandardCharsets.UTF_8))).findFirst();
5556
}
5657
}

backend/src/main/java/ch/xxx/aidoclibchat/adapter/config/ForwardServletFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
6161
} else {
6262
Iterable<Locale> iterable = () -> myRequest.getLocales().asIterator();
6363
Locale userLocale = StreamSupport.stream(iterable.spliterator(), false)
64-
.filter(myLocale -> SUPPORTED_LOCALES.contains(myLocale)).findFirst().orElse(Locale.ENGLISH);
64+
.filter(SUPPORTED_LOCALES::contains).findFirst().orElse(Locale.ENGLISH);
6565
String forwardPath = String.format("/%s/index.html", userLocale.getLanguage());
6666
// LOG.info(String.format("Forward to: %s", forwardPath));
6767
RequestDispatcher dispatcher = myRequest.getServletContext().getRequestDispatcher(forwardPath);

backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/DocumentService.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.Objects;
2122
import java.util.Optional;
2223
import java.util.StringTokenizer;
2324
import java.util.UUID;
2425
import java.util.concurrent.atomic.AtomicReference;
26+
import java.util.function.Predicate;
2527
import java.util.stream.Collectors;
2628
import java.util.stream.Stream;
2729

@@ -142,16 +144,16 @@ public Optional<Book> findBookByUuid(String uuidStr) {
142144

143145
public List<Book> findBooksByTitleAuthor(String titleAuthor) {
144146
return Optional.ofNullable(titleAuthor).stream().filter(myStr -> myStr.trim().length() > 2)
145-
.map(myStr -> myStr.toLowerCase())
147+
.map(String::toLowerCase)
146148
.flatMap(myStr -> Stream.of(this.bookRepository.findByTitleAuthorWithChapters(myStr)))
147-
.flatMap(myList -> myList.stream()).toList();
149+
.flatMap(List::stream).toList();
148150
}
149151

150152
@Async
151153
public void addBookSummaries(Book book) {
152154
var myChapters = book.getChapters().stream().map(myChapter -> this.addChapterSummary(myChapter)).toList();
153155
// LOGGER.info(myChapters.getLast().getSummary());
154-
var summaries = myChapters.stream().map(myChapter -> myChapter.getChapterText())
156+
var summaries = myChapters.stream().map(Chapter::getChapterText)
155157
.reduce((acc, myChapter) -> acc + "\n" + myChapter);
156158
book.setSummary(this.chatClient.prompt().user(u -> u.text(this.bookPrompt).params(Map.of("text", summaries)))
157159
.call().content());
@@ -171,7 +173,7 @@ private Chapter addChapterSummary(final Chapter myChapter) {
171173

172174
private Chapter createChapter(Book book, String heading, AtomicReference<List<String>> atomicRef) {
173175
var result = new Chapter();
174-
result.setTitle(atomicRef.get().stream().filter(myLine -> !myLine.isBlank()).findFirst().orElse(""));
176+
result.setTitle(atomicRef.get().stream().filter(Predicate.not(String::isBlank)).findFirst().orElse(""));
175177
result.setBook(book);
176178
var chapterText = atomicRef.get().stream().takeWhile(myLine -> !myLine.contains(heading))
177179
.collect(Collectors.joining(System.lineSeparator()));
@@ -249,8 +251,9 @@ public AiDocumentResult queryDocuments(SearchDto searchDto) {
249251
private Message getSystemMessage(List<org.springframework.ai.document.Document> similarDocuments, int tokenLimit,
250252
String prompt) {
251253
String documentStr = this.cutStringToTokenLimit(
252-
similarDocuments.stream().map(entry -> entry.getContent())
253-
.filter(myStr -> myStr != null && !myStr.isBlank()).collect(Collectors.joining("\n")),
254+
similarDocuments.stream().map(entry -> entry.getContent())
255+
.filter(Predicate.not(Objects::isNull))
256+
.filter(Predicate.not(String::isBlank)).collect(Collectors.joining("\n")),
254257
tokenLimit);
255258
SystemPromptTemplate systemPromptTemplate = this.activeProfile.contains("ollama")
256259
? new SystemPromptTemplate(this.ollamaPrompt)

backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/TableService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Optional;
2121
import java.util.Set;
2222
import java.util.concurrent.atomic.AtomicReference;
23+
import java.util.function.Predicate;
2324
import java.util.stream.Collectors;
2425
import java.util.stream.Stream;
2526

@@ -226,7 +227,7 @@ private EmbeddingContainer retrieveEmbeddings(SearchDto searchDto) {
226227
.collect(Collectors.joining(" ")));
227228
}
228229
}
229-
var rowDocuments = rowSearchStrs.stream().filter(myStr -> !myStr.isBlank())
230+
var rowDocuments = rowSearchStrs.stream().filter(Predicate.not(String::isBlank))
230231
.flatMap(myStr -> this.documentVsRepository
231232
.retrieve(myStr, MetaData.DataType.ROW, searchDto.getResultAmount()).stream())
232233
.toList();
@@ -286,10 +287,10 @@ private void updateEmbeddings() {
286287
List<Document> rowDocs = Stream.concat(
287288
this.importService.findAllSubjects().stream()
288289
.filter(mySubject -> Optional.ofNullable(mySubject.getSubject()).stream()
289-
.allMatch(mySubjectStr -> !mySubjectStr.isBlank()))
290+
.allMatch(Predicate.not(String::isBlank)))
290291
.map(this::map),
291292
this.importService.findAllWorks().stream().filter(myWork -> Optional.ofNullable(myWork.getStyle())
292-
.stream().allMatch(myStyle -> !myStyle.isBlank())).map(this::map))
293+
.stream().allMatch(Predicate.not(String::isBlank))).map(this::map))
293294
.toList();
294295
this.importService.addDocuments(rowDocs);
295296
LOGGER.info("Row Embeddings updated {}ms", new Date().getTime() - rowStart.getTime());
@@ -320,9 +321,9 @@ private Document map(ColumnMetadata columnMetadata) {
320321
result.getMetadata().put(MetaData.DATANAME, columnMetadata.getColumnName());
321322
result.getMetadata().put(MetaData.TABLE_NAME, columnMetadata.getTableMetadata().getTableName());
322323
result.getMetadata().put(MetaData.PRIMARY_KEY, columnMetadata.isColumnPrimaryKey());
323-
Optional.ofNullable(columnMetadata.getReferenceTableName()).stream().filter(myStr -> !myStr.isBlank())
324+
Optional.ofNullable(columnMetadata.getReferenceTableName()).stream().filter(Predicate.not(String::isBlank))
324325
.findFirst().ifPresent(myStr -> result.getMetadata().put(MetaData.REFERENCE_TABLE, myStr));
325-
Optional.ofNullable(columnMetadata.getReferenceTableColumn()).stream().filter(myStr -> !myStr.isBlank())
326+
Optional.ofNullable(columnMetadata.getReferenceTableColumn()).stream().filter(Predicate.not(String::isBlank))
326327
.findFirst().ifPresent(myStr -> result.getMetadata().put(MetaData.REFERENCE_COLUMN, myStr));
327328
return result;
328329
}

0 commit comments

Comments
 (0)