in sessions/fall24/books-genai-vertex-langchain4j/src/main/java/services/web/ImageProcessingController.java [105:147]
public ResponseEntity<String> receiveMessage(
@RequestBody Map<String, Object> body, @RequestHeader Map<String, String> headers) throws IOException, InterruptedException, ExecutionException {
String errorMsg = RequestValidationUtility.validateRequest(body,headers);
if (!errorMsg.isBlank()) {
return new ResponseEntity<>(errorMsg, HttpStatus.BAD_REQUEST);
}
String fileName = (String)body.get("name");
String bucketName = (String)body.get("bucket");
logger.info("New picture uploaded " + fileName);
// multi-modal call to retrieve text from the uploaded image
String response = vertexAIClient.promptOnImage(promptImage, bucketName, fileName);
// parse the response and extract the data
Map<String, Object> jsonMap = JsonUtility.parseJsonToMap(response);
// get book details
String title = (String) jsonMap.get("title");
String author = (String) jsonMap.get("author");
logger.info(String.format("Result: Author %s, Title %s", author, title));
// retrieve the book summary from the database
String summary = booksService.getBookSummary(title);
logger.info("The summary of the book:"+ title+ " is: " + summary);
// Function calling BookStoreService
UserMessage userMessage = new UserMessage(
String.format("Write a nice note including book author, book title and availability. Find out if the book with the title %s by author %s is available in the University bookstore.Please add also this book summary to the response, with the text available after the column, prefix it with My Book Summary: %s",
title, author, summary));
String bookStoreResponse = vertexAIClient.promptModelwithFunctionCalls(userMessage,
new BookStoreService());
// Saving result to Firestore
if (bookStoreResponse != null) {
ApiFuture<WriteResult> writeResult = eventService.storeBookInfo(fileName, title, author, summary, bookStoreResponse);
logger.info("Picture metadata saved in Firestore at " + writeResult.get().getUpdateTime());
}
return new ResponseEntity<String>(HttpStatus.OK);
}