白名单:直接放行,不消耗任何模型调用
TRUSTED_AUTHORS = {"[email protected]", "[email protected]"}
PROMPT = """你是一个博客评论审核员。判断以下评论是否可以发布。
返回 JSON:{"verdict": "approve" | "reject" | "review", "reason": "简要理由"}
评论者:{author}
评论内容:{content}"""
def moderate(author: str, content: str) -> dict:
if author in TRUSTED_AUTHORS:
return {"verdict": "approve", "reason": "白名单用户"}
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": PROMPT.format(author=author, content=content)}],
temperature=0,
)
return __import__("json").loads(resp.choices[0].message.content)
if __name__ == "__main__":
result = moderate("[email protected]", "好文章!顺便看看我的保健品网站 link.example")
print(result)
Key points in this code:
- **`base_url="https://api.thistoken.ai/v1"`**: ThisToken.AI is compatible with the OpenAI SDK protocol. Switching over only requires changing this one parameter—nothing else in the code needs to change.
- **Whitelist check comes first**: If `TRUSTED_AUTHORS` matches, it returns immediately with zero model calls. In a real project, this set should live in a database and be maintained automatically by rules like "no violations in the last N comments."
- **Three-state verdict**: Adding a `review` state beyond "spam/not spam" lets the AI hand off to humans when uncertain, avoiding false kills. This is a lesson I learned from the blacklist approach—better to manually review a few extra comments than to wrongly reject a single legitimate one.
Once it runs, you should see output like:
{"verdict": "review", "reason": "包含推广链接,但语气自然,建议人工确认"}
## Step 3: Integrate into Your Blog System
Once the minimal version works, integration only requires three changes:
1. **Comment submission entry point**: Hook `moderate()` into the comment processing pipeline (Hexo/Halo/WordPress all have plugin mechanisms or webhooks for this).
2. **Whitelist maintenance**: After a comment is approved—manually or by AI—automatically add the author to the whitelist table; set an expiration time and remove long-inactive users.
3. **Fallback degradation**: If the API call fails, don't block comment submission—default to dropping it into the manual review queue to guarantee availability.
In the first week after launch, my backend stats were: about 70% of comments hit the whitelist and passed through at zero cost, the rest went through the model, and no more than three per day needed manual review. Compared to the previous full-moderation approach, both cost and response latency dropped significantly—this is exactly the value of tiered routing.
## One Final Piece of Advice
Don't chase the perfect prompt right from the start. Run with the simplest possible judgment logic for a week, collect the misjudged cases, and then add targeted rules and examples—that's far more effective than designing a prompt in a vacuum.
If you're ready to get started, go register an account, grab your key, and run the code above—the whole process takes less than ten minutes: https://api.thistoken.ai/register
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.Token.AI を試してみませんか?
プロジェクトレベルの API Key を作成し、コンソールでチャネルを有効にして、ルーティング、予算、監査ログを設定しましょう。
注册 ThisToken.AI 并获取 API Key