Skip to main content

Maze 内容审核 SDK:文本审核先行,后续扩展图片与视频审核。

Project description

maze-moderation-sdk

Maze 内容审核 SDK。当前首发文本审核能力,后续在同一个 SDK 下扩展图片、视频审核。

文本审核模块是无状态文本审核核——不碰会话状态、不碰 Redis、不碰配置中心、不碰任何 channel。所有配置由调用方构造 ReviewConfig 注入,密钥由调用方构造 CloudProvider 时注入,永不进包逻辑。

完整设计见 ../../../to-tada/security/design-sdk.md,开发计划见 ../../../dev-plan/text-censor-sdk-dev-plan.md

作为 SDK 引入

阶段 A(本地联调)推荐使用 sibling editable path:

[project]
dependencies = ["maze-moderation-sdk"]

[tool.uv.sources]
maze-moderation-sdk = { path = "../content_security/sdks/moderation", editable = true }

团队内 CI 或跨仓验证可锁定 Git tag:

[project]
dependencies = ["maze-moderation-sdk"]

[tool.uv.sources]
maze-moderation-sdk = { git = "ssh://git@github.com/MazeAI-pro/content_security.git", tag = "v0.1.0", subdirectory = "sdks/moderation" }

公开发布到 PyPI 后:

dependencies = ["maze-moderation-sdk>=0.1,<0.2"]

不要把本地 path 依赖写进生产环境配置。PyPI 发布说明见 docs/publish.md

公共 API

文本审核能力位于 maze_moderation.text

函数 说明
normalize(text) 纯函数 变体归一化(零宽/全半角/繁简/大小写/重复压缩/拼音)
scan_local(text, *, lexicon) 只本地 已归一化文本上的 AC 自动机扫描
review_cloud(text, *, provider, chat_id, done) 只云 只调云 API,不跑本地 DFA
collect_signals(text, *, config) mechanism 跑 DFA + 云,返回原始 Signals,不做判定
review_text(text, *, config) mechanism+policy 收集信号后交由 config.risk_strategy 判定,出 Verdict

外加 RiskStrategy / CloudProvider 两个 Protocol 和一组 frozen dataclass(Hit/CloudVerdict/Signals/Verdict)。

三种用法

消费方 用法
tada review_text + DefaultRiskStrategy,阈值走 config 微调;流式切片在包外多次调
第二个项目 review_text + 默认策略,非流式,一段调一次
任意未来项目 collect_signals 拿原始信号自己合并,或传自定义 RiskStrategy

接入示例

from maze_moderation.text import ReviewConfig, review_text, DefaultRiskStrategy
from maze_moderation.text.cloud.aliyun import AliyunProvider

# 有状态编排(如 tada):编排层构造 config,会话状态/流式切片留在包外
cfg = ReviewConfig(
    cloud_provider=AliyunProvider(
        access_key="...",
        access_secret="...",
        region="cn-shanghai",
        service="llm_response_moderation",  # 场景由调用方选:评论/昵称/LLM 等
    ),
    risk_strategy=DefaultRiskStrategy(l2_confidence_threshold=0.75),
    lexicon_path="",            # "" = 包内默认词库
)
verdict = await review_text(user_text, config=cfg)
if verdict.action == "refuse":
    ...  # 调用方自己处置

# 无状态、非流式(第二个项目):同样的 review_text 直调
cfg = ReviewConfig(cloud_provider=AliyunProvider(...))
verdict = await review_text(output_text, config=cfg)

依赖边界

  • 依赖pyahocorasickopenccpypinyinhttpx
  • 不依赖redisfastapiagentscope、Config Center、任何 tada 内部模块。

版本策略

公共面(五函数签名 + frozen dataclass + 两个 Protocol)= SemVer 契约,改字段即 major bump。首发 0.1.0 已发布到 PyPI;后续 breaking change 走 major bump。

本地审核(M2)

local 层定位:不替云端识别敏感词,泛化召回(语义/上下文/对抗变体)交给云端。local 层只做云端难以低成本覆盖的四件事——确定性拦截(高置信明确违禁词,零延迟)、断网兜底(云端降级时仍出 L3)、隐私前置(PII 正则本地命中即拦,不外发)、业务热修(竞品/品牌/黑话黑名单,加词分钟级生效)。故词库控规模、提精度,不追求泛化覆盖。

from maze_moderation.text import load_lexicon, scan_local, ReviewConfig

# 包内默认词库(konsheng 快照 v1,3267 词,全 hard,按 4 类分文件)
lexicon = load_lexicon()

# 外部词库覆盖 + 业务黑名单
lexicon = load_lexicon(
    "/path/to/lexicon",
    extra_words=ReviewConfig(business_blacklist=("竞品名称",)).business_blacklist,
)
hits = scan_local("待审文本", lexicon=lexicon)

外部词库目录结构:

lexicon/
  manifest.json    # version + files 元数据(category/severity)
  v1/              # 与 manifest.version 同名的子目录
    涉政.txt        # 文件名即 category;本地层全 hard,故只分类不分级
    暴恐.txt        # 每行一个词;跨分类冲突按红线优先级归一文件
    色情.txt
    灰区.txt

词库维护:当前快照基于 konsheng 词库经人工加工(合并去重、剔除误伤词、分类调整), 维护与重建说明见 docs/lexicon-maintenance.md

开发

pip install -e ".[dev]"
pytest                # 覆盖率门槛 80%(见 pyproject.toml)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maze_moderation_sdk-0.1.1.tar.gz (37.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

maze_moderation_sdk-0.1.1-py3-none-any.whl (43.7 kB view details)

Uploaded Python 3

File details

Details for the file maze_moderation_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: maze_moderation_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 37.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maze_moderation_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7845d399595b343bdb9b91f442ea235c3082343be401db732791056fbb5889e9
MD5 85a52813174b4bc45293561abda1a797
BLAKE2b-256 3ac5f6f764e0184dbcdbbf50a61ed4a3cbd2007df3c257be4e222388b24044b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for maze_moderation_sdk-0.1.1.tar.gz:

Publisher: release.yml on MazeAI-pro/content_security

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maze_moderation_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for maze_moderation_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e0390893b58b5d0e224ae115adecb925f3514b03c2718f3d44f8721bd7fd0c8
MD5 c776e3e91c21ba5b4876206a0b847274
BLAKE2b-256 ece9e0d6b969cea20742290a56d1d41fe2d19f570bc36cac76b3caf20e9010fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for maze_moderation_sdk-0.1.1-py3-none-any.whl:

Publisher: release.yml on MazeAI-pro/content_security

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page