Metadata-Version: 2.3
Name: langchain_plainid
Version: 0.1.4
Summary: PlainID LangChain library
Author: PlainID
Requires-Python: >=3.9,<=3.12
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: chromadb (>0.5.12,<0.7.0)
Requires-Dist: langchain-chroma (>=0.2.3)
Requires-Dist: langchain-community (>=0.3.23,<0.4.0)
Requires-Dist: langchain-core (>=0.3.58,<0.4.0)
Requires-Dist: langchain-ollama (>=0.3.2,<0.4.0)
Requires-Dist: onnxruntime (==1.19.0)
Requires-Dist: presidio-analyzer (>=2.2.358,<3.0.0)
Requires-Dist: presidio-anonymizer (>=2.2.358,<3.0.0)
Requires-Dist: torch (>=2.7.0,<3.0.0)
Requires-Dist: transformers (>=4.51.3,<5.0.0)
Description-Content-Type: text/markdown

# langchain_plainid

PlainID for LangChain. Library which helps you to integrate PlainID with LangChain.

## Installation

Based on your environment, you can install the library using pip:

```bash
pip install langchain_plainid
```

## Setup with PlainID

Once you have installed the library, you can set up PlainID access.

1. Retrieve your PlainID credentials to access the platform - client ID and client secret.
2. Find you PlainID base URL. For productoin platform you can use `https://platform-product.us1.plainid.io`. Notice URL starts from `platform-product`.

These are 3 parameters you need to use with the library.
_Note_ Please don't share your credentials with anyone, don't store them in your code. Use environment variables or secret management tools to store them.

## Category filtering

To use category filtering with this library, you need to setup related ruleset in PlainID.
e.g if we are using `categories` template name, we need to add the following ruleset:

```rego
# METADATA
# custom:
#   plainid:
#     kind: Ruleset
#     name: All
ruleset(asset, identity, requestParams, action) if {
	asset.template == "categories"
}
```

and setup what categories are available in PlainID through asset types.
e.g. add the following assets: `contract`, `HR`.

Now it's time to use category filtering in your LangChain application.

```python
	from langchain_plainid import PlainIDCategorizer, PlainIDPermissionsProvider


	permissions_provider = PlainIDPermissionsProvider(
	    client_id="your_client_id",
	    client_secret="your_client_secret",
	    base_url="https://platform-product.us1.plainid.io",
		plainid_categories_resource_type="categories")

    plainid_categorizer = PlainIDCategorizer(classifier_provider=<classifier>,permissions_provider=permissions_provider)
    chain = plainid_categorizer
    query = "I'd like to know the weather forecast for today"
	result = chain.invoke(f"{query}") # push your prompt to the chain
```

Categorizer will connect to PlainID and retrieve the list of categories available in your PlainID account.
Then it will classify your prompt with provided `classifier` and pass your query to the next chain element or break execution with `ValueError` exception.

### Category classifiers

We provide 2 classifiers out of the box:

#### LLMCategoryClassifierProvider

This classifier uses LLM to classify your prompt. It uses `langchain` LLMs to classify your prompt. You can configure it with any LLM you want.

```python
	from langchain_plainid import LLMCategoryClassifierProvider

	llm_classifier = LLMCategoryClassifierProvider(llm=OllamaLLM(model="llama2"))
```

Use it with caution, quality of classification depends on the LLM you are using. Some base models could return bad or even wrong results, so use it with big models (OpenAI, Anthropic, etc.) or with models which are trained for classification tasks.

#### ZeroShotCategoryClassifierProvider

This used LLM model which suits best for classification tasks. During the work it will download the model from HuggingFace and use it to classify your prompt.

```python
	from langchain_plainid import ZeroShotCategoryClassifierProvider

	zeroshot_classifier = ZeroShotCategoryClassifierProvider()
```

Use it if you want better classification results, but also have free space on your disk, and can wait for the model to be downloaded.

## Anonymizer

To use anonymizer with this library, you need to setup related ruleset in PlainID.
e.g if we are using `entities` template name, we need to add the following ruleset:

```rego
# METADATA
# custom:
#   plainid:
#     kind: Ruleset
#     name: PERSON
ruleset(asset, identity, requestParams, action) if {
	asset.template == "entities"
	asset["path"] == "PERSON"
	action.id in ["MASK"]
}
```

We support 2 actions: `MASK` and `ENCRYPT`. You can use them to mask or encrypt your data.
Data is always masked with `***` symbols.

The list of possible anonymization sources are based on PII entities. We are using `presidio` library from Microsoft to detect PII entities in your text. You can find the list of supported entities [here](https://microsoft.github.io/presidio/supported_entities/)

Anonymizer will connect to PlainID and retrieve the list of categories available in your PlainID account.
Then it will classify your text and anonymize it. Processed text will be passed the next chain element or break execution with `ValueError` exception. Exception will be raised if there are some problems, or misalignment in your PlainID ruleset.

## Power of creating your chains with PlainID's internal category filtering and anonymization

As a result you can add something like this to your processing chain:

```python
	chain = plainid_categorizer | llm | vector_store | anonymizer | outpur_parser
```

This will allow you to filter your data based on categories and anonymize it before passing to the next chain element. You can use any chain element you want, and it will work with PlainID's internal category filtering and anonymization.

