Skip to content

Commit e872d9c

Browse files
ShreyaRzsimjee
andauthored
Update getting started guide (guardrails-ai#629)
* update getting started guide * update index * update contributing guide * making index work with docusaurus * making images work * img update * fix docs index images * use docusuarus img zoom plugin * delete static files * update contrbuting guide * update completions to chat completions --------- Co-authored-by: Zayd Simjee <[email protected]>
1 parent c30e70c commit e872d9c

File tree

11 files changed

+313
-931
lines changed

11 files changed

+313
-931
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ Thank you for your contribution and happy coding!
3939

4040
## Documentation
4141

42-
Docs are served via mkdocs using the mkdocstring plugin. To serve docs locally, run the following
42+
Docs are served via docusaurus. To serve docs locally, run the following
4343

4444
```bash
4545
# install dependencies
46-
pip install -e ".[dev]";
46+
pip install -e ".[dev]"
4747

48-
# serve docs
49-
mkdocs serve;
48+
# install npm dependencies
49+
npm i
50+
51+
# serve the docs
52+
npm run start
5053
```
51-
then navigate to `localhost:8000` in your browser.
54+
then navigate to `localhost:3000` in your browser.
File renamed without changes.

docs-graveyard/getting_started.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Getting Started
2+
3+
## Installation
4+
5+
```python
6+
pip install guardrails-ai
7+
```
8+
9+
## Create Input and Output Guards for LLM Validation
10+
11+
1. Download and configure the Guardrails Hub CLI.
12+
13+
```bash
14+
pip install guardrails-ai
15+
guardrails configure
16+
```
17+
2. Install a guardrail from Guardrails Hub.
18+
19+
```bash
20+
gudardrails hub install hub://guardrails/regex_match
21+
```
22+
3. Create a Guard from the installed guardrail.
23+
24+
```python
25+
# Import Guard and Validator
26+
from guardrails.hub import RegexMatch
27+
from guardrails import Guard
28+
29+
# Initialize the Guard with
30+
val = Guard().use(
31+
RegexMatch(regex="^[A-Z][a-z]*$")
32+
)
33+
34+
guard.parse("Caesar") # Guardrail Passes
35+
guard.parse("Caesar is a great leader") # Guardrail Fails
36+
```
37+
4. Run multiple guardrails within a Guard.
38+
First, install the necessary guardrails from Guardrails Hub.
39+
40+
```bash
41+
guardrails hub install hub://guardrails/competitor_check
42+
guardrails hub install hub://guardrails/toxic_language
43+
```
44+
45+
Then, create a Guard from the installed guardrails.
46+
47+
```python
48+
from guardrails.hub import RegexMatch, ValidLength
49+
from guardrails import Guard
50+
51+
guard = Guard().use(
52+
RegexMatch(regex="^[A-Z][a-z]*$"),
53+
ValidLength(min=1, max=32)
54+
)
55+
56+
guard.parse("Caesar") # Guardrail Passes
57+
guard.parse("Caesar is a great leader") # Guardrail Fails
58+
```
59+
60+
61+
## Use Guardrails to generate structured data from LLMs
62+
63+
64+
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](https://docs.pydantic.dev/latest/api/base_model/) that represents the structure of the output we want.
65+
66+
```py
67+
from pydantic import BaseModel, Field
68+
69+
class Pet(BaseModel):
70+
pet_type: str = Field(description="Species of pet")
71+
name: str = Field(description="a unique pet name")
72+
```
73+
74+
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:
75+
1. Function calling: For LLMs that support function calling, we generate structured data using the function call syntax.
76+
2. 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.
77+
78+
```py
79+
from guardrails import Guard
80+
import openai
81+
82+
prompt = """
83+
What kind of pet should I get and what should I name it?
84+
85+
${gr.complete_json_suffix_v2}
86+
"""
87+
guard = Guard.from_pydantic(output_class=Pet, prompt=prompt)
88+
89+
validated_output, *rest = guard(
90+
llm_api=openai.completions.create,
91+
engine="gpt-3.5-turbo-instruct"
92+
)
93+
94+
print(f"{validated_output}")
95+
```
96+
97+
This prints:
98+
```
99+
{
100+
"pet_type": "dog",
101+
"name": "Buddy
102+
}
103+
```

docs/examples/litellm_example.ipynb

Lines changed: 0 additions & 892 deletions
This file was deleted.

docs/guardrails_ai/getting_started.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Getting Started
2+
3+
## Installation
4+
5+
```python
6+
pip install guardrails-ai
7+
```
8+
9+
## Create Input and Output Guards for LLM Validation
10+
11+
1. Download and configure the Guardrails Hub CLI.
12+
13+
```bash
14+
pip install guardrails-ai
15+
guardrails configure
16+
```
17+
2. Install a guardrail from Guardrails Hub.
18+
19+
```bash
20+
gudardrails hub install hub://guardrails/regex_match
21+
```
22+
3. Create a Guard from the installed guardrail.
23+
24+
```python
25+
# Import Guard and Validator
26+
from guardrails.hub import RegexMatch
27+
from guardrails import Guard
28+
29+
# Initialize the Guard with
30+
val = Guard().use(
31+
RegexMatch(regex="^[A-Z][a-z]*$")
32+
)
33+
34+
guard.parse("Caesar") # Guardrail Passes
35+
guard.parse("Caesar is a great leader") # Guardrail Fails
36+
```
37+
4. Run multiple guardrails within a Guard.
38+
First, install the necessary guardrails from Guardrails Hub.
39+
40+
```bash
41+
guardrails hub install hub://guardrails/competitor_check
42+
guardrails hub install hub://guardrails/toxic_language
43+
```
44+
45+
Then, create a Guard from the installed guardrails.
46+
47+
```python
48+
from guardrails.hub import RegexMatch, ValidLength
49+
from guardrails import Guard
50+
51+
guard = Guard().use(
52+
RegexMatch(regex="^[A-Z][a-z]*$"),
53+
ValidLength(min=1, max=32)
54+
)
55+
56+
guard.parse("Caesar") # Guardrail Passes
57+
guard.parse("Caesar is a great leader") # Guardrail Fails
58+
```
59+
60+
61+
## Use Guardrails to generate structured data from LLMs
62+
63+
64+
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](https://docs.pydantic.dev/latest/api/base_model/) that represents the structure of the output we want.
65+
66+
```py
67+
from pydantic import BaseModel, Field
68+
69+
class Pet(BaseModel):
70+
pet_type: str = Field(description="Species of pet")
71+
name: str = Field(description="a unique pet name")
72+
```
73+
74+
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:
75+
1. Function calling: For LLMs that support function calling, we generate structured data using the function call syntax.
76+
2. 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.
77+
78+
```py
79+
from guardrails import Guard
80+
import openai
81+
82+
prompt = """
83+
What kind of pet should I get and what should I name it?
84+
85+
${gr.complete_json_suffix_v2}
86+
"""
87+
guard = Guard.from_pydantic(output_class=Pet, prompt=prompt)
88+
89+
validated_output, *rest = guard(
90+
llm_api=openai.chat.completions.create,
91+
engine="gpt-3.5-turbo"
92+
)
93+
94+
print(f"{validated_output}")
95+
```
96+
97+
This prints:
98+
```
99+
{
100+
"pet_type": "dog",
101+
"name": "Buddy
102+
}
103+
```

docs/index.md

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
# Guardrails AI
22

3-
## 🛤️ What is Guardrails AI?
3+
## What is Guardrails?
44

5-
Guardrails AI is the leading open-source framework to define and enforce assurance for LLM applications. It offers
5+
Guardrails is a Python framework that helps build reliable AI applications by performing two key functions:
6+
1. 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](https://hub.guardrailsai.com/).
7+
2. Guardrails help you generate structured data from LLMs.
68

7-
✅ Framework for creating custom validations at an application level
89

9-
✅ Orchestration of prompting → verification → re-prompting
10+
<div align="center">
11+
<img src="https://raw.githubusercontent.com/guardrails-ai/guardrails/main/docs/img/with_and_without_guardrails.svg" alt="Guardrails in your application" width="1500px" />
12+
</div>
1013

11-
✅ Library of commonly used validators for multiple use cases
14+
### Guardrails Hub
1215

13-
✅ Specification language for communicating requirements to LLM
16+
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](https://hub.guardrailsai.com/) to see the full list of validators and their documentation.
1417

15-
## 🚒 Under the hood
16-
17-
Guardrails provides an object definition called a `Rail` for enforcing a specification on an LLM output, and a lightweight wrapper called a `Guard` around LLM API calls to implement this spec.
18-
19-
1. `rail` (**R**eliable **AI** markup **L**anguage) files for specifying structure and type information, validators and corrective actions over LLM outputs. The concept of a Rail has evolved from markup - Rails can be defined in either <a href='/docs-graveyard/defining_guards/pydantic.ipynb'>Pydantic</a> or <a href='/docs/how_to_guides/rail'>RAIL</a> for structured outputs, or directly in <a href='/docs-graveyard/defining_guards/strings.ipynb'>Python</a> for string outputs.
20-
2. `Guard` wraps around LLM API calls to structure, validate and correct the outputs.
21-
22-
```mermaid
23-
graph LR
24-
A[Create `RAIL` spec] --> B["Initialize `guard` from spec"];
25-
B --> C["Wrap LLM API call with `guard`"];
26-
```
27-
28-
Check out the [Getting Started](/docs/guardrails_ai/getting_started) guide to learn how to use Guardrails.
29-
30-
## 📍 Roadmap
31-
32-
- [ ] Javascript SDK
33-
- [ ] Wider variety of language support (TypeScript, Go, etc)
34-
- [ ] Informative logging
35-
- [ ] VSCode extension for `.rail` files
36-
- [ ] Next version of `.rail` format
37-
- [ ] Validator playground
38-
- [x] Input Validation
39-
- [x] Pydantic 2.0
40-
- [x] Improving reasking logic
41-
- [x] Integration with LangChain
42-
- [x] Add more LLM providers
18+
<div align="center">
19+
<img src="https://raw.githubusercontent.com/guardrails-ai/guardrails/main/docs/img/guardrails_hub.gif" alt="Guardrails Hub gif" width="600px" />
20+
</div>

docusaurus/docusaurus.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const config = {
3737
defaultLocale: "en",
3838
locales: ["en"],
3939
},
40+
plugins: [
41+
'docusaurus-plugin-image-zoom'
42+
],
4043
presets: [[
4144
"@docusaurus/preset-classic",
4245
/** @type {import('@docusaurus/preset-classic').Options} */
@@ -88,6 +91,19 @@ const config = {
8891
footer: {
8992
copyright: `Copyright © ${new Date().getFullYear()} Guardrails AI`,
9093
},
94+
zoom: {
95+
// CSS selector to apply the plugin to, defaults to '.markdown img'
96+
// selector: '.markdown img',
97+
// Optional medium-zoom options
98+
// see: https://www.npmjs.com/package/medium-zoom#options
99+
options: {
100+
margin: 24,
101+
background: '#BADA55',
102+
scrollOffset: 0,
103+
container: '#zoom-container',
104+
template: '#zoom-template',
105+
},
106+
},
91107
}),
92108
};
93109

0 commit comments

Comments
 (0)