Skip to content

rnhc1000/spring-ai

Repository files navigation

Roadmap to Spring Certification

Continuing to improve my dev skills, now playing with Spring-AI and OpenAPI to be ready for the challenge of developing an application that will check a LLM to provide a specific grade to a transaction based on the information collected from the customer

Table of contents

Overview

"Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain. At its core, Spring AI addresses the fundamental challenge of AI integration: Connecting your enterprise Data and APIs with the AI Models." https://spring.io/projects/spring-ai


Requirements


Project Structure

  • src
    • main
    • java
      • br.dev.ferreiras.spring-ai
        • config
        • controller
          • handlers
        • dto
        • entity
        • enums
        • repository
        • services
          • exceptions
    • resources
    • test

Howto Build and Run

- profile active: dev
- service socket: 127.0.0.1:8080
- tweak a few knobs to get it up and running

Screenshot

Links

Built with

My Skills

Code Snippet

@Service
public class OpenAIServiceImpl  implements OpenAIService{
  /**
   * @param question - ask a question to openai
   * @return an answer
   */

  private final ChatModel chatModel;

  private static final Logger logger = LoggerFactory.getLogger(OpenAIServiceImpl.class);


  public OpenAIServiceImpl(ChatModel chatModel, ObjectMapper objectMapper) {
    this.chatModel = chatModel;
    this.objectMapper = objectMapper;
  }

  @Override
  public String getAnswer(String question) {
    PromptTemplate promptTemplate = new PromptTemplate(question);
    Prompt prompt = promptTemplate.create();
    ChatResponse response = chatModel.call(prompt);

    return response.getResult().getOutput().getContent();
  }

  @Override
  public Answer getAnswer(Question question) {
    PromptTemplate promptTemplate = new PromptTemplate(question.question());
    Prompt prompt = promptTemplate.create();
    ChatResponse response = chatModel.call(prompt);

    return new Answer(response.getResult().getOutput().getContent());
  }

  @Value("classpath:templates/get-capital-prompt-one.st")
  private Resource capitalInfoPrompt;

  @Value("classpath:templates/get-capital-prompt-json.st")
  private Resource capitalJsonPrompt;

  private ObjectMapper objectMapper;

  @Override
  public Answer getCapital(CapitalRequest capitalRequest) {
    PromptTemplate promptTemplate = new PromptTemplate(capitalInfoPrompt);
    Prompt prompt = promptTemplate.create(Map.of("stateOrCountry", capitalRequest.stateOrCountry()));
    ChatResponse response = chatModel.call(prompt);

    return new Answer(response.getResult().getOutput().getContent());
  }

  @Override
  public CapitalResponse getCapitalJson(CapitalRequest capitalRequest) {

    BeanOutputConverter<CapitalResponse> parser = new BeanOutputConverter<>(CapitalResponse.class);
    String format = parser.getFormat();
    PromptTemplate promptTemplate = new PromptTemplate(capitalJsonPrompt);
    Prompt prompt = promptTemplate.create(Map.of("stateOrCountry", capitalRequest.stateOrCountry(),
        "format", format));
    ChatResponse response = chatModel.call(prompt);

    logger.info("Response, {}", response.getResult().getOutput().getContent());

    return parser.convert(response.getResult().getOutput().getContent());
  }
}

Continued development

  • Unit Tests

Useful resources

Author

Ricardo Ferreira

- Portfolio

My Portfolio...

About

Spring AI , OpenAI and Milvus Vector Database

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published