27 agosto 2024

Sending Email Notifications with RabbitMQ Using Java (Spring Boot)



In this tutorial, you will learn the following
  • What is async programming?
  • How to Setting up rabbitmq using docker
  • Connect it with Spring boot application
  • Send email notification asynchronously
















29 maio 2024

Building a Generative AI Application with Spring AI





@Service
public class SpringAIService {

    @Autowired
    AiClient aiClient;

    @Value("${spring.ai.openai.apikey}")
    private String apiKey;

    @Value("${spring.ai.openai.imageUrl}")
    private String openAIImageUrl;


    public String getJoke(String topic){
        PromptTemplate promptTemplate = new PromptTemplate("""
                Crafting a compilation of programming jokes for my website. Would you like me to create a joke about {topic}?
                """);
        promptTemplate.add("topic", topic);
        return this.aiClient.generate(promptTemplate.create()).getGeneration().getText();
    }

    public String getBook(String category, String year) {
        PromptTemplate promptTemplate = new PromptTemplate("""
                I would like to research some books. Please give me a book about {category} in {year} to get started?
                But pick the best best you can think of. I'm a book critic. Ratings are great help.
                And who wrote it? And who help it? Can you give me a short plot summary and also it's name?
                But don't give me too much information. I don't want any spoilers.
                And please give me these details in the following JSON format: category, year, bookName, author, review, smallSummary.
                """);
        Map.of("category", category, "year", year).forEach(promptTemplate::add);
        AiResponse generate = this.aiClient.generate(promptTemplate.create());
        return generate.getGeneration().getText();
    }


    public InputStreamResource getImage(@RequestParam(name = "topic") String topic) throws URISyntaxException {
        PromptTemplate promptTemplate = new PromptTemplate("""
                 I am really bored from online memes. Can you create me a prompt about {topic}.
                 Elevate the given topic. Make it sophisticated.
                 Make a resolution of 256x256, but ensure that it is presented in json.
                 I want only one image creation. Give me as JSON format: prompt, n, size.
                """);
        promptTemplate.add("topic", topic);
        String imagePrompt = this.aiClient.generate(promptTemplate.create()).getGeneration().getText();

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer " + apiKey);
        headers.add("Content-Type", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(imagePrompt,headers);

        String imageUrl = restTemplate.exchange(openAIImageUrl, HttpMethod.POST, httpEntity, GeneratedImage.class)
                .getBody().getData().get(0).getUrl();
        byte[] imageBytes = restTemplate.getForObject(new URI(imageUrl), byte[].class);
        assert imageBytes != null;
        return new InputStreamResource(new java.io.ByteArrayInputStream(imageBytes));
    }
}




@RestController
@RequestMapping("/api/v1")
public class SpringAIController {
@Autowired
SpringAIService aiService;
@GetMapping("/joke")
public String getJoke(@RequestParam String topic) {
return aiService.getJoke(topic);
}
@GetMapping("/book")
public String getBook(@RequestParam(name = "category") String category, @RequestParam(name = "year") String year) {
return aiService.getBook(category, year);
}
@GetMapping(value = "/image", produces = "image/jpeg")
public ResponseEntity getImage(@RequestParam(name = "topic") String topic) throws URISyntaxException {
return ResponseEntity.ok().body(aiService.getImage(topic));
}
}






11 abril 2024

04 abril 2024

Implement JWT authentication in a Spring Boot 3 application



Prerequisites

  • JDK 11 or higher 
  • Maven 3.8 or higher 
  • MySQL running on Docker 
  • An HTTP client such as Postman, Insomnia, cURL, etc.






20 novembro 2023

ChatGPT Integration with Spring Boot Application


OpenAI ChatGPT APIs
API (POST https://api.openai.com/v1/chat/completions) to generate responses to a prompt. 

What we need to send to invoke the OpenAI API?
  • Upon accessing the “Create Chat Completion” API link, the following information regarding endpoint, request, and response is visible.
  • Endpoint: POST https://api.openai.com/v1/chat/completions
  • Go to Playgroud and type any message e.g. “What is spring boot?”



https://platform.openai.com/api-keys