OA0
OA0 是一个探索 AI 的社区
现在注册
已注册用户请  登录
OA0  ›  代码  ›  Guardrails — 为 LLM 输出增加校验、约束与安全防护

Guardrails — 为 LLM 输出增加校验、约束与安全防护

 
  neon ·  2026-03-13 12:30:15 · 3 次点击  · 0 条评论  

License
PyPI - Python Version
Downloads
CI
codecov
Checked with pyright
X (formerly Twitter) Follow
Discord
Static Badge
Static Badge
Gurubase

新闻与更新

  • [2025年2月12日] 我们刚刚发布了 Guardrails Index —— 首个同类基准测试,比较了6个最常见类别下24种护栏的性能和延迟!请在 index.guardrailsai.com 查看该指数。

什么是 Guardrails?

Guardrails 是一个 Python 框架,通过执行两个关键功能来帮助构建可靠的 AI 应用:
1. 运行输入/输出护栏:在您的应用程序中检测、量化和缓解特定类型的风险。要查看完整的风险套件,请访问 Guardrails Hub
2. 从 LLM 生成结构化数据

Guardrails Hub

Guardrails Hub 是一个预构建的特定风险度量(称为“验证器”)的集合。多个验证器可以组合成输入和输出护栏,以拦截 LLM 的输入和输出。访问 Guardrails Hub 查看完整的验证器列表及其文档。

安装

pip install guardrails-ai

快速开始

为 LLM 验证创建输入和输出护栏

  1. 下载并配置 Guardrails Hub CLI。

    bash pip install guardrails-ai guardrails configure
    2. 从 Guardrails Hub 安装一个护栏。

    bash guardrails hub install hub://guardrails/regex_match
    3. 从已安装的护栏创建一个 Guard。

    ```python
    from guardrails import Guard, OnFailAction
    from guardrails.hub import RegexMatch

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

    guard.validate("123-456-7890") # 护栏通过

    try:
    guard.validate("1234-789-0000") # 护栏失败
    except Exception as e:
    print(e)
    输出:console
    Validation failed for field with errors: Result must match (?\d{3})?-? \d{3}-? -?\d{4}
    ```
    4. 在一个 Guard 中运行多个护栏。
    首先,从 Guardrails Hub 安装必要的护栏。

    bash guardrails hub install hub://guardrails/competitor_check guardrails hub install hub://guardrails/toxic_language

    然后,从已安装的护栏创建一个 Guard。

    ```python
    from guardrails import Guard, OnFailAction
    from guardrails.hub import CompetitorCheck, 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."""
    ) # 两个护栏都通过

    try:
    guard.validate(
    """Shut the hell up! Apple just released a new iPhone."""
    ) # 两个护栏都失败
    except Exception as e:
    print(e)
    输出:console
    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 生成结构化数据

让我们通过一个示例,要求 LLM 生成虚构的宠物名称。为此,我们将创建一个 Pydantic BaseModel,它代表我们期望的输出结构。

from pydantic import BaseModel, Field

class Pet(BaseModel):
    pet_type: str = Field(description="宠物种类")
    name: str = Field(description="一个独特的宠物名称")

现在,从 Pet 类创建一个 Guard。该 Guard 可用于以特定方式调用 LLM,使其输出格式化为 Pet 类。在底层,这通过以下两种方法之一实现:
1. 函数调用:对于支持函数调用的 LLM,我们使用函数调用语法生成结构化数据。
2. 提示优化:对于不支持函数调用的 LLM,我们将预期输出的模式添加到提示中,以便 LLM 可以生成结构化数据。

from guardrails import Guard
import openai

prompt = """
    What kind of pet should I get and what should I name it?

    ${gr.complete_json_suffix_v2}
"""
guard = Guard.for_pydantic(output_class=Pet, prompt=prompt)

raw_output, validated_output, *rest = guard(
    llm_api=openai.completions.create,
    engine="gpt-3.5-turbo-instruct"
)

print(validated_output)

这将打印:

{
    "pet_type": "dog",
    "name": "Buddy
}

Guardrails 服务器

Guardrails 可以通过 guardrails start 设置为由 Flask 提供服务的独立服务,允许您通过 REST API 与其交互。这种方法简化了 Guardrails 驱动应用程序的开发和部署。

  1. 安装:pip install "guardrails-ai"
  2. 配置:guardrails configure
  3. 创建配置:guardrails create --validators=hub://guardrails/two_words --guard-name=two-word-guard
  4. 启动开发服务器:guardrails start --config=./config.py
  5. 通过以下代码片段与开发服务器交互
# 使用 guardrails 客户端
import guardrails as gr

gr.settings.use_server = True
guard = gr.Guard(name='two-word-guard')
guard.validate('this is more than two words')

# 或使用 openai sdk
import openai
openai.base_url = "http://localhost:8000/guards/two-word-guard/openai/v1/"
os.environ["OPENAI_API_KEY"] = "youropenaikey"

messages = [
        {
            "role": "user",
            "content": "tell me about an apple with 3 words exactly",
        },
    ]

completion = openai.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
)

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

常见问题

我在使用 Guardrails 时遇到问题。在哪里可以获得帮助?

您可以通过 DiscordTwitter 联系我们。

我可以将 Guardrails 与任何 LLM 一起使用吗?

是的,Guardrails 可以与专有和开源的 LLM 一起使用。查看这份关于如何将 Guardrails 与任何 LLM 一起使用的指南。

我可以创建自己的验证器吗?

是的,您可以创建自己的验证器并将其贡献给 Guardrails Hub。查看这份关于如何创建自定义验证器的指南。

Guardrails 支持其他语言吗?

Guardrails 可以与 Python 和 JavaScript 一起使用。查看关于如何从 JavaScript 使用 Guardrails 的文档。我们正在努力添加对其他语言的支持。如果您想为 Guardrails 做出贡献,请通过 DiscordTwitter 联系我们。

贡献

我们欢迎对 Guardrails 的贡献!

请先查看 Github issues 和贡献指南开始。如果您想为项目做出贡献,请随时提出问题或联系我们!

3 次点击  ∙  0 人收藏  
登录后收藏  
0 条回复
关于 ·  帮助 ·  PING ·  隐私政策 ·  服务条款   
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
耗时 19 ms
Developed with Cursor