A Django-based AI gateway service that provides a controlled and standardized interface for interacting with Groq LLM APIs. This service acts as an abstraction layer between backend systems and the LLM provider, ensuring consistent request validation, response formatting, and error handling.
This project implements an LLM Gateway Microservice designed to:
- Validate and sanitize incoming prompts
- Support conversational context via message history
- Abstract direct interaction with the Groq API
- Standardize responses for upstream services
- Provide structured logging and error handling
Instead of exposing the LLM provider directly, this service ensures that all interactions go through a controlled, observable, and maintainable interface.
This service sits between the backend (llm-ai-server) and the LLM provider:
Client / Backend → llm-ai-gateway → Groq API
- Input validation (DRF serializers)
- Message transformation (OpenAI-compatible format)
- LLM interaction via service layer
- Centralized exception handling
- Request tracing via
X-Request-ID - Rate limiting and abuse protection
- Django
- Django REST Framework
- Groq API (OpenAI-compatible)
- Docker
- colorlog (structured logging)
LLMServiceencapsulates all LLM interactions- Decouples API layer from provider implementation
- Strict validation via serializers
- Enforces prompt constraints and message structure
- Accepts conversation history
- Converts messages into OpenAI-compatible format
-
Custom exception handler for:
- validation errors
- provider failures
- unexpected server errors
-
Consistent error response format
-
Structured logging with:
- request IDs
- token usage
- response metadata
-
Colored logs for readability
- Anonymous request throttling (100/hour)
- Prevents abuse of public endpoint
POST /api/v1/prompt/
{
"prompt": "Explain hashmap in Java",
"conversation_history": [
{
"role": "user",
"content": "What is a map?"
}
]
}{
"prompt": "Explain hashmap in Java",
"result": "A HashMap in Java is a data structure..."
}GET /api/v1/health/
{
"status": "ok"
}All errors follow a consistent structure:
{
"error": "Error message",
"request_id": "optional-request-id"
}400→ Invalid input (validation errors)502→ LLM provider failure500→ Internal server error
Create a .env file based on .env.example:
GROQ_API_KEY=your_api_key_here
GROQ_DEFAULT_MODEL=llama-3.1-8b-instant
GROQ_DEFAULT_TEMPERATURE=0.7
GROQ_DEFAULT_MAX_TOKENS=1024
GROQ_SYSTEM_PROMPT=You are a helpful assistant.
DJANGO_PORT=8000git clone <repo-url>
cd llm-ai-gatewaycp .env.example .env
pip install -r requirements.txtpython manage.py runserverdocker-compose up --buildDirectly exposing LLM providers creates:
- tight coupling
- inconsistent responses
- lack of control over usage
This service introduces:
- a stable contract
- centralized validation
- observability and logging
Separating views → serializers → services ensures:
- maintainability
- testability
- scalability for future providers (OpenAI, Anthropic, etc.)
- Authentication & API keys
- Streaming responses (token streaming)
- Caching frequent prompts
- Multi-provider support
- Request persistence (audit/logging DB)
llm-ai-server→ Spring Boot backendllm-ai-client→ Angular frontend