enkryptai-sdk 0.1.2__py3-none-any.whl → 0.1.3__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.
@@ -27,13 +27,15 @@ class GuardrailsClient:
27
27
  headers = headers or {}
28
28
  if 'apikey' not in headers:
29
29
  headers['apikey'] = self.api_key
30
-
31
- response = self.session.request(method, url, headers=headers, **kwargs)
32
- response.raise_for_status()
30
+
33
31
  try:
32
+ response = self.session.request(method, url, headers=headers, **kwargs)
33
+ response.raise_for_status()
34
34
  return response.json()
35
- except ValueError:
36
- return response.text
35
+
36
+ except Exception as e:
37
+ print(e)
38
+ return {"error": str(e)}
37
39
 
38
40
  # ----------------------------
39
41
  # Basic Guardrails Endpoints
@@ -57,18 +59,24 @@ class GuardrailsClient:
57
59
  """
58
60
  return self._request("GET", "/guardrails/models")
59
61
 
60
- def detect(self, text, config):
62
+ def detect(self, text, config=None):
61
63
  """
62
64
  Detects prompt injection, toxicity, NSFW content, PII, hallucination, and more.
63
65
 
64
66
  Parameters:
65
67
  - text (str): The text to analyze.
66
- - guardrails_config (dict or GuardrailsConfig): A configuration for detectors.
68
+ - guardrails_config (dict or GuardrailsConfig, optional): A configuration for detectors.
67
69
  If a GuardrailsConfig instance is provided, its underlying dictionary will be used.
70
+ If not provided, defaults to injection attack detection only.
68
71
 
69
72
  Returns:
70
73
  - JSON response from the API.
71
74
  """
75
+ # Use injection attack config by default if none provided
76
+ if config is None:
77
+ from .guardrails_config import GuardrailsConfig
78
+ config = GuardrailsConfig.injection_attack()
79
+
72
80
  # Allow passing in either a dict or a GuardrailsConfig instance.
73
81
  if hasattr(config, "as_dict"):
74
82
  config = config.as_dict()
@@ -79,14 +87,15 @@ class GuardrailsClient:
79
87
  }
80
88
  return self._request("POST", "/guardrails/detect", json=payload)
81
89
 
82
- def pii(self, text, mode, key="null"):
90
+ def pii(self, text, mode, key="null", entities=None):
83
91
  """
84
92
  Detects Personally Identifiable Information (PII) and can de-anonymize it.
85
93
  """
86
94
  payload = {
87
95
  "text": text,
88
96
  "mode": mode,
89
- "key": key
97
+ "key": key,
98
+ "entities": entities
90
99
  }
91
100
  return self._request("POST", "/guardrails/pii", json=payload)
92
101
 
@@ -94,42 +103,77 @@ class GuardrailsClient:
94
103
  # Guardrails Policy Endpoints
95
104
  # ----------------------------
96
105
 
97
- def add_policy(self, name, description, config):
106
+ def add_policy(self, name, config, description="guardrails policy"):
98
107
  """
99
108
  Create a new policy with custom configurations.
100
- """
109
+
110
+ Args:
111
+ name (str): Name of the policy
112
+ config (dict or GuardrailsConfig): Configuration for the policy detectors.
113
+ If a GuardrailsConfig instance is provided, its underlying dictionary will be used.
114
+ description (str, optional): Description of the policy. Defaults to "guardrails policy"
115
+ """
116
+ # Allow passing in either a dict or a GuardrailsConfig instance
117
+ if hasattr(config, "as_dict"):
118
+ config = config.as_dict()
119
+
101
120
  payload = {
102
121
  "name": name,
103
122
  "description": description,
104
123
  "detectors": config
105
124
  }
106
- return self._request("POST", "/guardrails/add-policy", json=payload)
125
+
126
+ try:
127
+ return self._request("POST", "/guardrails/add-policy", json=payload)
128
+
129
+ except Exception as e:
130
+ print(e)
131
+ return {"error": str(e)}
107
132
 
108
133
  def get_policy(self, policy_name):
109
134
  """
110
135
  Retrieve an existing policy by providing its header identifier.
111
136
  """
112
137
  headers = {"X-Enkrypt-Policy": policy_name}
113
- return self._request("GET", "/guardrails/get-policy", headers=headers)
138
+ try:
139
+ return self._request("GET", "/guardrails/get-policy", headers=headers)
140
+ except Exception as e:
141
+ print(e)
142
+ return {"error": str(e)}
114
143
 
115
- def modify_policy(self, policy_name, name, description, config):
144
+ def modify_policy(self, policy_name, config, name=None, description="guardrails policy"):
116
145
  """
117
146
  Modify an existing policy.
118
147
  """
148
+ # Allow passing in either a dict or a GuardrailsConfig instance
149
+ if hasattr(config, "as_dict"):
150
+ config = config.as_dict()
151
+
152
+ if name is None:
153
+ name = policy_name
154
+
119
155
  headers = {"X-Enkrypt-Policy": policy_name}
120
156
  payload = {
157
+ "detectors": config,
121
158
  "name": name,
122
- "description": description,
123
- "detectors": config
159
+ "description": description
124
160
  }
125
- return self._request("PATCH", "/guardrails/modify-policy", headers=headers, json=payload)
161
+ try:
162
+ return self._request("PATCH", "/guardrails/modify-policy", headers=headers, json=payload)
163
+ except Exception as e:
164
+ print(e)
165
+ return {"error": str(e)}
126
166
 
127
167
  def delete_policy(self, policy_name):
128
168
  """
129
169
  Delete a policy.
130
170
  """
131
171
  headers = {"X-Enkrypt-Policy": policy_name}
132
- return self._request("DELETE", "/guardrails/delete-policy", headers=headers)
172
+ try:
173
+ return self._request("DELETE", "/guardrails/delete-policy", headers=headers)
174
+ except Exception as e:
175
+ print(e)
176
+ return {"error": str(e)}
133
177
 
134
178
  def policy_detect(self, policy_name, text):
135
179
  """
@@ -137,4 +181,8 @@ class GuardrailsClient:
137
181
  """
138
182
  headers = {"X-Enkrypt-Policy": policy_name}
139
183
  payload = {"text": text}
140
- return self._request("POST", "/guardrails/policy/detect", headers=headers, json=payload)
184
+ try:
185
+ return self._request("POST", "/guardrails/policy/detect", headers=headers, json=payload)
186
+ except Exception as e:
187
+ print(e)
188
+ return {"error": str(e)}
@@ -32,12 +32,13 @@ class GuardrailsConfig:
32
32
  Returns a configuration instance pre-configured for injection attack detection.
33
33
  """
34
34
  config = copy.deepcopy(DEFAULT_CONFIG)
35
- config["topic_detector"] = {"enabled": True, "topic": ["injection attack"]}
36
35
  config["injection_attack"] = {"enabled": True}
37
36
  return cls(config)
38
37
 
39
38
  @classmethod
40
- def policy_violation(cls, policy_text: str, need_explanation: bool = False):
39
+ def policy_violation(cls,
40
+ policy_text: str,
41
+ need_explanation: bool = False):
41
42
  """
42
43
  Returns a configuration instance pre-configured for policy violation detection.
43
44
  """
@@ -48,6 +49,102 @@ class GuardrailsConfig:
48
49
  }
49
50
  return cls(config)
50
51
 
52
+ @classmethod
53
+ def toxicity(cls):
54
+ """
55
+ Returns a configuration instance pre-configured for toxicity detection.
56
+ """
57
+ config = copy.deepcopy(DEFAULT_CONFIG)
58
+ config["toxicity"] = {"enabled": True}
59
+ return cls(config)
60
+
61
+ @classmethod
62
+ def nsfw(cls):
63
+ """
64
+ Returns a configuration instance pre-configured for NSFW content detection.
65
+ """
66
+ config = copy.deepcopy(DEFAULT_CONFIG)
67
+ config["nsfw"] = {"enabled": True}
68
+ return cls(config)
69
+
70
+ @classmethod
71
+ def bias(cls):
72
+ """
73
+ Returns a configuration instance pre-configured for bias detection.
74
+ """
75
+ config = copy.deepcopy(DEFAULT_CONFIG)
76
+ config["bias"] = {"enabled": True}
77
+ return cls(config)
78
+
79
+ @classmethod
80
+ def pii(cls, entities=None):
81
+ """
82
+ Returns a configuration instance pre-configured for PII detection.
83
+
84
+ Args:
85
+ entities (list, optional): List of PII entity types to detect.
86
+ """
87
+ config = copy.deepcopy(DEFAULT_CONFIG)
88
+ config["pii"] = {
89
+ "enabled": True,
90
+ "entities": entities if entities is not None else []
91
+ }
92
+ return cls(config)
93
+
94
+ @classmethod
95
+ def topic(cls, topics=None):
96
+ """
97
+ Returns a configuration instance pre-configured for topic detection.
98
+
99
+ Args:
100
+ topics (list, optional): List of topics to detect.
101
+ """
102
+ config = copy.deepcopy(DEFAULT_CONFIG)
103
+ config["topic_detector"] = {
104
+ "enabled": True,
105
+ "topic": topics if topics is not None else []
106
+ }
107
+ return cls(config)
108
+
109
+ @classmethod
110
+ def keyword(cls, keywords=None):
111
+ """
112
+ Returns a configuration instance pre-configured for keyword detection.
113
+
114
+ Args:
115
+ keywords (list, optional): List of banned keywords to detect.
116
+ """
117
+ config = copy.deepcopy(DEFAULT_CONFIG)
118
+ config["keyword_detector"] = {
119
+ "enabled": True,
120
+ "banned_keywords": keywords if keywords is not None else []
121
+ }
122
+ return cls(config)
123
+
124
+ @classmethod
125
+ def copyright_ip(cls):
126
+ """
127
+ Returns a configuration instance pre-configured for copyright/IP detection.
128
+ """
129
+ config = copy.deepcopy(DEFAULT_CONFIG)
130
+ config["copyright_ip"] = {"enabled": True}
131
+ return cls(config)
132
+
133
+ @classmethod
134
+ def system_prompt(cls, index="system"):
135
+ """
136
+ Returns a configuration instance pre-configured for system prompt detection.
137
+
138
+ Args:
139
+ index (str, optional): Index name for system prompt detection. Defaults to "system".
140
+ """
141
+ config = copy.deepcopy(DEFAULT_CONFIG)
142
+ config["system_prompt"] = {
143
+ "enabled": True,
144
+ "index": index
145
+ }
146
+ return cls(config)
147
+
51
148
  def update(self, **kwargs):
52
149
  """
53
150
  Update the configuration with custom values.
@@ -68,3 +165,57 @@ class GuardrailsConfig:
68
165
  Return the underlying configuration dictionary.
69
166
  """
70
167
  return self.config
168
+
169
+ @classmethod
170
+ def from_custom_config(cls, config_dict: dict):
171
+ """
172
+ Configure guardrails from a dictionary input.
173
+
174
+ Validates that the input dictionary matches the expected schema structure.
175
+ Each key must exist in the default configuration, and its value must be a dictionary.
176
+
177
+ Args:
178
+ config_dict (dict): Dictionary containing guardrails configuration
179
+
180
+ Returns:
181
+ GuardrailsConfig: Returns a new GuardrailsConfig instance
182
+
183
+ Raises:
184
+ ValueError: If the input dictionary contains invalid keys or malformed values
185
+ """
186
+ instance = cls()
187
+ for key, value in config_dict.items():
188
+ if key not in instance.config:
189
+ raise ValueError(f"Unknown detector config: {key}")
190
+ if not isinstance(value, dict):
191
+ raise ValueError(f"Config value for {key} must be a dictionary")
192
+
193
+ # Validate that all required fields exist in the default config
194
+ default_fields = set(DEFAULT_CONFIG[key].keys())
195
+ provided_fields = set(value.keys())
196
+
197
+ if not provided_fields.issubset(default_fields):
198
+ invalid_fields = provided_fields - default_fields
199
+ raise ValueError(f"Invalid fields for {key}: {invalid_fields}")
200
+
201
+ instance.config[key] = value
202
+
203
+ return instance
204
+
205
+ def get_config(self, detector_name: str) -> dict:
206
+ """
207
+ Get the configuration for a specific detector.
208
+
209
+ Args:
210
+ detector_name (str): Name of the detector to get configuration for
211
+
212
+ Returns:
213
+ dict: Configuration dictionary for the specified detector
214
+
215
+ Raises:
216
+ ValueError: If the detector name doesn't exist in the configuration
217
+ """
218
+ if detector_name not in self.config:
219
+ raise ValueError(f"Unknown detector: {detector_name}")
220
+
221
+ return copy.deepcopy(self.config[detector_name])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: enkryptai-sdk
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: A Python SDK with guardrails and red teaming functionality for API interactions
5
5
  Home-page: https://github.com/enkryptai/enkryptai-sdk
6
6
  Author: Enkrypt AI Team
@@ -68,7 +68,98 @@ config = GuardrailsConfig.policy_violation(policy_text="You must not use hate sp
68
68
  config = GuardrailsConfig.topic_detection(topic="finance")
69
69
  ```
70
70
 
71
+ ## Policy Management
71
72
 
73
+ Policies allow you to save and reuse guardrails configurations.
74
+
75
+ ### Create a Policy
76
+
77
+ ```python
78
+ from enkryptai_sdk import GuardrailsClient, GuardrailsConfig
79
+
80
+ client = GuardrailsClient(api_key="your_api_key")
81
+
82
+ # Create a policy with injection attack detection
83
+ injection_config = GuardrailsConfig.injection_attack()
84
+ client.add_policy(
85
+ name="my-security-policy",
86
+ config=injection_config,
87
+ description="Detects prompt injection attacks"
88
+ )
89
+
90
+ # Create a policy with multiple detectors
91
+ custom_config = GuardrailsConfig.from_custom_config({
92
+ "injection_attack": {"enabled": True},
93
+ "bias": {"enabled": True},
94
+ "policy_violation": {
95
+ "enabled": True,
96
+ "policy_text": "No discussion of hacking allowed",
97
+ "need_explanation": True
98
+ }
99
+ })
100
+
101
+ client.add_policy(
102
+ name="my-custom-policy",
103
+ config=custom_config,
104
+ description="Custom security policy"
105
+ )
106
+ ```
107
+
108
+ ### Modify a Policy
109
+
110
+ ```python
111
+ # Update policy with new configuration
112
+ new_config = GuardrailsConfig.bias() # Switch to bias detection
113
+ client.modify_policy(
114
+ policy_name="my-security-policy",
115
+ config=new_config,
116
+ description="Updated to detect bias"
117
+ )
118
+ ```
119
+
120
+ ### Use a Policy
121
+
122
+ ```python
123
+ # Apply policy to detect content
124
+ response = client.policy_detect(
125
+ policy_name="my-security-policy",
126
+ text="Check this text for policy violations"
127
+ )
128
+
129
+ print(response)
130
+ ```
131
+
132
+ ### Get Policy Details
133
+
134
+ ```python
135
+ # Retrieve policy configuration
136
+ policy = client.get_policy("my-security-policy")
137
+ print(policy)
138
+ ```
139
+
140
+ ### Delete a Policy
141
+
142
+ ```python
143
+ # Remove a policy
144
+ client.delete_policy("my-security-policy")
145
+ ```
146
+
147
+ ### Available Policy Options
148
+
149
+ Policies can include any combination of these detectors:
150
+
151
+ - `injection_attack`: Detect prompt injection attempts
152
+ - `bias`: Detect biased content
153
+ - `policy_violation`: Check against custom policy rules
154
+ - `topic_detection`: Detect specific topics
155
+ - `nsfw`: Filter inappropriate content
156
+ - `toxicity`: Detect toxic language
157
+ - `pii`: Detect personal information
158
+ - `copyright_ip`: Check for copyright/IP violations
159
+ - `system_prompt`: Detect system prompt leaks
160
+ - `keyword_detector`: Check for specific keywords
161
+
162
+ Each detector can be enabled/disabled and configured with specific options through `GuardrailsConfig`.
72
163
 
73
164
  ## Guardrails Client
74
165
 
@@ -0,0 +1,9 @@
1
+ enkryptai_sdk/__init__.py,sha256=git4pQzKT36zA-wIDz71crkRhp91FFIYrJXehUcnofg,141
2
+ enkryptai_sdk/guardrails.py,sha256=diBPOa07tUxxw4C-DwhWQWwWrHGoH_KoazsXI929OAc,6201
3
+ enkryptai_sdk/guardrails_config.py,sha256=V1QYo6aCk0l9FUE4LEfJhmgM1rZB7NWdvkmP-6FN-X8,7462
4
+ enkryptai_sdk/red_team.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ enkryptai_sdk-0.1.3.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ enkryptai_sdk-0.1.3.dist-info/METADATA,sha256=RuMr4SOP6iO3J8aCR2Y_Rbq68abZZ4Mymg74z3zy0-4,4578
7
+ enkryptai_sdk-0.1.3.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
8
+ enkryptai_sdk-0.1.3.dist-info/top_level.txt,sha256=s2X9UJJwvJamNmr6ZXWyyQe60sXtQGWFuaBYfhgHI_4,14
9
+ enkryptai_sdk-0.1.3.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- enkryptai_sdk/__init__.py,sha256=git4pQzKT36zA-wIDz71crkRhp91FFIYrJXehUcnofg,141
2
- enkryptai_sdk/guardrails.py,sha256=toEYdYMU0qM6wmrpxfoR-DgzV7tug4NHtilu01CVpP8,4457
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.2.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- enkryptai_sdk-0.1.2.dist-info/METADATA,sha256=rJYlv7XSpSu5O7VzXYFx11--CJIymKM01N2Era_AuO8,2324
7
- enkryptai_sdk-0.1.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
8
- enkryptai_sdk-0.1.2.dist-info/top_level.txt,sha256=s2X9UJJwvJamNmr6ZXWyyQe60sXtQGWFuaBYfhgHI_4,14
9
- enkryptai_sdk-0.1.2.dist-info/RECORD,,