From 286d4e029d0fde9fe99a5c934416f656b5b8ba1a Mon Sep 17 00:00:00 2001 From: Angular2guy Date: Sun, 12 May 2024 10:39:42 +0200 Subject: [PATCH] feat: add import --- .../adapter/controller/ImageController.java | 4 +- .../usecase/service/ImageService.java | 37 ++++++++++++------- .../angular/src/app/service/image.service.ts | 2 +- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/ImageController.java b/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/ImageController.java index d8c37ca..e47c1f3 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/ImageController.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/ImageController.java @@ -39,8 +39,8 @@ public ImageController(ImageMapper imageMapper, ImageService imageService) { } @PostMapping("/query") - public List postImageQuery(@RequestParam("query") String query,@RequestParam("type") String type, @RequestParam("file") MultipartFile imageQuery) { - var result = this.imageService.queryImage(this.imageMapper.map(imageQuery, query)); + public List postImageQuery(@RequestParam("query") String query,@RequestParam("type") String type) { + var result = this.imageService.queryImage(query); return result; } diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/ImageService.java b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/ImageService.java index cd76200..8667e5e 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/ImageService.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/ImageService.java @@ -18,8 +18,10 @@ import java.io.IOException; import java.util.Base64; import java.util.List; +import java.util.Map.Entry; import java.util.UUID; import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.imageio.ImageIO; @@ -57,6 +59,9 @@ public class ImageService { private record ResultData(String answer, ImageQueryDto imageQueryDto) { } + private record ImageContainer(Document document, Image image, Float distance) { + } + public ImageService(ChatClient chatClient, ImageRepository imageRepository, DocumentVsRepository documentVsRepository) { this.chatClient = chatClient; @@ -77,8 +82,8 @@ public ImageDto importImage(ImageQueryDto imageDto, Image image) { resultData.imageQueryDto().getImageType()); } - public List queryImage(ImageQueryDto imageDto) { - var aiDocuments = this.documentVsRepository.retrieve(imageDto.getQuery(), MetaData.DataType.IMAGE).stream() + public List queryImage(String imageQuery) { + var aiDocuments = this.documentVsRepository.retrieve(imageQuery, MetaData.DataType.IMAGE).stream() .filter(myDoc -> myDoc.getMetadata().get(MetaData.DATATYPE).equals(DataType.IMAGE.toString())) .sorted((myDocA, myDocB) -> ((Float) myDocA.getMetadata().get(MetaData.DISTANCE)) .compareTo(((Float) myDocB.getMetadata().get(MetaData.DISTANCE)))) @@ -87,18 +92,7 @@ public List queryImage(ImageQueryDto imageDto) { .findAllById(aiDocuments.stream().map(myDoc -> (String) myDoc.getMetadata().get(MetaData.ID)) .map(myUuid -> UUID.fromString(myUuid)).toList()) .stream().collect(Collectors.toMap(myDoc -> myDoc.getId(), myDoc -> myDoc)); - record Container(Document document, Image image, Float distance) { - } - return imageMap.entrySet().stream().map(myEntry -> new Container( - aiDocuments.stream() - .filter(myDoc -> myEntry.getKey().toString() - .equals((String) myDoc.getMetadata().get(MetaData.ID))) - .findFirst().orElseThrow(), - myEntry.getValue(), - aiDocuments.stream() - .filter(myDoc -> myEntry.getKey().toString() - .equals((String) myDoc.getMetadata().get(MetaData.ID))) - .map(myDoc -> (Float) myDoc.getMetadata().get(MetaData.DISTANCE)).findFirst().orElseThrow())) + return imageMap.entrySet().stream().map(myEntry -> createImageContainer(aiDocuments, myEntry)) .sorted((containerA, containerB) -> containerA.distance().compareTo(containerB.distance())) .map(myContainer -> new ImageDto(myContainer.document().getContent(), Base64.getEncoder().encodeToString(myContainer.image().getImageContent()), @@ -106,6 +100,21 @@ record Container(Document document, Image image, Float distance) { .limit(this.resultSize).toList(); } + private ImageContainer createImageContainer(List aiDocuments, Entry myEntry) { + return new ImageContainer( + createIdFilteredStream(aiDocuments, myEntry) + .findFirst().orElseThrow(), + myEntry.getValue(), + createIdFilteredStream(aiDocuments, myEntry) + .map(myDoc -> (Float) myDoc.getMetadata().get(MetaData.DISTANCE)).findFirst().orElseThrow()); + } + + private Stream createIdFilteredStream(List aiDocuments, Entry myEntry) { + return aiDocuments.stream() + .filter(myDoc -> myEntry.getKey().toString() + .equals((String) myDoc.getMetadata().get(MetaData.ID))); + } + private ResultData createAIResult(ImageQueryDto imageDto) { if (ImageType.JPEG.equals(imageDto.getImageType()) || ImageType.PNG.equals(imageDto.getImageType())) { imageDto = this.resizeImage(imageDto); diff --git a/frontend/src/angular/src/app/service/image.service.ts b/frontend/src/angular/src/app/service/image.service.ts index f519123..15b8a16 100644 --- a/frontend/src/angular/src/app/service/image.service.ts +++ b/frontend/src/angular/src/app/service/image.service.ts @@ -23,6 +23,6 @@ export class ImageService { constructor(private httpClient: HttpClient) { } public postImageForm(formData: FormData): Observable { - return this.httpClient.post('/rest/image/query', formData); + return this.httpClient.post('/rest/image/import', formData); } }