Skip to main content

Adding guardrails to large language models.

Project description

🛤️ Guardrails AI

Discord Twitter

Guardrails is an open-source Python package for specifying structure and type, validating and correcting the outputs of large language models (LLMs).

Docs

🧩 What is Guardrails?

Guardrails is a Python package that lets a user add structure, type and quality guarantees to the outputs of large language models (LLMs). Guardrails:

  • does pydantic-style validation of LLM outputs (including semantic validation such as checking for bias in generated text, checking for bugs in generated code, etc.)
  • takes corrective actions (e.g. reasking LLM) when validation fails,
  • enforces structure and type guarantees (e.g. JSON).

🚒 Under the hood

Guardrails provides a file format (.rail) for enforcing a specification on an LLM output, and a lightweight wrapper around LLM API calls to implement this spec.

  1. rail (Reliable AI markup Language) files for specifying structure and type information, validators and corrective actions over LLM outputs.
  2. gd.Guard wraps around LLM API calls to structure, validate and correct the outputs.
graph LR
    A[Create `RAIL` spec] --> B["Initialize `guard` from spec"];
    B --> C["Wrap LLM API call with `guard`"];

Check out the Getting Started guide to learn how to use Guardrails.

📜 RAIL spec

At the heart of Guardrails is the rail spec. rail is intended to be a language-agnostic, human-readable format for specifying structure and type information, validators and corrective actions over LLM outputs.

rail is a flavor of XML that lets users specify:

  1. the expected structure and types of the LLM output (e.g. JSON)
  2. the quality criteria for the output to be considered valid (e.g. generated text should be bias-free, generated code should be bug-free)
  3. and corrective actions to be taken if the output is invalid (e.g. reask the LLM, filter out the invalid output, etc.)

To learn more about the RAIL spec and the design decisions behind it, check out the docs. To learn how to write your own RAIL spec, check out this link.

📦 Installation

pip install guardrails-ai

📍 Roadmap

  • Javascript SDK
  • Wider variety of language support (TypeScript, Go, etc)
  • Informative logging
  • VSCode extension for .rail files
  • Next version of .rail format
  • Validator playground
  • Input Validation
  • Pydantic 2.0
  • Improving reasking logic
  • Integration with LangChain
  • Add more LLM providers

🚀 Getting Started

Let's go through an example where we ask an LLM to generate fake pet names. To do this, we'll use Pydantic, a popular data validation library for Python.

📝 Creating Structured Outputs

In order to create a LLM that generates fake pet names, we can create a class Pet that inherits from the Pydantic class Link BaseModel:

from pydantic import BaseModel, Field

class Pet(BaseModel):
    pet_type: str = Field(description="Species of pet")
    name: str = Field(description="a unique pet name")

We can now pass in this new Pet class as the output_class parameter in our Guard. When we run the code, the LLM's output is formatted to the pydnatic structure. We also add ${gr.complete_json_suffix_v2} to the prompt which tells our LLM to only respond with JSON:

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.from_pydantic(output_class=Pet, prompt=prompt)

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

print(f"{validated_output}")

This prints:

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

Structured Outputs with Validation

We can add validation to our Guard instead of just structuring the ouput in a specific format. In the below code, we add a Validator that checks if the pet name generated is of valid length. If it does not pass the validation, the reask is triggered and the query is reasked to the LLM. Check out the Link Validators API Spec for a list of supported validators.

from guardrails.validators import ValidLength, TwoWords
from rich import print

class Pet(BaseModel):
    pet_type: str = Field(description="Species of pet")
    name: str = Field(description="a unique pet name", validators=[ValidLength(min=1, max=32, on_fail='reask')])

guard = Guard.from_pydantic(output_class=Pet, prompt=prompt)

raw_llm_output, validated_output, *rest = guard(
    llm_api=openai.chat.completions.create,
    model="gpt-3.5-turbo",
    max_tokens=1024,
    temperature=0.5
)

print(guard.history.last.tree)

🛠️ Contributing

Get started by checking out Github issues and of course using Guardrails to familiarize yourself with the project. Guardrails is still actively under development and any support is gladly welcomed. Feel free to open an issue, or reach out if you would like to add to the project!

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

guardrails_ai-0.3.3.tar.gz (106.7 kB view details)

Uploaded Source

Built Distribution

guardrails_ai-0.3.3-py3-none-any.whl (148.8 kB view details)

Uploaded Python 3

File details

Details for the file guardrails_ai-0.3.3.tar.gz.

File metadata

  • Download URL: guardrails_ai-0.3.3.tar.gz
  • Upload date:
  • Size: 106.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Darwin/23.1.0

File hashes

Hashes for guardrails_ai-0.3.3.tar.gz
Algorithm Hash digest
SHA256 ff12115d5d2a6e841e6f1ed7eace8936f934e4d4b0a7b89eee981cb4caa2dcc4
MD5 ec33dc3e362a88b00e6ff610aa2baf7b
BLAKE2b-256 77b710883eeb856402536dc8a6d4ab03e2bf3237a66691e79f21b1890409d7d5

See more details on using hashes here.

File details

Details for the file guardrails_ai-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: guardrails_ai-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 148.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Darwin/23.1.0

File hashes

Hashes for guardrails_ai-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ecd3f1707816afce5f407627eaf7794624b39c63a5080d8947afd5023c4a2e50
MD5 1bb1e3de4102a8d6c488308e016ccc7a
BLAKE2b-256 ae5f656bb84c9be700fff22719ddd1cb98b078dfbff51738f7e19998f4cf4d64

See more details on using hashes here.

Supported by

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