Overview
Using Tools4AI function calling (tools integration) with OpenAI is simple and efficient. When a user provides a prompt, Tools4AI dynamically determines which group the prompt belongs to, using AI. Then, it calls the action associated with that group. In under 10 minutes, you can kickstart your journey to developing an autonomous agent in Java by leveraging the power of OpenAI. All you need is your OpenAI key and, of course, a refreshing cup of coffee (or tea) .
Let's illustrate this with examples :
Example:
Java Code
These 2 lines of code will take the prompt as input and trigger the action related to the prompt illustrate this with examples :
OpenAiActionProcessor processor = new OpenAiActionProcessor(gson);
String result = (String)processor.processSingleAction(prompt,new LoggingHumanDecision(),new LogginggExplainDecision());
Configuring actions :
Actions can be http rest calls
{
"endpoints" : [
{
"swaggerurl": "https://fakerestapi.azurewebsites.net/swagger/v1/swagger.json",
"group": "Books and Authors",
"description": "Actions related to books, authors, photos, and users",
"baseurl": "https://fakerestapi.azurewebsites.net/",
"id": "fakerestapi"
},
{
"swaggerurl": "https://petstore3.swagger.io/api/v3/openapi.json",
"baseurl": "https://petstore3.swagger.io/",
"group": "Pets",
"description": "Actions related to pets",
"id": "petstore"
}
]
}
How it Works:
User Prompt: A user submits a prompt, such as "Recommend a book on artificial intelligence."
Group Identification: Tools4AI analyzes the prompt using AI. It dynamically determines which group the prompt belongs to based on its content. For example, it identifies the prompt as related to books and authors. and there are http rest calls associated with it.
Action Invocation: Once the group is identified (e.g., "Books and Authors"), Tools4AI calls the corresponding action associated with that group. (all the rest calls associated associated in that group are eligible for function calling ) In this case, it might call a GET action to recommend a book on artificial intelligence. Tools4AI would execute the appropriate HTTP REST call associated with this group, such as "getBook" or "getAuthor," with parameters tailored to the request, which could be either simple parameters in the request or more complex JSON requests.
**Parameter Derivation: **Tools4AI extracts any necessary details from the prompt itself. For instance, it identifies "artificial intelligence" as the topic for the book recommendation.
To read about Kubernetes and AI integration click here
Java Actions:
Actions can be written in Java
@Predict(groupName = "customer support", groupDescription = "actions related to customer support")
public class ComplexAction {
@Action
public String computerRepair(Customer customer) {
return customer.toString();
}
}
Actions can be categorized in groups
@Predict( groupName = "customer support", groupDescription = "actions related to customer support")
public class CustomerQueryAction {
@Action
public String customerQuery(String query) {
// Process the customer query and provide assistance
return "Thank you for reaching out! Here's some assistance with your query: " + query;
}
}
Additional information can shared with annotations
public String computerRepairWithDetails(Customer customer, @Prompt(dateFormat = "yyyy-MM-dd") Date dateOfComp , @Prompt(describe = "this is customer complaint") String query) {
return customer.toString();
}
Annotations can be applied to parameters, method or class , above annotations tells AI to put the customer complaint in the query parameter ( this is optional if the name is not very clear
public class Customer {
private String firstName;
private String lastName;
private String reasonForCalling;
private String location;
@Prompt(dateFormat = "yyyy-MM-dd")
private Date dateJoined;
}
The above annotation will tell AI to convert the date from prompt into specific format
@Prompt(dateFormat = "yyyy-MM-dd") Date dateOfComp: This parameter is annotated with @Prompt, indicating special processing is required to populate this argument. The dateFormat attribute suggests that any date information extracted from the prompt should be formatted according to the specified pattern ("yyyy-MM-dd"). The type of the parameter is Date, implying that the extracted text should represent a date.
@Prompt(describe = "this is customer complaint") String query: Another use of the @Prompt annotation, this time without a specific format but with a description hinting at its purpose. The parameter is expected to hold the customer's complaint or query about their computer issue. The describe attribute could be used for documentation or to assist in parsing logic, indicating that the content of this string is the customer's complaint.
Explanation:
- This new Java class CustomerQueryAction is annotated with @Predict, indicating that it represents an action within the "customer support" group.
- The customerQuery method within this class accepts a String parameter representing the customer's query.
- Inside the customerQuery method, the query is processed, and assistance is provided accordingly.
- Upon receiving a prompt related to customer queries, Tools4AI will dynamically call the customerQuery method within the CustomerQueryAction class.
- With this additional class, you now have another action within the "customer support" group, allowing for more comprehensive support functionalities within your Java application powered by Tools4AI .
Spring integration click here
Creating Different Action Group
@Predict( groupName = "recipe generator", groupDescription = "actions related to recipe generation")
public class RecipeGenerationAction {
@Action
public String generateRecipe(String cuisine, int servings) {
// Generate a recipe based on the provided cuisine and servings
return "Here's a delicious " + cuisine + " recipe for " + servings + " servings: ...";
}
}
Explanation:
- Java class RecipeGenerationAction is annotated with @Predict, indicating that it represents an action within the "recipe generator" group.
- The generateRecipe method within this class accepts parameters such as the desired cuisine and servings.
- Inside the generateRecipe method, a recipe is generated based on the provided cuisine and servings.
- Upon receiving a prompt related to recipe generation, Tools4AI will dynamically call the generateRecipe method within the RecipeGenerationAction class.
Now, you have an action within the "recipe generator" group, allowing you to dynamically generate recipes based on user preferences within your Java application powered by Tools4AI.
Generate a recipe for Italian cuisine for 4 servings.
This prompt specifies the cuisine ("Italian") and the number of servings ("4"). When Tools4AI processes this prompt, it would recognize it as a request for recipe generation and call the generateRecipe method within the RecipeGenerationAction class, passing "Italian" as the cuisine and 4 as the number of servings. The action would then generate a recipe tailored to Italian cuisine for 4 servings and return the result to the user
Script Configuration Example:
Actions could be scripts ( shell, python , groovy)
groups:
- name: Employee Actions
description: This includes actions for all new employees
scripts:
- scriptName: "test_script.cmd"
actionName: saveEmployeeInformation
parameters: employeeName, employeeLocation
description: This command saves employee information
- scriptName: "C:\\another\\path\\to\\ps_issue.cmd"
actionName: raiseTicketForProductionIssue
parameters: applicationName, typeOfIssue, issueDetails
description: This script raises a ticket for a production issue in an application
- name: Enterprise Actions
description: This includes actions for all enterprise-related applications
scripts:
- scriptName: "D:\\scripts\\run_backup.sh"
actionName: runBackup
parameters: backUpReason, typeOfBackup, details, personWhoInitiated
description: This script performs a backup of the database
Explanation:
Groups: Scripts are organized into different groups based on their intended purposes.
- Employee Actions:
- Enterprise Actions: Key Features: Organization: Scripts are organized into meaningful groups based on their functionalities, making it easier for developers to locate and manage them. Description: Each group and script is accompanied by a description, providing additional context and clarity about their purposes. Parameter Specification: Scripts specify the parameters they require, allowing for clear communication of input expectations.
In summary, Tools4AI enables the organization and management of shell scripts into different groups, facilitating the seamless integration of shell-based actions into various workflows within the enterprise environment.