A Spring Boot-based backend service responsible for managing chat sessions, persisting conversation history, and orchestrating interactions with the LLM gateway service.
This service acts as the core orchestration layer in a multi-service AI system, ensuring reliable communication between the client application and the LLM provider.
The llm-api-server is designed to handle:
- Chat session lifecycle management
- Persistent storage of conversation history (PostgreSQL)
- Integration with the LLM gateway (
llm-api-gateway) - Structured request validation and error handling
- Clean separation of concerns using layered architecture
It abstracts away both the client and LLM provider, acting as a stateful backend service for conversational applications.
Angular Client → Spring Boot (llm-api-server) → Django (llm-api-gateway) → Groq API
- Manage chat sessions (create, retrieve, delete)
- Store and retrieve message history
- Call the LLM gateway with contextual conversation data
- Aggregate and return structured responses
- Ensure transactional consistency and data integrity
- Spring Boot
- Spring Web (REST APIs)
- Spring Data JPA
- PostgreSQL
- Docker / Docker Compose
- Lombok
- UUID-based session handling
- Automatic session creation if not provided
- Title generation based on first prompt
- Messages stored in PostgreSQL
- Indexed queries for efficient retrieval
- Ordered message history per session
- Communicates with
llm-api-gatewayvia REST - Sends prompt + conversation history
- Handles downstream failures gracefully
- Controller → API layer
- Service → business logic & orchestration
- Repository → database access
- Transform → DTO ↔ Entity mapping
- Bean validation (
@Valid, constraints) - Centralized exception handling (
@RestControllerAdvice) - Structured error responses
- Service-level transaction boundaries
- Ensures consistency for session + message operations
POST /api/chat/send
{
"prompt": "Explain hashmap in Java",
"sessionId": "optional-uuid"
}{
"prompt": "Explain hashmap in Java",
"result": "A HashMap in Java is...",
"sessionId": "generated-or-existing-id"
}GET /api/chat/sessions
Returns all sessions sorted by last update.
GET /api/chat/sessions/{sessionId}/messages
Returns ordered conversation history.
DELETE /api/chat/sessions/{sessionId}
Deletes a session and all associated messages.
id(UUID)titlecreated_atupdated_at
idsession_id(FK)role(user/assistant)contenttimestamp
- One-to-many relationship (session → messages)
- Indexed for efficient retrieval
- Cascade delete for cleanup
Example .env:
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=llmdb
POSTGRES_USER=postgres
POSTGRES_PASSWORD=system
DJANGO_URL=http://django-llm:8000/api/v1/prompt/
BACKEND_PORT=8080./mvnw clean package./mvnw spring-boot:rundocker-compose up --buildServices included:
- PostgreSQL
- pgAdmin
- Spring Boot backend
Instead of calling the LLM gateway directly from the client:
- Enables persistence of conversation history
- Centralizes business logic
- Improves scalability and maintainability
- Allows integration of additional services in the future
Separating concerns ensures:
- Maintainability
- Testability
- Clear boundaries between API, business logic, and data access
- Prevents leaking internal entities
- Enables strict API contracts
- Decouples persistence from external interfaces
- Authentication & user accounts
- Pagination for messages
- Caching frequently accessed sessions
- Streaming responses from LLM
- Multi-tenant support
- Metrics & monitoring (Prometheus/Grafana)
llm-api-gateway→ Django service for LLM interactionllm-api-client→ Angular frontend