Skip to content

gulevi1/topic-detection-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Topic Detection API Service

A FastAPI-based service that detects sensitive topics in prompts using OpenAI's GPT API.

Features

  • 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

Endpoints

POST /detect

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"]
}

POST /protect

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

GET /logs

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
}

Installation

  1. Install dependencies:
pip install -r requirements.txt
  1. Run the server:
python main.py

Or use uvicorn directly:

uvicorn main:app --reload --host 0.0.0.0 --port 8000

API Documentation

Once running, access:

Testing

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/detect

Topics Detected

The service uses OpenAI's GPT-4.1 to detect the following topics:

  • Healthcare (key: health)
  • Finance (key: finance)
  • Legal (key: legal)
  • HR (key: hr)

Configuration

The service uses AIM Security's OpenAI proxy. The API key and base URL are configured in main.py.

Development Roadmap

Performance Optimization

  • Implement regex-based text matching for faster /protect endpoint response
  • Migrate to AsyncOpenAI for improved concurrency
  • Add await support for /detect endpoint
  • Keep /protect synchronous for fail-fast behavior
  • Add port validation to ensure service runs on configured secure port (8000+)

Testing & Logging

  • 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

Security & Deployment

  • 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

Infrastructure

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages