Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ prompt = """

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

raw_output, validated_output, *rest = guard(
llm_api=openai.completions.create,
Expand Down
8 changes: 4 additions & 4 deletions docs/api_reference/async_guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
options:
members:
- "__init__"
- "from_rail"
- "from_rail_string"
- "from_pydantic"
- "from_string"
- "for_rail"
- "for_rail_string"
- "for_pydantic"
- "for_string"
- "configure"
- "use"
- "use_many"
Expand Down
8 changes: 4 additions & 4 deletions docs/api_reference/guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
options:
members:
- "__init__"
- "from_rail"
- "from_rail_string"
- "from_pydantic"
- "from_string"
- "for_rail"
- "for_rail_string"
- "for_pydantic"
- "for_string"
- "configure"
- "use"
- "use_many"
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Calls can be further decomposed into a stack of `Iteration` objects. These are s
## General Access
Given:
```py
my_guard = Guard.from_pydantic(...)
my_guard = Guard.for_pydantic(...)

response_1 = my_guard(...)

Expand Down
684 changes: 295 additions & 389 deletions docs/concepts/streaming_structured_data.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/concepts/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Occasionally, validators need additional metadata that is only available during
As an example, the `ExtractedSummarySentencesMatch` validator accepts a `filepaths` property in the metadata dictionary to specify what source files to compare the summary against to ensure similarity. Unlike arguments which are specified at validator initialization, metadata is specified when calling `guard.validate` or `guard.__call__` (this is the `guard()` function).

```python
guard = Guard.from_rail("my_railspec.rail")
guard = Guard.for_rail("my_railspec.rail")

outcome = guard(
llm_api=openai.chat.completions.create,
Expand Down
180 changes: 90 additions & 90 deletions docs/examples/constrained_decoding.ipynb
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"! pip install ipywidgets -q"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "81b91198c45249a2b0a7075d13ebf838",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"model.safetensors: 0%| | 10.5M/2.20G [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from guardrails import Guard\n",
"from pydantic import BaseModel\n",
"\n",
"class Dog(BaseModel):\n",
" name: str\n",
" color: str\n",
" weight_kg: float\n",
"\n",
"class NewFriends(BaseModel):\n",
" dogs: list[Dog]\n",
"\n",
"guard = Guard.from_pydantic(NewFriends, output_formatter=\"jsonformer\")\n",
"\n",
"# JSONFormer is only compatible with HF Pipelines and HF Models:\n",
"from transformers import pipeline\n",
"tiny_llama_pipeline = pipeline(\"text-generation\", \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\")\n",
"\n",
"# Inference is straightforward:\n",
"response = guard(tiny_llama_pipeline, prompt=\"Please enjoy this list of good dogs:\")\n",
"\n",
"# `out` is a dict. Format it as JSON for readability:\n",
"import json\n",
"print(json.dumps(response.validated_output, indent=2))"
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"! pip install ipywidgets -q"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "81b91198c45249a2b0a7075d13ebf838",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"model.safetensors: 0%| | 10.5M/2.20G [00:00<?, ?B/s]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from guardrails import Guard\n",
"from pydantic import BaseModel\n",
"\n",
"class Dog(BaseModel):\n",
" name: str\n",
" color: str\n",
" weight_kg: float\n",
"\n",
"class NewFriends(BaseModel):\n",
" dogs: list[Dog]\n",
"\n",
"guard = Guard.for_pydantic(NewFriends, output_formatter=\"jsonformer\")\n",
"\n",
"# JSONFormer is only compatible with HF Pipelines and HF Models:\n",
"from transformers import pipeline\n",
"tiny_llama_pipeline = pipeline(\"text-generation\", \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\")\n",
"\n",
"# Inference is straightforward:\n",
"response = guard(tiny_llama_pipeline, prompt=\"Please enjoy this list of good dogs:\")\n",
"\n",
"# `out` is a dict. Format it as JSON for readability:\n",
"import json\n",
"print(json.dumps(response.validated_output, indent=2))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion docs/examples/extracting_entities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
"metadata": {},
"outputs": [],
"source": [
"guard = gd.Guard.from_pydantic(output_class=CreditCardAgreement)"
"guard = gd.Guard.for_pydantic(output_class=CreditCardAgreement)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/generate_structured_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"metadata": {},
"outputs": [],
"source": [
"guard = gd.Guard.from_pydantic(output_class=Orders)"
"guard = gd.Guard.for_pydantic(output_class=Orders)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/generate_structured_data_cohere.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
"source": [
"from rich import print\n",
"import guardrails as gd\n",
"guard = gd.Guard.from_pydantic(output_class=Orders)\n",
"guard = gd.Guard.for_pydantic(output_class=Orders)\n",
"\n",
"raw_llm_response, validated_response, *rest = guard(\n",
" \tmessages=[{\"role\":\"user\", \"content\":prompt}],\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/guard_use.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"from guardrails.errors import ValidationError\n",
"\n",
"\n",
"guard = Guard.from_pydantic(Person)\n",
"guard = Guard.for_pydantic(Person)\n",
"\n",
"try:\n",
" guard.validate(json.dumps({\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/guardrails_with_chat_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@
"metadata": {},
"outputs": [],
"source": [
"guard = gd.Guard.from_rail_string(rail_str)"
"guard = gd.Guard.for_rail_string(rail_str)"
]
},
{
Expand All @@ -387,7 +387,7 @@
"metadata": {},
"outputs": [],
"source": [
"guard = gd.Guard.from_pydantic(output_class=CreditCardAgreement)"
"guard = gd.Guard.for_pydantic(output_class=CreditCardAgreement)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/input_validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"</rail>\n",
"\"\"\"\n",
"\n",
"guard = Guard.from_rail_string(rail_spec)"
"guard = Guard.for_rail_string(rail_spec)"
]
},
{
Expand Down Expand Up @@ -123,7 +123,7 @@
" age: int\n",
"\n",
"\n",
"guard = Guard.from_pydantic(Pet)\n",
"guard = Guard.for_pydantic(Pet)\n",
"guard.use(TwoWords(on_fail=\"exception\"), on=\"prompt\")\n",
"\n",
"try:\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/json_function_calling_tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"class Schedule(BaseModel):\n",
" deliveries: List[Delivery]\n",
"\n",
"pydantic_guard = Guard.from_pydantic(Schedule)\n",
"pydantic_guard = Guard.for_pydantic(Schedule)\n",
"\n",
"# Generate the function calling tool and add it to the list\n",
"pydantic_guard_tools = pydantic_guard.json_function_calling_tool([])\n",
Expand Down Expand Up @@ -246,7 +246,7 @@
"</rail>\n",
"\"\"\"\n",
"\n",
"rail_guard = Guard.from_rail_string(rail)\n",
"rail_guard = Guard.for_rail_string(rail)\n",
"\n",
"\n",
"# Generate the function calling tool and add it to the list\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/llamaindex-output-parsing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@
"metadata": {},
"outputs": [],
"source": [
"# You can either define a RailSpec and initialise a Guard object from_rail_string()\n",
"# OR define Pydantic classes and initialise a Guard object from_pydantic()\n",
"# You can either define a RailSpec and initialise a Guard object for_rail_string()\n",
"# OR define Pydantic classes and initialise a Guard object for_pydantic()\n",
"# For more info: https://docs.guardrailsai.com/defining_guards/pydantic/\n",
"# Guardrails recommends Pydantic\n",
"\n",
Expand Down Expand Up @@ -237,7 +237,7 @@
"outputs": [],
"source": [
"# Create a guard object\n",
"guard = gd.Guard.from_pydantic(output_class=BulletPoints, prompt=prompt)\n",
"guard = gd.Guard.for_pydantic(output_class=BulletPoints, prompt=prompt)\n",
"\n",
"# Create output parse object\n",
"output_parser = GuardrailsOutputParser(guard, llm=llm_predictor.llm.complete)"
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/no_secrets_in_generated_text.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
"metadata": {},
"outputs": [],
"source": [
"guard = gd.Guard.from_rail_string(rail_str)"
"guard = gd.Guard.for_rail_string(rail_str)"
]
},
{
Expand All @@ -191,7 +191,7 @@
"metadata": {},
"outputs": [],
"source": [
"guard = gd.Guard.from_pydantic(output_class=ScrubbedCode)"
"guard = gd.Guard.for_pydantic(output_class=ScrubbedCode)"
]
},
{
Expand Down
Loading
Loading