{"slug": "build-an-ai-moderation-classifier-in-python", "title": "Build an AI Moderation Classifier in Python", "summary": "A developer built a two-stage AI moderation classifier in Python using Telnyx AI Inference. The pipeline uses embeddings similarity to catch obvious spam and abuse patterns, then falls back to an LLM for nuanced content. The open-source Flask example provides endpoints for blocklisting, moderation, and batch processing.", "body_md": "Moderation is one of those product features where \"just ask an LLM\" sounds tempting, but it is not always the best workflow.\n\nSome content is obvious. Known spam, repeated abuse patterns, and common scam text should be caught quickly and consistently.\n\nOther content needs judgment. It may be contextual, vague, sarcastic, or borderline.\n\nI put together a Python Flask example that handles both paths with Telnyx AI Inference:\n\n[https://github.com/team-telnyx/telnyx-code-examples/tree/main/moderation-classifier-python](https://github.com/team-telnyx/telnyx-code-examples/tree/main/moderation-classifier-python)\n\nThe app uses a two-stage pipeline:\n\nBefore moderating content, build the blocklist index:\n\n```\ncurl -X POST http://localhost:5000/blocklist/index\n```\n\nThat embeds the bundled `sample_blocklist.json`\n\nentries through:\n\n```\nPOST /v2/ai/openai/embeddings\n```\n\nThen you can classify a single piece of content:\n\n```\ncurl -X POST http://localhost:5000/moderate \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"content\": \"Great product, really enjoyed using it.\",\n    \"source\": \"review\",\n    \"author_id\": \"user-123\"\n  }'\n```\n\nThe app returns structured fields:\n\n```\n{\n  \"category\": \"safe\",\n  \"confidence\": 1.0,\n  \"flags\": [],\n  \"blocklist_match\": false,\n  \"recommended_action\": \"allow\",\n  \"reason\": \"Benign positive product review with no policy violations.\"\n}\n```\n\nIf content matches the blocklist strongly enough, the app skips the LLM and returns the matched category directly.\n\nFor anything else, it calls:\n\n```\nPOST /v2/ai/chat/completions\n```\n\nThe default models are:\n\n```\nAI_MODEL=moonshotai/Kimi-K2.6\nEMBEDDING_MODEL=thenlper/gte-large\n```\n\nThe example includes:\n\n`POST /blocklist/index`\n\n`POST /moderate`\n\n`POST /moderate/batch`\n\n`POST /blocklist`\n\n`GET /blocklist`\n\n`GET /moderations`\n\n`GET /moderations/<id>`\n\n`GET /stats`\n\n`GET /health`\n\nBatch moderation supports up to 20 items:\n\n```\ncurl -X POST http://localhost:5000/moderate/batch \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"items\": [\n      {\"content\": \"Great product!\", \"source\": \"review\"},\n      {\"content\": \"Buy cheap followers now!\", \"source\": \"message\"}\n    ]\n  }'\n```\n\nThe important idea is separating obvious moderation cases from nuanced ones.\n\nKnown-bad patterns can be handled with embeddings similarity.\n\nAmbiguous content can go to an LLM for classification and reasoning.\n\nThe response is structured enough to plug into product logic:\n\n`allow`\n\n`flag`\n\n`remove`\n\n`escalate`\n\nFor production, I would add persistence, audit logs, human review queues, rate limiting, and a feedback loop so reviewers can tune the blocklist and thresholds over time.\n\nResources:", "url": "https://wpnews.pro/news/build-an-ai-moderation-classifier-in-python", "canonical_source": "https://dev.to/sonam_50a41a4ced7e6b4f3fa/build-an-ai-moderation-classifier-in-python-3p2", "published_at": "2026-07-22 22:17:32+00:00", "updated_at": "2026-07-22 22:58:42.633102+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "developer-tools"], "entities": ["Telnyx", "Flask", "Kimi-K2.6", "gte-large"], "alternates": {"html": "https://wpnews.pro/news/build-an-ai-moderation-classifier-in-python", "markdown": "https://wpnews.pro/news/build-an-ai-moderation-classifier-in-python.md", "text": "https://wpnews.pro/news/build-an-ai-moderation-classifier-in-python.txt", "jsonld": "https://wpnews.pro/news/build-an-ai-moderation-classifier-in-python.jsonld"}}