Spring into Action : Integrating AI & Spring Applications
Tags: java, springboot, ai
Published: 2024-03-27 14:03:55

## Overview
[Tools4AI ](https://github.com/vishalmysore/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.


```java
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;
    }
}
```

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.


```java
 @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);
        }

    }
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21pgx297qhpy5se8fi3e.png)

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


```java
 @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);
        }
    
```
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.