We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating an user image message from a URL is straight forward using UserMessage.buildImageMessage.
UserMessage.buildImageMessage
Creating an user image message from a local image file is much more tedious, here's some sample code:
public void postDiagramImage(String userMessageText, Path diagramImagePath) { List<ChatMessage> messages = ... UserMessage imageMessage = createImageMessage(userMessageText, diagramImagePath); messages.add(imageMessage); ... } private UserMessage createImageMessage(String userMessageText, Path imagePath) { ImageContent userMessageContent = new ImageContent(userMessageText); String imagePathString = imagePath.toAbsolutePath().toString(); String extension = imagePathString.substring(imagePathString.lastIndexOf('.') + 1); String imageUrl = "data:image/" + extension + ";base64," + encodeImage(imagePath); ImageContent imageContent = new ImageContent(new ImageUrl(imageUrl)); UserMessage imageMessage = new UserMessage(List.of(userMessageContent, imageContent)); return imageMessage; } private String encodeImage(Path imagePath) { byte[] fileContent; try { fileContent = Files.readAllBytes(imagePath); return Base64.getEncoder().encodeToString(fileContent); } catch (IOException e){ throw new RuntimeException(e); } }
It would be great if code like this could be encapsulated, and provided to the user in a friendly manner.
The text was updated successfully, but these errors were encountered:
ok
Sorry, something went wrong.
now add method in UserMessage.java
/** * 构件一个图片识别请求消息,支持多个图片 * @author liangtao * @date 2024/8/15 * @param prompt query text * @param imagePaths 文件本地路径 * @return com.theokanning.openai.completion.chat.UserMessage **/ public static UserMessage buildImageMessage(String prompt, Path... imagePaths) { List<ImageContent> imageContents = Arrays.stream(imagePaths).map(ImageContent::new).collect(Collectors.toList()); imageContents.add(0, new ImageContent(prompt)); return new UserMessage(imageContents); } ```java
Works great. Thank you.
No branches or pull requests
Creating an user image message from a URL is straight forward using
UserMessage.buildImageMessage
.Creating an user image message from a local image file is much more tedious, here's some sample code:
It would be great if code like this could be encapsulated, and provided to the user in a friendly manner.
The text was updated successfully, but these errors were encountered: