NOTE: moderation_callback
is different from LangChain Chain Callbacks. You can still use LangChain Chain callbacks with
AmazonComprehendModerationChain
via the callbacks parameter. Example:
\n",
- "
\n",
- "from langchain.callbacks.stdout import StdOutCallbackHandler\n",
- "comp_moderation_with_config = AmazonComprehendModerationChain(verbose=True, callbacks=[StdOutCallbackHandler()])\n",
- "
\n",
- "
"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "0ec38536-8cc9-408e-860b-e4a439283643",
- "metadata": {
- "tags": []
- },
- "outputs": [],
- "source": [
- "from langchain_experimental.comprehend_moderation import BaseModerationCallbackHandler"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1be744c7-3f99-4165-bf7f-9c5c249bbb53",
- "metadata": {
- "tags": []
- },
- "outputs": [],
- "source": [
- "# Define callback handlers by subclassing BaseModerationCallbackHandler\n",
- "\n",
- "\n",
- "class MyModCallback(BaseModerationCallbackHandler):\n",
- " async def on_after_pii(self, output_beacon, unique_id):\n",
- " import json\n",
- "\n",
- " moderation_type = output_beacon[\"moderation_type\"]\n",
- " chain_id = output_beacon[\"moderation_chain_id\"]\n",
- " with open(f\"output-{moderation_type}-{chain_id}.json\", \"w\") as file:\n",
- " data = {\"beacon_data\": output_beacon, \"unique_id\": unique_id}\n",
- " json.dump(data, file)\n",
- "\n",
- " \"\"\"\n",
- " async def on_after_toxicity(self, output_beacon, unique_id):\n",
- " pass\n",
- " \n",
- " async def on_after_prompt_safety(self, output_beacon, unique_id):\n",
- " pass\n",
- " \"\"\"\n",
- "\n",
- "\n",
- "my_callback = MyModCallback()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "362a3fe0-f09f-411e-9df1-d79b3e87510c",
- "metadata": {
- "tags": []
- },
- "outputs": [],
- "source": [
- "pii_config = ModerationPiiConfig(labels=[\"SSN\"], redact=True, mask_character=\"X\")\n",
- "\n",
- "toxicity_config = ModerationToxicityConfig(threshold=0.5)\n",
- "\n",
- "moderation_config = BaseModerationConfig(filters=[pii_config, toxicity_config])\n",
- "\n",
- "comp_moderation_with_config = AmazonComprehendModerationChain(\n",
- " moderation_config=moderation_config, # specify the configuration\n",
- " client=comprehend_client, # optionally pass the Boto3 Client\n",
- " unique_id=\"john.doe@email.com\", # A unique ID\n",
- " moderation_callback=my_callback, # BaseModerationCallbackHandler\n",
- " verbose=True,\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2af07937-67ea-4738-8343-c73d4d28c2cc",
- "metadata": {
- "tags": []
- },
- "outputs": [],
- "source": [
- "from langchain_community.llms.fake import FakeListLLM\n",
- "from langchain_core.prompts import PromptTemplate\n",
- "\n",
- "template = \"\"\"Question: {question}\n",
- "\n",
- "Answer:\"\"\"\n",
- "\n",
- "prompt = PromptTemplate.from_template(template)\n",
- "\n",
- "responses = [\n",
- " \"Final Answer: A credit card number looks like 1289-2321-1123-2387. A fake SSN number looks like 323-22-9980. John Doe's phone number is (999)253-9876.\",\n",
- " # replace with your own expletive\n",
- " \"Final Answer: This is a really