A FastAPI-based service that detects sensitive topics in prompts using OpenAI's GPT API.
- Topic Detection: Analyzes prompts for Healthcare, Finance, Legal, and HR content
- Fail-Fast Protection: Time-critical endpoint that returns immediately upon detecting any topic
- Audit Logging: Complete audit trail of all API calls
Analyzes a prompt and returns all detected topics.
Request:
{
"prompt": "How would you suggest to treat depression?",
"settings": {
"health": true,
"finance": false,
"hr": true,
"legal": false
}
}Response:
{
"detected_topics": ["health"]
}Fail-fast version of /detect - returns as soon as at least one topic is detected. Optimized for in-line protection in automatic pipelines.
Request/Response: Same format as /detect
Returns audit trail of all API calls.
Response:
{
"logs": [
{
"timestamp": "2026-03-08T10:30:00Z",
"endpoint": "detect",
"prompt": "How would you suggest to treat depression?",
"result": ["health"],
"settings": {"health": true, "finance": false, "hr": true, "legal": false}
}
],
"total_calls": 1
}- Install dependencies:
pip install -r requirements.txt- Run the server:
python main.pyOr use uvicorn directly:
uvicorn main:app --reload --host 0.0.0.0 --port 8000Once running, access:
- Interactive API docs: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
Example curl command:
curl -H "Content-Type: application/json" -d "{\"prompt\":\"How would you suggest to treat depression?\",\"settings\":{\"health\": true, \"finance\": false, \"hr\": true, \"legal\": false}}" http://localhost:8000/detectThe service uses OpenAI's GPT-4.1 to detect the following topics:
- Healthcare (key:
health) - Finance (key:
finance) - Legal (key:
legal) - HR (key:
hr)
The service uses AIM Security's OpenAI proxy. The API key and base URL are configured in main.py.
- Implement regex-based text matching for faster
/protectendpoint response - Migrate to
AsyncOpenAIfor improved concurrency - Add
awaitsupport for/detectendpoint - Keep
/protectsynchronous for fail-fast behavior - Add port validation to ensure service runs on configured secure port (8000+)
- Add comprehensive unit and integration tests
- Move audit logs from in-memory variable to persistent file storage
- Migrate audit logs from in-memory list to persistent file storage
- Implement structured logging for investigation and debugging
- Add validation to handle unexpected request body or header formats
- Return appropriate HTTP error responses (400 Bad Request) for malformed requests
- Implement API authentication (API keys or JWT tokens)
- Add input validation to prevent prompt injection attacks
- Implement rate limiting and DDoS protection
- Configure comprehensive exception handling with proper error responses
- Define security policies per request before OpenAI API calls
- Deploy service to external secure server (not localhost)
- Implement HTTPS/TLS encryption
- Consider API gateway or middleware for request routing
- Ensure compliance with security standards for sensitive data handling