Skip to content

The agent engineering platform.

add guardrails profanity (#12609)

Harrison Chasea7d5e0ce8a30
diff --git a/templates/guardrails-output-parser/LICENSE b/templates/guardrails-output-parser/LICENSE
new file mode 100644
index 0000000000..426b650903
--- /dev/null
+++ b/templates/guardrails-output-parser/LICENSE
@@ -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.
diff --git a/templates/guardrails-output-parser/README.md b/templates/guardrails-output-parser/README.md
new file mode 100644
index 0000000000..8f3841c90d
--- /dev/null
+++ b/templates/guardrails-output-parser/README.md
@@ -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.
diff --git a/templates/guardrails-output-parser/guardrails_output_parser/__init__.py b/templates/guardrails-output-parser/guardrails_output_parser/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/templates/guardrails-output-parser/guardrails_output_parser/chain.py b/templates/guardrails-output-parser/guardrails_output_parser/chain.py
new file mode 100644
index 0000000000..e6c850eb2f
--- /dev/null
+++ b/templates/guardrails-output-parser/guardrails_output_parser/chain.py
@@ -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)
diff --git a/templates/guardrails-output-parser/pyproject.toml b/templates/guardrails-output-parser/pyproject.toml
new file mode 100644
index 0000000000..95e0436964
--- /dev/null
+++ b/templates/guardrails-output-parser/pyproject.toml
@@ -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"
diff --git a/templates/guardrails-output-parser/tests/__init__.py b/templates/guardrails-output-parser/tests/__init__.py
new file mode 100644
index 0000000000..e69de29bb2