-
Notifications
You must be signed in to change notification settings - Fork 926
Add a configurable LangExtract recognizer for use with any provider. #1815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f559d70
Add a basic, configurable LangExtract-based recognizer class for use …
telackey 0a72da8
Add a basic, configurable LangExtract-based recognizer class for use …
telackey 5e8a049
Merge branch 'main' into telackey/lellm
telackey 8424ddb
Merge branch 'main' into telackey/lellm
Kassymkhan919 7530daf
Address comments (#4)
telackey 3b96eca
Address comment in telackey/lellm (#5)
telackey 798abe9
Remove changes not required
telackey a484ff6
docstring
telackey 00d5a6f
update docs
telackey 81b2cdd
ruff
telackey 8b2fbdc
Merge branch 'main' into telackey/lellm
telackey 7528eae
Merge branch 'main' into telackey/lellm
telackey 90cdc30
Merge branch 'main' into telackey/lellm
telackey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
presidio-analyzer/presidio_analyzer/conf/langextract_config_basic.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Configurable LangExtract Configuration | ||
| # Supports multiple LLM providers via LangExtract's ModelConfig | ||
|
|
||
| lm_recognizer: | ||
| supported_entities: | ||
| - PERSON | ||
| - EMAIL_ADDRESS | ||
| - PHONE_NUMBER | ||
| - US_SSN | ||
| - LOCATION | ||
| - ORGANIZATION | ||
| - DATE_TIME | ||
| - CREDIT_CARD | ||
| - IP_ADDRESS | ||
| - URL | ||
|
|
||
| labels_to_ignore: | ||
| - payment_status | ||
| - metadata | ||
| - annotation | ||
|
|
||
| enable_generic_consolidation: true | ||
| min_score: 0.5 | ||
|
|
||
| langextract: | ||
| prompt_file: "presidio_analyzer/conf/langextract_prompts/default_pii_phi_prompt.j2" | ||
| examples_file: "presidio_analyzer/conf/langextract_prompts/default_pii_phi_examples.yaml" | ||
|
|
||
| model: | ||
| model_id: "gpt-4o" | ||
| provider: | ||
| name: "openai" | ||
| kwargs: | ||
| base_url: "https://api.openai.com/v1" | ||
| # api_key: "API_KEY_GOES_HERE" or set env LANGEXTRACT_API_KEY | ||
|
|
||
| entity_mappings: | ||
| person: PERSON | ||
| name: PERSON | ||
| email: EMAIL_ADDRESS | ||
| phone: PHONE_NUMBER | ||
| ssn: US_SSN | ||
| location: LOCATION | ||
| address: LOCATION | ||
| organization: ORGANIZATION | ||
| date: DATE_TIME | ||
| credit_card: CREDIT_CARD | ||
| ip_address: IP_ADDRESS | ||
| url: URL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...yzer/presidio_analyzer/predefined_recognizers/third_party/basic_langextract_recognizer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from presidio_analyzer.llm_utils import lx, lx_factory | ||
| from presidio_analyzer.predefined_recognizers.third_party.\ | ||
| langextract_recognizer import LangExtractRecognizer | ||
|
|
||
| logger = logging.getLogger("presidio-analyzer") | ||
|
|
||
| class BasicLangExtractRecognizer(LangExtractRecognizer): | ||
| """Basic LangExtract recognizer using configurable backend.""" | ||
|
|
||
| DEFAULT_CONFIG_PATH = ( | ||
| Path(__file__).parent.parent.parent / "conf" / "langextract_config_basic.yaml" | ||
| ) | ||
|
|
||
| def __init__( | ||
| self, | ||
| config_path: Optional[str] = None, | ||
| supported_language: str = "en", | ||
| context: Optional[list] = None | ||
| ): | ||
| """Initialize Basic LangExtract recognizer. | ||
|
|
||
| :param config_path: Path to configuration file (optional). | ||
| :param supported_language: Language this recognizer supports | ||
| (optional, default: "en"). | ||
| :param context: List of context words | ||
| (optional, currently not used by LLM recognizers). | ||
| """ | ||
| actual_config_path = ( | ||
| config_path if config_path else str(self.DEFAULT_CONFIG_PATH) | ||
| ) | ||
|
|
||
| super().__init__( | ||
telackey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| config_path=actual_config_path, | ||
| name="Basic LangExtract PII", | ||
| supported_language=supported_language | ||
| ) | ||
|
|
||
| model_config = self.config.get("model", {}) | ||
| provider_config = model_config.get("provider", {}) | ||
| self.model_id = model_config.get("model_id") | ||
| self.provider = provider_config.get("name") | ||
telackey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.provider_kwargs = provider_config.get("kwargs", {}) | ||
| if not self.model_id: | ||
| raise ValueError("Configuration must contain 'model_id'") | ||
| if not self.provider: | ||
| raise ValueError("Configuration must contain 'provider'") | ||
|
|
||
| self.fence_output = model_config.get("fence_output", "openai" in self.provider.lower()) | ||
| self.use_schema_constraints = model_config.get("use_schema_constraints", False) | ||
|
|
||
| if "api_key" not in self.provider_kwargs and "LANGEXTRACT_API_KEY" in os.environ: | ||
| self.provider_kwargs["api_key"] = os.environ["LANGEXTRACT_API_KEY"] | ||
|
|
||
| self.lx_model_config = lx_factory.ModelConfig( | ||
| model_id=self.model_id, | ||
| provider=self.provider, | ||
| provider_kwargs=self.provider_kwargs, | ||
| ) | ||
|
|
||
| def _get_provider_params(self): | ||
telackey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Return Azure OpenAI-specific params.""" | ||
telackey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return { | ||
| "config": self.lx_model_config, | ||
| "fence_output": self.fence_output, | ||
| "use_schema_constraints": self.use_schema_constraints, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.