Adding guardrails to large language models.
Project description
What is Guardrails?
Guardrails is a Python framework that helps build reliable AI applications by performing two key functions:
- Guardrails runs Input/Output Guards in your application that detect, quantify and mitigate the presence of specific types of risks. To look at the full suite of risks, check out Guardrails Hub.
- Guardrails help you generate structured data from LLMs.
Guardrails Hub
Guardrails Hub is a collection of pre-built measures of specific types of risks (called 'validators'). Multiple validators can be combined together into Input and Output Guards that intercept the inputs and outputs of LLMs. Visit Guardrails Hub to see the full list of validators and their documentation.
Installation
pip install guardrails-ai
Getting Started
Create Input and Output Guards for LLM Validation
-
Download and configure the Guardrails Hub CLI.
pip install guardrails-ai guardrails configure
-
Install a guardrail from Guardrails Hub.
guardrails hub install hub://guardrails/regex_match
-
Create a Guard from the installed guardrail.
# Import Guard and Validator from guardrails.hub import RegexMatch from guardrails import Guard # Initialize the Guard with val = Guard().use( RegexMatch(regex="^[A-Z][a-z]*$") ) guard.parse("Caesar") # Guardrail Passes guard.parse("Caesar is a great leader") # Guardrail Fails
-
Run multiple guardrails within a Guard. First, install the necessary guardrails from Guardrails Hub.
guardrails hub install hub://guardrails/competitor_check guardrails hub install hub://guardrails/toxic_language
Then, create a Guard from the installed guardrails.
from guardrails.hub import RegexMatch, ValidLength from guardrails import Guard guard = Guard().use( RegexMatch(regex="^[A-Z][a-z]*$"), ValidLength(min=1, max=32) ) guard.parse("Caesar") # Guardrail Passes guard.parse("Caesar is a great leader") # Guardrail Fails
Use Guardrails to generate structured data from LLMs
Let's go through an example where we ask an LLM to generate fake pet names. To do this, we'll create a Pydantic BaseModel that represents the structure of the output we want.
from pydantic import BaseModel, Field
class Pet(BaseModel):
pet_type: str = Field(description="Species of pet")
name: str = Field(description="a unique pet name")
Now, create a Guard from the Pet
class. The Guard can be used to call the LLM in a manner so that the output is formatted to the Pet
class. Under the hood, this is done by either of two methods:
- Function calling: For LLMs that support function calling, we generate structured data using the function call syntax.
- Prompt optimization: For LLMs that don't support function calling, we add the schema of the expected output to the prompt so that the LLM can generate structured data.
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
}
FAQ
I'm running into issues with Guardrails. Where can I get help?
You can reach out to us on Discord or Twitter.
Can I use Guardrails with any LLM?
Yes, Guardrails can be used with proprietary and open-source LLMs. Check out this guide on how to use Guardrails with any LLM.
Can I create my own validators?
Yes, you can create your own validators and contribute them to Guardrails Hub. Check out this guide on how to create your own validators.
Does Guardrails support other languages?
Guardrails can be used with Python and JavaScript. Check out the docs on how to use Guardrails from JavaScript. We are working on adding support for other languages. If you would like to contribute to Guardrails, please reach out to us on Discord or Twitter.
Contributing
We welcome contributions to Guardrails!
Get started by checking out Github issues and check out the Contributing Guide. 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
Built Distribution
File details
Details for the file guardrails_ai-0.4.2.tar.gz
.
File metadata
- Download URL: guardrails_ai-0.4.2.tar.gz
- Upload date:
- Size: 131.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ada89d7dbd6397567fe8856447423b3d921eea5c1cea6f108413d8c82fa78b19 |
|
MD5 | 3b330f872d24874b557939493b0bdadf |
|
BLAKE2b-256 | a2f74e6fec25aac7710f4223e8c7db47c16c5bdcb59eced8699d2095c327a2db |
File details
Details for the file guardrails_ai-0.4.2-py3-none-any.whl
.
File metadata
- Download URL: guardrails_ai-0.4.2-py3-none-any.whl
- Upload date:
- Size: 184.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2879d6041133ace01b7a79552180db9ffd338ad916a64d35dc0ddec21ddd7e85 |
|
MD5 | 887c9644e15ae9bd352f75b0d9ce0fd5 |
|
BLAKE2b-256 | e4237319c74f5a8808432b33700012a9ab9bb50520cf4d12c453106e1daf2140 |