public String promptModel()

in sessions/next24/books-genai-vertex-springai/src/main/java/services/ai/VertexAIClient.java [80:120]


    public String promptModel(String prompt, String model) {
        long start = System.currentTimeMillis();
        logger.info("Chat model prompt: {} ...", prompt.substring(0, Math.min(500, prompt.length())));

        int maxRetries = 3;  // Set the maximum number of retry attempts
        int retryCount = 0;
        ChatResponse chatResponse = null;

        while (retryCount < maxRetries) {
            try {
                chatResponse = chatClient.call(new Prompt(prompt,
                        VertexAiGeminiChatOptions.builder()
                                .withTemperature(0.4f)
                                .withModel(model)
                                .build())
                );

                // Check if the response is valid before exiting the loop
                if (chatResponse.getResult() != null) {
                    break; // Exit the loop if we have a valid response
                } else {
                    logger.warn("Received invalid response from model. Retrying...");
                }

            } catch (Exception exception) {
                logger.error("Error calling chat model: ", exception);
            }

            retryCount++;
        }

        logger.info("Elapsed time ( {}, with SpringAI): {} ms", model, (System.currentTimeMillis() - start));

        String output = VertexModels.RETRY_MSG;
        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;
    }