A Java library for creating AI agents that can interact with OpenAI's language models and execute Java functions:
// Create a class with methods public class Calculator { private double memory = 0.0; public double add(double a, double b) { return a + b; } public double getMemory() { return memory; } public void setMemory(double value) { this.memory = value; } } // Create an agent instance String apiKey = System.getenv("OPENAI_API_KEY"); OpenAIAgent agent = new OpenAIAgent(apiKey, "gpt-4o"); // Register all methods automatically Calculator calculator = new Calculator(); agent.registerMethods(calculator); String[] questions = { "What is 15 added to 3?", "Store the number 42 in memory", "Add ten to the number stored in memory", }; for (String question : questions) { try { System.out.println("\nQuestion: " + question); String response = agent.sendMessage(question).get(); System.out.println("Answer: " + response); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } }
Output:
Question: What is 15 added to 3?
Tool called: add(15.0, 3.0)
Answer: 15 added to 3 is 18.0.
Question: Store the number 42 in memory
Tool called: setMemory(42.0)
Answer: The number 42 has been stored in memory.
Question: Add ten to the number stored in memory
Tool called: getMemory()
Tool called: add(42.0, 10.0)
Answer: The number stored in memory is 42. Adding ten to it results in 52.0.
No more extensive schema creation, no more manual API calls. Just a single agent.registerMethods
to register the available tools, and the agent will handle the rest.
See the ReadmeCalculator for the runnable version of above example.
Add the following dependency to your pom.xml
:
<dependency> <groupId>com.simonbrs</groupId> <artifactId>aiagent</artifactId> <version>1.2</version> </dependency>
// Create an agent instance String apiKey = System.getenv("OPENAI_API_KEY"); OpenAIAgent agent = new OpenAIAgent(apiKey, "gpt-4o"); // Send a message and get the response CompletableFuture<String> response = agent.sendMessage("What is 2+2?"); String result = response.get(); System.out.println(result);
Map<String, Object> context = new HashMap<>(); context.put("language", "English"); context.put("style", "concise"); CompletableFuture<String> response = agent.sendMessage("What is the capital of France?", context); String result = response.get(); System.out.println(result);
The library includes several example applications demonstrating different features:
SimpleCalculator
: Demonstrates manual function registration with schemas. Shows how to:
mvn exec:java -Dexec.mainClass="com.simonbrs.aiagent.examples.SimpleCalculator"
ReflectionCalculator
: Shows automatic method registration using reflection. Features:
mvn exec:java -Dexec.mainClass="com.simonbrs.aiagent.examples.ReflectionCalculator"
MultiToolCalculator
: Demonstrates multi-tool-call pipelines. Shows how to:
mvn exec:java -Dexec.mainClass="com.simonbrs.aiagent.examples.MultiToolCalculator"
AdvancedCalculator
: Showcases advanced mathematical and utility functions:
mvn exec:java -Dexec.mainClass="com.simonbrs.aiagent.examples.AdvancedCalculator"
To run any example:
export OPENAI_API_KEY=your_api_key_here mvn exec:java -Dexec.mainClass="com.simonbrs.aiagent.examples.$EXAMPLE_NAME"
Each example demonstrates different aspects of the library's capabilities. The MultiToolCalculator
and AdvancedCalculator
are particularly interesting as they show how the AI agent can:
See the individual example files for detailed documentation and usage scenarios.
mvn clean install
export OPENAI_API_KEY=your_api_key_here mvn test
This project is licensed under the MIT License - see the LICENSE file for details.
There are no datasets linked
There are no models linked
There are no datasets linked
There are no models linked