使用 DeepSeek 检测博客评论中的敏感内容
在开发博客系统时,评论功能是用户互动的重要组成部分。但评论中往往可能出现敏感或暴力言论,这不仅影响用户体验,还可能违反社区准则。虽然限制评论字数比较简单(直接在响应请求中计算字符数即可),但检测评论中是否存在敏感、暴力、脏话等内容则相对复杂。传统方案需要维护大量规则和敏感词列表,既费时又难以维护。
最近,我突发奇想:能否借助 DeepSeek 的强大能力来检测评论内容,从而大幅简化审核逻辑?这种思路既避免了繁琐的规则管理,又能利用 AI 的理解能力进行高效判断。下面是一个完整的 Go 语言 demo,展示如何调用 DeepSeek API 实现这一功能。
实现思路
- 敏感内容检测
我们通过构造自定义 prompt,要求 DeepSeek 扮演专业的博客评论审核员,对评论内容进行检测。Prompt 中列出了需要关注的不当内容(如脏话、人身攻击、仇恨言论、歧视、骚扰、威胁、色情、淫秽和暴力等)。为了简化返回结果,prompt 要求 AI 严格返回true
(存在违规内容)或false
(内容安全)。
完整代码示例
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
/*
调用 DeepSeek API 检测评论是否存在敏感词汇
示例:
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
*/
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type RequestPayload struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
}
// ResponseResult 用于映射返回的 JSON 数据
type ResponseResult struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model