koreshield 0.1.4__tar.gz → 0.2.0__tar.gz

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.
Files changed (35) hide show
  1. {koreshield-0.1.4/src/koreshield.egg-info → koreshield-0.2.0}/PKG-INFO +289 -50
  2. koreshield-0.2.0/README.md +625 -0
  3. koreshield-0.2.0/examples/batch_processing.py +78 -0
  4. koreshield-0.2.0/examples/framework_integrations.py +171 -0
  5. koreshield-0.2.0/examples/streaming_scan.py +137 -0
  6. {koreshield-0.1.4 → koreshield-0.2.0}/pyproject.toml +3 -1
  7. {koreshield-0.1.4 → koreshield-0.2.0/src/koreshield.egg-info}/PKG-INFO +289 -50
  8. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield.egg-info/SOURCES.txt +7 -1
  9. koreshield-0.2.0/src/koreshield_sdk/async_client.py +544 -0
  10. koreshield-0.2.0/src/koreshield_sdk/integrations/__init__.py +39 -0
  11. koreshield-0.2.0/src/koreshield_sdk/integrations/frameworks.py +361 -0
  12. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield_sdk/types.py +53 -1
  13. koreshield-0.2.0/tests/test_async_client.py +115 -0
  14. {koreshield-0.1.4 → koreshield-0.2.0}/tests/test_client.py +2 -1
  15. koreshield-0.2.0/tests/test_integrations.py +273 -0
  16. koreshield-0.1.4/README.md +0 -387
  17. koreshield-0.1.4/src/koreshield_sdk/async_client.py +0 -263
  18. koreshield-0.1.4/src/koreshield_sdk/integrations/__init__.py +0 -15
  19. {koreshield-0.1.4 → koreshield-0.2.0}/CHANGELOG.md +0 -0
  20. {koreshield-0.1.4 → koreshield-0.2.0}/LICENSE +0 -0
  21. {koreshield-0.1.4 → koreshield-0.2.0}/MANIFEST.in +0 -0
  22. {koreshield-0.1.4 → koreshield-0.2.0}/examples/async_usage.py +0 -0
  23. {koreshield-0.1.4 → koreshield-0.2.0}/examples/basic_usage.py +0 -0
  24. {koreshield-0.1.4 → koreshield-0.2.0}/examples/deepseek_integration.py +0 -0
  25. {koreshield-0.1.4 → koreshield-0.2.0}/examples/fastapi_integration.py +0 -0
  26. {koreshield-0.1.4 → koreshield-0.2.0}/examples/langchain_integration.py +0 -0
  27. {koreshield-0.1.4 → koreshield-0.2.0}/setup.cfg +0 -0
  28. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield.egg-info/dependency_links.txt +0 -0
  29. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield.egg-info/requires.txt +0 -0
  30. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield.egg-info/top_level.txt +0 -0
  31. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield_sdk/__init__.py +0 -0
  32. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield_sdk/client.py +0 -0
  33. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield_sdk/exceptions.py +0 -0
  34. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield_sdk/integrations/langchain.py +0 -0
  35. {koreshield-0.1.4 → koreshield-0.2.0}/src/koreshield_sdk/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: koreshield
3
- Version: 0.1.4
3
+ Version: 0.2.0
4
4
  Summary: Python SDK for KoreShield LLM Security Platform
5
5
  Author-email: KoreShield Team <team@koreshield.com>
6
6
  Maintainer-email: KoreShield Team <team@koreshield.com>
@@ -10,6 +10,7 @@ Project-URL: Documentation, https://docs.koreshield.com
10
10
  Project-URL: Repository, https://github.com/koreshield/koreshield.git
11
11
  Project-URL: Issues, https://github.com/koreshield/koreshield/issues
12
12
  Project-URL: Changelog, https://github.com/koreshield/koreshield/blob/main/koreshield-python-sdk/CHANGELOG.md
13
+ Project-URL: PyPI, https://pypi.org/project/koreshield/
13
14
  Keywords: llm,security,ai,safety,prompt-injection,koreshield
14
15
  Classifier: Development Status :: 3 - Alpha
15
16
  Classifier: Intended Audience :: Developers
@@ -61,6 +62,16 @@ Dynamic: license-file
61
62
 
62
63
  A comprehensive Python SDK for integrating KoreShield's LLM security features into your applications with ease.
63
64
 
65
+ ## New in v0.2.0
66
+
67
+ - **Enhanced Async Support**: Improved async/await patterns with context managers and performance monitoring
68
+ - **Advanced Batch Processing**: Optimized batch scanning with progress callbacks, concurrency control, and batching
69
+ - **Streaming Content Scanning**: Real-time scanning of long content with overlapping chunks
70
+ - **Security Policies**: Configurable allowlist/blocklist patterns and custom threat rules
71
+ - **Framework Integrations**: Built-in middleware for FastAPI, Flask, and Django
72
+ - **Performance Monitoring**: Comprehensive metrics collection and analytics
73
+ - **Type Safety**: Full Pydantic models for all data structures
74
+
64
75
  ## Supported LLM Providers
65
76
 
66
77
  KoreShield supports multiple LLM providers through its proxy architecture. Configure your preferred provider in the KoreShield API:
@@ -80,11 +91,11 @@ providers:
80
91
  deepseek:
81
92
  enabled: true
82
93
  base_url: "https://api.deepseek.com/v1"
83
-
94
+
84
95
  openai:
85
96
  enabled: false
86
97
  base_url: "https://api.openai.com/v1"
87
-
98
+
88
99
  anthropic:
89
100
  enabled: false
90
101
  base_url: "https://api.anthropic.com/v1"
@@ -95,7 +106,7 @@ Set the corresponding API key as an environment variable:
95
106
  export DEEPSEEK_API_KEY="your-deepseek-key"
96
107
  # or
97
108
  export OPENAI_API_KEY="your-openai-key"
98
- # or
109
+ # or
99
110
  export ANTHROPIC_API_KEY="your-anthropic-key"
100
111
  ```
101
112
 
@@ -130,17 +141,21 @@ result = client.scan_prompt("Hello, how are you?")
130
141
  print(f"Safe: {result.is_safe}, Threat Level: {result.threat_level}")
131
142
  ```
132
143
 
133
- ### Async Usage
144
+ ### Enhanced Async Usage
134
145
 
135
146
  ```python
136
147
  import asyncio
137
148
  from koreshield_sdk import AsyncKoreShieldClient
138
149
 
139
150
  async def main():
140
- async with AsyncKoreShieldClient(api_key="your-api-key") as client:
151
+ async with AsyncKoreShieldClient(api_key="your-api-key", enable_metrics=True) as client:
141
152
  result = await client.scan_prompt("Tell me a joke")
142
153
  print(f"Confidence: {result.confidence}")
143
154
 
155
+ # Get performance metrics
156
+ metrics = await client.get_performance_metrics()
157
+ print(f"Total requests: {metrics.total_requests}")
158
+
144
159
  asyncio.run(main())
145
160
  ```
146
161
 
@@ -177,14 +192,27 @@ response = llm([HumanMessage(content="Hello!")])
177
192
 
178
193
  ### AsyncKoreShieldClient
179
194
 
180
- #### Methods
195
+ #### Core Methods
181
196
 
182
197
  - `scan_prompt(prompt: str, **kwargs) -> DetectionResult` (async)
183
- - `scan_batch(prompts: List[str], parallel=True, max_concurrent=10) -> List[DetectionResult]` (async)
198
+ - `scan_batch(prompts: List[str], parallel=True, max_concurrent=10, progress_callback=None) -> List[DetectionResult]` (async)
199
+ - `scan_stream(content: str, chunk_size=1000, overlap=100, **kwargs) -> StreamingScanResponse` (async)
184
200
  - `get_scan_history(limit=50, offset=0, **filters) -> Dict` (async)
185
201
  - `get_scan_details(scan_id: str) -> Dict` (async)
186
202
  - `health_check() -> Dict` (async)
187
203
 
204
+ #### Security Policy Methods
205
+
206
+ - `set_security_policy(policy: SecurityPolicy) -> None` (async)
207
+ - `get_security_policy() -> SecurityPolicy` (async)
208
+ - `update_security_policy(**updates) -> SecurityPolicy` (async)
209
+
210
+ #### Performance Monitoring Methods
211
+
212
+ - `get_performance_metrics() -> PerformanceMetrics` (async)
213
+ - `reset_metrics() -> None` (async)
214
+ - `enable_metrics(enabled: bool = True) -> None` (async)
215
+
188
216
  ### DetectionResult
189
217
 
190
218
  ```python
@@ -198,6 +226,48 @@ class DetectionResult:
198
226
  metadata: Optional[Dict[str, Any]]
199
227
  ```
200
228
 
229
+ ### New Types (v0.2.0)
230
+
231
+ #### StreamingScanResponse
232
+
233
+ ```python
234
+ class StreamingScanResponse:
235
+ overall_result: DetectionResult
236
+ chunk_results: List[ChunkResult]
237
+ total_chunks: int
238
+ processing_time_ms: float
239
+ scan_id: str
240
+ ```
241
+
242
+ #### SecurityPolicy
243
+
244
+ ```python
245
+ class SecurityPolicy:
246
+ name: str
247
+ description: Optional[str]
248
+ threat_threshold: ThreatLevel
249
+ blocked_detection_types: List[str]
250
+ allowlist_patterns: List[str]
251
+ blocklist_patterns: List[str]
252
+ custom_rules: List[Dict[str, Any]]
253
+ ```
254
+
255
+ #### PerformanceMetrics
256
+
257
+ ```python
258
+ class PerformanceMetrics:
259
+ total_requests: int
260
+ total_processing_time: float
261
+ average_response_time: float
262
+ min_response_time: float
263
+ max_response_time: float
264
+ error_count: int
265
+ success_rate: float
266
+ requests_per_second: float
267
+ start_time: datetime
268
+ last_request_time: Optional[datetime]
269
+ ```
270
+
201
271
  ## Configuration
202
272
 
203
273
  ### Environment Variables
@@ -242,93 +312,200 @@ for prompt, result in zip(prompts, results):
242
312
  print(f"'{prompt}': {result.threat_level} ({result.confidence:.2f})")
243
313
  ```
244
314
 
315
+ ### Advanced Async Features
316
+
317
+ ```python
318
+ import asyncio
319
+ from koreshield_sdk import AsyncKoreShieldClient
320
+
321
+ async def main():
322
+ async with AsyncKoreShieldClient(api_key="your-api-key", enable_metrics=True) as client:
323
+
324
+ # Enhanced batch processing with progress callback
325
+ def progress_callback(completed, total, current_result=None):
326
+ print(f"Progress: {completed}/{total} completed")
327
+ if current_result:
328
+ print(f" Latest result: {current_result.threat_level}")
329
+
330
+ prompts = ["Prompt 1", "Prompt 2", "Prompt 3", "Prompt 4", "Prompt 5"]
331
+ results = await client.scan_batch(
332
+ prompts,
333
+ parallel=True,
334
+ max_concurrent=3,
335
+ progress_callback=progress_callback
336
+ )
337
+
338
+ # Streaming content scanning for long documents
339
+ long_content = "Your very long document content here..." * 100
340
+ stream_result = await client.scan_stream(
341
+ content=long_content,
342
+ chunk_size=1000,
343
+ overlap=100
344
+ )
345
+
346
+ print(f"Overall safe: {stream_result.overall_result.is_safe}")
347
+ print(f"Chunks processed: {stream_result.total_chunks}")
348
+
349
+ # Get performance metrics
350
+ metrics = await client.get_performance_metrics()
351
+ print(f"Total requests: {metrics.total_requests}")
352
+ print(".2f"
353
+ asyncio.run(main())
354
+ ```
355
+
356
+ ### Security Policies
357
+
358
+ ```python
359
+ from koreshield_sdk import AsyncKoreShieldClient
360
+ from koreshield_sdk.types import SecurityPolicy, ThreatLevel
361
+
362
+ async def main():
363
+ # Create custom security policy
364
+ policy = SecurityPolicy(
365
+ name="strict_policy",
366
+ description="Strict security for sensitive applications",
367
+ threat_threshold=ThreatLevel.LOW,
368
+ allowlist_patterns=["safe", "trusted"],
369
+ blocklist_patterns=["hack", "exploit", "attack"],
370
+ custom_rules=[
371
+ {"name": "no_code_execution", "pattern": "exec\\(|eval\\("},
372
+ {"name": "no_file_operations", "pattern": "open\\(|file\\("}
373
+ ]
374
+ )
375
+
376
+ async with AsyncKoreShieldClient(
377
+ api_key="your-api-key",
378
+ security_policy=policy
379
+ ) as client:
380
+
381
+ # Test against policy
382
+ test_prompts = [
383
+ "This is a safe message",
384
+ "This contains hack attempts",
385
+ "Let's execute: exec('print(hello)')"
386
+ ]
387
+
388
+ for prompt in test_prompts:
389
+ result = await client.scan_prompt(prompt)
390
+ status = "✅ ALLOWED" if result.is_safe else "❌ BLOCKED"
391
+ print(f"{status}: {prompt}")
392
+
393
+ asyncio.run(main())
394
+ ```
395
+
245
396
  ### FastAPI Integration
246
397
 
247
398
  ```python
248
- from fastapi import FastAPI, HTTPException
249
- from koreshield_sdk import KoreShieldClient
399
+ from fastapi import FastAPI, Request
400
+ from koreshield_sdk.integrations import create_fastapi_middleware
250
401
 
251
402
  app = FastAPI()
252
- client = KoreShieldClient(api_key="your-api-key")
253
403
 
254
- @app.post("/chat")
255
- async def chat(message: str):
256
- # Scan user input
257
- result = client.scan_prompt(message)
404
+ # Create and add KoreShield middleware
405
+ middleware = create_fastapi_middleware(
406
+ api_key="your-api-key",
407
+ scan_request_body=True,
408
+ threat_threshold="medium",
409
+ block_on_threat=False, # Log but don't block
410
+ exclude_paths=["/health", "/docs"]
411
+ )
258
412
 
259
- if not result.is_safe and result.threat_level in ["high", "critical"]:
260
- raise HTTPException(status_code=400, detail="Unsafe content detected")
413
+ app.middleware("http")(middleware)
414
+
415
+ @app.post("/chat")
416
+ async def chat(request: Request, message: str):
417
+ # Request is automatically scanned by middleware
418
+ # Access scan results from request state if needed
419
+ scan_result = getattr(request.state, 'koreshield_result', None)
420
+ if scan_result and not scan_result.is_safe:
421
+ print(f"Threat detected: {scan_result.threat_level}")
261
422
 
262
423
  # Process with your LLM
263
424
  response = f"Processed: {message}"
264
- return {"response": response, "safety": result.dict()}
425
+ return {"response": response}
265
426
  ```
266
427
 
267
428
  ### Flask Integration
268
429
 
269
430
  ```python
270
- from flask import Flask, request, jsonify
271
- from koreshield_sdk import KoreShieldClient
431
+ from flask import Flask, request, jsonify, g
432
+ from koreshield_sdk.integrations import create_flask_middleware
272
433
 
273
434
  app = Flask(__name__)
274
- client = KoreShieldClient(api_key="your-api-key")
435
+
436
+ # Create and register KoreShield middleware
437
+ middleware = create_flask_middleware(
438
+ api_key="your-api-key",
439
+ scan_request_body=True,
440
+ threat_threshold="high",
441
+ block_on_threat=True,
442
+ exclude_paths=["/health"]
443
+ )
444
+
445
+ app.before_request(middleware)
275
446
 
276
447
  @app.route("/api/chat", methods=["POST"])
277
448
  def chat():
449
+ # Check if request was blocked by middleware
450
+ if hasattr(g, 'koreshield_blocked') and g.koreshield_blocked:
451
+ return jsonify({"error": "Request blocked by security policy"}), 403
452
+
278
453
  data = request.get_json()
279
454
  message = data.get("message", "")
280
455
 
281
- # Scan user input
282
- result = client.scan_prompt(message)
283
-
284
- if not result.is_safe:
285
- return jsonify({
286
- "error": "Unsafe content detected",
287
- "threat_level": result.threat_level,
288
- "confidence": result.confidence
289
- }), 400
456
+ # Access scan results
457
+ scan_result = getattr(g, 'koreshield_result', None)
290
458
 
291
459
  # Process with your LLM
292
460
  response = f"Echo: {message}"
293
- return jsonify({"response": response})
461
+ return jsonify({
462
+ "response": response,
463
+ "safety": scan_result.dict() if scan_result else None
464
+ })
294
465
  ```
295
466
 
296
467
  ### Django Integration
297
468
 
298
469
  ```python
470
+ # settings.py
471
+ KORESHIELD_CONFIG = {
472
+ 'api_key': 'your-api-key',
473
+ 'scan_request_body': True,
474
+ 'threat_threshold': 'medium',
475
+ 'block_on_threat': False,
476
+ 'exclude_paths': ['/health/', '/admin/']
477
+ }
478
+
479
+ # middleware.py
480
+ from koreshield_sdk.integrations import create_django_middleware
481
+
482
+ KoreShieldMiddleware = create_django_middleware()
483
+
299
484
  # views.py
300
485
  from django.http import JsonResponse
301
486
  from django.views.decorators.csrf import csrf_exempt
302
487
  from django.utils.decorators import method_decorator
303
488
  from django.views import View
304
489
  import json
305
- from koreshield_sdk import KoreShieldClient
306
-
307
- client = KoreShieldClient(api_key="your-api-key")
308
490
 
309
491
  @method_decorator(csrf_exempt, name='dispatch')
310
492
  class ChatView(View):
311
493
  def post(self, request):
494
+ # Check if request was blocked by middleware
495
+ if hasattr(request, 'koreshield_blocked') and request.koreshield_blocked:
496
+ return JsonResponse({"error": "Request blocked by security policy"}, status=403)
497
+
312
498
  data = json.loads(request.body)
313
499
  message = data.get("message", "")
314
500
 
315
- # Scan user input
316
- result = client.scan_prompt(message)
317
-
318
- if not result.is_safe and result.threat_level == "critical":
319
- return JsonResponse({
320
- "error": "Critical threat detected"
321
- }, status=400)
501
+ # Access scan results
502
+ scan_result = getattr(request, 'koreshield_result', None)
322
503
 
323
504
  # Process with your LLM
324
505
  response = f"Response to: {message}"
325
506
  return JsonResponse({
326
507
  "response": response,
327
- "safety_check": {
328
- "safe": result.is_safe,
329
- "threat_level": result.threat_level,
330
- "confidence": result.confidence
331
- }
508
+ "safety_check": scan_result.dict() if scan_result else None
332
509
  })
333
510
  ```
334
511
 
@@ -387,6 +564,68 @@ results = await client.scan_batch(
387
564
  )
388
565
  ```
389
566
 
567
+ ### Streaming Content Scanning
568
+
569
+ ```python
570
+ # Scan long documents with overlapping chunks
571
+ long_document = "Very long content..." * 1000
572
+
573
+ result = await client.scan_stream(
574
+ content=long_document,
575
+ chunk_size=2000, # Process in 2000-character chunks
576
+ overlap=200 # 200-character overlap between chunks
577
+ )
578
+
579
+ print(f"Overall safe: {result.overall_result.is_safe}")
580
+ print(f"Total chunks: {result.total_chunks}")
581
+ for i, chunk_result in enumerate(result.chunk_results):
582
+ print(f"Chunk {i+1}: {chunk_result.result.threat_level}")
583
+ ```
584
+
585
+ ### Performance Monitoring
586
+
587
+ ```python
588
+ async with AsyncKoreShieldClient(api_key="your-api-key", enable_metrics=True) as client:
589
+ # Perform operations...
590
+ await client.scan_prompt("Test prompt")
591
+ await client.scan_batch(["Prompt 1", "Prompt 2"])
592
+
593
+ # Get comprehensive metrics
594
+ metrics = await client.get_performance_metrics()
595
+ print(f"Total requests: {metrics.total_requests}")
596
+ print(".2f" print(".2f" print(f"Success rate: {metrics.success_rate:.1%}")
597
+
598
+ # Reset metrics if needed
599
+ await client.reset_metrics()
600
+ ```
601
+
602
+ ### Security Policy Management
603
+
604
+ ```python
605
+ from koreshield_sdk.types import SecurityPolicy, ThreatLevel
606
+
607
+ # Create and apply custom policy
608
+ policy = SecurityPolicy(
609
+ name="enterprise_policy",
610
+ threat_threshold=ThreatLevel.MEDIUM,
611
+ allowlist_patterns=["approved", "safe"],
612
+ blocklist_patterns=["banned", "dangerous"],
613
+ custom_rules=[
614
+ {"name": "no_pii", "pattern": "\\b\\d{3}-\\d{2}-\\d{4}\\b"}, # SSN pattern
615
+ {"name": "no_emails", "pattern": "\\S+@\\S+\\.\\S+"}
616
+ ]
617
+ )
618
+
619
+ await client.set_security_policy(policy)
620
+
621
+ # Update policy dynamically
622
+ await client.update_security_policy(threat_threshold=ThreatLevel.HIGH)
623
+
624
+ # Get current policy
625
+ current_policy = await client.get_security_policy()
626
+ print(f"Current threshold: {current_policy.threat_threshold}")
627
+ ```
628
+
390
629
  ### Monitoring and Analytics
391
630
 
392
631
  ```python
@@ -436,7 +675,7 @@ MIT License - see [LICENSE](LICENSE) file for details.
436
675
 
437
676
  ## Support
438
677
 
439
- - 📖 [Documentation](https://docs.koreshield.com)
440
- - 🐛 [Issue Tracker](https://github.com/koreshield/koreshield-python-sdk/issues)
441
- - 💬 [Discussions](https://github.com/koreshield/koreshield-python-sdk/discussions)
442
- - 📧 [Email Support](mailto:support@koreshield.com)
678
+ - [Documentation](https://docs.koreshield.com)
679
+ - [Issue Tracker](https://github.com/koreshield/koreshield-python-sdk/issues)
680
+ - [Discussions](https://github.com/koreshield/koreshield-python-sdk/discussions)
681
+ - [Email Support](mailto:support@koreshield.com)