public ResponseEntity receiveMessage()

in sessions/next24/books-genai-vertex-springai/src/main/java/services/web/ImageProcessingController.java [112:164]


    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 to Cloud Storage" + fileName);

        // multi-modal call to retrieve text from the uploaded image
        // property file ```promptImage: ${PROMPT_IMAGE:Extract the title and author from the image, strictly in JSON format}```
        String response = vertexAIClient.promptOnImage(promptImage, bucketName, fileName, model);

        // 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("Image Analysis Result: Author %s, Title %s", title, author));

        // retrieve the book summary from the database
        String summary = booksService.getBookSummary(title);
        logger.info("The summary of the book "+ title+ ",as retrieved from the database, is: " + summary);
        logger.info("End of summary of the book "+ title+ ",as retrieved from the database");

        // Function calling BookStoreService
        SystemMessage systemMessage = new SystemMessage("""
                Use Multi-turn function calling.
                Answer with precision.
                If the information was not fetched call the function again. Repeat at most 3 times.
                """);

        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(systemMessage, 
                                                                               userMessage, 
                                                                               "bookStoreAvailability",
                                                                               model);

        // 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<>(HttpStatus.OK);
    }