Laboration 1: Middleware service acting as a bridge between clients and Large Language Models (LLMs).
This application is a Spring Boot-based middleware that allows users to interact with AI models (via OpenRouter) with added features like personalities and conversation memory. It is designed to be resilient, secure, and easy to test.
- RESTful API: Clean JSON interface for chat interactions.
- Personalities: Support for multiple AI personas:
coder: Concise technical expert.poet: Creative and rhyming.pirate: Salty sea dog with a focus on treasure.helper: Patient and clear assistant.
- Conversation Memory: In-memory FIFO storage that remembers the last 10 messages per session.
- Resilience: Automatic retry logic with exponential backoff for transient AI provider errors.
- Interactive Documentation: Fully integrated Swagger UI for testing.
- Java 21/26
- Spring Boot 3.4.1
- Spring Web (RestClient for HTTP communication)
- Spring Retry (Resilience)
- Springdoc-OpenAPI (Swagger UI)
- Mockito & MockMvc (Testing)
- JDK 21 or higher.
- An API Key from OpenRouter.
The application requires an OpenRouter API key to function. Set the following environment variable in your IDE (IntelliJ) or Terminal:
OPENAI_API_KEY=sk-or-v1-...your-key-here...You can override default settings in src/main/resources/application.properties:
openai.api.url: Change the LLM provider (Default: OpenRouter).openai.model: Change the model (Default:google/gemini-2.0-flash-001).
- Open the project in IntelliJ.
- Edit your Run Configuration and add the
OPENAI_API_KEYenvironment variable. - Click the Run button.
$env:OPENAI_API_KEY="your_key"
./mvnw spring-boot:runOnce the app is running, access the interactive test interface at: 👉 http://localhost:8080/swagger-ui/index.html
POST /api/v1/chat
{
"personality": "pirate",
"message": "What is the best way to store gold?",
"sessionId": "user-unique-id"
}GET /api/v1/chat/history/{sessionId}
Returns the current memory state for the specific session.
Run the test suite using Maven:
./mvnw testIf you encounter 500 errors on the /v3/api-docs endpoint while using Java 26, ensure the GlobalExceptionHandler is annotated with @io.swagger.v3.oas.annotations.Hidden to prevent introspection crashes during documentation generation.