Skip to content

Commit

Permalink
feat: add chatclient
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed May 1, 2024
1 parent cc30c52 commit d2b4c8c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package ch.xxx.aidoclibchat.domain.common;

import org.springframework.http.MediaType;

public class MetaData {
public enum DataType {
DOCUMENT, TABLE, COLUMN, ROW
Expand All @@ -22,18 +24,24 @@ public enum DocumentType {
};

public enum ImageType {
JPEG("jpg"), PNG("png"), SVG("svg"), UNKNOWN("unknown");
JPEG("jpg", MediaType.IMAGE_JPEG_VALUE), PNG("png", MediaType.IMAGE_JPEG_VALUE), SVG("svg", "image/svg+xml"), UNKNOWN("unknown", "unknown");

private String type;
private String mediaType;

private ImageType(String type) {
private ImageType(String type, String mediaType) {
this.type = type;
this.mediaType = mediaType;
}

@Override
public String toString() {
return this.type;
}

public String getMediaType() {
return this.mediaType;
}
}

public static final String ID = "id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,45 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.messages.Media;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeType;

import ch.xxx.aidoclibchat.domain.model.dto.ImageDto;

@Service
public class ImageService {
private static final Logger LOG = LoggerFactory.getLogger(ImageService.class);

private ChatClient chatClient;

public ImageService(ChatClient chatClient) {
this.chatClient = chatClient;
}

public String queryImage(ImageDto imageDto) {
imageDto = resizeImage(imageDto);
return "";
var prompt = new Prompt(new UserMessage(imageDto.getQuery(), List
.of(new Media(MimeType.valueOf(imageDto.getImageType().getMediaType()), imageDto.getImageContent()))));
var response = this.chatClient.call(prompt);
return response.getResult().getOutput().getContent();
}

private ImageDto resizeImage(ImageDto imageDto) {
try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageDto.getImageContent()));
int targetHeight = image.getHeight();
int targetWidth = image.getWidth();
if(image.getHeight() > 672 && image.getWidth() > 672) {
if(image.getHeight() > image.getWidth()) {
if (image.getHeight() > 672 && image.getWidth() > 672) {
if (image.getHeight() < image.getWidth()) {
targetHeight = image.getHeight() / (image.getHeight() / 672);
targetWidth = image.getWidth() / (image.getHeight() / 672);
} else {
Expand All @@ -50,13 +64,14 @@ private ImageDto resizeImage(ImageDto imageDto) {
}
}
var outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH), 0,0, null);
outputImage.getGraphics().drawImage(image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH),
0, 0, null);
var ios = new ByteArrayOutputStream();
ImageIO.write(outputImage, imageDto.getImageType().toString(), ios);
ImageIO.write(outputImage, imageDto.getImageType().toString(), ios);
imageDto.setImageContent(ios.toByteArray());
imageDto.setContentSize(ios.toByteArray().length);
} catch (IOException e) {
LOG.info("Image resize failed.",e);
LOG.info("Image resize failed.", e);
}
return imageDto;
}
Expand Down

0 comments on commit d2b4c8c

Please sign in to comment.