Spring into Action : Integrating AI & Spring Applications

vishalmysore

vishalmysore

Posted on March 27, 2024

Spring into Action : Integrating AI & Spring Applications

Overview

Tools4AI an open-source project, aims to simplify the integration of AI into Spring-based applications. By leveraging the power of Spring Framework's dependency injection and inversion of control principles, along with the simplicity of Tools4AI's annotation-based approach, developers can seamlessly transform their Spring services into AI-enabled actions with minimal effort.

Spring Services
At its core, Tools4AI provides a set of annotations and utilities that streamline the process of exposing Spring services as AI actions. By annotating Spring services with the @Predict annotation, developers can effortlessly convert their existing services into actionable components within an AI-driven ecosystem.

package org.example;

import com.t4a.api.JavaMethodAction;
import com.t4a.predict.Predict;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;

@Service
@Log
@Predict(actionName ="compareCar", description = "Provide 2 cars and compare them")
public class CompareCarService implements JavaMethodAction {
    public String compareCar(String car1 , String car2) {
        log.info(car2);
        log.info(car1);
        // implement the comparison logic here
        return car2;
    }
}
Enter fullscreen mode Exit fullscreen mode

The actionName defined in the @Predict should match the name of the service method , and voila at runtime the controlller will call the service dynamically.

AI Actions

Tools4AI automatically detects Spring services annotated with @Predict and compiles them into a comprehensive list of potential AI actions. This dynamic discovery mechanism ensures that all relevant services are seamlessly integrated into the AI ecosystem without manual intervention.
When a user prompt is received by the application, Tools4AI uses Gemini or OpenAi/LocalAI to map the prompt to the most appropriate action . The associated Spring service method is then invoked automatically, with parameters extracted from the prompt supplied as method arguments.

 @Autowired
    private ApplicationContext applicationContext;
    @GetMapping("/action")
    public String actOnPrompt(@RequestParam("prompt") String prompt) {
        SpringAwareActionProcessor processor = new SpringAwareActionProcessor(applicationContext);
        try {
            return (String) processor.processSingleAction(prompt);
        } catch (AIProcessingException e) {
            throw new RuntimeException(e);
        }

    }
Enter fullscreen mode Exit fullscreen mode

Image description

You can also customize the prompts for Post Actions and call them dynamically

 @PostMapping("/action")
    public String actOnPromptWithPost(  @RequestParam("car1") String car1,  @RequestParam("car2") String car2) {
        SpringAwareActionProcessor processor = new SpringAwareActionProcessor(applicationContext);
        try {
            return (String) processor.processSingleAction(" This is car1 "+car1+" this is car2 "+car2);
        } catch (AIProcessingException e) {
            throw new RuntimeException(e);
        }

Enter fullscreen mode Exit fullscreen mode

SpringAwareActionProcessor keeps track of all your spring beans and uses them as action based on prompts
You can have list of services and use all the dependency injections normally as with any Spring application , as soon as you add the Predict annotation the service class turns into an AI action and becomes eligible to be called automatically by the AI , all the parameters and values are automatically derived from the prompts.

💖 💪 🙅 🚩
vishalmysore
vishalmysore

Posted on March 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related