langchain_experimental.comprehend_moderation.amazon_comprehend_moderation.AmazonComprehendModerationChain

Note

AmazonComprehendModerationChain implements the standard Runnable Interface. 🏃

class langchain_experimental.comprehend_moderation.amazon_comprehend_moderation.AmazonComprehendModerationChain[source]

Bases: Chain

Moderation Chain, based on Amazon Comprehend service.

See more at https://aws.amazon.com/comprehend/

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param callback_manager: Optional[BaseCallbackManager] = None

[DEPRECATED] Use callbacks instead.

param callbacks: Callbacks = None

Optional list of callback handlers (or callback manager). Defaults to None. Callback handlers are called throughout the lifecycle of a call to a chain, starting with on_chain_start, ending with on_chain_end or on_chain_error. Each custom chain can optionally call additional callback methods, see Callback docs for full details.

param client: Optional[Any] = None

boto3 client object for connection to Amazon Comprehend

param credentials_profile_name: Optional[str] = None

The name of the profile in the ~/.aws/credentials or ~/.aws/config files, which has either access keys or role information specified. If not specified, the default credential profile or, if on an EC2 instance, credentials from IMDS will be used. See: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

param input_key: str = 'input'

Key used to fetch/store the input in data containers. Defaults to input

param memory: Optional[BaseMemory] = None

Optional memory object. Defaults to None. Memory is a class that gets called at the start and at the end of every chain. At the start, memory loads variables and passes them along in the chain. At the end, it saves any returned variables. There are many different types of memory - please see memory docs for the full catalog.

param metadata: Optional[Dict[str, Any]] = None

Optional metadata associated with the chain. Defaults to None. This metadata will be associated with each call to this chain, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a chain with its use case.

param moderation_callback: Optional[BaseModerationCallbackHandler] = None

Callback handler for moderation, this is different from regular callbacks which can be used in addition to this.

param moderation_config: BaseModerationConfig = BaseModerationConfig(filters=[ModerationPiiConfig(threshold=0.5, labels=[], redact=False, mask_character='*'), ModerationToxicityConfig(threshold=0.5, labels=[]), ModerationPromptSafetyConfig(threshold=0.5)])

Configuration settings for moderation, defaults to BaseModerationConfig with default values

param output_key: str = 'output'

Key used to fetch/store the output in data containers. Defaults to output

param region_name: Optional[str] = None

The aws region e.g., us-west-2. Fallsback to AWS_DEFAULT_REGION env variable or region specified in ~/.aws/config in case it is not provided here.

param tags: Optional[List[str]] = None

Optional list of tags associated with the chain. Defaults to None. These tags will be associated with each call to this chain, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a chain with its use case.

param unique_id: Optional[str] = None

A unique id that can be used to identify or group a user or session

param verbose: bool [Optional]

Whether or not run in verbose mode. In verbose mode, some intermediate logs will be printed to the console. Defaults to the global verbose value, accessible via langchain.globals.get_verbose().

__call__(inputs: Union[Dict[str, Any], Any], return_only_outputs: bool = False, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, include_run_info: bool = False) Dict[str, Any]

[Deprecated] Execute the chain.

Parameters
  • inputs (Union[Dict[str, Any], Any]) – Dictionary of inputs, or single input if chain expects only one param. Should contain all inputs specified in Chain.input_keys except for inputs that will be set by the chain’s memory.

  • return_only_outputs (bool) – Whether to return only outputs in the response. If True, only new keys generated by this chain will be returned. If False, both input keys and new keys generated by this chain will be returned. Defaults to False.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • metadata (Optional[Dict[str, Any]]) – Optional metadata associated with the chain. Defaults to None

  • include_run_info (bool) – Whether to include run info in the response. Defaults to False.

  • run_name (Optional[str]) –

Returns

A dict of named outputs. Should contain all outputs specified in

Chain.output_keys.

Return type

Dict[str, Any]

Notes

Deprecated since version langchain==0.1.0: Use invoke instead.

async acall(inputs: Union[Dict[str, Any], Any], return_only_outputs: bool = False, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, include_run_info: bool = False) Dict[str, Any]

[Deprecated] Asynchronously execute the chain.

Parameters
  • inputs (Union[Dict[str, Any], Any]) – Dictionary of inputs, or single input if chain expects only one param. Should contain all inputs specified in Chain.input_keys except for inputs that will be set by the chain’s memory.

  • return_only_outputs (bool) – Whether to return only outputs in the response. If True, only new keys generated by this chain will be returned. If False, both input keys and new keys generated by this chain will be returned. Defaults to False.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • metadata (Optional[Dict[str, Any]]) – Optional metadata associated with the chain. Defaults to None

  • include_run_info (bool) – Whether to include run info in the response. Defaults to False.

  • run_name (Optional[str]) –

Returns

A dict of named outputs. Should contain all outputs specified in

Chain.output_keys.

Return type

Dict[str, Any]

Notes

Deprecated since version langchain==0.1.0: Use ainvoke instead.

apply(input_list: List[Dict[str, Any]], callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None) List[Dict[str, str]]

[Deprecated] Call the chain on all inputs in the list.

Notes

Deprecated since version langchain==0.1.0: Use batch instead.

Parameters
Return type

List[Dict[str, str]]

async aprep_inputs(inputs: Union[Dict[str, Any], Any]) Dict[str, str]

Prepare chain inputs, including adding inputs from memory.

Parameters

inputs (Union[Dict[str, Any], Any]) – Dictionary of raw inputs, or single input if chain expects only one param. Should contain all inputs specified in Chain.input_keys except for inputs that will be set by the chain’s memory.

Returns

A dictionary of all inputs, including those added by the chain’s memory.

Return type

Dict[str, str]

async aprep_outputs(inputs: Dict[str, str], outputs: Dict[str, str], return_only_outputs: bool = False) Dict[str, str]

Validate and prepare chain outputs, and save info about this run to memory.

Parameters
  • inputs (Dict[str, str]) – Dictionary of chain inputs, including any inputs added by chain memory.

  • outputs (Dict[str, str]) – Dictionary of initial chain outputs.

  • return_only_outputs (bool) – Whether to only return the chain outputs. If False, inputs are also added to the final outputs.

Returns

A dict of the final chain outputs.

Return type

Dict[str, str]

async arun(*args: Any, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

[Deprecated] Convenience method for executing chain.

The main difference between this method and Chain.__call__ is that this method expects inputs to be passed directly in as positional arguments or keyword arguments, whereas Chain.__call__ expects a single input dictionary with all the inputs

Parameters
  • *args (Any) – If the chain expects a single input, it can be passed in as the sole positional argument.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • **kwargs (Any) – If the chain expects multiple inputs, they can be passed in directly as keyword arguments.

  • metadata (Optional[Dict[str, Any]]) –

  • **kwargs

Returns

The chain output.

Return type

Any

Example

# Suppose we have a single-input chain that takes a 'question' string:
await chain.arun("What's the temperature in Boise, Idaho?")
# -> "The temperature in Boise is..."

# Suppose we have a multi-input chain that takes a 'question' string
# and 'context' string:
question = "What's the temperature in Boise, Idaho?"
context = "Weather report for Boise, Idaho on 07/03/23..."
await chain.arun(question=question, context=context)
# -> "The temperature in Boise is..."

Notes

Deprecated since version langchain==0.1.0: Use ainvoke instead.

prep_inputs(inputs: Union[Dict[str, Any], Any]) Dict[str, str]

Prepare chain inputs, including adding inputs from memory.

Parameters

inputs (Union[Dict[str, Any], Any]) – Dictionary of raw inputs, or single input if chain expects only one param. Should contain all inputs specified in Chain.input_keys except for inputs that will be set by the chain’s memory.

Returns

A dictionary of all inputs, including those added by the chain’s memory.

Return type

Dict[str, str]

prep_outputs(inputs: Dict[str, str], outputs: Dict[str, str], return_only_outputs: bool = False) Dict[str, str]

Validate and prepare chain outputs, and save info about this run to memory.

Parameters
  • inputs (Dict[str, str]) – Dictionary of chain inputs, including any inputs added by chain memory.

  • outputs (Dict[str, str]) – Dictionary of initial chain outputs.

  • return_only_outputs (bool) – Whether to only return the chain outputs. If False, inputs are also added to the final outputs.

Returns

A dict of the final chain outputs.

Return type

Dict[str, str]

run(*args: Any, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

[Deprecated] Convenience method for executing chain.

The main difference between this method and Chain.__call__ is that this method expects inputs to be passed directly in as positional arguments or keyword arguments, whereas Chain.__call__ expects a single input dictionary with all the inputs

Parameters
  • *args (Any) – If the chain expects a single input, it can be passed in as the sole positional argument.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • **kwargs (Any) – If the chain expects multiple inputs, they can be passed in directly as keyword arguments.

  • metadata (Optional[Dict[str, Any]]) –

  • **kwargs

Returns

The chain output.

Return type

Any

Example

# Suppose we have a single-input chain that takes a 'question' string:
chain.run("What's the temperature in Boise, Idaho?")
# -> "The temperature in Boise is..."

# Suppose we have a multi-input chain that takes a 'question' string
# and 'context' string:
question = "What's the temperature in Boise, Idaho?"
context = "Weather report for Boise, Idaho on 07/03/23..."
chain.run(question=question, context=context)
# -> "The temperature in Boise is..."

Notes

Deprecated since version langchain==0.1.0: Use invoke instead.

save(file_path: Union[Path, str]) None

Save the chain.

Expects Chain._chain_type property to be implemented and for memory to be

null.

Parameters

file_path (Union[Path, str]) – Path to file to save the chain to.

Return type

None

Example

chain.save(file_path="path/chain.yaml")
property input_keys: List[str]

Returns a list of input keys expected by the prompt.

This method defines the input keys that the prompt expects in order to perform its processing. It ensures that the specified keys are available for providing input to the prompt.

Returns

A list of input keys.

Return type

List[str]

Note

This method is considered private and may not be intended for direct external use.

property output_keys: List[str]

Returns a list of output keys.

This method defines the output keys that will be used to access the output values produced by the chain or function. It ensures that the specified keys are available to access the outputs.

Returns

A list of output keys.

Return type

List[str]

Note

This method is considered private and may not be intended for direct external use.

Examples using AmazonComprehendModerationChain