Skip to content
Open
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ full:
poetry install --all-extras

docs-gen:
poetry run python ./docs/pydocs/generate_pydocs.py
cp -r docs docs-build
poetry run nbdoc_build --force_all True --srcdir ./docs-build
poetry run python ./docs/docs/pydocs/generate_pydocs.py
cp -r docs/docs docs/docs-build
poetry run nbdoc_build --force_all True --srcdir ./docs/docs-build

self-install:
pip install -e .
Expand Down
3,234 changes: 3,234 additions & 0 deletions docs/bun.lock

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"# Few imports and global variables\n",
"from rich import print\n",
"import guardrails as gd\n",
"import litellm\n",
"from IPython.display import clear_output\n",
"import time"
]
Expand Down Expand Up @@ -86,10 +85,7 @@
"# Wrap the litellm OpenAI API call with the `guard` object\n",
"raw_llm_output, validated_output, *rest = await guard(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[{\n",
" \"role\": \"system\",\n",
" \"content\": prompt\n",
" }],\n",
" messages=[{\"role\": \"system\", \"content\": prompt}],\n",
" max_tokens=1024,\n",
" temperature=0.3,\n",
")"
Expand Down Expand Up @@ -249,10 +245,7 @@
"# Wrap the litellm OpenAI API call with the `guard` object\n",
"fragment_generator = await guard(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[{\n",
" \"role\": \"user\",\n",
" \"content\": prompt\n",
" }],\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" max_tokens=1024,\n",
" temperature=0,\n",
" stream=True,\n",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"metadata": {},
"outputs": [],
"source": [
"from guardrails import Guard\n"
"from guardrails import Guard"
]
},
{
Expand Down Expand Up @@ -70,10 +70,7 @@
"source": [
"from guardrails.hub import RegexMatch\n",
"\n",
"guard = Guard().use(\n",
" RegexMatch, \n",
" regex=r\"^[a-zA-Z0-9_]+$\"\n",
")\n",
"guard = Guard().use(RegexMatch, regex=r\"^[a-zA-Z0-9_]+$\")\n",
"\n",
"result = guard(\n",
" model=\"gpt-3.5-turbo\",\n",
Expand Down Expand Up @@ -101,10 +98,8 @@
"outputs": [],
"source": [
"from guardrails.hub import CompetitorCheck\n",
"guard = Guard().use(\n",
" CompetitorCheck, \n",
" [\"Apple\"]\n",
")\n",
"\n",
"guard = Guard().use(CompetitorCheck, [\"Apple\"])\n",
"\n",
"result = guard(\n",
" model=\"gpt-3.5-turbo\",\n",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@
"from guardrails import Guard\n",
"from guardrails.hub import ToxicLanguage\n",
"\n",
"guard = Guard().use(\n",
" ToxicLanguage()\n",
")"
"guard = Guard().use(ToxicLanguage())"
]
},
{
Expand Down Expand Up @@ -109,12 +107,11 @@
")\n",
"\n",
"\n",
"\n",
"accumulated_output = \"\"\n",
"for op in fragment_generator:\n",
" clear_output()\n",
" accumulated_output += op.validated_output\n",
" display(accumulated_output)\n"
" display(accumulated_output)"
]
},
{
Expand Down Expand Up @@ -161,14 +158,15 @@
"outputs": [],
"source": [
"from guardrails import Guard, install\n",
"\n",
"try:\n",
" from guardrails.hub import ToxicLanguage\n",
"except ImportError:\n",
" install(\"hub://guardrails/toxic_language\", install_local_models=True)\n",
" from guardrails.hub import ToxicLanguage\n",
"\n",
"# uses validator locally.\n",
"guard.use(ToxicLanguage(use_local=True))\n"
"guard.use(ToxicLanguage(use_local=True))"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"source": [
"from rich import print\n",
"import guardrails as gd\n",
"import litellm\n",
"from IPython.display import clear_output\n",
"import time"
]
Expand All @@ -44,6 +43,7 @@
"outputs": [],
"source": [
"from guardrails.hub import CompetitorCheck\n",
"\n",
"prompt = \"Tell me about the Apple Iphone\"\n",
"\n",
"guard = gd.Guard().use(CompetitorCheck, [\"Apple\"])"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"# Few imports and global variables\n",
"from rich import print\n",
"import guardrails as gd\n",
"import litellm\n",
"from IPython.display import clear_output\n",
"import time"
]
Expand All @@ -43,7 +42,7 @@
" gd.install(\"hub://guardrails/uppercase\")\n",
" gd.install(\"hub://guardrails/lowercase\")\n",
" gd.install(\"hub://guardrails/one_line\")\n",
" from guardrails.hub import LowerCase, UpperCase, ValidRange, OneLine\n"
" from guardrails.hub import LowerCase, UpperCase, ValidRange, OneLine"
]
},
{
Expand Down Expand Up @@ -195,7 +194,7 @@
"# Wrap the litellm OpenAI API call with the `guard` object\n",
"raw_llm_output, validated_output, *rest = guard(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[{\"role\":\"user\", \"content\":prompt}],\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" prompt_params={\"doctors_notes\": doctors_notes},\n",
" max_tokens=1024,\n",
" temperature=0.3,\n",
Expand Down Expand Up @@ -531,7 +530,7 @@
"# Wrap the litellm OpenAI API call with the `guard` object\n",
"fragment_generator = guard(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[{\"role\":\"user\", \"content\":prompt}],\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" prompt_params={\"doctors_notes\": doctors_notes},\n",
" max_tokens=1024,\n",
" temperature=0,\n",
Expand Down Expand Up @@ -828,7 +827,7 @@
" OneLine(on_fail=\"fix\"),\n",
" ],\n",
" description=\"testmeout\",\n",
" messages=[{\"role\":\"user\", \"content\": prompt}],\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
")"
]
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,9 @@
"\n",
"response = guard(\n",
" model=\"gpt-4o\",\n",
" messages=[{\n",
" \"role\": \"user\",\n",
" \"content\": prompt\n",
" }],\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" prompt_params={\"leetcode_problem\": leetcode_problem},\n",
" temperature=0\n",
" temperature=0,\n",
")\n",
"\n",
"print(response)"
Expand Down Expand Up @@ -206,7 +203,7 @@
"try:\n",
" exec(response.validated_output)\n",
" print(\"Success!\")\n",
"except Exception as e:\n",
"except Exception:\n",
" print(\"Failed!\")"
]
}
Expand Down
19 changes: 11 additions & 8 deletions docs/examples/chatbot.ipynb → docs/docs/examples/chatbot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"from guardrails.hub import ProfanityFree, ToxicLanguage\n",
"\n",
"guard = Guard()\n",
"guard.name = 'ChatBotGuard'\n",
"guard.name = \"ChatBotGuard\"\n",
"guard.use_many(ProfanityFree(), ToxicLanguage())"
]
},
Expand All @@ -151,15 +151,15 @@
"metadata": {},
"outputs": [],
"source": [
"base_message ={\n",
" \"role\": \"system\",\n",
" \"content\": \"\"\"You are a helpful assistant. \n",
"base_message = {\n",
" \"role\": \"system\",\n",
" \"content\": \"\"\"You are a helpful assistant. \n",
"\n",
" Use the document provided to answer the user's question.\n",
"\n",
" ${document}\n",
" \"\"\"\n",
" }"
" \"\"\",\n",
"}"
]
},
{
Expand All @@ -182,13 +182,15 @@
"\n",
"import gradio as gr\n",
"\n",
"\n",
"def history_to_messages(history):\n",
" messages = [base_message]\n",
" for message in history:\n",
" messages.append({\"role\": \"user\", \"content\": message[0]})\n",
" messages.append({\"role\": \"assistant\", \"content\": message[1]})\n",
" messages.append({\"role\": \"user\", \"content\": message[0]})\n",
" messages.append({\"role\": \"assistant\", \"content\": message[1]})\n",
" return messages\n",
"\n",
"\n",
"def random_response(message, history):\n",
" messages = history_to_messages(history)\n",
" messages.append({\"role\": \"user\", \"content\": message})\n",
Expand All @@ -205,6 +207,7 @@
" return \"I'm sorry there was a problem, I can't answer that question.\"\n",
" return response.validated_output\n",
"\n",
"\n",
"gr.ChatInterface(random_response).launch()"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
" \"JP Morgan\",\n",
" \"JP Morgan Chase\",\n",
" \"JPMorgan Chase\",\n",
" \"Chase\" \"M1 Finance\",\n",
" \"ChaseM1 Finance\",\n",
" \"Stash Financial Incorporated\",\n",
" \"Stash\",\n",
" \"Tastytrade Incorporated\",\n",
Expand Down Expand Up @@ -215,9 +215,7 @@
],
"source": [
"# Create the Guard with the CompetitorCheck Validator\n",
"guard = gd.Guard().use(\n",
" CompetitorCheck(competitors=competitors_list, on_fail=\"fix\")\n",
")\n",
"guard = gd.Guard().use(CompetitorCheck(competitors=competitors_list, on_fail=\"fix\"))\n",
"\n",
"# Test with a given text\n",
"output = guard.parse(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -63,25 +63,41 @@
"from guardrails import Guard\n",
"from pydantic import BaseModel\n",
"\n",
"# JSONFormer is only compatible with HF Pipelines and HF Models:\n",
"from transformers import pipeline\n",
"\n",
"\n",
"class Dog(BaseModel):\n",
" name: str\n",
" color: str\n",
" weight_kg: float\n",
"\n",
"\n",
"class NewFriends(BaseModel):\n",
" dogs: list[Dog]\n",
"\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",
"\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, messages=[{\"role\":\"user\", \"content\":\"Please enjoy this list of good dogs:\"}])\n",
"\n",
"response = guard(\n",
" tiny_llama_pipeline,\n",
" messages=[{\"role\": \"user\", \"content\": \"Please enjoy this list of good dogs:\"}],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# `out` is a dict. Format it as JSON for readability:\n",
"import json\n",
"\n",
"print(json.dumps(response.validated_output, indent=2))"
]
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,27 @@
"${gr.complete_xml_suffix_v2}\n",
"\"\"\"\n",
"\n",
"\n",
"class Fee(BaseModel):\n",
" name: str = Field(validators=[LowerCase(on_fail=\"fix\"), TwoWords(on_fail=\"reask\")])\n",
" explanation: str = Field(validators=[OneLine(on_fail=\"noop\")])\n",
" value: float = Field(description=\"The fee amount in USD or as a percentage.\")\n",
"\n",
"\n",
"class AccountFee(BaseModel):\n",
" account_type: str = Field(validators=[LowerCase(on_fail=\"fix\")])\n",
" rate: float = Field(description=\"The annual percentage rate (APR) for the account type.\")\n",
" rate: float = Field(\n",
" description=\"The annual percentage rate (APR) for the account type.\"\n",
" )\n",
"\n",
"\n",
"class CreditCardAgreement(BaseModel):\n",
" fees: list[Fee] = Field(description=\"What fees and charges are associated with my account?\")\n",
" interest_rates: list[AccountFee] = Field(description=\"What are the interest rates offered by the bank on different kinds of accounts and products?\")"
" fees: list[Fee] = Field(\n",
" description=\"What fees and charges are associated with my account?\"\n",
" )\n",
" interest_rates: list[AccountFee] = Field(\n",
" description=\"What are the interest rates offered by the bank on different kinds of accounts and products?\"\n",
" )"
]
},
{
Expand Down Expand Up @@ -228,11 +237,11 @@
"# os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\"\n",
"\n",
"raw_llm_response, validated_response, *rest = guard(\n",
" messages=[{\"role\":\"user\", \"content\": prompt}],\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" prompt_params={\"document\": content[:6000]},\n",
" model=\"gpt-4o-mini\",\n",
" max_tokens=2048,\n",
" temperature=0\n",
" temperature=0,\n",
")"
]
},
Expand Down
Loading
Loading