in sessions/next24/books-genai-vertex-springai/src/main/java/services/domain/BooksService.java [105:151]
public String createBookSummary(BufferedReader reader, String fileName) {
String summary = "";
try {
String bookTitle = FileUtility.getTitle(fileName);
bookTitle = SqlUtility.replaceUnderscoresWithSpaces(bookTitle);
summary = getBookSummary(bookTitle);
if (!summary.isEmpty()) {
return summary;
}
summary = "";
Map<String, Object> book = dao.findBook(bookTitle);
Integer bookId = (Integer) book.get("book_id");
String content="";
Integer page = 1;
char[] cbuf = new char[summaryChunkCharacters];
int charsRead;
String context = "";
logger.info("The prompt build summary: " +promptSubSummary.formatted(context, content));
while ((charsRead = reader.read(cbuf)) != -1) {
content = new String(cbuf, 0, charsRead);
try {
context = vertexAIClient.promptModel(promptSubSummary.formatted(context, content), model);
} catch (io.grpc.StatusRuntimeException statusRuntimeException) {
logger.warn("vertexAIClient.promptModel(promptSubSummary.formatted(context, content)) statusRuntimeException: " + statusRuntimeException.getMessage());
continue;
} catch (RuntimeException e) {
logger.warn("Failed to interact with Vertex AI model: "+e.getMessage(), e);
continue;
}
summary += "\n"+context;
if(page%10==0)
logger.info("The prompt build summary: " +summary);
page++;
}
reader.close();
logger.info("The book "+bookTitle +" has pages: " +page);
logger.info("The summary for book "+bookTitle +" is: " +summary);
logger.info("The prompt summary: " +promptSummary.formatted(summary));
summary = vertexAIClient.promptModel(promptSummary.formatted(summary), model);
dao.insertSummaries(bookId, summary);
} catch (FileNotFoundException e) {
logger.error("File %s not found. Failed with exception %s", fileName, e.getMessage());
} catch (IOException e) {
logger.error("Reading from file %s failure. Failed with exception %s", fileName, e.getMessage());
}
return summary;
}