enkryptai-sdk 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ from .guardrails import GuardrailsClient
2
+ from .guardrails_config import GuardrailsConfig
3
+
4
+ __all__ = ["GuardrailsClient", "GuardrailsConfig"]
@@ -0,0 +1,140 @@
1
+ import requests
2
+
3
+ class GuardrailsClient:
4
+ """
5
+ A client for interacting with Enkrypt AI Guardrails API endpoints.
6
+ """
7
+
8
+ def __init__(self, api_key, base_url="https://api.enkryptai.com"):
9
+ """
10
+ Initializes the client.
11
+
12
+ Parameters:
13
+ - api_key (str): Your API key for authenticating with the service.
14
+ - base_url (str): Base URL of the API (default: "https://api.enkryptai.com").
15
+ """
16
+ self.api_key = api_key
17
+ self.base_url = base_url.rstrip('/')
18
+ self.session = requests.Session()
19
+
20
+ def _request(self, method, endpoint, headers=None, **kwargs):
21
+ """
22
+ Internal helper to send an HTTP request.
23
+
24
+ Automatically adds the API key to headers.
25
+ """
26
+ url = self.base_url + endpoint
27
+ headers = headers or {}
28
+ if 'apikey' not in headers:
29
+ headers['apikey'] = self.api_key
30
+
31
+ response = self.session.request(method, url, headers=headers, **kwargs)
32
+ response.raise_for_status()
33
+ try:
34
+ return response.json()
35
+ except ValueError:
36
+ return response.text
37
+
38
+ # ----------------------------
39
+ # Basic Guardrails Endpoints
40
+ # ----------------------------
41
+
42
+ def health(self):
43
+ """
44
+ Get the health status of the service.
45
+ """
46
+ return self._request("GET", "/guardrails/health")
47
+
48
+ def status(self):
49
+ """
50
+ Check if the API is up and running.
51
+ """
52
+ return self._request("GET", "/guardrails/status")
53
+
54
+ def models(self):
55
+ """
56
+ Retrieve the list of models loaded by the service.
57
+ """
58
+ return self._request("GET", "/guardrails/models")
59
+
60
+ def detect(self, text, guardrails_config):
61
+ """
62
+ Detects prompt injection, toxicity, NSFW content, PII, hallucination, and more.
63
+
64
+ Parameters:
65
+ - text (str): The text to analyze.
66
+ - guardrails_config (dict or GuardrailsConfig): A configuration for detectors.
67
+ If a GuardrailsConfig instance is provided, its underlying dictionary will be used.
68
+
69
+ Returns:
70
+ - JSON response from the API.
71
+ """
72
+ # Allow passing in either a dict or a GuardrailsConfig instance.
73
+ if hasattr(guardrails_config, "as_dict"):
74
+ guardrails_config = guardrails_config.as_dict()
75
+
76
+ payload = {
77
+ "text": text,
78
+ "detectors": guardrails_config
79
+ }
80
+ return self._request("POST", "/guardrails/detect", json=payload)
81
+
82
+ def pii(self, text, mode, key="null"):
83
+ """
84
+ Detects Personally Identifiable Information (PII) and can de-anonymize it.
85
+ """
86
+ payload = {
87
+ "text": text,
88
+ "mode": mode,
89
+ "key": key
90
+ }
91
+ return self._request("POST", "/guardrails/pii", json=payload)
92
+
93
+ # ----------------------------
94
+ # Guardrails Policy Endpoints
95
+ # ----------------------------
96
+
97
+ def add_policy(self, name, description, guardrails_config):
98
+ """
99
+ Create a new policy with custom configurations.
100
+ """
101
+ payload = {
102
+ "name": name,
103
+ "description": description,
104
+ "detectors": guardrails_config
105
+ }
106
+ return self._request("POST", "/guardrails/add-policy", json=payload)
107
+
108
+ def get_policy(self, x_enkrypt_policy):
109
+ """
110
+ Retrieve an existing policy by providing its header identifier.
111
+ """
112
+ headers = {"X-Enkrypt-Policy": x_enkrypt_policy}
113
+ return self._request("GET", "/guardrails/get-policy", headers=headers)
114
+
115
+ def modify_policy(self, x_enkrypt_policy, name, description, guardrails_config):
116
+ """
117
+ Modify an existing policy.
118
+ """
119
+ headers = {"X-Enkrypt-Policy": x_enkrypt_policy}
120
+ payload = {
121
+ "name": name,
122
+ "description": description,
123
+ "detectors": guardrails_config
124
+ }
125
+ return self._request("PATCH", "/guardrails/modify-policy", headers=headers, json=payload)
126
+
127
+ def delete_policy(self, x_enkrypt_policy):
128
+ """
129
+ Delete a policy.
130
+ """
131
+ headers = {"X-Enkrypt-Policy": x_enkrypt_policy}
132
+ return self._request("DELETE", "/guardrails/delete-policy", headers=headers)
133
+
134
+ def policy_detect(self, x_enkrypt_policy, text):
135
+ """
136
+ Apply a specific policy to detect and filter content.
137
+ """
138
+ headers = {"X-Enkrypt-Policy": x_enkrypt_policy}
139
+ payload = {"text": text}
140
+ return self._request("POST", "/guardrails/policy/detect", headers=headers, json=payload)
@@ -0,0 +1,70 @@
1
+ import copy
2
+
3
+ # Base default configuration for all detectors.
4
+ DEFAULT_CONFIG = {
5
+ "topic_detector": {"enabled": False, "topic": []},
6
+ "nsfw": {"enabled": False},
7
+ "toxicity": {"enabled": False},
8
+ "pii": {"enabled": False, "entities": []},
9
+ "injection_attack": {"enabled": False},
10
+ "keyword_detector": {"enabled": False, "banned_keywords": []},
11
+ "policy_violation": {"enabled": False, "policy_text": "", "need_explanation": False},
12
+ "bias": {"enabled": False},
13
+ "copyright_ip": {"enabled": False},
14
+ "system_prompt": {"enabled": False, "index": "system"}
15
+ }
16
+
17
+
18
+ class GuardrailsConfig:
19
+ """
20
+ A helper class to manage Guardrails configuration.
21
+
22
+ Users can either use preset configurations or build a custom one.
23
+ """
24
+
25
+ def __init__(self, config=None):
26
+ # Use a deep copy of the default to avoid accidental mutation.
27
+ self.config = copy.deepcopy(DEFAULT_CONFIG) if config is None else config
28
+
29
+ @classmethod
30
+ def injection_attack(cls):
31
+ """
32
+ Returns a configuration instance pre-configured for injection attack detection.
33
+ """
34
+ config = copy.deepcopy(DEFAULT_CONFIG)
35
+ config["topic_detector"] = {"enabled": True, "topic": ["injection attack"]}
36
+ config["injection_attack"] = {"enabled": True}
37
+ return cls(config)
38
+
39
+ @classmethod
40
+ def policy_violation(cls, policy_text: str, need_explanation: bool = False):
41
+ """
42
+ Returns a configuration instance pre-configured for policy violation detection.
43
+ """
44
+ config = copy.deepcopy(DEFAULT_CONFIG)
45
+ config["policy_violation"] = {"enabled": True,
46
+ "policy_text": policy_text,
47
+ "need_explanation": need_explanation
48
+ }
49
+ return cls(config)
50
+
51
+ def update(self, **kwargs):
52
+ """
53
+ Update the configuration with custom values.
54
+
55
+ Only keys that exist in the default configuration can be updated.
56
+ For example:
57
+ config.update(nsfw={"enabled": True}, toxicity={"enabled": True})
58
+ """
59
+ for key, value in kwargs.items():
60
+ if key in self.config:
61
+ self.config[key] = value
62
+ else:
63
+ raise ValueError(f"Unknown detector config: {key}")
64
+ return self
65
+
66
+ def as_dict(self):
67
+ """
68
+ Return the underlying configuration dictionary.
69
+ """
70
+ return self.config
File without changes
File without changes
@@ -0,0 +1,96 @@
1
+ Metadata-Version: 2.2
2
+ Name: enkryptai-sdk
3
+ Version: 0.1.0
4
+ Summary: A Python SDK with guardrails and red teaming functionality for API interactions
5
+ Home-page: https://github.com/enkryptai/enkryptai-sdk
6
+ Author: Enkrypt AI Team
7
+ Author-email: software@enkryptai.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.11
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: home-page
20
+ Dynamic: requires-python
21
+ Dynamic: summary
22
+
23
+ # enkryptai-sdk
24
+
25
+ A Python SDK with guardrails and red teaming functionality for API interactions.
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ pip install enkryptai-sdk
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ```python
36
+ from enkryptai_sdk import GuardrailsClient, GuardrailsConfig
37
+
38
+ client = GuardrailsClient(api_key="your_api_key", base_url="https://api.enkryptai.com")
39
+
40
+ config = GuardrailsConfig.injection_attack()
41
+ ```
42
+
43
+ ## Guardrails Configs
44
+
45
+ ### Injection Attack
46
+
47
+ ```python
48
+ config = GuardrailsConfig.injection_attack()
49
+ ```
50
+
51
+ ### Policy Violation
52
+
53
+ ```python
54
+ config = GuardrailsConfig.policy_violation(policy_text="You must be 18 years or older to use this service.")
55
+ ```
56
+
57
+ ### Topic Detection
58
+
59
+ ```python
60
+ config = GuardrailsConfig.topic_detection(topic="injection attack")
61
+ ```
62
+
63
+ ### Red Teaming
64
+
65
+ ```python
66
+ config = GuardrailsConfig.red_teaming()
67
+ ```
68
+
69
+ ## Guardrails Client
70
+
71
+ ```python
72
+ client = GuardrailsClient(api_key="your_api_key")
73
+
74
+ ```
75
+
76
+ ## Detect Attack
77
+
78
+ ```python
79
+ config = GuardrailsConfig.injection_attack()
80
+ response = client.detect(text="Hello, world!", config=config)
81
+ ```
82
+
83
+ ## Detect Policy Violation
84
+
85
+ ```python
86
+ config = GuardrailsConfig.policy_violation(policy_text="No rude content or hate speech allowed")
87
+ response = client.detect(text="I hate everyone", config=config)
88
+ ```
89
+
90
+ ## Detect Topic Detection
91
+
92
+ ```python
93
+ config = GuardrailsConfig.topic_detection(topic="finance")
94
+ response = client.detect(text="I am buying $1000 of BTC", config=config)
95
+ ```
96
+
@@ -0,0 +1,9 @@
1
+ enkryptai_sdk/__init__.py,sha256=git4pQzKT36zA-wIDz71crkRhp91FFIYrJXehUcnofg,141
2
+ enkryptai_sdk/guardrails.py,sha256=rxUiHPAvdgIjX00XtAY5jPGde-_OEnpmV51jDxAZOSA,4596
3
+ enkryptai_sdk/guardrails_config.py,sha256=oFhCX2hJGVFQfGcaJqji4enc35gK5dTc95uSWungRPE,2487
4
+ enkryptai_sdk/red_team.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ enkryptai_sdk-0.1.0.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ enkryptai_sdk-0.1.0.dist-info/METADATA,sha256=4m42h7ENkvXslzPybV0VOQjyybiyVAKFByXelkJTdiA,2077
7
+ enkryptai_sdk-0.1.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
8
+ enkryptai_sdk-0.1.0.dist-info/top_level.txt,sha256=s2X9UJJwvJamNmr6ZXWyyQe60sXtQGWFuaBYfhgHI_4,14
9
+ enkryptai_sdk-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.8.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ enkryptai_sdk