add guardrails profanity (#12609)

pull/12611/head
Harrison Chase 7 months ago committed by GitHub
parent e933212a3d
commit a7d5e0ce8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 LangChain, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -0,0 +1,9 @@
# guardrails-output-parser
Uses [guardrails-ai](https://github.com/guardrails-ai/guardrails) to validate output.
This example protects against profanity, but with Guardrails you can protect against a multitude of things.
If Guardrails does not find any profanity, then the translated output is returned as is.
If Guardrails does find profanity, then an empty string is returned.

@ -0,0 +1,40 @@
from langchain.llms import OpenAI
from langchain.output_parsers import GuardrailsOutputParser
from langchain.prompts import PromptTemplate
# Define rail string
rail_str = """
<rail version="0.1">
<output>
<string
description="Profanity-free translation"
format="is-profanity-free"
name="translated_statement"
on-fail-is-profanity-free="fix">
</string>
</output>
<prompt>
Translate the given statement into English:
${statement_to_be_translated}
${gr.complete_json_suffix}
</prompt>
</rail>
"""
# Create the GuardrailsOutputParser object from the rail string
output_parser = GuardrailsOutputParser.from_rail_string(rail_str)
# Define the prompt, model and chain
prompt = PromptTemplate(
template=output_parser.guard.prompt.escape(),
input_variables=output_parser.guard.prompt.variable_names,
)
chain = prompt | OpenAI() | output_parser
# This is needed because GuardrailsOutputParser does not have an inferrable type
chain = chain.with_types(output_type=dict)

@ -0,0 +1,26 @@
[tool.poetry]
name = "guardrails_output_parser"
version = "0.0.1"
description = ""
authors = []
readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain = ">=0.0.313, <0.1"
openai = "^0.28.1"
guardrails-ai = "^0.2.4"
alt-profanity-check = "^1.3.1"
[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.4"
fastapi = "^0.104.0"
sse-starlette = "^1.6.5"
[tool.langserve]
export_module = "guardrails_output_parser.chain"
export_attr = "chain"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Loading…
Cancel
Save