langchain/docs
Nikhil Jha dff24285ea
Comprehend Moderation 0.2 (#11730)
This PR replaces the previous `Intent` check with the new `Prompt
Safety` check. The logic and steps to enable chain moderation via the
Amazon Comprehend service, allowing you to detect and redact PII, Toxic,
and Prompt Safety information in the LLM prompt or answer remains
unchanged.
This implementation updates the code and configuration types with
respect to `Prompt Safety`.


### Usage sample

```python
from langchain_experimental.comprehend_moderation import (BaseModerationConfig, 
                                 ModerationPromptSafetyConfig, 
                                 ModerationPiiConfig, 
                                 ModerationToxicityConfig
)

pii_config = ModerationPiiConfig(
    labels=["SSN"],
    redact=True,
    mask_character="X"
)

toxicity_config = ModerationToxicityConfig(
    threshold=0.5
)

prompt_safety_config = ModerationPromptSafetyConfig(
    threshold=0.5
)

moderation_config = BaseModerationConfig(
    filters=[pii_config, toxicity_config, prompt_safety_config]
)

comp_moderation_with_config = AmazonComprehendModerationChain(
    moderation_config=moderation_config, #specify the configuration
    client=comprehend_client,            #optionally pass the Boto3 Client
    verbose=True
)

template = """Question: {question}

Answer:"""

prompt = PromptTemplate(template=template, input_variables=["question"])

responses = [
    "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.", 
    "Final Answer: This is a really shitty way of constructing a birdhouse. This is fucking insane to think that any birds would actually create their motherfucking nests here."
]
llm = FakeListLLM(responses=responses)

llm_chain = LLMChain(prompt=prompt, llm=llm)

chain = ( 
    prompt 
    | comp_moderation_with_config 
    | {llm_chain.input_keys[0]: lambda x: x['output'] }  
    | llm_chain 
    | { "input": lambda x: x['text'] } 
    | comp_moderation_with_config 
)

try:
    response = chain.invoke({"question": "A sample SSN number looks like this 123-456-7890. Can you give me some more samples?"})
except Exception as e:
    print(str(e))
else:
    print(response['output'])

```

### Output

```python
> Entering new AmazonComprehendModerationChain chain...
Running AmazonComprehendModerationChain...
Running pii Validation...
Running toxicity Validation...
Running prompt safety Validation...

> Finished chain.


> Entering new AmazonComprehendModerationChain chain...
Running AmazonComprehendModerationChain...
Running pii Validation...
Running toxicity Validation...
Running prompt safety Validation...

> Finished chain.
Final Answer: A credit card number looks like 1289-2321-1123-2387. A fake SSN number looks like XXXXXXXXXXXX John Doe's phone number is (999)253-9876.
```

---------

Co-authored-by: Jha <nikjha@amazon.com>
Co-authored-by: Anjan Biswas <anjanavb@amazon.com>
Co-authored-by: Anjan Biswas <84933469+anjanvb@users.noreply.github.com>
2023-10-26 09:42:18 -07:00
..
api_reference Fix flexible dimension and doc for DingoDB (#12187) 2023-10-24 10:16:19 -07:00
docs Comprehend Moderation 0.2 (#11730) 2023-10-26 09:42:18 -07:00
docs_skeleton/docs/guides/langsmith mv old integration docs (#12217) 2023-10-24 12:38:16 -07:00
extras/guides/langsmith Bagatur/mv singlestore doc (#12053) 2023-10-19 15:06:26 -07:00
scripts removed CardLists for LLMs and ChatModels (#12307) 2023-10-25 19:13:44 -07:00
src add cookbook table (#12043) 2023-10-19 14:05:24 -07:00
static Docs: QA Privacy Nit (#12025) 2023-10-19 09:43:47 -04:00
.local_build.sh Add dev guide to docs(#12291) 2023-10-25 12:28:43 -07:00
babel.config.js Restructure docs (#11620) 2023-10-10 12:55:19 -07:00
code-block-loader.js Restructure docs (#11620) 2023-10-10 12:55:19 -07:00
docusaurus.config.js Add dev guide to docs(#12291) 2023-10-25 12:28:43 -07:00
package-lock.json Restructure docs (#11620) 2023-10-10 12:55:19 -07:00
package.json Restructure docs (#11620) 2023-10-10 12:55:19 -07:00
README.md Fix typos (#11663) 2023-10-12 11:44:03 -04:00
settings.ini Restructure docs (#11620) 2023-10-10 12:55:19 -07:00
sidebars.js Docs: consolidate top nav (#12219) 2023-10-24 12:28:08 -07:00
vercel_build.sh Add dev guide to docs(#12291) 2023-10-25 12:28:43 -07:00
vercel_requirements.txt Add api cross ref linking (#8275) 2023-07-26 12:38:58 -07:00
vercel.json docs: Google Cloud Documentation Cleanup (#12224) 2023-10-24 14:54:43 -07:00

Website

This website is built using Docusaurus 2, a modern static website generator.

Installation

$ yarn

Local Development

$ yarn start

This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.

Build

$ yarn build

This command generates static content into the build directory and can be served using any static contents hosting service.

Deployment

Using SSH:

$ USE_SSH=true yarn deploy

Not using SSH:

$ GIT_USER=<Your GitHub username> yarn deploy

If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the gh-pages branch.

Continuous Integration

Some common defaults for linting/formatting have been set for you. If you integrate your project with an open-source Continuous Integration system (e.g. Travis CI, CircleCI), you may check for issues using the following command.

$ yarn ci