in sessions/fall24/books-genai-vertex-springai/src/main/java/services/orchestration/BooksService.java [142:181]
public String createBookSummaryWebGrounded(String title, String author, String publicationYear, String bucketName) {
// lookup book summary in the database
String summary = booksDataService.getBookSummary(title);
if (!summary.isEmpty())
return summary;
// find the book in the book table
// extract the book id
Integer bookId = booksDataService.findBookByTitle(title);
// create a SystemMessage
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(textSystemMessage);
Message systemMessage = systemPromptTemplate.createMessage(
Map.of("name", "Gemini", "voice", "literary critic"));
PromptTemplate userPromptTemplate;
Message userMessage;
if(bookId == null) {
// insert the book data in the books and authors tables
String defaultFormattedFileNameInCloudStorage = String.format("%s-%s-%s-public.txt", title, author, publicationYear);
bookId = booksDataService.insertBookAndAuthorData(defaultFormattedFileNameInCloudStorage);
}
// book content has not been found in Cloud Storage
// use grounding with Web search to get a book summary
userPromptTemplate = new PromptTemplate(summaryGroundedUserMessage);
userMessage = userPromptTemplate.createMessage(Map.of(
"title", title,
"author", author));
// prompt the model for a summary
summary = vertexAIClient.promptModelGrounded(systemMessage, userMessage, model, true);
logger.info("The summary for book {} is: {}", title, summary);
// insert summary in table
booksDataService.insertBookSummary(bookId, summary);
return summary;
}