-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from guardrails-ai/list-support
Top Level List Support
- Loading branch information
Showing
22 changed files
with
549 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# Generate structured data with Guardrails AI | ||
|
||
Guardrails AI is effective for generating structured data across from a variety of LLMs. This guide contains | ||
the following: | ||
1. General instructions on generating structured data from Guardrails using `Pydantic` or Markup (i.e. `RAIL`), and | ||
2. Examples to generate structured data using `Pydantic` or Markup. | ||
|
||
## Syntax for generating structured data | ||
|
||
There are two ways to generate structured data with Guardrails AI: using `Pydantic` or Markup (i.e. `RAIL`). | ||
|
||
1. **Pydantic**: In order to generate structured data with Pydantic models, create a Pydantic model with the desired fields and types, then create a `Guard` object that uses the Pydantic model to generate structured data, and finally call the LLM of your choice with the `guard` object to generate structured data. | ||
2. **RAIL**: In order to generate structured data with RAIL specs, create a RAIL spec with the desired fields and types, then create a `Guard` object that uses the RAIL spec to generate structured data, and finally call the LLM of your choice with the `guard` object to generate structured data. | ||
|
||
Below is the syntax for generating structured data with Guardrails AI using `Pydantic` or Markup (i.e. `RAIL`). | ||
|
||
<Tabs> | ||
<TabItem value="pydantic" label="Pydantic" default> | ||
In order to generate structured data, first create a Pydantic model with the desired fields and types. | ||
```python | ||
from pydantic import BaseModel | ||
|
||
class Person(BaseModel): | ||
name: str | ||
age: int | ||
is_employed: bool | ||
``` | ||
|
||
Then, create a `Guard` object that uses the Pydantic model to generate structured data. | ||
```python | ||
from guardrails import Guard | ||
|
||
guard = Guard.from_pydantic(Person) | ||
``` | ||
|
||
Finally, call the LLM of your choice with the `guard` object to generate structured data. | ||
```python | ||
import openai | ||
|
||
res = guard( | ||
openai.chat.completion.create, | ||
model="gpt-3.5-turbo", | ||
) | ||
``` | ||
</TabItem> | ||
<TabItem value="rail" label="RAIL"> | ||
In order to generate structured data, first create a RAIL spec with the desired fields and types. | ||
```xml | ||
<rail version="0.1"> | ||
<output> | ||
<string name="name" /> | ||
<integer name="age" /> | ||
<boolean name="is_employed" /> | ||
</output> | ||
``` | ||
|
||
Then, create a `Guard` object that uses the RAIL spec to generate structured data. | ||
```python | ||
from guardrails import Guard | ||
|
||
guard = Guard.from_s(""" | ||
<rail version="0.1"> | ||
<output> | ||
<string name="name" /> | ||
<integer name="age" /> | ||
<boolean name="is_employed" /> | ||
</output> | ||
</rail> | ||
""") | ||
``` | ||
|
||
Finally, call the LLM of your choice with the `guard` object to generate structured data. | ||
```python | ||
import openai | ||
|
||
res = guard( | ||
openai.chat.completion.create, | ||
model="gpt-3.5-turbo", | ||
) | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
## Generate a JSON object with simple types | ||
|
||
<Tabs> | ||
<TabItem value="json" label="JSON" default> | ||
```json | ||
{ | ||
"name": "John Doe", | ||
"age": 30, | ||
"is_employed": true | ||
} | ||
``` | ||
</TabItem> | ||
<TabItem value="pydantic" label="Pydantic"> | ||
```python | ||
from pydantic import BaseModel | ||
|
||
class Person(BaseModel): | ||
name: str | ||
age: int | ||
is_employed: bool | ||
``` | ||
</TabItem> | ||
<TabItem value="rail" label="Markup"> | ||
```xml | ||
<rail version="0.1"> | ||
<output> | ||
<string name="name" /> | ||
<integer name="age" /> | ||
<boolean name="is_employed" /> | ||
</output> | ||
</rail> | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
|
||
## Generate a dictionary of nested types | ||
|
||
<Tabs> | ||
<TabItem value="json" label="JSON" default> | ||
```json | ||
{ | ||
"name": "John Doe", | ||
"age": 30, | ||
"is_employed": true, | ||
"address": { | ||
"street": "123 Main St", | ||
"city": "Anytown", | ||
"zip": "12345" | ||
} | ||
} | ||
``` | ||
</TabItem> | ||
<TabItem value="pydantic" label="Pydantic"> | ||
```python | ||
from pydantic import BaseModel | ||
|
||
class Address(BaseModel): | ||
street: str | ||
city: str | ||
zip: str | ||
|
||
class Person(BaseModel): | ||
name: str | ||
age: int | ||
is_employed: bool | ||
address: Address | ||
``` | ||
</TabItem> | ||
<TabItem value="rail" label="Markup"> | ||
```xml | ||
<rail version="0.1"> | ||
<output> | ||
<string name="name" /> | ||
<integer name="age" /> | ||
<boolean name="is_employed" /> | ||
<object name="address"> | ||
<string name="street" /> | ||
<string name="city" /> | ||
<string name="zip" /> | ||
</object> | ||
</output> | ||
</rail> | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
|
||
## Generate a list of types | ||
|
||
<Tabs> | ||
<TabItem value="json" label="JSON" default> | ||
```json | ||
[ | ||
{ | ||
"name": "John Doe", | ||
"age": 30, | ||
"is_employed": true | ||
}, | ||
{ | ||
"name": "Jane Smith", | ||
"age": 25, | ||
"is_employed": false | ||
} | ||
] | ||
``` | ||
</TabItem> | ||
<TabItem value="pydantic" label="Pydantic"> | ||
```python | ||
from pydantic import BaseModel | ||
|
||
class Person(BaseModel): | ||
name: str | ||
age: int | ||
is_employed: bool | ||
|
||
people = list[Person] | ||
``` | ||
</TabItem> | ||
<TabItem value="rail" label="Markup"> | ||
```xml | ||
<rail version="0.1"> | ||
<output type="list"> | ||
<object> | ||
<string name="name" /> | ||
<integer name="age" /> | ||
<boolean name="is_employed" /> | ||
</object> | ||
</output> | ||
</rail> | ||
``` | ||
</TabItem> | ||
</Tabs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,3 +366,6 @@ def tree(self) -> Tree: | |
) | ||
|
||
return tree | ||
|
||
def __str__(self) -> str: | ||
return pretty_repr(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,3 +189,6 @@ def create_msg_history_table( | |
style="on #F0FFF0", | ||
), | ||
) | ||
|
||
def __str__(self) -> str: | ||
return pretty_repr(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from typing import Dict, TypeVar | ||
from typing import Dict, List, TypeVar | ||
|
||
OT = TypeVar("OT", str, Dict) | ||
OT = TypeVar("OT", str, List, Dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.