pyagentshield 0.1.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 (101) hide show
  1. pyagentshield-0.1.0/.env.example +119 -0
  2. pyagentshield-0.1.0/.gitignore +234 -0
  3. pyagentshield-0.1.0/2601.12359v1.pdf +0 -0
  4. pyagentshield-0.1.0/LANDING_PAGE_INSTRUCTIONS.md +408 -0
  5. pyagentshield-0.1.0/LICENSE +21 -0
  6. pyagentshield-0.1.0/PKG-INFO +616 -0
  7. pyagentshield-0.1.0/README.md +541 -0
  8. pyagentshield-0.1.0/Zero_Shot_Embedding_Drift_Detection_A_Lightweight_Defense_Against_Prompt_Injections_in_LLMs.ipynb +5463 -0
  9. pyagentshield-0.1.0/agentguard.example.yaml +119 -0
  10. pyagentshield-0.1.0/docs/COST_ANALYSIS.md +247 -0
  11. pyagentshield-0.1.0/docs/DEPLOYMENT.md +256 -0
  12. pyagentshield-0.1.0/docs/FINETUNING.md +261 -0
  13. pyagentshield-0.1.0/examples/simple_rag.py +590 -0
  14. pyagentshield-0.1.0/pyproject.toml +108 -0
  15. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/1_Pooling/config.json +10 -0
  16. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/README.md +564 -0
  17. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/calibration.json +5 -0
  18. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/1_Pooling/config.json +10 -0
  19. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/README.md +563 -0
  20. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/config.json +23 -0
  21. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/config_sentence_transformers.json +14 -0
  22. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/modules.json +20 -0
  23. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/rng_state.pth +0 -0
  24. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/sentence_bert_config.json +4 -0
  25. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/special_tokens_map.json +51 -0
  26. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/tokenizer.json +30636 -0
  27. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/tokenizer_config.json +73 -0
  28. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/trainer_state.json +581 -0
  29. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-3051/vocab.txt +30527 -0
  30. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/1_Pooling/config.json +10 -0
  31. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/README.md +518 -0
  32. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/config.json +23 -0
  33. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/config_sentence_transformers.json +14 -0
  34. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/modules.json +20 -0
  35. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/rng_state.pth +0 -0
  36. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/sentence_bert_config.json +4 -0
  37. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/special_tokens_map.json +51 -0
  38. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/tokenizer.json +30636 -0
  39. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/tokenizer_config.json +73 -0
  40. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/trainer_state.json +178 -0
  41. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/checkpoint-800/vocab.txt +30527 -0
  42. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/config.json +23 -0
  43. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/config_sentence_transformers.json +14 -0
  44. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/modules.json +20 -0
  45. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/results.json +14 -0
  46. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/sentence_bert_config.json +4 -0
  47. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/special_tokens_map.json +51 -0
  48. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/tokenizer.json +30636 -0
  49. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/tokenizer_config.json +73 -0
  50. pyagentshield-0.1.0/scripts/agentshield-embeddings-finetuned/vocab.txt +30527 -0
  51. pyagentshield-0.1.0/scripts/cache/clean_pairs.json +1 -0
  52. pyagentshield-0.1.0/scripts/cache/cleaned_injected.json +1 -0
  53. pyagentshield-0.1.0/scripts/finetune_local.py +549 -0
  54. pyagentshield-0.1.0/src/agentguard/__init__.py +65 -0
  55. pyagentshield-0.1.0/src/agentguard/api/__init__.py +9 -0
  56. pyagentshield-0.1.0/src/agentguard/api/decorator.py +157 -0
  57. pyagentshield-0.1.0/src/agentguard/api/scan.py +84 -0
  58. pyagentshield-0.1.0/src/agentguard/cleaning/__init__.py +27 -0
  59. pyagentshield-0.1.0/src/agentguard/cleaning/base.py +54 -0
  60. pyagentshield-0.1.0/src/agentguard/cleaning/finetuned.py +363 -0
  61. pyagentshield-0.1.0/src/agentguard/cleaning/heuristic.py +298 -0
  62. pyagentshield-0.1.0/src/agentguard/cleaning/hybrid.py +360 -0
  63. pyagentshield-0.1.0/src/agentguard/cleaning/llm.py +146 -0
  64. pyagentshield-0.1.0/src/agentguard/cli/__init__.py +1 -0
  65. pyagentshield-0.1.0/src/agentguard/cli/main.py +412 -0
  66. pyagentshield-0.1.0/src/agentguard/core/__init__.py +23 -0
  67. pyagentshield-0.1.0/src/agentguard/core/config.py +267 -0
  68. pyagentshield-0.1.0/src/agentguard/core/exceptions.py +66 -0
  69. pyagentshield-0.1.0/src/agentguard/core/results.py +134 -0
  70. pyagentshield-0.1.0/src/agentguard/core/setup.py +169 -0
  71. pyagentshield-0.1.0/src/agentguard/core/shield.py +328 -0
  72. pyagentshield-0.1.0/src/agentguard/data/__init__.py +1 -0
  73. pyagentshield-0.1.0/src/agentguard/detectors/__init__.py +10 -0
  74. pyagentshield-0.1.0/src/agentguard/detectors/base.py +80 -0
  75. pyagentshield-0.1.0/src/agentguard/detectors/zedd.py +327 -0
  76. pyagentshield-0.1.0/src/agentguard/integrations/__init__.py +12 -0
  77. pyagentshield-0.1.0/src/agentguard/integrations/langchain.py +267 -0
  78. pyagentshield-0.1.0/src/agentguard/providers/__init__.py +23 -0
  79. pyagentshield-0.1.0/src/agentguard/providers/base.py +73 -0
  80. pyagentshield-0.1.0/src/agentguard/providers/local.py +208 -0
  81. pyagentshield-0.1.0/src/agentguard/providers/mlx.py +446 -0
  82. pyagentshield-0.1.0/src/agentguard/providers/openai.py +216 -0
  83. pyagentshield-0.1.0/src/agentguard/py.typed +0 -0
  84. pyagentshield-0.1.0/src/agentguard/threshold/__init__.py +11 -0
  85. pyagentshield-0.1.0/src/agentguard/threshold/calibrator.py +421 -0
  86. pyagentshield-0.1.0/src/agentguard/threshold/manager.py +260 -0
  87. pyagentshield-0.1.0/src/agentguard/threshold/registry.py +90 -0
  88. pyagentshield-0.1.0/tests/__init__.py +1 -0
  89. pyagentshield-0.1.0/tests/conftest.py +106 -0
  90. pyagentshield-0.1.0/tests/test_config.py +160 -0
  91. pyagentshield-0.1.0/tests/test_decorator.py +151 -0
  92. pyagentshield-0.1.0/tests/test_exceptions.py +47 -0
  93. pyagentshield-0.1.0/tests/test_heuristic_cleaner.py +184 -0
  94. pyagentshield-0.1.0/tests/test_hybrid_cleaner.py +170 -0
  95. pyagentshield-0.1.0/tests/test_langchain.py +153 -0
  96. pyagentshield-0.1.0/tests/test_results.py +101 -0
  97. pyagentshield-0.1.0/tests/test_scan_api.py +68 -0
  98. pyagentshield-0.1.0/tests/test_setup.py +150 -0
  99. pyagentshield-0.1.0/tests/test_shield.py +125 -0
  100. pyagentshield-0.1.0/tests/test_threshold.py +134 -0
  101. pyagentshield-0.1.0/tests/test_zedd.py +302 -0
@@ -0,0 +1,119 @@
1
+ # AgentGuard Environment Variables
2
+ # Copy this file to .env and fill in your values
3
+ # NEVER commit .env to version control!
4
+
5
+ # =============================================================================
6
+ # REQUIRED: OpenAI API Key
7
+ # =============================================================================
8
+
9
+ # Your OpenAI API key - REQUIRED for:
10
+ # 1. LLM cleaning (recommended for best accuracy ~90%)
11
+ # 2. Finetuning the embedding model (data preparation step)
12
+ #
13
+ # Get your key from: https://platform.openai.com/api-keys
14
+ # Cost: ~$0.0003 per document for cleaning, ~$3-5 total for finetuning
15
+ OPENAI_API_KEY=sk-...
16
+
17
+ # =============================================================================
18
+ # Embedding Model Configuration
19
+ # =============================================================================
20
+
21
+ # Provider: "local" uses sentence-transformers (recommended)
22
+ # AGENTGUARD_EMBEDDINGS__PROVIDER=local
23
+
24
+ # Model path - use your finetuned model for best accuracy (~95%)
25
+ # After running: python scripts/finetune_local.py
26
+ # AGENTGUARD_EMBEDDINGS__MODEL=./agentguard-embeddings-finetuned
27
+ #
28
+ # Or use a pre-trained model (less accurate but no finetuning needed):
29
+ # - all-MiniLM-L6-v2 (fast, ~70% accuracy)
30
+ # - all-mpnet-base-v2 (better, ~80% accuracy)
31
+ # AGENTGUARD_EMBEDDINGS__MODEL=all-MiniLM-L6-v2
32
+
33
+ # =============================================================================
34
+ # Text Cleaning Configuration
35
+ # =============================================================================
36
+
37
+ # Cleaning method - how AgentGuard removes injection patterns before comparing
38
+ #
39
+ # Options:
40
+ # - "heuristic" : Free, fast, regex-based (~70% accuracy)
41
+ # - "llm" : Uses GPT-4o-mini, ~$0.0003/doc (~90% accuracy) [RECOMMENDED]
42
+ #
43
+ # AGENTGUARD_CLEANING__METHOD=llm
44
+
45
+ # LLM model for cleaning (when method=llm)
46
+ # gpt-4o-mini is cheapest and works great for this task
47
+ # AGENTGUARD_CLEANING__LLM_MODEL=gpt-4o-mini
48
+
49
+ # =============================================================================
50
+ # ZEDD Detection Threshold
51
+ # =============================================================================
52
+
53
+ # Threshold determines when text is flagged as suspicious
54
+ #
55
+ # How it works:
56
+ # - ZEDD computes "drift" = how much text changes after cleaning
57
+ # - If drift > threshold → suspicious
58
+ #
59
+ # Options:
60
+ # - Leave empty/null : Auto-load from model's calibration.json [RECOMMENDED]
61
+ # - Set explicit value: e.g., 0.0083 (from your finetuned model)
62
+ #
63
+ # Higher threshold = fewer false positives, might miss some attacks
64
+ # Lower threshold = catches more attacks, but more false positives
65
+ #
66
+ # AGENTGUARD_ZEDD__THRESHOLD=
67
+
68
+ # =============================================================================
69
+ # Behavior on Detection
70
+ # =============================================================================
71
+
72
+ # What to do when a prompt injection is detected
73
+ #
74
+ # Options:
75
+ # - "block" : Raise PromptInjectionDetected exception (strict)
76
+ # - "filter" : Remove suspicious documents silently (recommended for RAG)
77
+ # - "flag" : Add metadata but pass through (for logging/monitoring)
78
+ # - "warn" : Log warning but pass through (permissive)
79
+ #
80
+ # AGENTGUARD_BEHAVIOR__ON_DETECT=filter
81
+
82
+ # Minimum confidence to trigger the on_detect action (0.0 to 1.0)
83
+ # Higher = only act on high-confidence detections
84
+ # AGENTGUARD_BEHAVIOR__CONFIDENCE_THRESHOLD=0.5
85
+
86
+ # =============================================================================
87
+ # Performance Settings
88
+ # =============================================================================
89
+
90
+ # Batch size for embedding operations (higher = faster but more memory)
91
+ # AGENTGUARD_PERFORMANCE__BATCH_SIZE=32
92
+
93
+ # Cache embeddings to avoid recomputing for repeated texts
94
+ # AGENTGUARD_PERFORMANCE__CACHE_EMBEDDINGS=true
95
+
96
+ # =============================================================================
97
+ # Logging
98
+ # =============================================================================
99
+
100
+ # Log level: DEBUG, INFO, WARNING, ERROR
101
+ # AGENTGUARD_LOGGING__LEVEL=INFO
102
+
103
+ # =============================================================================
104
+ # Finetuning Configuration (for scripts/finetune_local.py)
105
+ # =============================================================================
106
+
107
+ # These are used when running the finetuning script, not at runtime
108
+
109
+ # Max samples to process (more = better model but higher API cost)
110
+ # Default: 5000 samples costs ~$3-5 in OpenAI API calls
111
+ # FINETUNE_MAX_SAMPLES=5000
112
+
113
+ # Training batch size (reduce if you get OOM errors)
114
+ # For 16GB Mac: use 8
115
+ # For 8GB Mac: use 4
116
+ # FINETUNE_BATCH_SIZE=8
117
+
118
+ # Output directory for finetuned model
119
+ # FINETUNE_OUTPUT_DIR=./agentguard-embeddings-finetuned
@@ -0,0 +1,234 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ .claude/
7
+ ARCHITECTURE.md
8
+ CLAUDE.md
9
+ .DS_Store
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .Python
15
+ build/
16
+ develop-eggs/
17
+ dist/
18
+ downloads/
19
+ eggs/
20
+ .eggs/
21
+ lib/
22
+ lib64/
23
+ parts/
24
+ sdist/
25
+ var/
26
+ wheels/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py.cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+ cover/
57
+
58
+ # Translations
59
+ *.mo
60
+ *.pot
61
+
62
+ # Django stuff:
63
+ *.log
64
+ local_settings.py
65
+ db.sqlite3
66
+ db.sqlite3-journal
67
+
68
+ # Flask stuff:
69
+ instance/
70
+ .webassets-cache
71
+
72
+ # Scrapy stuff:
73
+ .scrapy
74
+
75
+ # Sphinx documentation
76
+ docs/_build/
77
+
78
+ # PyBuilder
79
+ .pybuilder/
80
+ target/
81
+
82
+ # Jupyter Notebook
83
+ .ipynb_checkpoints
84
+
85
+ # IPython
86
+ profile_default/
87
+ ipython_config.py
88
+
89
+ # pyenv
90
+ # For a library or package, you might want to ignore these files since the code is
91
+ # intended to run in multiple environments; otherwise, check them in:
92
+ # .python-version
93
+
94
+ # pipenv
95
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
97
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
98
+ # install all needed dependencies.
99
+ #Pipfile.lock
100
+
101
+ # UV
102
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
103
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
104
+ # commonly ignored for libraries.
105
+ #uv.lock
106
+
107
+ # poetry
108
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
109
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
110
+ # commonly ignored for libraries.
111
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
112
+ #poetry.lock
113
+ #poetry.toml
114
+
115
+ # pdm
116
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
117
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
118
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
119
+ #pdm.lock
120
+ #pdm.toml
121
+ .pdm-python
122
+ .pdm-build/
123
+
124
+ # pixi
125
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
126
+ #pixi.lock
127
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
128
+ # in the .venv directory. It is recommended not to include this directory in version control.
129
+ .pixi
130
+
131
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
132
+ __pypackages__/
133
+
134
+ # Celery stuff
135
+ celerybeat-schedule
136
+ celerybeat.pid
137
+
138
+ # SageMath parsed files
139
+ *.sage.py
140
+
141
+ # Environments
142
+ .env
143
+ .envrc
144
+ .venv
145
+ env/
146
+ venv/
147
+ ENV/
148
+ env.bak/
149
+ venv.bak/
150
+
151
+ # Spyder project settings
152
+ .spyderproject
153
+ .spyproject
154
+
155
+ # Rope project settings
156
+ .ropeproject
157
+
158
+ # mkdocs documentation
159
+ /site
160
+
161
+ # mypy
162
+ .mypy_cache/
163
+ .dmypy.json
164
+ dmypy.json
165
+
166
+ # Pyre type checker
167
+ .pyre/
168
+
169
+ # pytype static type analyzer
170
+ .pytype/
171
+
172
+ # Cython debug symbols
173
+ cython_debug/
174
+
175
+ # PyCharm
176
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
177
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
178
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
179
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
180
+ #.idea/
181
+
182
+ # Abstra
183
+ # Abstra is an AI-powered process automation framework.
184
+ # Ignore directories containing user credentials, local state, and settings.
185
+ # Learn more at https://abstra.io/docs
186
+ .abstra/
187
+
188
+ # Visual Studio Code
189
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
190
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
191
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
192
+ # you could uncomment the following to ignore the entire vscode folder
193
+ # .vscode/
194
+
195
+ # Ruff stuff:
196
+ .ruff_cache/
197
+
198
+ # PyPI configuration file
199
+ .pypirc
200
+
201
+ # Cursor
202
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
203
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
204
+ # refer to https://docs.cursor.com/context/ignore-files
205
+ .cursorignore
206
+ .cursorindexingignore
207
+
208
+ # Marimo
209
+ marimo/_static/
210
+ marimo/_lsp/
211
+ __marimo__/
212
+
213
+ # AgentShield specific
214
+ # Model cache and outputs
215
+ *.pt
216
+ *.bin
217
+ *.safetensors
218
+ agentshield-cleaner-lora/
219
+ agentshield-cleaner-lora-merged/
220
+ models/
221
+ checkpoints/
222
+
223
+ # HuggingFace cache
224
+ .cache/huggingface/
225
+
226
+ # Training outputs
227
+ wandb/
228
+ outputs/
229
+ runs/
230
+
231
+ # Never commit credentials
232
+ .env.local
233
+ credentials.json
234
+ secrets.yaml
Binary file