SuperAPI AI 工具集

guardrails

Guardrails 是一个 Python 框架,用于构建可靠的 AI 应用。它通过运行输入/输出守卫(Guards)来检测、量化和缓解特定类型的风险,并帮助从大语言模型(LLM)生成结构化数据。Guardrails Hub 提供了预构建的验证器(validators),可组合成输入和输出守卫,拦截 LLM 的输入和输出。该框架支持专有和开源 LLM,可与 Python 和 JavaScript 一起使用,并可作为独立服务通过 REST API 交互。

访问官网

工具介绍

Guardrails 是一个 Python 框架,旨在帮助构建可靠的 AI 应用,主要执行两项关键功能:一是运行输入/输出守卫(Input/Output Guards),检测、量化和缓解应用中特定类型的风险;二是帮助从大语言模型(LLM)生成结构化数据。

主要能力

Guardrails 通过 Guardrails Hub 提供预构建的验证器(validators),这些验证器是针对特定风险类型的度量。多个验证器可以组合成输入和输出守卫,拦截 LLM 的输入和输出。Guardrails Hub 是验证器的集合,可访问其网站查看完整列表和文档。

对于结构化数据生成,Guardrails 支持两种方法:对于支持函数调用的 LLM,使用函数调用语法生成结构化数据;对于不支持函数调用的 LLM,将预期输出的模式添加到提示中,以便 LLM 生成结构化数据。

使用方式

创建输入和输出守卫

  1. 安装并配置 Guardrails Hub CLI:pip install guardrails-ai guardrails configure
  2. 从 Guardrails Hub 安装守卫:pip install guardrails-ai-regex-match
  3. 从安装的守卫创建 Guard:
from guardrails import Guard, OnFailAction
from guardrails_ai.regex_match import RegexMatch

guard = Guard().use(
    RegexMatch, regex="\\(?\\d{3}\\)?-? *\\d{3}-? *-?\\d{4}", on_fail=OnFailAction.EXCEPTION
)

guard.validate("123-456-7890")  # Guardrail passes

try:
    guard.validate("1234-789-0000")  # Guardrail fails
except Exception as e:
    print(e)

输出:Validation failed for field with errors: Result must match \\(?\\d{3}\\)?-? *\\d{3}-? *-?\\d{4}

在单个 Guard 中运行多个守卫

首先安装必要的守卫:pip install guardrails-ai-competitor-check guardrails-ai-toxic-language

然后创建 Guard:

from guardrails import Guard, OnFailAction
from guardrails_ai.competitor_check import CompetitorCheck
from guardrails_ai.toxic_language import ToxicLanguage

guard = Guard().use(
    CompetitorCheck(["Apple", "Microsoft", "Google"], on_fail=OnFailAction.EXCEPTION),
    ToxicLanguage(threshold=0.5, validation_method="sentence", on_fail=OnFailAction.EXCEPTION)
)

guard.validate("""An apple a day keeps a doctor away. This is good advice for keeping your health.""")  # Both the guardrails pass

try:
    guard.validate("""Shut the hell up! Apple just released a new iPhone.""")  # Both the guardrails fail
except Exception as e:
    print(e)

输出:Validation failed for field with errors: Found the following competitors: [['Apple']]. Please avoid naming those competitors next time, The following sentences in your response were found to be toxic: - Shut the hell up!

使用 Guardrails 从 LLM 生成结构化数据

创建一个 Pydantic BaseModel 表示期望的输出结构,然后从该类创建 Guard。Guard 可用于调用 LLM,使输出格式化为该类。底层通过函数调用或提示优化实现。

Guardrails 服务器

Guardrails 可以设置为由 Flask 提供的独立服务,通过 guardrails start 启动,允许通过 REST API 交互。

安装:pip install "guardrails-ai" 配置:guardrails configure 创建配置:guardrails create --validators=hub://guardrails/two_words --guard-name=two-word-guard 启动开发服务器:guardrails start --config=./config.py

对于生产部署,建议使用 Docker 与 Gunicorn 作为 WSGI 服务器,以提高性能和可扩展性。

适用场景

Guardrails 适用于需要确保 LLM 输出可靠性、安全性和结构化的 AI 应用开发。可用于检测和缓解风险(如竞争对手提及、有毒语言等),以及从 LLM 生成符合特定模式的结构化数据。

限制

Guardrails 可与专有和开源 LLM 一起使用。目前支持 Python 和 JavaScript,其他语言的支持正在开发中。用户可以创建自己的验证器并贡献到 Guardrails Hub。

新闻与更新

  • [2026年7月6日] Guardrails 验证器正在迁移到标准 PyPI 包,可直接通过 pip 安装,Guardrails 正在停止其托管的远程推理。计划截止日期:2026年8月25日。
  • [2025年2月12日] 推出了 Guardrails Index——首个比较 24 个守卫在 6 个最常见类别中的性能和延迟的基准测试。