-
Notifications
You must be signed in to change notification settings - Fork 336
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 #938 from guardrails-ai/ml_based_validator_docs
ML Based validator concept doc
- Loading branch information
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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 | ||
} |