Skip to content

Build Agentic Java applications without heavy weight frameworks

Notifications You must be signed in to change notification settings

vishalmysore/agenticjava

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tools4AI - Agentic Java Framework

Tools4AI is a Java framework for creating AI-powered agents and actions using annotations. It enables seamless integration of AI processing into Java applications through simple annotations and automatic prompt mapping.

Quick Start

Simple Action Example

Create an agent class with @Agent and @Action annotations:

@Agent(groupName = "SimpleAction", groupDescription = "This is a simple action which can be used to test the action processor")
public class SimpleAction {

    @Action(description = "Provide persons name and then find out what does that person like")
    public String whatFoodDoesThisPersonLike(String name) {
        if("vishal".equalsIgnoreCase(name))
            return "Paneer Butter Masala";
        else if ("vinod".equalsIgnoreCase(name)) {
            return "aloo kofta";
        }else
            return "something yummy";
    }
}

Process actions using AIProcessor:

public class SimpleActionExample {
    public static void main(String[] args) {
        System.setProperty("tools4ai.properties.path", "io/github/vishalmysore/simple/tools4ai.properties");
        AIProcessor processor = PredictionLoader.getInstance().createOrGetAIProcessor();
        try {
            String result = (String) processor.processSingleAction("what does Vishal like to eat");
            System.out.println(result); // Output: Paneer Butter Masala
        } catch (AIProcessingException e) {
            e.printStackTrace();
        }
    }
}

Annotations

@Agent

Class-level annotation to mark a class as an AI agent. All methods with @Action are added to the prediction list.

@Agent(groupName = "personal", groupDescription = "all personal actions are here")
public class MyAgent {
    // ...
}

@Action

Method-level annotation to define executable actions.

@Action(description = "Handles travel preferences")
public String preferTravel(String name, String preference) {
    // implementation
}

@Prompt

Field-level annotation for special processing:

public class Customer {
    @Prompt(describe = "convert this to Hindi")
    private String reasonForCalling;

    @Prompt(ignore = true)
    private String location;

    @Prompt(dateFormat = "yyyy-MM-dd", describe = "if you dont find date provide todays date")
    private Date dateJoined;
}

Collection Annotations

@Action
public String[] myTaskList(String[] myTaskList) {
    return myTaskList;
}

@Action
public Map<Integer, String> addSports(@MapKeyType(Integer.class) @MapValueType(String.class) Map<Integer, String> mapOfSportsName) {
    return mapOfSportsName;
}

Examples

List Processing

@Agent
public class TaskListAction {
    @Action
    public String[] myTaskList(String[] myTaskList) {
        return myTaskList;
    }
}

// Usage
String[] taskList = (String[]) processor.processSingleAction("Vishal would like to go to gym, then play football and finally watch a cricket match.");

Map Processing

@Agent
public class SportsDirectory {
    @Action(description = "add new Sports into the map")
    public Map<Integer, String> addSports(@MapKeyType(Integer.class) @MapValueType(String.class) Map<Integer, String> mapOfSportsName) {
        return mapOfSportsName;
    }
}

// Usage
Map<Integer, String> sportsMap = (Map<Integer, String>) processor.processSingleAction("Vishal like cricket its number one, then comes soccer which is number 3, since in between he likes chess as well");

Date Handling

@Agent
public class CustomerWithQueryAction {
    @Action(description = "Customer has problem create ticket for him")
    public String customerComplaintWithNameandFullInfo(Customer customer, @Prompt(dateFormat = "yyyy-MM-dd") Date dateOfComp, @Prompt(describe = "this is customer complaint") String query) {
        return customer.toString();
    }
}

Multi-Action Processing

// Process multiple actions in sequence
String result1 = (String) processor.processSingleAction("vishal wants to travel and wants adventure");
String result2 = (String) processor.processSingleAction("vishals computer is broken he needs to fix it");

Series Processing

ScriptProcessor script = new ScriptProcessor(processor);
String commands = "Vishal wants to eat food first find his food preference\n then find his favorite restaurants and book a table for dinner tonight at 7 PM";
ScriptResult result = script.process(commands);

Autonomous Agent Action Script

For complex multi-step workflows:

ScriptProcessor script = new ScriptProcessor(processor);
String complexScript = "can you reserve the flight for Vishal from Toronto to Bangalore for 3 Days on 7th december " +
    "If flight booking is successful, can you reserve the car for Vishal from Bangalore to Toronto for 10 Days on 17th december " +
    "if car booking is successful and flight cost are less than $1000 then book the sight seeing attraction called 5 star palace " +
    "if car booking is successful and flight cost are more than $1000 then book the sight seeing attraction called peanut palace";
ScriptResult result = script.process(complexScript);
String summary = script.summarize(result);

Prompt Transformation

PromptTransformer transformer = PredictionLoader.getInstance().createOrGetPromptTransformer();

// Transform to POJO
Organization org = (Organization) transformer.transformIntoPojo(promptText, Organization.class.getName(), "", "");

// Transform to JSON
String jsonString = transformer.transformIntoJson(jsonTemplate, promptText, "Customer", "get Customer details");

Custom Actions

@Agent
public class CustomExample {
    @Action
    public Organization addOrganization(Organization organization) {
        return organization;
    }
}

Advanced Features

HTTP Actions (Swagger)

Configure REST APIs as actions via swagger_actions.json:

{
  "endpoints": [
    {
      "swaggerurl": "https://fakerestapi.azurewebsites.net/swagger/v1/swagger.json",
      "group": "Books Author Activity",
      "description": "This is for all the actions related books, Authors, photos and users",
      "baseurl": "https://fakerestapi.azurewebsites.net/",
      "id": "fakerestapi"
    }
  ]
}

Shell Actions

Configure shell scripts via shell_actions.yml:

groups:
  - name: Employee Actions
    description: This is actions for all the new employees
    scripts:
      - scriptName: "test_script.cmd"
        actionName: saveEmployeeInformation
        parameters: employeeName, employeeLocation
        description: This is a command which will save employee information

Custom HTTP Actions

Define HTTP endpoints manually in http_actions.json:

{
  "endpoints": [
    {
      "actionName": "getUserDetails",
      "description": "this will fetch User details from the user inventory corporate application",
      "url": "https://api.example.com/users/",
      "type": "GET",
      "input_object": [
        {
          "name": "userId",
          "type": "path_parameter",
          "description": "User ID"
        }
      ],
      "output_object": {
        "type": "json",
        "description": "User object"
      },
      "auth_interface": {
        "type": "Bearer Token",
        "description": "Authentication token required"
      }
    }
  ]
}

High Risk Actions

@Agent(actionName = "restartTheECOMServer", description = "will be used to restart the server", riskLevel = ActionRisk.HIGH, groupName = "customer support", groupDescription = "actions related to customer support")
public class ServerRestartAction implements JavaMethodAction {
    public String restartTheECOMServer(String reasonForRestart, String requestedBy) {
        return "Server has been restarted by " + requestedBy + " due to following reason " + reasonForRestart;
    }
}

Image Actions

GeminiImageActionProcessor processor = new GeminiImageActionProcessor();
String imageDescription = processor.imageToText(imageBytes);
Object result = actionProcessor.processSingleAction(imageDescription);

Selenium Integration

SeleniumProcessor processor = new SeleniumProcessor(driver);
processor.processWebAction("go to website https://the-internet.herokuapp.com");
boolean buttonPresent = processor.trueFalseQuery("do you see Add/Remove Elements?");

Spring Integration

SpringOpenAIProcessor springProcessor = new SpringOpenAIProcessor(applicationContext);

POJO Examples

Organization

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class Organization {
    private String name;
    @ListType(Employee.class)
    private List<Employee> em;
    @ListType(String.class)
    private List<String> locations;
}

Customer

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class Customer {
    private String firstName;
    private String lastName;
    @Prompt(describe = "convert this to Hindi")
    private String reasonForCalling;
    @Prompt(ignore = true)
    private String location;
    @Prompt(dateFormat = "yyyy-MM-dd", describe = "if you dont find date provide todays date")
    private Date dateJoined;
}

Configuration

Set the properties file path:

System.setProperty("tools4ai.properties.path", "path/to/tools4ai.properties");

Example tools4ai.properties:

action.packages.to.scan=io.github.vishalmysore.simple

About

Build Agentic Java applications without heavy weight frameworks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages