3.9 KiB
Spring AI Reflection Agent Application
This project demonstrates the use of Spring AI to create a self-improving code generation system. The Reflection Agent uses two ChatClient instances in an iterative loop - one for generation and one for critique - to produce high-quality Java code.
It is based on the code in the repository https://github.com/neural-maze/agentic_patterns
The application implements a reflection-based system where the Reflection Agent:
- Uses a generation ChatClient to create code based on user prompts
- Uses a critique ChatClient to review the generated code
- Iteratively improves the code by feeding critique back to the generation ChatClient
- Continues this loop until the critique ChatClient is satisfied with the quality
Prerequisites
- Java 17 or higher
- Maven
This examples uses OpenAI as the model provider.
Before using the AI commands, make sure you have a developer token from OpenAI.
Create an account at OpenAI Signup and generate the token at API Keys.
The Spring AI project defines a configuration property named spring.ai.openai.api-key that you should set to the value of the API key obtained from OpenAI.
Exporting an environment variable is one way to set that configuration property:
export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>
Running the Application
- Clone the repository
- Navigate to the project directory
- Run the application using Maven wrapper:
./mvnw spring-boot:run
Project Structure
Main Components
Application.java: The main Spring Boot application that provides the command-line interfaceReflectionAgent.java: The core component that manages the iteration between generation and critique
How It Works
Initial Setup
The Reflection Agent creates two ChatClient instances:
generateChatClient: For generating Java code based on user requestscritiqueChatClient: For reviewing and critiquing the generated code
Generation Process
- User inputs a request
- The generation
ChatClientcreates initial code - The critique
ChatClientreviews the code - If improvements are needed, the generation
ChatClientcreates a revised version - This continues for up to
maxIterationsor until the critiqueChatClientapproves (<OK>)
ChatClient Configurations
- Generation ChatClient system prompt:
You are a Java programmer tasked with generating high quality
Java code. Your task is to generate the best content possible
for the user's request.
- Critique ChatClient system prompt:
You are tasked with generating critique and recommendations for
the user's generated content. If the user content has something
wrong or something to be improved, output a list of
recommendations and critiques.
Example Run
In this sample run, the user requested a JUnit 5 test for a Person class. See the file JacksonTestAgent.md for the actual output.
Initial Generation
- The generation
ChatClientcreated a basicPersonclass and test class - Included serialization/deserialization functionality
- Implemented basic test cases
Critique Phase
The critique `ChatClient identified several improvements:
- Better error handling
- Improved code readability
- Need for edge case testing
- Better test structure
- Expanded test coverage
- Enhanced Java class structure
- Modern Java feature usage
Final Result
The generation ChatClient created improved code with:
- Separated test methods for better modularity
- Enhanced error handling with detailed messages
- Added edge case testing for null values
- Improved code structure and readability
- Better test coverage
MergeSort
The file AgentMergeSort.md shows a similar run to create a merge sort algorithm. There is also a JUnit test of the code that was generated to show it works.