[BACK-AI-07] FastAPI Service Development for AI Text Moderation and Profanity Filtering
AI Components (AI Service)
NoteTask Brief
- Repository / Component:
ai_profanity_filter. - Language: Python 3.10+ (FastAPI, Pydantic, Requests).
- Task Type: Backend (AI Content Moderation).
- Status: Ready for Implementation
Task Description
Implement a specialized moderation microservice based on FastAPI. The service intercepts user-input text data (names of created dishes, text audio transcriptions) and checks them for profanity, explicit language, spam, or insults using the lightweight local qwen2.5:3b model.
Step-by-Step Instructions (Python)
- Design the Request Pydantic Schema: Define the
PromptRequeststructure with a mandatorypromptfield to pass the text being verified. Fix themodel="qwen2.5:3b"andformat="json"parameters as immutable properties by default. - Routing the Filter Endpoint: Create a
POST /api/generatemethod that sends the sanitized payload to the local Ollama API (OLLAMA_HOST). - Cleaning Markdown Formatting from Response: Set up the regular expression
re.sub(r"```json\s*|```", "", raw_text)to remove any potential formatting tags returned by the language model before converting the string into JSON. - Implement a Soft Fault Tolerance Policy (Soft Fallback):
- Set an execution limit of
25seconds (timeout=25) for requests processing in Ollama. - In case of
json.JSONDecodeErrorexceptions, network timeouts, or Ollama overload errors (status_code != 200), invoke thefallback_responsemethod. - The fallback function must not return an interface-blocking error. Instead, it generates and returns a clean, safe default list of products (Chicken fillet, Potatoes, Cream), allowing the ingestion pipeline to complete the user session successfully.
- Set an execution limit of