Building Autonomous AI Agent in Java with Action Scripts
Published: 2024-04-01 23:10:14

## Overview
An autonomous agent is a system capable of performing tasks or making decisions independently, based on its programming and the data it perceives from its environment. These agents operate with a degree of intelligence, allowing them to adapt to new situations and execute actions without direct human oversight. Key to modern AI systems, they're pivotal in automating complex workflows, enhancing efficiency, and driving innovation across industries.

> Code for this article is [here](https://github.com/vishalmysore/sam)

Building on the foundation of autonomous agent concept, action scripts are a specialized form of these intelligent systems, designed specifically for enterprise AI applications. While retaining the core capabilities of autonomy and adaptability, scripts can operates within a controlled framework, executing tasks and making decisions that align with predefined business rules and objectives

> Action Scripts are written in plain English. Yes, that's it. It's that simple. No tags, no keywords, no brackets—just plain and simple English commands.

**Examples**

Given the capabilities of [Tools4AI ](https://github.com/vishalmysore/Tools4AI)to interpret natural language prompts into executable actions, this open-source project has the potential to significantly impact and innovate within several domains by simplifying or enhancing current automation and scripting solutions. 
First lets look at a sample script

`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 is less than $1000 then book the sight seeing attraction called 5 star palace
if car booking is successful and flight cost is more than $1000 then book the sight seeing attraction called peanut palace `

You can run this script simply by using these [3 lines of code](https://github.com/vishalmysore/sam/blob/main/src/main/java/org/example/script/ProcessScriptComplex.java)

```java
   ScriptProcessor script = new ScriptProcessor();
   ScriptResult result =  script.process("scriptFileName");
   log.info(script.summarize(result));
```
and the result is 
`OK. I have booked the sight seeing attraction called peanut palace for you.

The user requested a flight booking from Toronto to Bangalore for 3 days on December 7th, a car booking from Bangalore to Toronto for 10 days on December 17th, and a sightseeing attraction booking called "peanut palace" if the flight cost is more than $1000. The flight and car bookings were successful, and the flight cost was more than $1000, so the sightseeing attraction booking was also successful.`

Another example action script

`can you check the logs of CustMange and see if there is null pointer exception in log file called status.log
if yes then restart the application CustManage
Once the CustMange has been restarted please notify LibrayManagementSystem and AccountUpdateSystem`

and its result
`The user requested to check the logs of the "CustMange" application for a null pointer exception in the "status.log" file. The check revealed that the exception was present, so the user then requested to restart the "CustMange" application. After the application was restarted, the user requested to notify the "LibrayManagementSystem" and "AccountUpdateSystem" applications about the restart. All actions were successfully completed.`

Here’s a deeper dive into various scenarios and scripts across different domains where Tools4AI could be highly beneficial:

**1. Behavior-Driven Development (BDD) Tools**

Examples: Cucumber, SpecFlow, Behat:

`Feature: User login
Scenario: Successful login
    Given I am on the login page
    When I enter valid credentials
    Then I am redirected to the dashboard`

Tools4AI Example: By providing a more intuitive or flexible way to define behavior specifications and executable tests, especially if it can seamlessly integrate with existing development workflows.
Execute login test with valid credentials and verify redirection to dashboard.

**2. Automated Testing Scripts**

Examples: Selenium scripts for web testing, Appium for mobile apps, Postman for API testing.

`driver.findElement(By.id("username")).sendKeys("user@example.com");
driver.findElement(By.id("password")).sendKeys("password123");
driver.findElement(By.id("login")).click();
assertTrue(driver.findElement(By.id("dashboard")).isDisplayed());
`
Tools4AI Example:Tools4AI could offer a unified way to write tests across different platforms, simplifying the test creation process with natural language prompts and reducing the learning curve for new testers.

`Fill "user@example.com" in "username", "password123" in "password", click "login", and verify "dashboard" is visible.`

Actions can be Java Methods, HTTP End points, Shell Scripts, Groovy Script or Swagger Config file , please read this article for further information. You can also configure the actions or scripting in Spring Boot project as described in this article. 
To further secure your actions you can use Multi Ai Voting , please read this article for more information 

## Future Thoughts?

This list represents my aspirations and ideas for future enhancements to Tools4AI. Some features are straightforward, while others present more complex challenges. Nonetheless, I'm sharing these ideas to spark further thought and discussion within our community.


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

**3. Task Automation and Configuration Management Scripts**
Examples: Ansible playbooks, Puppet manifests, Chef recipes.

```java
- hosts: servers
  tasks:
  - name: Install nginx
    apt:
      name: nginx
      state: present
```

Tools4AI Example: Can Tools4AI interpret complex prompts into actions that configure systems or deploy software ? can it provide a more straightforward or dynamic way to manage configurations and automate routine tasks. 

`Install nginx on servers. `

**4. Workflow Automation Tools**
Examples: Zapier, IFTTT, Microsoft Power Automate

`If new item in RSS feed, then post a tweet with item title and URL.`

Tools4AI Example:Tools4AI could work alongside these tools by providing more customizable and complex automation capabilities that go beyond predefined triggers and actions, especially for bespoke enterprise workflows. The major challenge here would be be map the specifics to actions if they are outside shell, http, java, or regular programming languages.

`When RSS feed updates, tweet the new item's title and URL.`

**5. Data Processing and ETL Scripts**
Examples: Apache NiFi, Talend, custom Python scripts for data transformation

```python
import pandas as pd
df = pd.read_csv('data.csv')
df['new_column'] = df['column1'] + df['column2']
df.to_csv('transformed_data.csv')
```

Tools4AI Example: By allowing users to specify data processing workflows in a more descriptive and potentially less error-prone way, Tools4AI could streamline the creation and maintenance of ETL (Extract, Transform, Load) pipelines. The actions themselves will be written in Python or any programming language but Tools4ai will provide plain English based approach to execute the actions

`
Add "column1" and "column2" in "data.csv" and save to "transformed_data.csv".
`

6. General Automation Scripting Languages
Examples: Bash scripts, PowerShell scripts, Python scripts for general automation

`#!/bin/bash
echo "Updating system packages"
sudo apt-get update && sudo apt-get upgrade -y`

Tools4AI Example:Tools4AI could serve as a higher-level, more user-friendly interface for defining automation tasks that are typically handled by these scripts, especially for users with less programming experience.

`Update system packages.`

**7. Chatbots and Conversational Interfaces**

Examples: Custom chatbots built with frameworks like Microsoft Bot Framework or Dialogflow

`User says: "What's the weather like today?"
Bot responds: "The weather is sunny with a high of 75 degrees."`

Tools4AI Example: By dynamically generating responses and actions based on conversational prompts, Tools4AI could enhance the capabilities of chatbots, making them more versatile in handling user requests.
When asked about today's weather, fetch and provide the current weather conditions.

These examples illustrate how Tools4AI could simplify scripting across various domains by using more natural, higher-level descriptions to automate complex tasks and processes used across various domains and practices (Production Support, Customer Service, etc). The key advantages include reducing the need for detailed technical knowledge of specific scripting languages or APIs and enabling more intuitive, human-readable script creation.