A Spring Boot application that uses Spring AI 1.0.0 and OpenAI to intelligently summarize email content with configurable prompt templates.
- Configurable Prompts: Easily customizable prompt templates
- REST API: Simple HTTP endpoints for email summarization
- Validation: Input validation for email content
- Structured Format: All summaries formatted with "Summary:" header and bullet points
- Spring Boot 3.5.0
- Spring AI 1.0.0
- OpenAI GPT-4o-mini
- Java 21
- Maven
smart-email-summarizer/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com.example.emailsummarizer/
│ │ │ ├── controller/
│ │ │ │ └── EmailSummarizerController.java
│ │ │ ├── config/
│ │ │ │ └── PromptConfig.java
│ │ │ ├── model/
│ │ │ │ └── EmailRequest.java
│ │ │ └── EmailSummarizerApplication.java
│ │ └── resources/
│ │ └── application.yml
└── pom.xml
- ChatClient API: New fluent ChatClient.Builder pattern replaces the old ChatClient
- Message Structure: Updated to use UserMessage classes
- Prompt Building: Enhanced Prompt class with List of messages
- Method Chaining: New fluent API with
.prompt().call().content()
pattern - Spring Boot 3.3.0: Updated to latest Spring Boot version
- Java 21: Updated to use Java 21 for better performance
- GPT-4o-mini: Updated to use the latest OpenAI model
- Java 21 or higher
- Maven 3.8+
- OpenAI API Key
-
Clone the repository
git clone <repository-url> cd smart-email-summarizer
-
Set up OpenAI API Key
Option 1: Environment Variable
export OPENAI_API_KEY=your-actual-openai-api-key
Option 2: Update application.yml
spring: ai: openai: api-key: your-actual-openai-api-key
-
Build the project
mvn clean install
-
Run the application
mvn spring-boot:run
The application will start on http://localhost:8080
POST /api/email/summarize
Request Body:
{
"subject": "Q2 Revenue Forecast",
"from": "[email protected]",
"content": "Hi team, please find attached the latest projections for Q2. Revenue is expected to increase by 15% compared to Q1...",
}
Parameters:
subject
(required): Email subject linefrom
(required): Sender's email addresscontent
(required): The email content to summarize
Response:
{
"summary": "Summary:\n• CEO shared Q2 revenue forecast\n• Projections indicate 15% growth compared to Q1\n• Detailed projections attached in document",
"subject": "Q2 Revenue Forecast",
"from": "[email protected]"
}
curl -X POST http://localhost:8080/api/email/summarize \
-H "Content-Type: application/json" \
-d '{
"subject": "Q4 Budget Review",
"from": "[email protected]",
"content": "The Q4 budget review meeting is scheduled for next Friday at 2 PM. Please prepare your department budgets and expense reports. We need to finalize the numbers before the board meeting on Monday."
}
Customize OpenAI settings in application.yml
:
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4o-mini # Latest OpenAI model
temperature: 0.3 # Creativity level (0.0-1.0)
max-tokens: 1000 # Response length limit
base-url: https://api.openai.com
gpt-4o-mini
(recommended for cost efficiency)gpt-4o
gpt-4-turbo
gpt-3.5-turbo
-
ChatClient Injection:
@Autowired public EmailSummarizerController(ChatClient.Builder chatClientBuilder, PromptConfig promptConfig) { this.chatClient = chatClientBuilder.build(); }
-
Message Creation:
Message userMessage = userPromptTemplate.createMessage(variables); Prompt prompt = new Prompt(List.of(userMessage));
-
Chat Invocation:
String summary = chatClient.prompt(prompt).call().content();
mvn clean package
java -jar target/smart-email-summarizer-0.0.1-SNAPSHOT.jar
Run tests with:
mvn test
-
Spring AI 1.0.0 Compatibility
- Ensure you're using Spring Boot 3.3.0+
- Java 21 is recommended for optimal performance
-
OpenAI API Key Error
- Verify your API key is valid and has sufficient credits
- Check environment variable or application.yml configuration
-
Model Availability
- GPT-4o-mini is the default model for cost efficiency
- Switch to GPT-4o for enhanced capabilities if needed
This project is licensed under the MIT License. Organized list format
- Priority Modifiers: Additional context for high/low priority emails
Customize OpenAI settings in application.yml
:
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-3.5-turbo # or gpt-4
temperature: 0.3 # creativity level (0.0-1.0)
max-tokens: 1000 # response length limit
Run tests with:
mvn test
mvn clean package
java -jar target/smart-email-summarizer-0.0.1-SNAPSHOT.jar
-
OpenAI API Key Error
- Ensure your API key is valid and has sufficient credits
- Check environment variable or application.yml configuration
-
Connection Timeout
- Verify internet connection
- Check OpenAI service status
-
Invalid Request Format
- Ensure JSON is properly formatted
- Check required fields are present
Application logs are written to:
- Console output
logs/email-summarizer.log
file
This project is licensed under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions, please create an issue in the repository or contact the owner.