Skip to content

Commit

Permalink
Merge pull request #938 from guardrails-ai/ml_based_validator_docs
Browse files Browse the repository at this point in the history
ML Based validator concept doc
  • Loading branch information
zsimjee authored Jul 17, 2024
2 parents 26fc877 + 14366b1 commit bcbafd7
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
Binary file added docs/concepts/img/infra_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 143 additions & 0 deletions docs/concepts/ml_based_validators.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ML Based Validators\n",
"\n",
"Although simple validation can often be handled with rule-based approaches, more complex scenarios require machine learning models for effective validation. \n",
"\n",
"ML based validators are able to handle more complex scenarios, providing some level of 'intelligence' to the validation method that is used."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the Guardrails Hub, we provide an easy way to filter and search for different validator types!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Validator Hub Filtering](img/infra_filter.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While different validators may be rules-based or machine learning based, it is still just as easy to implement both in your codebase."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from guardrails import Guard\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"source": [
"```bash\n",
"guardrails hub install hub://guardrails/competitor_check\n",
"guardrails hub install hub://guardrails/regex_match\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rules-based validators"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from guardrails.hub import RegexMatch\n",
"\n",
"guard = Guard().use(\n",
" RegexMatch, \n",
" regex=r\"^[a-zA-Z0-9_]+$\"\n",
")\n",
"\n",
"result = guard(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"Tell me about the Apple Iphone.\"},\n",
" ],\n",
" max_tokens=1024,\n",
" temperature=0,\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ML Based Validator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from guardrails.hub import CompetitorCheck\n",
"guard = Guard().use(\n",
" CompetitorCheck, \n",
" [\"Apple\"]\n",
")\n",
"\n",
"result = guard(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"Tell me about the Apple Iphone.\"},\n",
" ],\n",
" max_tokens=1024,\n",
" temperature=0,\n",
")\n",
"print(result)"
]
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit bcbafd7

Please sign in to comment.