in sessions/fall24/books-genai-vertex-springai/src/main/java/services/ai/VertexAIClient.java [87:117]
public String promptModelGrounded(Message systemMessage,
Message userMessage,
String model,
boolean useGoogleWebSearch) {
long start = System.currentTimeMillis();
ChatClient client = ChatClient.create(chatClient);
ChatResponse chatResponse = client.prompt()
.advisors(new LoggingAdvisor(),
new GuardrailsAdvisor())
.messages(List.of(systemMessage, userMessage))
.options(VertexAiGeminiChatOptions.builder()
.withTemperature(0.4)
.withMaxOutputTokens(8192)
.withTopK(3f)
.withTopP(0.5)
.withModel(model)
.withGoogleSearchRetrieval(useGoogleWebSearch)
.build())
.call()
.chatResponse();
logger.info("Elapsed time ( {}, with SpringAI): {} ms", model, (System.currentTimeMillis() - start));
String output = "No response from model";
if (chatResponse != null && chatResponse.getResult() != null) { // Ensure chatResponse is not null
output = chatResponse.getResult().getOutput().getContent();
}
logger.info("Chat model output: {} ...", output.substring(0, Math.min(1000, output.length())));
return output;
}