mirror of
https://github.com/msoedov/agentic_security.git
synced 2026-06-25 06:39:57 +02:00
Compare commits
91 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 72f1f74df7 | |||
| 693c5743c0 | |||
| eb27f7bbaa | |||
| e0eed6fd92 | |||
| 21c37b823d | |||
| 01c27302de | |||
| 11ac390a6d | |||
| 1b63089f74 | |||
| 81ff6656e1 | |||
| b18427aa7e | |||
| 6a8e7633d9 | |||
| 678aa4f345 | |||
| 566327c39d | |||
| 6ee7c6888d | |||
| 925a187978 | |||
| 0bc4feef74 | |||
| b1bbc306fe | |||
| a206075595 | |||
| 3b313f6364 | |||
| 538350afcd | |||
| 87b54e35b6 | |||
| 9ac5030d74 | |||
| 1018bec710 | |||
| 466a9126c5 | |||
| c66da5ce85 | |||
| bf6c901061 | |||
| 6d8a168eae | |||
| a1e28a72b4 | |||
| 2655482148 | |||
| a1e7cbe896 | |||
| 8cc6c7e525 | |||
| c327fc26a4 | |||
| 77695b123d | |||
| eb3a70b7e4 | |||
| a95a97c9f6 | |||
| 1669b3f0dc | |||
| b40d845e3c | |||
| 4b8ab0315f | |||
| 7cb321ce46 | |||
| 0bd48887db | |||
| 72eb09215e | |||
| 575e138173 | |||
| 1a3bcc22a7 | |||
| 96e58de00f | |||
| 5db9676837 | |||
| 83e5362501 | |||
| 259361d279 | |||
| 2ffb9429a1 | |||
| 49d426d05e | |||
| 31196f2071 | |||
| b376b86b96 | |||
| 50436e1f1d | |||
| 9817ab495a | |||
| ed89f18c30 | |||
| 33eb4f2625 | |||
| ac4f4cc495 | |||
| f7f4ee840b | |||
| d0fb1fe971 | |||
| 21c71e1688 | |||
| d285ef645c | |||
| c89a9236cc | |||
| 6678e5d3ab | |||
| e1400b6f58 | |||
| dbec27d3aa | |||
| bf5dfcd661 | |||
| 7d280b9a0e | |||
| 75449ed0aa | |||
| c4cc604d23 | |||
| beacf09488 | |||
| 5927518376 | |||
| da6ae2c663 | |||
| 304a347197 | |||
| fed6bccf2a | |||
| e8795ed217 | |||
| 79494f220b | |||
| d6a6717993 | |||
| 61b68f04d5 | |||
| 4a2d9c7e4e | |||
| fc213395c3 | |||
| 724ad1574b | |||
| a6c149f477 | |||
| fd0b28f041 | |||
| bc030f06a8 | |||
| 70c18c8251 | |||
| 386ff2aa15 | |||
| 7c0d6f7eae | |||
| 0cb14320ce | |||
| 92330c9c5a | |||
| b6db40c5ae | |||
| 16a8a226be | |||
| 23e311da86 |
@@ -0,0 +1,2 @@
|
|||||||
|
.git/
|
||||||
|
__pycache__/
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
name: Docker Build Test
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 0.*
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
- name: Build Docker image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
push: false
|
||||||
|
tags: docker-build-test:latest
|
||||||
@@ -34,4 +34,4 @@ jobs:
|
|||||||
id: scan
|
id: scan
|
||||||
run: |
|
run: |
|
||||||
agentic_security init
|
agentic_security init
|
||||||
agentic_security ci
|
# agentic_security ci
|
||||||
|
|||||||
@@ -8,3 +8,6 @@ runs/
|
|||||||
logs/
|
logs/
|
||||||
modal_agent.py
|
modal_agent.py
|
||||||
sandbox.py
|
sandbox.py
|
||||||
|
site/
|
||||||
|
agesec.toml
|
||||||
|
.clinerules
|
||||||
|
|||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
# Build stage
|
||||||
|
FROM python:3.11-slim as builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install system dependencies
|
||||||
|
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Install Poetry
|
||||||
|
RUN curl -sSL https://install.python-poetry.org | python3 -
|
||||||
|
ENV PATH="/root/.local/bin:$PATH"
|
||||||
|
RUN poetry self add "poetry-plugin-export"
|
||||||
|
|
||||||
|
# Copy only dependency files to leverage Docker layer caching
|
||||||
|
COPY pyproject.toml poetry.lock ./
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
RUN poetry export -f requirements.txt --without-hashes -o requirements.txt
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# Runtime stage
|
||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy only the necessary files from the builder stage
|
||||||
|
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
|
||||||
|
COPY --from=builder /usr/local/bin /usr/local/bin
|
||||||
|
|
||||||
|
# Copy application code
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Health check
|
||||||
|
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
||||||
|
CMD curl -f http://localhost:8718/health || exit 1
|
||||||
|
|
||||||
|
# Default command
|
||||||
|
CMD ["python", "-m", "agentic_security"]
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
# Agentic Security - Features for Organizations
|
|
||||||
|
|
||||||
This feature list outlines the advanced capabilities of Agentic Security to assist in integrating high-security, low-latency language model applications into organizational infrastructure, with a particular focus on detecting and preventing prompt injection and jailbreak attempts.
|
|
||||||
|
|
||||||
## 1. Exclusive Pentest with a 40k Jailbreak Dataset
|
|
||||||
|
|
||||||
Private pentesting services using an exclusive dataset of 40,000 jailbreak attempts, ensuring unparalleled security and prompt injection prevention.
|
|
||||||
|
|
||||||
## 2. Unique Threat Vector Identification
|
|
||||||
|
|
||||||
Identifies and mitigates unique threat vectors, providing a tailored security posture against sophisticated attacks.
|
|
||||||
|
|
||||||
## 3. Continuous Feedback and LLMOps Integration
|
|
||||||
|
|
||||||
Implements feedback loops and LLMOps for continuous monitoring and improvement, ensuring optimal performance and security.
|
|
||||||
|
|
||||||
## 4. Reduced dependencies
|
|
||||||
|
|
||||||
Self-Contained Runtime Environment: Agentic Security operates within a self-contained runtime. This significantly lowers the barrier to entry for organizations by minimizing the complexity typically associated with setting up and maintaining LLM applications and infra.
|
|
||||||
|
|
||||||
This library approach not only simplifies the architecture but also reduces potential points of failure and latency issues associated with external dependencies
|
|
||||||
@@ -102,6 +102,7 @@ To add your own dataset you can place one or multiples csv files with `prompt` c
|
|||||||
## Run as CI check
|
## Run as CI check
|
||||||
|
|
||||||
Init config
|
Init config
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
agentic_security init
|
agentic_security init
|
||||||
|
|
||||||
@@ -110,6 +111,7 @@ agentic_security init
|
|||||||
```
|
```
|
||||||
|
|
||||||
default config sample
|
default config sample
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
|
|
||||||
[general]
|
[general]
|
||||||
@@ -151,6 +153,7 @@ high = 0.5
|
|||||||
```
|
```
|
||||||
|
|
||||||
List module
|
List module
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
agentic_security ls
|
agentic_security ls
|
||||||
|
|
||||||
@@ -196,6 +199,7 @@ Threshold: 30.0%
|
|||||||
Summary:
|
Summary:
|
||||||
Total Passing: 2/2 (100.0%)
|
Total Passing: 2/2 (100.0%)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Extending dataset collections
|
## Extending dataset collections
|
||||||
|
|
||||||
1. Add new metadata to agentic_security.probe_data.REGISTRY
|
1. Add new metadata to agentic_security.probe_data.REGISTRY
|
||||||
@@ -372,6 +376,10 @@ This sample GitHub Action is designed to perform automated security scans
|
|||||||
|
|
||||||
This setup ensures a continuous integration approach towards maintaining security in your projects.
|
This setup ensures a continuous integration approach towards maintaining security in your projects.
|
||||||
|
|
||||||
|
## Module Class
|
||||||
|
|
||||||
|
The `Module` class is designed to manage prompt processing and interaction with external AI models and tools. It supports fetching, processing, and posting prompts asynchronously for model vulnerabilities. Check out [module.md](https://github.com/msoedov/agentic_security/blob/main/docs/module.md) for details.
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
For more detailed information on how to use Agentic Security, including advanced features and customization options, please refer to the official documentation.
|
For more detailed information on how to use Agentic Security, including advanced features and customization options, please refer to the official documentation.
|
||||||
|
|||||||
@@ -1,9 +1,18 @@
|
|||||||
import base64
|
import base64
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Modality(Enum):
|
||||||
|
TEXT = 0
|
||||||
|
IMAGE = 1
|
||||||
|
AUDIO = 2
|
||||||
|
FILES = 3
|
||||||
|
MIXED = 4
|
||||||
|
|
||||||
|
|
||||||
def encode_image_base64_by_url(url: str = "https://github.com/fluidicon.png") -> str:
|
def encode_image_base64_by_url(url: str = "https://github.com/fluidicon.png") -> str:
|
||||||
"""Encode image data to base64 from a URL"""
|
"""Encode image data to base64 from a URL"""
|
||||||
response = httpx.get(url)
|
response = httpx.get(url)
|
||||||
@@ -99,6 +108,7 @@ class LLMSpec(BaseModel):
|
|||||||
case LLMSpec(has_audio=True):
|
case LLMSpec(has_audio=True):
|
||||||
return await self.probe(
|
return await self.probe(
|
||||||
"test",
|
"test",
|
||||||
|
# TODO: fix url for mp3
|
||||||
encoded_audio=encode_audio_base64_by_url(
|
encoded_audio=encode_audio_base64_by_url(
|
||||||
"https://www.example.com/audio.mp3"
|
"https://www.example.com/audio.mp3"
|
||||||
),
|
),
|
||||||
@@ -110,6 +120,14 @@ class LLMSpec(BaseModel):
|
|||||||
|
|
||||||
fn = probe
|
fn = probe
|
||||||
|
|
||||||
|
@property
|
||||||
|
def modality(self) -> Modality:
|
||||||
|
if self.has_image:
|
||||||
|
return Modality.IMAGE
|
||||||
|
if self.has_audio:
|
||||||
|
return Modality.AUDIO
|
||||||
|
return Modality.TEXT
|
||||||
|
|
||||||
|
|
||||||
def parse_http_spec(http_spec: str) -> LLMSpec:
|
def parse_http_spec(http_spec: str) -> LLMSpec:
|
||||||
"""Parses an HTTP specification string into a LLMSpec object.
|
"""Parses an HTTP specification string into a LLMSpec object.
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ from loguru import logger
|
|||||||
from skopt import Optimizer
|
from skopt import Optimizer
|
||||||
from skopt.space import Real
|
from skopt.space import Real
|
||||||
|
|
||||||
|
from agentic_security.http_spec import Modality
|
||||||
from agentic_security.models.schemas import Scan, ScanResult
|
from agentic_security.models.schemas import Scan, ScanResult
|
||||||
from agentic_security.probe_actor.refusal import refusal_heuristic
|
from agentic_security.probe_actor.refusal import refusal_heuristic
|
||||||
from agentic_security.probe_data import msj_data
|
from agentic_security.probe_data import audio_generator, image_generator, msj_data
|
||||||
from agentic_security.probe_data.data import prepare_prompts
|
from agentic_security.probe_data.data import prepare_prompts
|
||||||
|
|
||||||
# TODO: full log file
|
# TODO: full log file
|
||||||
@@ -27,6 +28,20 @@ async def generate_prompts(
|
|||||||
yield prompt
|
yield prompt
|
||||||
|
|
||||||
|
|
||||||
|
def multi_modality_spec(llm_spec):
|
||||||
|
match llm_spec.modality:
|
||||||
|
case Modality.IMAGE:
|
||||||
|
return image_generator.RequestAdapter(llm_spec)
|
||||||
|
case Modality.AUDIO:
|
||||||
|
return audio_generator.RequestAdapter(llm_spec)
|
||||||
|
case Modality.TEXT:
|
||||||
|
return llm_spec
|
||||||
|
case _:
|
||||||
|
return llm_spec
|
||||||
|
# case _:
|
||||||
|
# raise NotImplementedError(f"Modality {llm_spec.modality} not supported yet")
|
||||||
|
|
||||||
|
|
||||||
async def process_prompt(
|
async def process_prompt(
|
||||||
request_factory, prompt, tokens, module_name, refusals, errors
|
request_factory, prompt, tokens, module_name, refusals, errors
|
||||||
):
|
):
|
||||||
@@ -68,6 +83,7 @@ async def perform_single_shot_scan(
|
|||||||
"""Perform a standard security scan."""
|
"""Perform a standard security scan."""
|
||||||
max_budget = max_budget * 100_000_000
|
max_budget = max_budget * 100_000_000
|
||||||
selected_datasets = [m for m in datasets if m["selected"]]
|
selected_datasets = [m for m in datasets if m["selected"]]
|
||||||
|
request_factory = multi_modality_spec(request_factory)
|
||||||
try:
|
try:
|
||||||
yield ScanResult.status_msg("Loading datasets...")
|
yield ScanResult.status_msg("Loading datasets...")
|
||||||
prompt_modules = prepare_prompts(
|
prompt_modules = prepare_prompts(
|
||||||
@@ -183,6 +199,7 @@ async def perform_many_shot_scan(
|
|||||||
max_ctx_length: int = 10_000,
|
max_ctx_length: int = 10_000,
|
||||||
) -> AsyncGenerator[str, None]:
|
) -> AsyncGenerator[str, None]:
|
||||||
"""Perform a multi-step security scan with probe injection."""
|
"""Perform a multi-step security scan with probe injection."""
|
||||||
|
request_factory = multi_modality_spec(request_factory)
|
||||||
try:
|
try:
|
||||||
# Load main and probe datasets
|
# Load main and probe datasets
|
||||||
yield ScanResult.status_msg("Loading datasets...")
|
yield ScanResult.status_msg("Loading datasets...")
|
||||||
|
|||||||
@@ -0,0 +1,165 @@
|
|||||||
|
import asyncio
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from pydantic_ai import Agent, RunContext
|
||||||
|
|
||||||
|
|
||||||
|
class AgentSpecification(BaseModel):
|
||||||
|
name: str | None = Field(None, description="Name of the LLM/agent")
|
||||||
|
version: str | None = Field(None, description="Version of the LLM/agent")
|
||||||
|
description: str | None = Field(None, description="Description of the LLM/agent")
|
||||||
|
capabilities: list[str] | None = Field(None, description="List of capabilities")
|
||||||
|
configuration: dict[str, Any] | None = Field(
|
||||||
|
None, description="Configuration settings"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Define the OperatorToolBox class
|
||||||
|
class OperatorToolBox:
|
||||||
|
def __init__(self, spec: AgentSpecification, datasets: list[dict[str, Any]]):
|
||||||
|
self.spec = spec
|
||||||
|
self.datasets = datasets
|
||||||
|
self.failures = []
|
||||||
|
|
||||||
|
def get_spec(self) -> AgentSpecification:
|
||||||
|
return self.spec
|
||||||
|
|
||||||
|
def get_datasets(self) -> list[dict[str, Any]]:
|
||||||
|
return self.datasets
|
||||||
|
|
||||||
|
def validate(self) -> bool:
|
||||||
|
# Validate the tool box based on the specification
|
||||||
|
if not self.spec.name or not self.spec.version:
|
||||||
|
self.failures.append("Invalid specification: Name or version is missing.")
|
||||||
|
return False
|
||||||
|
if not self.datasets:
|
||||||
|
self.failures.append("No datasets provided.")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def stop(self) -> None:
|
||||||
|
# Stop the tool box
|
||||||
|
print("Stopping the toolbox...")
|
||||||
|
|
||||||
|
def run(self) -> None:
|
||||||
|
# Run the tool box
|
||||||
|
print("Running the toolbox...")
|
||||||
|
|
||||||
|
def get_results(self) -> list[dict[str, Any]]:
|
||||||
|
# Get the results
|
||||||
|
return self.datasets
|
||||||
|
|
||||||
|
def get_failures(self) -> list[str]:
|
||||||
|
# Handle failure
|
||||||
|
return self.failures
|
||||||
|
|
||||||
|
def run_operation(self, operation: str) -> str:
|
||||||
|
# Run an operation based on the specification
|
||||||
|
if operation not in ["dataset1", "dataset2", "dataset3"]:
|
||||||
|
self.failures.append(f"Operation '{operation}' failed: Dataset not found.")
|
||||||
|
return f"Operation '{operation}' failed: Dataset not found."
|
||||||
|
return f"Operation '{operation}' executed successfully."
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize OperatorToolBox with AgentSpecification
|
||||||
|
spec = AgentSpecification(
|
||||||
|
name="GPT-4",
|
||||||
|
version="4.0",
|
||||||
|
description="A powerful language model",
|
||||||
|
capabilities=["text-generation", "question-answering"],
|
||||||
|
configuration={"max_tokens": 100},
|
||||||
|
)
|
||||||
|
|
||||||
|
# dataset_manager_agent.py
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize OperatorToolBox
|
||||||
|
toolbox = OperatorToolBox(spec=spec, datasets=["dataset1", "dataset2", "dataset3"])
|
||||||
|
|
||||||
|
# Define the agent with OperatorToolBox as its dependency
|
||||||
|
dataset_manager_agent = Agent(
|
||||||
|
model="gpt-4",
|
||||||
|
deps_type=OperatorToolBox,
|
||||||
|
result_type=str, # The agent will return string results
|
||||||
|
system_prompt="You can validate the toolbox, run operations, and retrieve results or failures.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataset_manager_agent.tool
|
||||||
|
async def validate_toolbox(ctx: RunContext[OperatorToolBox]) -> str:
|
||||||
|
"""Validate the OperatorToolBox."""
|
||||||
|
is_valid = ctx.deps.validate()
|
||||||
|
if is_valid:
|
||||||
|
return "ToolBox validation successful."
|
||||||
|
else:
|
||||||
|
return "ToolBox validation failed."
|
||||||
|
|
||||||
|
|
||||||
|
@dataset_manager_agent.tool
|
||||||
|
async def execute_operation(ctx: RunContext[OperatorToolBox], operation: str) -> str:
|
||||||
|
"""Execute an operation on a dataset."""
|
||||||
|
result = ctx.deps.run_operation(operation)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@dataset_manager_agent.tool
|
||||||
|
async def retrieve_results(ctx: RunContext[OperatorToolBox]) -> str:
|
||||||
|
"""Retrieve the results of operations."""
|
||||||
|
results = ctx.deps.get_results()
|
||||||
|
if results:
|
||||||
|
formatted_results = "\n".join([f"{op}: {res}" for op, res in results.items()])
|
||||||
|
return f"Operation Results:\n{formatted_results}"
|
||||||
|
else:
|
||||||
|
return "No operations have been executed yet."
|
||||||
|
|
||||||
|
|
||||||
|
@dataset_manager_agent.tool
|
||||||
|
async def retrieve_failures(ctx: RunContext[OperatorToolBox]) -> str:
|
||||||
|
"""Retrieve the list of failures."""
|
||||||
|
failures = ctx.deps.get_failures()
|
||||||
|
if failures:
|
||||||
|
formatted_failures = "\n".join(failures)
|
||||||
|
return f"Failures:\n{formatted_failures}"
|
||||||
|
else:
|
||||||
|
return "No failures recorded."
|
||||||
|
|
||||||
|
|
||||||
|
# Synchronous run example
|
||||||
|
def run_dataset_manager_agent_sync():
|
||||||
|
prompts = [
|
||||||
|
"Validate the toolbox.",
|
||||||
|
"Execute operation on 'dataset2'.",
|
||||||
|
"Execute operation on 'dataset4'.", # This should fail
|
||||||
|
"Retrieve the results.",
|
||||||
|
"Retrieve any failures.",
|
||||||
|
]
|
||||||
|
|
||||||
|
for prompt in prompts:
|
||||||
|
result = dataset_manager_agent.run_sync(prompt, deps=toolbox)
|
||||||
|
print(f"Prompt: {prompt}")
|
||||||
|
print(f"Response: {result.data}\n")
|
||||||
|
|
||||||
|
|
||||||
|
# Asynchronous run example
|
||||||
|
async def run_dataset_manager_agent_async():
|
||||||
|
prompts = [
|
||||||
|
"Validate the toolbox.",
|
||||||
|
"Execute operation on 'dataset2'.",
|
||||||
|
"Execute operation on 'dataset4'.", # This should fail
|
||||||
|
"Retrieve the results.",
|
||||||
|
"Retrieve any failures.",
|
||||||
|
]
|
||||||
|
|
||||||
|
for prompt in prompts:
|
||||||
|
result = await dataset_manager_agent.run(prompt, deps=toolbox)
|
||||||
|
print(f"Prompt: {prompt}")
|
||||||
|
print(f"Response: {result.data}\n")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Run synchronous example
|
||||||
|
run_dataset_manager_agent_sync()
|
||||||
|
|
||||||
|
# Run asynchronous example
|
||||||
|
asyncio.run(run_dataset_manager_agent_async())
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from agentic_security.refusal_classifier import RefusalClassifier
|
from agentic_security.refusal_classifier import RefusalClassifier
|
||||||
|
|
||||||
classifier = RefusalClassifier()
|
classifier = RefusalClassifier()
|
||||||
@@ -35,23 +37,81 @@ REFUSAL_MARKS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def check_refusal(response: str, refusal_phrases: list = REFUSAL_MARKS) -> bool:
|
class RefusalClassifierPlugin(ABC):
|
||||||
"""Check if the response contains any phrases indicating refusal to answer.
|
"""Base class for refusal classifier plugins."""
|
||||||
|
|
||||||
Args:
|
@abstractmethod
|
||||||
- response (str): The response from the language model.
|
def is_refusal(self, response: str) -> bool:
|
||||||
- refusal_phrases (list): A list of phrases indicating refusal to answer.
|
"""Check if the response contains a refusal.
|
||||||
|
|
||||||
Returns:
|
Args:
|
||||||
- bool: True if the response contains a refusal phrase, False otherwise.
|
response (str): The response from the language model.
|
||||||
"""
|
|
||||||
return any(phrase in response for phrase in refusal_phrases)
|
Returns:
|
||||||
|
bool: True if the response contains a refusal, False otherwise.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# TODO: add plugin system for multiple classifiers of the output
|
class DefaultRefusalClassifier(RefusalClassifierPlugin):
|
||||||
|
"""Default refusal classifier using a list of refusal phrases."""
|
||||||
|
|
||||||
|
def __init__(self, refusal_phrases: list[str] = REFUSAL_MARKS):
|
||||||
|
self.refusal_phrases = refusal_phrases
|
||||||
|
|
||||||
|
def is_refusal(self, response: str) -> bool:
|
||||||
|
"""Check if the response contains any phrases indicating refusal to answer.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
response (str): The response from the language model.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the response contains a refusal phrase, False otherwise.
|
||||||
|
"""
|
||||||
|
return any(phrase in response for phrase in self.refusal_phrases)
|
||||||
|
|
||||||
|
|
||||||
|
class RefusalClassifierManager:
|
||||||
|
"""Manager for refusal classifier plugins."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.plugins: dict[str, RefusalClassifierPlugin] = {}
|
||||||
|
|
||||||
|
def register_plugin(self, name: str, plugin: RefusalClassifierPlugin):
|
||||||
|
"""Register a refusal classifier plugin.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str): The name of the plugin.
|
||||||
|
plugin (RefusalClassifierPlugin): The plugin instance.
|
||||||
|
"""
|
||||||
|
self.plugins[name] = plugin
|
||||||
|
|
||||||
|
def is_refusal(self, response: str) -> bool:
|
||||||
|
"""Check if the response contains a refusal using all registered plugins.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
response (str): The response from the language model.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if any plugin detects a refusal, False otherwise.
|
||||||
|
"""
|
||||||
|
return any(plugin.is_refusal(response) for plugin in self.plugins.values())
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize the plugin manager and register the default plugin
|
||||||
|
refusal_classifier_manager = RefusalClassifierManager()
|
||||||
|
refusal_classifier_manager.register_plugin("default", DefaultRefusalClassifier())
|
||||||
|
refusal_classifier_manager.register_plugin("ml_classifier", classifier)
|
||||||
|
|
||||||
|
|
||||||
def refusal_heuristic(request_json):
|
def refusal_heuristic(request_json):
|
||||||
# TODO: improve this heuristic
|
"""Check if the request contains a refusal using the plugin system.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request_json: The request to check.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the request contains a refusal, False otherwise.
|
||||||
|
"""
|
||||||
request = str(request_json)
|
request = str(request_json)
|
||||||
return check_refusal(request) or classifier.is_refusal(request)
|
return refusal_classifier_manager.is_refusal(request)
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
from agentic_security.probe_actor.refusal import check_refusal
|
from agentic_security.probe_actor.refusal import DefaultRefusalClassifier
|
||||||
|
|
||||||
|
|
||||||
class TestCheckRefusal:
|
class TestCheckRefusal:
|
||||||
# The function correctly identifies a refusal phrase in the response.
|
# The function correctly identifies a refusal phrase in the response.
|
||||||
def test_identify_refusal_phrase(self):
|
def test_identify_refusal_phrase(self):
|
||||||
response = "I'm sorry, but I cannot provide that information."
|
response = "I'm sorry, but I cannot provide that information."
|
||||||
assert check_refusal(response)
|
assert DefaultRefusalClassifier().is_refusal(response)
|
||||||
|
|
||||||
# The response is an empty string.
|
# The response is an empty string.
|
||||||
def test_empty_response(self):
|
def test_empty_response(self):
|
||||||
response = ""
|
response = ""
|
||||||
assert not check_refusal(response)
|
assert not DefaultRefusalClassifier().is_refusal(response)
|
||||||
|
|||||||
@@ -408,6 +408,21 @@ REGISTRY = REGISTRY_V0 + [
|
|||||||
},
|
},
|
||||||
"modality": "text",
|
"modality": "text",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"dataset_name": "Reinforcement Learning Optimization",
|
||||||
|
"num_prompts": 0,
|
||||||
|
"tokens": 0,
|
||||||
|
"approx_cost": 0.0,
|
||||||
|
"source": "Cloud hosted model",
|
||||||
|
"selected": False,
|
||||||
|
"url": "",
|
||||||
|
"dynamic": True,
|
||||||
|
"opts": {
|
||||||
|
"port": 8718,
|
||||||
|
"modules": ["encoding"],
|
||||||
|
},
|
||||||
|
"modality": "text",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"dataset_name": "InspectAI",
|
"dataset_name": "InspectAI",
|
||||||
"num_prompts": 0,
|
"num_prompts": 0,
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
|
import base64
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import httpx
|
||||||
from cache_to_disk import cache_to_disk
|
from cache_to_disk import cache_to_disk
|
||||||
|
|
||||||
|
|
||||||
|
def encode(content: bytes) -> str:
|
||||||
|
encoded_content = base64.b64encode(content).decode("utf-8")
|
||||||
|
return "data:audio/mpeg;base64," + encoded_content
|
||||||
|
|
||||||
|
|
||||||
def generate_audio_mac_wav(prompt: str) -> bytes:
|
def generate_audio_mac_wav(prompt: str) -> bytes:
|
||||||
"""
|
"""
|
||||||
Generate an audio file from the provided prompt using macOS 'say' command
|
Generate an audio file from the provided prompt using macOS 'say' command
|
||||||
@@ -64,3 +71,21 @@ def generate_audioform(prompt: str) -> bytes:
|
|||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"Audio generation is only supported on macOS for now."
|
"Audio generation is only supported on macOS for now."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RequestAdapter:
|
||||||
|
# Adapter of http_spec.LLMSpec
|
||||||
|
|
||||||
|
def __init__(self, llm_spec):
|
||||||
|
self.llm_spec = llm_spec
|
||||||
|
if not llm_spec.has_audio:
|
||||||
|
raise ValueError("LLMSpec must have an image")
|
||||||
|
|
||||||
|
async def probe(
|
||||||
|
self, prompt: str, encoded_image: str = "", encoded_audio: str = "", files={}
|
||||||
|
) -> httpx.Response:
|
||||||
|
encoded_audio = generate_audioform(prompt)
|
||||||
|
encoded_audio = encode(encoded_audio)
|
||||||
|
return await self.llm_spec.probe(prompt, encoded_image, encoded_audio, files)
|
||||||
|
|
||||||
|
fn = probe
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from agentic_security.probe_data.modules import (
|
|||||||
fine_tuned,
|
fine_tuned,
|
||||||
garak_tool,
|
garak_tool,
|
||||||
inspect_ai_tool,
|
inspect_ai_tool,
|
||||||
|
rl_model,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -265,6 +266,11 @@ def prepare_prompts(dataset_names, budget, tools_inbox=None, options=[]):
|
|||||||
garak_tool.Module(group, tools_inbox=tools_inbox, opts=opts).apply(),
|
garak_tool.Module(group, tools_inbox=tools_inbox, opts=opts).apply(),
|
||||||
lazy=True,
|
lazy=True,
|
||||||
),
|
),
|
||||||
|
"Reinforcement Learning Optimization": lambda opts: dataset_from_iterator(
|
||||||
|
"Reinforcement Learning Optimization",
|
||||||
|
rl_model.Module(group, tools_inbox=tools_inbox, opts=opts).apply(),
|
||||||
|
lazy=True,
|
||||||
|
),
|
||||||
"InspectAI": lambda opts: dataset_from_iterator(
|
"InspectAI": lambda opts: dataset_from_iterator(
|
||||||
"InspectAI",
|
"InspectAI",
|
||||||
inspect_ai_tool.Module(group, tools_inbox=tools_inbox).apply(),
|
inspect_ai_tool.Module(group, tools_inbox=tools_inbox).apply(),
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
import base64
|
||||||
import io
|
import io
|
||||||
|
|
||||||
|
import httpx
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
from cache_to_disk import cache_to_disk
|
from cache_to_disk import cache_to_disk
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
@@ -75,3 +77,26 @@ def generate_image(prompt: str) -> bytes:
|
|||||||
|
|
||||||
# Return the image bytes
|
# Return the image bytes
|
||||||
return buffer.getvalue()
|
return buffer.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def encode(image: bytes) -> str:
|
||||||
|
encoded_content = base64.b64encode(image).decode("utf-8")
|
||||||
|
return "data:image/jpeg;base64," + encoded_content
|
||||||
|
|
||||||
|
|
||||||
|
class RequestAdapter:
|
||||||
|
# Adapter of http_spec.LLMSpec
|
||||||
|
|
||||||
|
def __init__(self, llm_spec):
|
||||||
|
self.llm_spec = llm_spec
|
||||||
|
if not llm_spec.has_image:
|
||||||
|
raise ValueError("LLMSpec must have an image")
|
||||||
|
|
||||||
|
async def probe(
|
||||||
|
self, prompt: str, encoded_image: str = "", encoded_audio: str = "", files={}
|
||||||
|
) -> httpx.Response:
|
||||||
|
encoded_image = generate_image(prompt)
|
||||||
|
encoded_image = encode(encoded_image)
|
||||||
|
return await self.llm_spec.probe(prompt, encoded_image, encoded_audio, files)
|
||||||
|
|
||||||
|
fn = probe
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class Module:
|
|||||||
self.batch_size = self.opts.get("batch_size", 500)
|
self.batch_size = self.opts.get("batch_size", 500)
|
||||||
|
|
||||||
async def apply(self):
|
async def apply(self):
|
||||||
for _ in range(self.max_prompts // self.batch_size):
|
for _ in range(max(self.max_prompts // self.batch_size, 1)):
|
||||||
# Fetch prompts from the API
|
# Fetch prompts from the API
|
||||||
prompts = await self.fetch_prompts()
|
prompts = await self.fetch_prompts()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,247 @@
|
|||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import uuid as U
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from collections import deque
|
||||||
|
from typing import Deque
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import requests
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
AUTH_TOKEN: str = os.getenv("AS_TOKEN", "gh0-5f4a8ed2-37c6-4bd7-a0cf-7070eae8115b")
|
||||||
|
|
||||||
|
|
||||||
|
class PromptSelectionInterface(ABC):
|
||||||
|
"""Abstract base class for prompt selection strategies."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def select_next_prompt(self, current_prompt: str, passed_guard: bool) -> str:
|
||||||
|
"""Selects the next prompt based on current state and guard result."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def select_next_prompts(self, current_prompt: str, passed_guard: bool) -> list[str]:
|
||||||
|
"""Selects the next prompts based on current state and guard result."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def update_rewards(
|
||||||
|
self,
|
||||||
|
previous_prompt: str,
|
||||||
|
current_prompt: str,
|
||||||
|
reward: float,
|
||||||
|
passed_guard: bool,
|
||||||
|
) -> None:
|
||||||
|
"""Updates internal rewards based on the outcome of the last selected prompt."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class RandomPromptSelector(PromptSelectionInterface):
|
||||||
|
"""Random prompt selector with cycle prevention using history."""
|
||||||
|
|
||||||
|
def __init__(self, prompts: list[str], history_size: int = 300):
|
||||||
|
if not prompts:
|
||||||
|
raise ValueError("Prompts list cannot be empty")
|
||||||
|
self.prompts = prompts
|
||||||
|
self.history: Deque[str] = deque(maxlen=history_size)
|
||||||
|
|
||||||
|
def select_next_prompts(self, current_prompt: str, passed_guard: bool) -> list[str]:
|
||||||
|
return [self.select_next_prompt(current_prompt, passed_guard)]
|
||||||
|
|
||||||
|
def select_next_prompt(self, current_prompt: str, passed_guard: bool) -> str:
|
||||||
|
self.history.append(current_prompt)
|
||||||
|
available = [p for p in self.prompts if p not in self.history]
|
||||||
|
|
||||||
|
if not available:
|
||||||
|
available = self.prompts
|
||||||
|
self.history.clear()
|
||||||
|
|
||||||
|
return random.choice(available)
|
||||||
|
|
||||||
|
def update_rewards(
|
||||||
|
self,
|
||||||
|
previous_prompt: str,
|
||||||
|
current_prompt: str,
|
||||||
|
reward: float,
|
||||||
|
passed_guard: bool,
|
||||||
|
) -> None:
|
||||||
|
pass # No learning in random selection
|
||||||
|
|
||||||
|
|
||||||
|
class CloudRLPromptSelector(PromptSelectionInterface):
|
||||||
|
"""Cloud-based reinforcement learning prompt selector with fallback."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
prompts: list[str],
|
||||||
|
api_url: str,
|
||||||
|
auth_token: str = AUTH_TOKEN,
|
||||||
|
history_size: int = 300,
|
||||||
|
timeout: int = 5,
|
||||||
|
run_id: str = "",
|
||||||
|
):
|
||||||
|
if not prompts:
|
||||||
|
raise ValueError("Prompts list cannot be empty")
|
||||||
|
self.prompts = prompts
|
||||||
|
self.api_url = api_url
|
||||||
|
self.headers = {"Authorization": f"Bearer {auth_token}"}
|
||||||
|
self.timeout = timeout
|
||||||
|
self.run_id = run_id or U.uuid4().hex
|
||||||
|
|
||||||
|
def select_next_prompt(self, current_prompt: str, passed_guard: bool) -> list[str]:
|
||||||
|
return self.select_next_prompts(current_prompt, passed_guard)[0]
|
||||||
|
|
||||||
|
def select_next_prompts(self, current_prompt: str, passed_guard: bool) -> str:
|
||||||
|
try:
|
||||||
|
response = requests.post(
|
||||||
|
f"{self.api_url}/rl-model/select-next-prompt",
|
||||||
|
json={
|
||||||
|
"run_id": U.uuid4().hex,
|
||||||
|
"current_prompt": current_prompt,
|
||||||
|
"passed_guard": passed_guard,
|
||||||
|
},
|
||||||
|
headers=self.headers,
|
||||||
|
timeout=self.timeout,
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json().get("next_prompts", [])
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logger.error(f"Cloud request failed: {e}")
|
||||||
|
return [self._fallback_selection()]
|
||||||
|
|
||||||
|
def _fallback_selection(self) -> str:
|
||||||
|
return random.choice(self.prompts)
|
||||||
|
|
||||||
|
def update_rewards(
|
||||||
|
self,
|
||||||
|
previous_prompt: str,
|
||||||
|
current_prompt: str,
|
||||||
|
reward: float,
|
||||||
|
passed_guard: bool,
|
||||||
|
) -> None:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
class QLearningPromptSelector(PromptSelectionInterface):
|
||||||
|
"""Q-Learning based prompt selector with exploration/exploitation tradeoff."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
prompts: list[str],
|
||||||
|
learning_rate: float = 0.1,
|
||||||
|
discount_factor: float = 0.9,
|
||||||
|
initial_exploration: float = 1.0,
|
||||||
|
exploration_decay: float = 0.995,
|
||||||
|
min_exploration: float = 0.01,
|
||||||
|
history_size: int = 300,
|
||||||
|
):
|
||||||
|
if not prompts:
|
||||||
|
raise ValueError("Prompts list cannot be empty")
|
||||||
|
|
||||||
|
self.prompts = prompts
|
||||||
|
self.learning_rate = learning_rate
|
||||||
|
self.discount_factor = discount_factor
|
||||||
|
self.exploration_rate = initial_exploration
|
||||||
|
self.exploration_decay = exploration_decay
|
||||||
|
self.min_exploration = min_exploration
|
||||||
|
self.history: Deque[str] = deque(maxlen=history_size)
|
||||||
|
|
||||||
|
# Initialize Q-table with small random values
|
||||||
|
self.q_table: dict[str, dict[str, float]] = {
|
||||||
|
state: {
|
||||||
|
action: np.random.uniform(0, 0.1)
|
||||||
|
for action in prompts
|
||||||
|
if action != state
|
||||||
|
}
|
||||||
|
for state in prompts
|
||||||
|
}
|
||||||
|
|
||||||
|
def select_next_prompts(self, current_prompt: str, passed_guard: bool) -> list[str]:
|
||||||
|
return [self.select_next_prompt(current_prompt, passed_guard)]
|
||||||
|
|
||||||
|
def select_next_prompt(self, current_prompt: str, passed_guard: bool) -> str:
|
||||||
|
self.history.append(current_prompt)
|
||||||
|
available = [a for a in self.prompts if a not in self.history]
|
||||||
|
|
||||||
|
if not available:
|
||||||
|
available = self.prompts
|
||||||
|
self.history.clear()
|
||||||
|
|
||||||
|
# Exploration-exploitation tradeoff
|
||||||
|
if np.random.random() < self.exploration_rate:
|
||||||
|
selected = random.choice(available)
|
||||||
|
else:
|
||||||
|
q_values = {a: self.q_table[current_prompt][a] for a in available}
|
||||||
|
selected = max(q_values, key=q_values.get) # type: ignore
|
||||||
|
|
||||||
|
# Decay exploration rate
|
||||||
|
self.exploration_rate = max(
|
||||||
|
self.min_exploration, self.exploration_rate * self.exploration_decay
|
||||||
|
)
|
||||||
|
return selected
|
||||||
|
|
||||||
|
def update_rewards(
|
||||||
|
self,
|
||||||
|
previous_prompt: str,
|
||||||
|
current_prompt: str,
|
||||||
|
reward: float,
|
||||||
|
passed_guard: bool,
|
||||||
|
) -> None:
|
||||||
|
if (
|
||||||
|
previous_prompt not in self.q_table
|
||||||
|
or current_prompt not in self.q_table[previous_prompt]
|
||||||
|
):
|
||||||
|
return
|
||||||
|
|
||||||
|
# Calculate temporal difference error
|
||||||
|
max_future_q = max(self.q_table[current_prompt].values(), default=0.0)
|
||||||
|
td_target = reward + self.discount_factor * max_future_q
|
||||||
|
td_error = td_target - self.q_table[previous_prompt][current_prompt]
|
||||||
|
|
||||||
|
# Update Q-value
|
||||||
|
self.q_table[previous_prompt][current_prompt] += self.learning_rate * td_error
|
||||||
|
|
||||||
|
|
||||||
|
class Module:
|
||||||
|
def __init__(
|
||||||
|
self, prompt_groups: list[str], tools_inbox: asyncio.Queue, opts: dict = {}
|
||||||
|
):
|
||||||
|
self.tools_inbox = tools_inbox
|
||||||
|
self.opts = opts
|
||||||
|
self.prompt_groups = prompt_groups
|
||||||
|
self.max_prompts = self.opts.get("max_prompts", 10) # Default max M prompts
|
||||||
|
self.run_id = U.uuid4().hex
|
||||||
|
self.batch_size = self.opts.get("batch_size", 500)
|
||||||
|
self.rl_model = CloudRLPromptSelector(
|
||||||
|
prompt_groups, "https://edge.metaheuristic.co", run_id=self.run_id
|
||||||
|
)
|
||||||
|
|
||||||
|
async def apply(self):
|
||||||
|
current_prompt = "What is AI?"
|
||||||
|
passed_guard = False
|
||||||
|
for _ in range(max(self.max_prompts, 1)):
|
||||||
|
# Fetch prompts from the API
|
||||||
|
prompts = await asyncio.to_thread(
|
||||||
|
lambda: self.rl_model.select_next_prompts(
|
||||||
|
current_prompt, passed_guard=passed_guard
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if not prompts:
|
||||||
|
logger.error("No prompts retrieved from the API.")
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.info(f"Retrieved {len(prompts)} prompts.")
|
||||||
|
|
||||||
|
for i, prompt in enumerate(prompts):
|
||||||
|
logger.info(f"Processing prompt {i+1}/{len(prompts)}: {prompt}")
|
||||||
|
yield prompt
|
||||||
|
current_prompt = prompt
|
||||||
|
while not self.tools_inbox.empty():
|
||||||
|
ref = await self.tools_inbox.get()
|
||||||
|
print(ref, "ref")
|
||||||
|
message, _, ready = ref["message"], ref["reply"], ref["ready"]
|
||||||
|
yield message
|
||||||
|
ready.set()
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
import asyncio
|
||||||
|
from collections import deque
|
||||||
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
|
||||||
|
# Import the classes to be tested
|
||||||
|
from agentic_security.probe_data.modules.rl_model import (
|
||||||
|
CloudRLPromptSelector,
|
||||||
|
Module,
|
||||||
|
QLearningPromptSelector,
|
||||||
|
RandomPromptSelector,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Fixtures for reusable test data
|
||||||
|
@pytest.fixture
|
||||||
|
def dataset_prompts() -> list[str]:
|
||||||
|
return [
|
||||||
|
"What is AI?",
|
||||||
|
"How does RL work?",
|
||||||
|
"Explain supervised learning.",
|
||||||
|
"What is reinforcement learning?",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_requests() -> Mock:
|
||||||
|
with patch("requests.post") as mock_requests:
|
||||||
|
yield mock_requests
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_rl_selector() -> Mock:
|
||||||
|
return CloudRLPromptSelector(
|
||||||
|
dataset_prompts,
|
||||||
|
api_url="https://edge.metaheuristic.co",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def tools_inbox() -> asyncio.Queue:
|
||||||
|
return asyncio.Queue()
|
||||||
|
|
||||||
|
|
||||||
|
# Tests for RandomPromptSelector
|
||||||
|
class TestRandomPromptSelector:
|
||||||
|
def test_initialization(self, dataset_prompts):
|
||||||
|
selector = RandomPromptSelector(dataset_prompts)
|
||||||
|
assert selector.prompts == dataset_prompts
|
||||||
|
assert isinstance(selector.history, deque)
|
||||||
|
assert selector.history.maxlen == 300
|
||||||
|
|
||||||
|
def test_select_next_prompt(self, dataset_prompts):
|
||||||
|
selector = RandomPromptSelector(dataset_prompts)
|
||||||
|
current_prompt = "What is AI?"
|
||||||
|
next_prompt = selector.select_next_prompt(current_prompt, passed_guard=True)
|
||||||
|
assert next_prompt in dataset_prompts
|
||||||
|
assert next_prompt != current_prompt
|
||||||
|
|
||||||
|
def test_update_rewards_no_op(self, dataset_prompts):
|
||||||
|
selector = RandomPromptSelector(dataset_prompts)
|
||||||
|
selector.update_rewards("What is AI?", "How does RL work?", 1.0, True)
|
||||||
|
assert len(selector.history) == 0
|
||||||
|
|
||||||
|
|
||||||
|
# Tests for CloudRLPromptSelector
|
||||||
|
class TestCloudRLPromptSelector:
|
||||||
|
def test_initialization(self, dataset_prompts):
|
||||||
|
selector = CloudRLPromptSelector(dataset_prompts, "http://example.com", "token")
|
||||||
|
assert selector.prompts == dataset_prompts
|
||||||
|
assert selector.api_url == "http://example.com"
|
||||||
|
assert selector.headers == {"Authorization": "Bearer token"}
|
||||||
|
|
||||||
|
def test_select_next_prompt_success(self, dataset_prompts, mock_requests):
|
||||||
|
mock_requests.return_value.status_code = 200
|
||||||
|
mock_requests.return_value.json.return_value = {"next_prompts": ["What is AI?"]}
|
||||||
|
|
||||||
|
selector = CloudRLPromptSelector(dataset_prompts, "http://example.com", "token")
|
||||||
|
next_prompt = selector.select_next_prompt(
|
||||||
|
"How does RL work?", passed_guard=True
|
||||||
|
)
|
||||||
|
assert next_prompt == "What is AI?"
|
||||||
|
mock_requests.assert_called_once()
|
||||||
|
|
||||||
|
def test_fallback_on_failure(self, dataset_prompts, mock_requests):
|
||||||
|
mock_requests.side_effect = requests.exceptions.RequestException
|
||||||
|
selector = CloudRLPromptSelector(dataset_prompts, "http://example.com", "token")
|
||||||
|
next_prompt = selector.select_next_prompt("What is AI?", passed_guard=True)
|
||||||
|
assert next_prompt in dataset_prompts
|
||||||
|
|
||||||
|
def test_select_next_prompt_success_service(self, dataset_prompts):
|
||||||
|
selector = CloudRLPromptSelector(
|
||||||
|
dataset_prompts,
|
||||||
|
api_url="https://edge.metaheuristic.co",
|
||||||
|
)
|
||||||
|
next_prompt = selector.select_next_prompt(
|
||||||
|
"How does RL work?", passed_guard=True
|
||||||
|
)
|
||||||
|
assert next_prompt
|
||||||
|
|
||||||
|
|
||||||
|
# Tests for QLearningPromptSelector
|
||||||
|
class TestQLearningPromptSelector:
|
||||||
|
def test_initialization(self, dataset_prompts):
|
||||||
|
selector = QLearningPromptSelector(dataset_prompts)
|
||||||
|
assert selector.prompts == dataset_prompts
|
||||||
|
assert selector.exploration_rate == 1.0
|
||||||
|
assert len(selector.q_table) == len(dataset_prompts)
|
||||||
|
assert all(
|
||||||
|
len(v) == len(dataset_prompts) - 1 for v in selector.q_table.values()
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_select_next_prompt_exploration(self, dataset_prompts):
|
||||||
|
selector = QLearningPromptSelector(dataset_prompts, initial_exploration=1.0)
|
||||||
|
next_prompt = selector.select_next_prompt("What is AI?", passed_guard=True)
|
||||||
|
assert next_prompt in dataset_prompts
|
||||||
|
assert next_prompt != "What is AI?"
|
||||||
|
|
||||||
|
def test_select_next_prompt_exploitation(self, dataset_prompts):
|
||||||
|
selector = QLearningPromptSelector(dataset_prompts, initial_exploration=0.0)
|
||||||
|
selector.q_table["What is AI?"]["How does RL work?"] = 10.0
|
||||||
|
next_prompt = selector.select_next_prompt("What is AI?", passed_guard=True)
|
||||||
|
assert next_prompt == "How does RL work?"
|
||||||
|
|
||||||
|
def test_update_rewards(self, dataset_prompts):
|
||||||
|
selector = QLearningPromptSelector(dataset_prompts)
|
||||||
|
selector.update_rewards("What is AI?", "How does RL work?", 1.0, True)
|
||||||
|
assert selector.q_table["What is AI?"]["How does RL work?"] > 0.0
|
||||||
|
|
||||||
|
def test_exploration_rate_decay(self, dataset_prompts):
|
||||||
|
selector = QLearningPromptSelector(
|
||||||
|
dataset_prompts, initial_exploration=1.0, exploration_decay=0.9
|
||||||
|
)
|
||||||
|
assert selector.exploration_rate == 1.0
|
||||||
|
selector.select_next_prompt("What is AI?", passed_guard=True)
|
||||||
|
assert selector.exploration_rate == 0.9
|
||||||
|
selector.select_next_prompt("How does RL work?", passed_guard=True)
|
||||||
|
assert selector.exploration_rate == 0.81
|
||||||
|
|
||||||
|
|
||||||
|
# Edge Cases and Error Handling
|
||||||
|
def test_empty_prompts():
|
||||||
|
with pytest.raises(ValueError, match="Prompts list cannot be empty"):
|
||||||
|
RandomPromptSelector([])
|
||||||
|
|
||||||
|
|
||||||
|
def test_cloud_rl_selector_invalid_url(dataset_prompts):
|
||||||
|
selector = CloudRLPromptSelector(dataset_prompts, "invalid_url", "token")
|
||||||
|
next_prompt = selector.select_next_prompt("What is AI?", passed_guard=True)
|
||||||
|
assert next_prompt in dataset_prompts
|
||||||
|
|
||||||
|
|
||||||
|
def test_q_learning_selector_invalid_reward(dataset_prompts):
|
||||||
|
selector = QLearningPromptSelector(dataset_prompts)
|
||||||
|
selector.update_rewards("What is AI?", "How does RL work?", np.nan, True)
|
||||||
|
|
||||||
|
|
||||||
|
# Tests for Module class
|
||||||
|
class TestModule:
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_uuid(self):
|
||||||
|
with patch("uuid.uuid4") as mock:
|
||||||
|
mock.return_value.hex = "test_run_id"
|
||||||
|
yield mock
|
||||||
|
|
||||||
|
def test_initialization(self, dataset_prompts, tools_inbox, mock_uuid):
|
||||||
|
module = Module(dataset_prompts, tools_inbox)
|
||||||
|
assert module.prompt_groups == dataset_prompts
|
||||||
|
assert module.tools_inbox == tools_inbox
|
||||||
|
assert module.max_prompts == 10
|
||||||
|
assert module.batch_size == 500
|
||||||
|
assert module.run_id == "test_run_id"
|
||||||
|
assert isinstance(module.rl_model, CloudRLPromptSelector)
|
||||||
|
|
||||||
|
def test_initialization_with_options(self, dataset_prompts, tools_inbox, mock_uuid):
|
||||||
|
opts = {
|
||||||
|
"max_prompts": 100,
|
||||||
|
"batch_size": 50,
|
||||||
|
}
|
||||||
|
module = Module(dataset_prompts, tools_inbox, opts)
|
||||||
|
assert module.max_prompts == 100
|
||||||
|
assert module.batch_size == 50
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_apply_basic_flow(
|
||||||
|
self, dataset_prompts, tools_inbox, mock_rl_selector
|
||||||
|
):
|
||||||
|
module = Module(dataset_prompts, tools_inbox)
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
async for prompt in module.apply():
|
||||||
|
assert prompt
|
||||||
|
count += 1
|
||||||
|
if count >= 3: # Test a few iterations
|
||||||
|
break
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_apply_rl_with_tools_inbox(self, dataset_prompts, tools_inbox):
|
||||||
|
# Add a test message to the tools inbox
|
||||||
|
test_message = {
|
||||||
|
"message": "Test message",
|
||||||
|
"reply": None,
|
||||||
|
"ready": asyncio.Event(),
|
||||||
|
}
|
||||||
|
await tools_inbox.put(test_message)
|
||||||
|
|
||||||
|
module = Module(dataset_prompts, tools_inbox)
|
||||||
|
|
||||||
|
async for output in module.apply():
|
||||||
|
if output == "Test message":
|
||||||
|
test_message["ready"].set()
|
||||||
|
break
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import base64
|
import base64
|
||||||
import random
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
|
|
||||||
def rot13(input_text):
|
def rot13(input_text):
|
||||||
@@ -98,3 +99,47 @@ def zigzag_obfuscation(text):
|
|||||||
else:
|
else:
|
||||||
new_text += char
|
new_text += char
|
||||||
return new_text
|
return new_text
|
||||||
|
|
||||||
|
|
||||||
|
def caesar_cipher(text, shift=3):
|
||||||
|
"""Encrypts text using Caesar cipher with specified shift."""
|
||||||
|
result = []
|
||||||
|
for char in text:
|
||||||
|
if char.isupper():
|
||||||
|
result.append(chr((ord(char) + shift - 65) % 26 + 65))
|
||||||
|
elif char.islower():
|
||||||
|
result.append(chr((ord(char) + shift - 97) % 26 + 97))
|
||||||
|
else:
|
||||||
|
result.append(char)
|
||||||
|
return "".join(result)
|
||||||
|
|
||||||
|
|
||||||
|
def substitution_cipher(text, key=None):
|
||||||
|
"""Encrypts text using a substitution cipher with optional key."""
|
||||||
|
if key is None:
|
||||||
|
key = list(string.ascii_lowercase)
|
||||||
|
random.shuffle(key)
|
||||||
|
key = "".join(key)
|
||||||
|
|
||||||
|
# Create translation table
|
||||||
|
alphabet = string.ascii_lowercase
|
||||||
|
translation = str.maketrans(alphabet, key)
|
||||||
|
|
||||||
|
# Apply translation
|
||||||
|
return text.lower().translate(translation)
|
||||||
|
|
||||||
|
|
||||||
|
def vigenere_cipher(text, key):
|
||||||
|
"""Encrypts text using Vigenère cipher with provided key."""
|
||||||
|
result = []
|
||||||
|
key_length = len(key)
|
||||||
|
key_as_int = [ord(i) for i in key.lower()]
|
||||||
|
text = text.lower()
|
||||||
|
|
||||||
|
for i, char in enumerate(text):
|
||||||
|
if char.isalpha():
|
||||||
|
shift = key_as_int[i % key_length] - 97
|
||||||
|
result.append(chr((ord(char) + shift - 97) % 26 + 97))
|
||||||
|
else:
|
||||||
|
result.append(char)
|
||||||
|
return "".join(result)
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,7 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
from fastapi import APIRouter, File, Header, HTTPException, UploadFile
|
from fastapi import APIRouter, File, Header, HTTPException, UploadFile
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
from ..models.schemas import FileProbeResponse, Probe
|
from ..models.schemas import FileProbeResponse, Probe
|
||||||
from ..probe_actor.refusal import REFUSAL_MARKS
|
from ..probe_actor.refusal import REFUSAL_MARKS
|
||||||
@@ -70,3 +71,9 @@ async def self_probe_image():
|
|||||||
@router.get("/v1/data-config")
|
@router.get("/v1/data-config")
|
||||||
async def data_config():
|
async def data_config():
|
||||||
return [m for m in REGISTRY]
|
return [m for m in REGISTRY]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/health")
|
||||||
|
async def health_check():
|
||||||
|
"""Health check endpoint."""
|
||||||
|
return JSONResponse(content={"status": "ok"})
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from ..app import app
|
||||||
|
|
||||||
|
|
||||||
|
def test_health_check():
|
||||||
|
"""Test the health check endpoint."""
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
response = client.get("/health")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"status": "ok"}
|
||||||
@@ -17,7 +17,7 @@ Content-Type: application/json
|
|||||||
|
|
||||||
`,
|
`,
|
||||||
`POST https://api.openai.com/v1/chat/completions
|
`POST https://api.openai.com/v1/chat/completions
|
||||||
Authorization: Bearer sk-xxxxxxxxx
|
Authorization: Bearer $OPENAI_API_KEY
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -25,6 +25,20 @@ Content-Type: application/json
|
|||||||
"messages": [{"role": "user", "content": "<<PROMPT>>"}],
|
"messages": [{"role": "user", "content": "<<PROMPT>>"}],
|
||||||
"temperature": 0.7
|
"temperature": 0.7
|
||||||
}
|
}
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
POST https://api.deepseek.com/chat/completions
|
||||||
|
Authorization: Bearer $DEEPSEEK_API_KEY
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"model": "deepseek-chat",
|
||||||
|
"messages": [
|
||||||
|
{"role": "system", "content": "You are a helpful assistant."},
|
||||||
|
{"role": "user", "content": "<<PROMPT>>"}
|
||||||
|
],
|
||||||
|
"stream": false
|
||||||
|
}
|
||||||
`,
|
`,
|
||||||
`POST https://api.replicate.com/v1/models/mistralai/mixtral-8x7b-instruct-v0.1/predictions
|
`POST https://api.replicate.com/v1/models/mistralai/mixtral-8x7b-instruct-v0.1/predictions
|
||||||
Authorization: Bearer $APIKEY
|
Authorization: Bearer $APIKEY
|
||||||
@@ -165,6 +179,7 @@ Content-Type: application/json
|
|||||||
let LLM_CONFIGS = [
|
let LLM_CONFIGS = [
|
||||||
{ name: 'Custom API', prompts: 40000, customInstructions: 'Requires api spec' },
|
{ name: 'Custom API', prompts: 40000, customInstructions: 'Requires api spec' },
|
||||||
{ name: 'Open AI', prompts: 24000 },
|
{ name: 'Open AI', prompts: 24000 },
|
||||||
|
{ name: 'Deepseek v1', prompts: 24000 },
|
||||||
{ name: 'Replicate', prompts: 40000 },
|
{ name: 'Replicate', prompts: 40000 },
|
||||||
{ name: 'Groq', prompts: 40000 },
|
{ name: 'Groq', prompts: 40000 },
|
||||||
{ name: 'Together.ai', prompts: 40000 },
|
{ name: 'Together.ai', prompts: 40000 },
|
||||||
|
|||||||
@@ -124,6 +124,34 @@ class TestAS:
|
|||||||
print(result)
|
print(result)
|
||||||
assert len(result) in [0, 1]
|
assert len(result) in [0, 1]
|
||||||
|
|
||||||
|
def test_image_modality(self):
|
||||||
|
llmSpec = test_spec_assets.IMAGE_SPEC
|
||||||
|
maxBudget = 2
|
||||||
|
max_th = 0.3
|
||||||
|
datasets = [
|
||||||
|
{
|
||||||
|
"dataset_name": "AgenticBackend",
|
||||||
|
"num_prompts": 0,
|
||||||
|
"tokens": 0,
|
||||||
|
"approx_cost": 0.0,
|
||||||
|
"source": "Fine-tuned cloud hosted model",
|
||||||
|
"selected": True,
|
||||||
|
"url": "",
|
||||||
|
"dynamic": True,
|
||||||
|
"opts": {
|
||||||
|
# "port": 8718,
|
||||||
|
"port": 9094,
|
||||||
|
"modules": ["encoding"],
|
||||||
|
"max_prompts": 2,
|
||||||
|
},
|
||||||
|
"modality": "text",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
result = AgenticSecurity.scan(llmSpec, maxBudget, datasets, max_th)
|
||||||
|
assert isinstance(result, dict)
|
||||||
|
print(result)
|
||||||
|
assert len(result) in [0, 1]
|
||||||
|
|
||||||
|
|
||||||
class TestEntrypointCI:
|
class TestEntrypointCI:
|
||||||
def test_generate_default_cfg_to_tmp_path(self):
|
def test_generate_default_cfg_to_tmp_path(self):
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
# API Reference
|
||||||
|
|
||||||
|
This section provides detailed information about the Agentic Security API.
|
||||||
|
|
||||||
|
## Endpoints
|
||||||
|
|
||||||
|
### `/v1/self-probe`
|
||||||
|
|
||||||
|
- **Method**: POST
|
||||||
|
- **Description**: Used for integration testing.
|
||||||
|
- **Request Body**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"prompt": "<<PROMPT>>"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `/v1/self-probe-image`
|
||||||
|
|
||||||
|
- **Method**: POST
|
||||||
|
- **Description**: Probes the image modality.
|
||||||
|
- **Request Body**:
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"text": "What is in this image?"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "image_url",
|
||||||
|
"image_url": {
|
||||||
|
"url": "data:image/jpeg;base64,<<BASE64_IMAGE>>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Authentication
|
||||||
|
|
||||||
|
All API requests require an API key. Include it in the `Authorization` header:
|
||||||
|
|
||||||
|
```
|
||||||
|
Authorization: Bearer YOUR_API_KEY
|
||||||
|
```
|
||||||
|
|
||||||
|
## Further Reading
|
||||||
|
|
||||||
|
For more details on API usage, refer to the [Configuration](configuration.md) section.
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
# CI/CD Integration
|
||||||
|
|
||||||
|
Integrate Agentic Security into your CI/CD pipeline to automate security scans.
|
||||||
|
|
||||||
|
## GitHub Actions
|
||||||
|
|
||||||
|
Use the provided GitHub Action workflow to perform automated scans:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
name: Security Scan
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
scan:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: 3.11
|
||||||
|
- name: Install dependencies
|
||||||
|
run: pip install agentic_security
|
||||||
|
- name: Run security scan
|
||||||
|
run: agentic_security ci
|
||||||
|
```
|
||||||
|
|
||||||
|
## Custom CI/CD Pipelines
|
||||||
|
|
||||||
|
For custom pipelines, ensure the following steps:
|
||||||
|
|
||||||
|
1. Install dependencies.
|
||||||
|
1. Run the `agentic_security ci` command.
|
||||||
|
|
||||||
|
## Further Reading
|
||||||
|
|
||||||
|
For more details on CI/CD integration, refer to the [API Reference](api_reference.md).
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
# Configuration
|
||||||
|
|
||||||
|
This section provides information on configuring Agentic Security to suit your needs.
|
||||||
|
|
||||||
|
## Default Configuration
|
||||||
|
|
||||||
|
The default configuration file is `agesec.toml`. It includes settings for:
|
||||||
|
|
||||||
|
- General settings
|
||||||
|
- Module configurations
|
||||||
|
- Thresholds
|
||||||
|
|
||||||
|
## Customizing Configuration
|
||||||
|
|
||||||
|
1. Open the `agesec.toml` file in a text editor.
|
||||||
|
1. Modify the settings as needed. For example, to change the port:
|
||||||
|
```toml
|
||||||
|
[modules.AgenticBackend.opts]
|
||||||
|
port = 8718
|
||||||
|
```
|
||||||
|
|
||||||
|
## Advanced Configuration
|
||||||
|
|
||||||
|
For advanced configuration options, refer to the [API Reference](api_reference.md).
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# Contributing
|
||||||
|
|
||||||
|
We welcome contributions to Agentic Security! Follow these steps to get started:
|
||||||
|
|
||||||
|
## How to Contribute
|
||||||
|
|
||||||
|
1. **Fork the Repository**: Click the "Fork" button at the top of the repository page.
|
||||||
|
1. **Clone Your Fork**: Clone your forked repository to your local machine.
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/mmsoedov/agentic_security.git
|
||||||
|
```
|
||||||
|
1. **Create a Branch**: Create a new branch for your feature or bugfix.
|
||||||
|
```bash
|
||||||
|
git checkout -b feature-name
|
||||||
|
```
|
||||||
|
1. **Make Changes**: Implement your changes and commit them.
|
||||||
|
```bash
|
||||||
|
git commit -m "Description of changes"
|
||||||
|
```
|
||||||
|
1. **Push Changes**: Push your changes to your fork.
|
||||||
|
```bash
|
||||||
|
git push origin feature-name
|
||||||
|
```
|
||||||
|
1. **Open a Pull Request**: Go to the original repository and open a pull request.
|
||||||
|
|
||||||
|
## Code of Conduct
|
||||||
|
|
||||||
|
Please adhere to the [Code of Conduct](CODE_OF_CONDUCT.md) in all interactions.
|
||||||
|
|
||||||
|
## Further Reading
|
||||||
|
|
||||||
|
For more details on contributing, refer to the [Documentation](index.md) section.
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# Dataset Extension
|
||||||
|
|
||||||
|
Agentic Security allows you to extend datasets to enhance its capabilities.
|
||||||
|
|
||||||
|
## Adding New Datasets
|
||||||
|
|
||||||
|
1. Place your dataset files in the `datasets` directory.
|
||||||
|
1. Ensure each file contains a `prompt` column for processing.
|
||||||
|
|
||||||
|
## Supported Formats
|
||||||
|
|
||||||
|
- CSV
|
||||||
|
- JSON
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
To add a new dataset:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp my_dataset.csv datasets/
|
||||||
|
```
|
||||||
|
|
||||||
|
## Further Reading
|
||||||
|
|
||||||
|
For more details on dataset formats and processing, refer to the [API Reference](api_reference.md).
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
## Module Interface Documentation
|
||||||
|
|
||||||
|
The `Module` class interface provides a standardized way to create and use modules in the `agentic_security` project.
|
||||||
|
|
||||||
|
Here is an example of a module that implements the `ModuleProtocol` interface. This example shows how to create a module that processes prompts and sends results to a queue.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import List, Dict, Any, AsyncGenerator
|
||||||
|
import asyncio
|
||||||
|
from .module_protocol import ModuleProtocol
|
||||||
|
|
||||||
|
class ModuleProtocol(ModuleProtocol):
|
||||||
|
def __init__(self, prompt_groups: List[Any], tools_inbox: asyncio.Queue, opts: Dict[str, Any]):
|
||||||
|
self.prompt_groups = prompt_groups
|
||||||
|
self.tools_inbox = tools_inbox
|
||||||
|
self.opts = opts
|
||||||
|
|
||||||
|
async def apply(self) -> AsyncGenerator[str, None]:
|
||||||
|
for group in self.prompt_groups:
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
result = f"Processed {group}"
|
||||||
|
await self.tools_inbox.put(result)
|
||||||
|
yield result
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Usage Example
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
import ModuleProtocol
|
||||||
|
|
||||||
|
tools_inbox = asyncio.Queue()
|
||||||
|
prompt_groups = ["group1", "group2"]
|
||||||
|
opts = {"max_prompts": 1000, "batch_size": 100}
|
||||||
|
|
||||||
|
module = ModuleProtocol(prompt_groups, tools_inbox, opts)
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async for result in module.apply():
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
```
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Getting Started
|
||||||
|
|
||||||
|
Welcome to Agentic Security! This guide will help you get started with using the tool.
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
1. Ensure you have completed the [installation](installation.md) steps.
|
||||||
|
1. Run the following command to start the application:
|
||||||
|
```bash
|
||||||
|
agentic_security
|
||||||
|
```
|
||||||
|
1. Access the application at `http://localhost:8718`.
|
||||||
|
|
||||||
|
## Basic Usage
|
||||||
|
|
||||||
|
- To view available commands, use:
|
||||||
|
```bash
|
||||||
|
agentic_security --help
|
||||||
|
```
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
Explore the [Configuration](configuration.md) section to customize your setup.
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
# HTTP Specification Documentation
|
||||||
|
|
||||||
|
The HTTP specification in the Agentic Security project is designed to handle various types of requests, including text, image, audio, and file uploads. This documentation provides a detailed overview of the HTTP specification and its usage.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The HTTP specification is implemented in the `LLMSpec` class, which is used to define and execute HTTP requests. The class supports different modalities, including text, image, audio, and file uploads, and provides methods to validate and execute these requests.
|
||||||
|
|
||||||
|
## Modalities
|
||||||
|
|
||||||
|
The HTTP specification supports the following modalities:
|
||||||
|
|
||||||
|
### Text
|
||||||
|
|
||||||
|
Text-based requests are the most common type of request. The `LLMSpec` class replaces the `<<PROMPT>>` placeholder in the request body with the provided prompt.
|
||||||
|
|
||||||
|
### Image
|
||||||
|
|
||||||
|
Image-based requests include an image encoded in base64 format. The `LLMSpec` class replaces the `<<BASE64_IMAGE>>` placeholder in the request body with the provided base64-encoded image.
|
||||||
|
|
||||||
|
### Audio
|
||||||
|
|
||||||
|
Audio-based requests include an audio file encoded in base64 format. The `LLMSpec` class replaces the `<<BASE64_AUDIO>>` placeholder in the request body with the provided base64-encoded audio.
|
||||||
|
|
||||||
|
### Files
|
||||||
|
|
||||||
|
File-based requests include file uploads. The `LLMSpec` class handles multipart form data and includes the provided files in the request.
|
||||||
|
|
||||||
|
## LLMSpec Class
|
||||||
|
|
||||||
|
The `LLMSpec` class is the core of the HTTP specification. It provides the following methods and properties:
|
||||||
|
|
||||||
|
### Methods
|
||||||
|
|
||||||
|
- **`from_string(http_spec: str) -> LLMSpec`**: Parses an HTTP specification string into an `LLMSpec` object.
|
||||||
|
- **`validate(prompt: str, encoded_image: str, encoded_audio: str, files: dict) -> None`**: Validates the request parameters based on the specified modality.
|
||||||
|
- **`probe(prompt: str, encoded_image: str = "", encoded_audio: str = "", files: dict = {}) -> httpx.Response`**: Sends an HTTP request using the specified parameters.
|
||||||
|
- **`verify() -> httpx.Response`**: Verifies the HTTP specification by sending a test request.
|
||||||
|
|
||||||
|
### Properties
|
||||||
|
|
||||||
|
- **`modality: Modality`**: Returns the modality of the request (text, image, audio, or files).
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Text Request
|
||||||
|
|
||||||
|
```python
|
||||||
|
http_spec = """
|
||||||
|
POST https://api.example.com/v1/chat/completions
|
||||||
|
Authorization: Bearer sk-xxxxxxxxx
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"model": "gpt-3.5-turbo",
|
||||||
|
"messages": [{"role": "user", "content": "<<PROMPT>>"}],
|
||||||
|
"temperature": 0.7
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
spec = LLMSpec.from_string(http_spec)
|
||||||
|
response = await spec.probe("What is the capital of France?")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Image Request
|
||||||
|
|
||||||
|
```python
|
||||||
|
http_spec = """
|
||||||
|
POST https://api.example.com/v1/chat/completions
|
||||||
|
Authorization: Bearer sk-xxxxxxxxx
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"model": "gpt-4-vision-preview",
|
||||||
|
"messages": [{"role": "user", "content": "What is in this image? <<BASE64_IMAGE>>"}],
|
||||||
|
"temperature": 0.7
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
spec = LLMSpec.from_string(http_spec)
|
||||||
|
encoded_image = encode_image_base64_by_url("https://example.com/image.jpg")
|
||||||
|
response = await spec.probe("What is in this image?", encoded_image=encoded_image)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Audio Request
|
||||||
|
|
||||||
|
```python
|
||||||
|
http_spec = """
|
||||||
|
POST https://api.example.com/v1/chat/completions
|
||||||
|
Authorization: Bearer sk-xxxxxxxxx
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"model": "whisper-large-v3",
|
||||||
|
"messages": [{"role": "user", "content": "Transcribe this audio: <<BASE64_AUDIO>>"}],
|
||||||
|
"temperature": 0.7
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
spec = LLMSpec.from_string(http_spec)
|
||||||
|
encoded_audio = encode_audio_base64_by_url("https://example.com/audio.mp3")
|
||||||
|
response = await spec.probe("Transcribe this audio:", encoded_audio=encoded_audio)
|
||||||
|
```
|
||||||
|
|
||||||
|
### File Request
|
||||||
|
|
||||||
|
```python
|
||||||
|
http_spec = """
|
||||||
|
POST https://api.example.com/v1/chat/completions
|
||||||
|
Authorization: Bearer sk-xxxxxxxxx
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
|
||||||
|
{
|
||||||
|
"model": "gpt-3.5-turbo",
|
||||||
|
"messages": [{"role": "user", "content": "Process this file: <<FILE>>"}],
|
||||||
|
"temperature": 0.7
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
spec = LLMSpec.from_string(http_spec)
|
||||||
|
files = {"file": ("document.txt", open("document.txt", "rb"))}
|
||||||
|
response = await spec.probe("Process this file:", files=files)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
The HTTP specification in the Agentic Security project provides a flexible and powerful way to handle various types of requests. This documentation serves as a guide to understanding and utilizing the HTTP specification effectively.
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
# Image Generation System
|
||||||
|
|
||||||
|
The image generation system creates visual probes for security testing by converting text prompts into images. This document explains its architecture and implementation.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The system:
|
||||||
|
|
||||||
|
1. Converts text datasets into image datasets
|
||||||
|
1. Generates images using matplotlib
|
||||||
|
1. Encodes images for transmission
|
||||||
|
1. Integrates with the LLM probing system
|
||||||
|
|
||||||
|
## Core Components
|
||||||
|
|
||||||
|
### Image Generation
|
||||||
|
|
||||||
|
```python
|
||||||
|
@cache_to_disk()
|
||||||
|
def generate_image(prompt: str) -> bytes:
|
||||||
|
"""
|
||||||
|
Generates a JPEG image containing the provided text prompt
|
||||||
|
"""
|
||||||
|
# Create figure with light blue background
|
||||||
|
fig, ax = plt.subplots(figsize=(6, 4))
|
||||||
|
ax.set_facecolor("lightblue")
|
||||||
|
|
||||||
|
# Add centered text
|
||||||
|
ax.text(
|
||||||
|
0.5, 0.5,
|
||||||
|
prompt,
|
||||||
|
fontsize=16,
|
||||||
|
ha="center",
|
||||||
|
va="center",
|
||||||
|
wrap=True,
|
||||||
|
color="darkblue"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Save to buffer
|
||||||
|
buffer = io.BytesIO()
|
||||||
|
plt.savefig(buffer, format="jpeg", bbox_inches="tight")
|
||||||
|
return buffer.getvalue()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Dataset Conversion
|
||||||
|
|
||||||
|
```python
|
||||||
|
def generate_image_dataset(text_dataset: list[ProbeDataset]) -> list[ImageProbeDataset]:
|
||||||
|
"""
|
||||||
|
Converts text datasets into image datasets
|
||||||
|
"""
|
||||||
|
image_datasets = []
|
||||||
|
|
||||||
|
for dataset in text_dataset:
|
||||||
|
image_prompts = [
|
||||||
|
generate_image(prompt)
|
||||||
|
for prompt in tqdm(dataset.prompts)
|
||||||
|
]
|
||||||
|
|
||||||
|
image_datasets.append(ImageProbeDataset(
|
||||||
|
test_dataset=dataset,
|
||||||
|
image_prompts=image_prompts
|
||||||
|
))
|
||||||
|
|
||||||
|
return image_datasets
|
||||||
|
```
|
||||||
|
|
||||||
|
### Image Encoding
|
||||||
|
|
||||||
|
```python
|
||||||
|
def encode(image: bytes) -> str:
|
||||||
|
"""
|
||||||
|
Encodes image bytes into base64 data URL
|
||||||
|
"""
|
||||||
|
encoded = base64.b64encode(image).decode("utf-8")
|
||||||
|
return "data:image/jpeg;base64," + encoded
|
||||||
|
```
|
||||||
|
|
||||||
|
## Integration
|
||||||
|
|
||||||
|
### RequestAdapter
|
||||||
|
|
||||||
|
The RequestAdapter class integrates image generation with LLM probing:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class RequestAdapter:
|
||||||
|
def __init__(self, llm_spec):
|
||||||
|
if not llm_spec.has_image:
|
||||||
|
raise ValueError("LLMSpec must have an image")
|
||||||
|
self.llm_spec = llm_spec
|
||||||
|
|
||||||
|
async def probe(self, prompt: str, encoded_image: str = "",
|
||||||
|
encoded_audio: str = "", files={}) -> httpx.Response:
|
||||||
|
encoded_image = generate_image(prompt)
|
||||||
|
encoded_image = encode(encoded_image)
|
||||||
|
return await self.llm_spec.probe(prompt, encoded_image, encoded_audio, files)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Key Features
|
||||||
|
|
||||||
|
- **Caching**: Generated images are cached to disk using @cache_to_disk
|
||||||
|
- **Progress Tracking**: tqdm progress bars for dataset conversion
|
||||||
|
- **Error Handling**: Validates LLM specifications before probing
|
||||||
|
- **Standard Formats**: Uses JPEG format with base64 encoding
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
The system is configured through:
|
||||||
|
|
||||||
|
1. Figure size (6x4 inches)
|
||||||
|
1. Background color (light blue)
|
||||||
|
1. Text styling (16pt dark blue centered text)
|
||||||
|
1. Image format (JPEG)
|
||||||
|
|
||||||
|
## Limitations
|
||||||
|
|
||||||
|
- Currently only supports text-based image generation
|
||||||
|
- Fixed visual style and formatting
|
||||||
|
- Requires matplotlib and associated dependencies
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<p align="center">
|
||||||
|
|
||||||
|
<h1 align="center">Agentic Security</h1>
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
The open-source Agentic LLM Vulnerability Scanner
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Customizable Rule Sets or Agent based attacks🛠️
|
||||||
|
- Comprehensive fuzzing for any LLMs 🧪
|
||||||
|
- LLM API integration and stress testing 🛠️
|
||||||
|
- Wide range of fuzzing and attack techniques 🌀
|
||||||
|
|
||||||
|
Note: Please be aware that Agentic Security is designed as a safety scanner tool and not a foolproof solution. It cannot guarantee complete protection against all possible threats.
|
||||||
|
|
||||||
|
## UI 🧙
|
||||||
|
|
||||||
|
<img width="100%" alt="booking-screen" src="https://res.cloudinary.com/dq0w2rtm9/image/upload/v1736433557/z0bsyzhsqlgcr3w4ovwp.gif">
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# Installation
|
||||||
|
|
||||||
|
This section will guide you through the installation process for Agentic Security.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- Python 3.11
|
||||||
|
- pip
|
||||||
|
|
||||||
|
## Installation Steps
|
||||||
|
|
||||||
|
1. Install the package using pip:
|
||||||
|
```bash
|
||||||
|
pip install agentic_security
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
If you encounter any issues during installation, please refer to the [troubleshooting guide](#) or contact support.
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
# Bayesian Optimization in Security Fuzzing
|
||||||
|
|
||||||
|
The fuzzer implements an optimization system using scikit-optimize (skopt) to minimize failure rates during security scans. This document explains the optimizer's implementation and behavior.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The optimizer is used in both single-shot and many-shot scanning modes when the `optimize` parameter is True. It dynamically adjusts scan parameters to minimize failure rates while staying within budget constraints.
|
||||||
|
|
||||||
|
## Implementation Details
|
||||||
|
|
||||||
|
### Initialization
|
||||||
|
|
||||||
|
The optimizer is initialized with:
|
||||||
|
|
||||||
|
```python
|
||||||
|
Optimizer(
|
||||||
|
[Real(0, 1)], # Single parameter space (0 to 1)
|
||||||
|
base_estimator="GP", # Gaussian Process estimator
|
||||||
|
n_initial_points=25 # Initial exploration points
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Optimization Process
|
||||||
|
|
||||||
|
1. **Parameter Space**: A single real-valued parameter between 0 and 1
|
||||||
|
1. **Objective**: Minimize the failure rate (negative failure rate is maximized)
|
||||||
|
1. **Update Mechanism**:
|
||||||
|
```python
|
||||||
|
next_point = optimizer.ask()
|
||||||
|
optimizer.tell(next_point, -failure_rate)
|
||||||
|
```
|
||||||
|
1. **Early Stopping**: If best failure rate exceeds 50%:
|
||||||
|
```python
|
||||||
|
if best_failure_rate > 0.5:
|
||||||
|
yield ScanResult.status_msg(
|
||||||
|
f"High failure rate detected ({best_failure_rate:.2%}). Stopping this module..."
|
||||||
|
)
|
||||||
|
break
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage in Scanning
|
||||||
|
|
||||||
|
The optimizer is integrated into both scan types:
|
||||||
|
|
||||||
|
### Single-shot Scan
|
||||||
|
|
||||||
|
- Used in `perform_single_shot_scan()`
|
||||||
|
- Optimizes failure rates across prompt modules
|
||||||
|
- Considers token budget constraints
|
||||||
|
|
||||||
|
### Many-shot Scan
|
||||||
|
|
||||||
|
- Used in `perform_many_shot_scan()`
|
||||||
|
- Handles more complex multi-step attacks
|
||||||
|
- Maintains separate failure rate tracking
|
||||||
|
|
||||||
|
## Key Parameters
|
||||||
|
|
||||||
|
| Parameter | Description |
|
||||||
|
|-----------|-------------|
|
||||||
|
| base_estimator | Gaussian Process (GP) used for optimization |
|
||||||
|
| n_initial_points | 25 initial exploration points |
|
||||||
|
| Real(0, 1) | Single parameter space being optimized |
|
||||||
|
| failure_rate | Current failure rate being minimized |
|
||||||
|
|
||||||
|
## Optimization Flow
|
||||||
|
|
||||||
|
1. Initialize optimizer with GP estimator
|
||||||
|
1. Collect initial 25 data points
|
||||||
|
1. For each prompt:
|
||||||
|
- Calculate current failure rate
|
||||||
|
- Update optimizer with new point
|
||||||
|
- Check for early stopping conditions
|
||||||
|
1. Continue until scan completes or budget exhausted
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
The optimizer is wrapped in try/except blocks to ensure scan failures don't crash the entire process. Any optimization errors are logged and the scan continues with default parameters.
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
# Probe Actor Module Documentation
|
||||||
|
|
||||||
|
The `probe_actor` module is a critical component of the Agentic Security project, responsible for generating prompts, performing scans, and handling refusal checks. This documentation provides an overview of the module's structure and functionality.
|
||||||
|
|
||||||
|
## Files and Key Components
|
||||||
|
|
||||||
|
### fuzzer.py
|
||||||
|
|
||||||
|
- **Functions:**
|
||||||
|
- `async def generate_prompts(...)`: Asynchronously generates prompts for scanning.
|
||||||
|
- `def multi_modality_spec(llm_spec)`: Defines specifications for multi-modality.
|
||||||
|
- `async def process_prompt(...)`: Processes a given prompt asynchronously.
|
||||||
|
- `async def perform_single_shot_scan(...)`: Performs a single-shot scan asynchronously.
|
||||||
|
- `async def perform_many_shot_scan(...)`: Performs a many-shot scan asynchronously.
|
||||||
|
- `def scan_router(...)`: Routes scan requests.
|
||||||
|
|
||||||
|
### refusal.py
|
||||||
|
|
||||||
|
- **Functions:**
|
||||||
|
- `def check_refusal(response: str, refusal_phrases: list = REFUSAL_MARKS) -> bool`: Checks if a response contains refusal phrases.
|
||||||
|
- `def refusal_heuristic(request_json)`: Applies heuristics to determine refusal.
|
||||||
|
|
||||||
|
## Usage Examples
|
||||||
|
|
||||||
|
### Performing a Single-Shot Scan
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_actor.fuzzer import perform_single_shot_scan
|
||||||
|
|
||||||
|
await perform_single_shot_scan(prompt="Test prompt")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking for Refusal
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_actor.refusal import check_refusal
|
||||||
|
|
||||||
|
is_refusal = check_refusal(response="I'm sorry, I can't do that.")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
The `probe_actor` module provides essential functionality for generating prompts, performing scans, and handling refusal checks within the Agentic Security project. This documentation serves as a guide to understanding and utilizing the module's capabilities.
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
# Probe Data Module Documentation
|
||||||
|
|
||||||
|
The `probe_data` module is a core component of the Agentic Security project, responsible for handling datasets, generating audio and image data, and applying various transformations. This documentation provides an overview of the module's structure and functionality.
|
||||||
|
|
||||||
|
## Files and Key Components
|
||||||
|
|
||||||
|
### audio_generator.py
|
||||||
|
|
||||||
|
- **Functions:**
|
||||||
|
- `encode(content: bytes) -> str`: Encodes audio content to a string format.
|
||||||
|
- `generate_audio_mac_wav(prompt: str) -> bytes`: Generates audio in WAV format for macOS.
|
||||||
|
- `generate_audioform(prompt: str) -> bytes`: Generates audio from a given prompt.
|
||||||
|
- **Classes:**
|
||||||
|
- `RequestAdapter`: Handles requests for audio generation.
|
||||||
|
|
||||||
|
### data.py
|
||||||
|
|
||||||
|
- **Functions:**
|
||||||
|
- `load_dataset_general(...)`: Loads datasets with general specifications.
|
||||||
|
- `count_words_in_list(str_list)`: Counts words in a list of strings.
|
||||||
|
- `prepare_prompts(...)`: Prepares prompts for dataset processing.
|
||||||
|
- **Classes:**
|
||||||
|
- `Stenography`: Applies transformations to prompt groups.
|
||||||
|
|
||||||
|
### image_generator.py
|
||||||
|
|
||||||
|
- **Functions:**
|
||||||
|
- `generate_image_dataset(...)`: Generates a dataset of images.
|
||||||
|
- `generate_image(prompt: str) -> bytes`: Generates an image from a prompt.
|
||||||
|
- **Classes:**
|
||||||
|
- `RequestAdapter`: Handles requests for image generation.
|
||||||
|
|
||||||
|
### models.py
|
||||||
|
|
||||||
|
- **Classes:**
|
||||||
|
- `ProbeDataset`: Represents a dataset for probing.
|
||||||
|
- `ImageProbeDataset`: Extends `ProbeDataset` for image data.
|
||||||
|
|
||||||
|
### msj_data.py
|
||||||
|
|
||||||
|
- **Functions:**
|
||||||
|
- `load_dataset_generic(...)`: Loads a generic dataset.
|
||||||
|
- **Classes:**
|
||||||
|
- `ProbeDataset`: Represents a dataset for probing.
|
||||||
|
|
||||||
|
### stenography_fn.py
|
||||||
|
|
||||||
|
- **Functions:**
|
||||||
|
- `rot13(input_text)`: Applies ROT13 transformation.
|
||||||
|
- `base64_encode(data)`: Encodes data in base64 format.
|
||||||
|
- `mirror_words(text)`: Mirrors words in the text.
|
||||||
|
|
||||||
|
### rl_model.py
|
||||||
|
|
||||||
|
- **Classes:**
|
||||||
|
- `PromptSelectionInterface`: Abstract base class for prompt selection strategies.
|
||||||
|
|
||||||
|
- Methods:
|
||||||
|
- `select_next_prompt(current_prompt: str, passed_guard: bool) -> str`: Selects next prompt
|
||||||
|
- `select_next_prompts(current_prompt: str, passed_guard: bool) -> list[str]`: Selects multiple prompts
|
||||||
|
- `update_rewards(previous_prompt: str, current_prompt: str, reward: float, passed_guard: bool) -> None`: Updates rewards
|
||||||
|
|
||||||
|
- `RandomPromptSelector`: Basic random selection with history tracking.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- `prompts: list[str]`: List of available prompts
|
||||||
|
- `history_size: int = 3`: Size of history to prevent cycles
|
||||||
|
|
||||||
|
- `CloudRLPromptSelector`: Cloud-based RL implementation with fallback.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- `prompts: list[str]`: List of available prompts
|
||||||
|
- `api_url: str`: URL of RL service
|
||||||
|
- `auth_token: str = AUTH_TOKEN`: Authentication token
|
||||||
|
- `history_size: int = 300`: Size of history
|
||||||
|
- `timeout: int = 5`: Request timeout
|
||||||
|
- `run_id: str = ""`: Unique run identifier
|
||||||
|
|
||||||
|
- `QLearningPromptSelector`: Local Q-learning implementation.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- `prompts: list[str]`: List of available prompts
|
||||||
|
- `learning_rate: float = 0.1`: Learning rate
|
||||||
|
- `discount_factor: float = 0.9`: Discount factor
|
||||||
|
- `initial_exploration: float = 1.0`: Initial exploration rate
|
||||||
|
- `exploration_decay: float = 0.995`: Exploration decay rate
|
||||||
|
- `min_exploration: float = 0.01`: Minimum exploration rate
|
||||||
|
- `history_size: int = 300`: Size of history
|
||||||
|
|
||||||
|
- `Module`: Main class that uses CloudRLPromptSelector.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- `prompt_groups: list[str]`: Groups of prompts
|
||||||
|
- `tools_inbox: asyncio.Queue`: Queue for tool communication
|
||||||
|
- `opts: dict = {}`: Configuration options
|
||||||
|
|
||||||
|
## Usage Examples
|
||||||
|
|
||||||
|
### Generating Audio
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_data.audio_generator import generate_audioform
|
||||||
|
|
||||||
|
audio_bytes = generate_audioform("Hello, world!")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Loading a Dataset
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_data.data import load_dataset_general
|
||||||
|
|
||||||
|
dataset = load_dataset_general("example_dataset")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using RL Model
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_data.modules.rl_model import QLearningPromptSelector
|
||||||
|
|
||||||
|
prompts = ["What is AI?", "Explain machine learning"]
|
||||||
|
selector = QLearningPromptSelector(prompts)
|
||||||
|
|
||||||
|
current_prompt = "What is AI?"
|
||||||
|
next_prompt = selector.select_next_prompt(current_prompt, passed_guard=True)
|
||||||
|
selector.update_rewards(current_prompt, next_prompt, reward=1.0, passed_guard=True)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
The `probe_data` module provides essential functionality for handling and transforming datasets within the Agentic Security project. This documentation serves as a guide to understanding and utilizing the module's capabilities.
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
# Refusal Classifier Plugin System Documentation
|
||||||
|
|
||||||
|
The refusal classifier plugin system allows for the creation and use of custom refusal classifiers. This system is designed to be modular and extensible, enabling users to add their own refusal detection logic.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The plugin system is based on the `RefusalClassifierPlugin` abstract base class, which defines the interface for all refusal classifier plugins. The `RefusalClassifierManager` is used to register and manage these plugins.
|
||||||
|
|
||||||
|
## Creating a Plugin
|
||||||
|
|
||||||
|
To create a custom refusal classifier plugin, you must implement the `RefusalClassifierPlugin` abstract base class. This class requires the implementation of the `is_refusal` method, which checks if a response contains a refusal.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
class RefusalClassifierPlugin(ABC):
|
||||||
|
"""Base class for refusal classifier plugins."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def is_refusal(self, response: str) -> bool:
|
||||||
|
"""Check if the response contains a refusal.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
response (str): The response from the language model.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the response contains a refusal, False otherwise.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example Plugin
|
||||||
|
|
||||||
|
Here is an example of a custom refusal classifier plugin that checks for specific phrases:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class CustomRefusalClassifier(RefusalClassifierPlugin):
|
||||||
|
def __init__(self, custom_phrases: List[str]):
|
||||||
|
self.custom_phrases = custom_phrases
|
||||||
|
|
||||||
|
def is_refusal(self, response: str) -> bool:
|
||||||
|
"""Check if the response contains any custom refusal phrases.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
response (str): The response from the language model.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the response contains a custom refusal phrase, False otherwise.
|
||||||
|
"""
|
||||||
|
return any(phrase in response for phrase in self.custom_phrases)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Registering a Plugin
|
||||||
|
|
||||||
|
To register a custom refusal classifier plugin, use the `RefusalClassifierManager`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_actor.refusal import RefusalClassifierManager
|
||||||
|
|
||||||
|
# Initialize the plugin manager
|
||||||
|
refusal_classifier_manager = RefusalClassifierManager()
|
||||||
|
|
||||||
|
# Register the custom plugin
|
||||||
|
refusal_classifier_manager.register_plugin("custom", CustomRefusalClassifier(custom_phrases=["I can't", "I won't"]))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Using the Plugin System
|
||||||
|
|
||||||
|
The `refusal_heuristic` function automatically uses all registered plugins to check for refusals:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_actor.refusal import refusal_heuristic
|
||||||
|
|
||||||
|
is_refusal = refusal_heuristic(request_json)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
The refusal classifier plugin system provides a flexible and extensible way to add custom refusal detection logic to the Agentic Security project. This documentation serves as a guide to creating, registering, and using custom refusal classifier plugins.
|
||||||
@@ -0,0 +1,194 @@
|
|||||||
|
# RL Model Module
|
||||||
|
|
||||||
|
The RL Model module provides reinforcement learning-based prompt selection strategies for the probe system.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The module implements several prompt selection strategies that use reinforcement learning techniques to optimize prompt selection based on guard results and rewards.
|
||||||
|
|
||||||
|
## Classes
|
||||||
|
|
||||||
|
### PromptSelectionInterface
|
||||||
|
|
||||||
|
Abstract base class defining the interface for prompt selection strategies.
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
|
||||||
|
- `select_next_prompt(current_prompt: str, passed_guard: bool) -> str`
|
||||||
|
- `select_next_prompts(current_prompt: str, passed_guard: bool) -> list[str]`
|
||||||
|
- `update_rewards(previous_prompt: str, current_prompt: str, reward: float, passed_guard: bool) -> None`
|
||||||
|
|
||||||
|
### RandomPromptSelector
|
||||||
|
|
||||||
|
Basic random selection strategy with cycle prevention using history.
|
||||||
|
|
||||||
|
**Configuration:**
|
||||||
|
|
||||||
|
- `prompts`: List of available prompts
|
||||||
|
- `history_size`: Size of history buffer to prevent cycles (default: 300)
|
||||||
|
|
||||||
|
### CloudRLPromptSelector
|
||||||
|
|
||||||
|
Cloud-based reinforcement learning prompt selector with fallback to random selection.
|
||||||
|
|
||||||
|
**Configuration:**
|
||||||
|
|
||||||
|
- `prompts`: List of available prompts
|
||||||
|
- `api_url`: URL of the RL service
|
||||||
|
- `auth_token`: Authentication token (default: AS_TOKEN environment variable)
|
||||||
|
- `history_size`: Size of history buffer (default: 300)
|
||||||
|
- `timeout`: Request timeout in seconds (default: 5)
|
||||||
|
- `run_id`: Unique identifier for the run
|
||||||
|
|
||||||
|
### QLearningPromptSelector
|
||||||
|
|
||||||
|
Q-Learning based prompt selector with exploration/exploitation tradeoff.
|
||||||
|
|
||||||
|
**Configuration:**
|
||||||
|
|
||||||
|
- `prompts`: List of available prompts
|
||||||
|
- `learning_rate`: Learning rate (default: 0.1)
|
||||||
|
- `discount_factor`: Discount factor (default: 0.9)
|
||||||
|
- `initial_exploration`: Initial exploration rate (default: 1.0)
|
||||||
|
- `exploration_decay`: Exploration decay rate (default: 0.995)
|
||||||
|
- `min_exploration`: Minimum exploration rate (default: 0.01)
|
||||||
|
- `history_size`: Size of history buffer (default: 300)
|
||||||
|
|
||||||
|
### Module
|
||||||
|
|
||||||
|
Main class that implements the RL-based prompt selection functionality.
|
||||||
|
|
||||||
|
**Configuration:**
|
||||||
|
|
||||||
|
- `prompt_groups`: List of prompt groups
|
||||||
|
- `tools_inbox`: asyncio.Queue for tool communication
|
||||||
|
- `opts`: Additional options
|
||||||
|
- `max_prompts`: Maximum number of prompts to generate (default: 10)
|
||||||
|
- `batch_size`: Batch size for processing (default: 500)
|
||||||
|
|
||||||
|
## Usage Example
|
||||||
|
|
||||||
|
```python
|
||||||
|
from agentic_security.probe_data.modules.rl_model import (
|
||||||
|
Module,
|
||||||
|
CloudRLPromptSelector,
|
||||||
|
QLearningPromptSelector
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initialize with prompt groups
|
||||||
|
prompt_groups = ["What is AI?", "Explain ML", "Describe RL"]
|
||||||
|
module = Module(prompt_groups, asyncio.Queue())
|
||||||
|
|
||||||
|
# Use the module
|
||||||
|
async for prompt in module.apply():
|
||||||
|
print(f"Selected prompt: {prompt}")
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### PromptSelectionInterface
|
||||||
|
|
||||||
|
```python
|
||||||
|
class PromptSelectionInterface(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def select_next_prompt(self, current_prompt: str, passed_guard: bool) -> str:
|
||||||
|
"""Select next prompt based on current state and guard result."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def select_next_prompts(self, current_prompt: str, passed_guard: bool) -> list[str]:
|
||||||
|
"""Select next prompts based on current state and guard result."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def update_rewards(
|
||||||
|
self,
|
||||||
|
previous_prompt: str,
|
||||||
|
current_prompt: str,
|
||||||
|
reward: float,
|
||||||
|
passed_guard: bool,
|
||||||
|
) -> None:
|
||||||
|
"""Update internal rewards based on outcome of last selected prompt."""
|
||||||
|
```
|
||||||
|
|
||||||
|
### RandomPromptSelector
|
||||||
|
|
||||||
|
```python
|
||||||
|
class RandomPromptSelector(PromptSelectionInterface):
|
||||||
|
def __init__(self, prompts: list[str], history_size: int = 300):
|
||||||
|
"""Initialize with prompts and history size."""
|
||||||
|
|
||||||
|
def select_next_prompt(self, current_prompt: str, passed_guard: bool) -> str:
|
||||||
|
"""Select next prompt randomly with cycle prevention."""
|
||||||
|
|
||||||
|
def update_rewards(
|
||||||
|
self,
|
||||||
|
previous_prompt: str,
|
||||||
|
current_prompt: str,
|
||||||
|
reward: float,
|
||||||
|
passed_guard: bool,
|
||||||
|
) -> None:
|
||||||
|
"""No learning in random selection."""
|
||||||
|
```
|
||||||
|
|
||||||
|
### CloudRLPromptSelector
|
||||||
|
|
||||||
|
```python
|
||||||
|
class CloudRLPromptSelector(PromptSelectionInterface):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
prompts: list[str],
|
||||||
|
api_url: str,
|
||||||
|
auth_token: str = AUTH_TOKEN,
|
||||||
|
history_size: int = 300,
|
||||||
|
timeout: int = 5,
|
||||||
|
run_id: str = "",
|
||||||
|
):
|
||||||
|
"""Initialize with cloud RL configuration."""
|
||||||
|
|
||||||
|
def select_next_prompts(self, current_prompt: str, passed_guard: bool) -> list[str]:
|
||||||
|
"""Select next prompts using cloud RL with fallback."""
|
||||||
|
|
||||||
|
def _fallback_selection(self) -> str:
|
||||||
|
"""Fallback to random selection if cloud request fails."""
|
||||||
|
```
|
||||||
|
|
||||||
|
### QLearningPromptSelector
|
||||||
|
|
||||||
|
```python
|
||||||
|
class QLearningPromptSelector(PromptSelectionInterface):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
prompts: list[str],
|
||||||
|
learning_rate: float = 0.1,
|
||||||
|
discount_factor: float = 0.9,
|
||||||
|
initial_exploration: float = 1.0,
|
||||||
|
exploration_decay: float = 0.995,
|
||||||
|
min_exploration: float = 0.01,
|
||||||
|
history_size: int = 300,
|
||||||
|
):
|
||||||
|
"""Initialize Q-Learning configuration."""
|
||||||
|
|
||||||
|
def select_next_prompt(self, current_prompt: str, passed_guard: bool) -> str:
|
||||||
|
"""Select next prompt using Q-Learning with exploration/exploitation."""
|
||||||
|
|
||||||
|
def update_rewards(
|
||||||
|
self,
|
||||||
|
previous_prompt: str,
|
||||||
|
current_prompt: str,
|
||||||
|
reward: float,
|
||||||
|
passed_guard: bool,
|
||||||
|
) -> None:
|
||||||
|
"""Update Q-values based on reward."""
|
||||||
|
```
|
||||||
|
|
||||||
|
### Module
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Module:
|
||||||
|
def __init__(
|
||||||
|
self, prompt_groups: list[str], tools_inbox: asyncio.Queue, opts: dict = {}
|
||||||
|
):
|
||||||
|
"""Initialize module with prompt groups and configuration."""
|
||||||
|
|
||||||
|
async def apply(self):
|
||||||
|
"""Apply the RL model to generate prompts."""
|
||||||
|
```
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
# Stenography Functions
|
||||||
|
|
||||||
|
The stenography module provides various text obfuscation and transformation techniques for security testing. This document explains its architecture and implementation.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The module implements:
|
||||||
|
|
||||||
|
1. Rotation ciphers (ROT13, ROT5)
|
||||||
|
1. Base64 encoding
|
||||||
|
1. Text manipulation functions
|
||||||
|
1. Randomization techniques
|
||||||
|
1. Character substitution methods
|
||||||
|
|
||||||
|
## Core Functions
|
||||||
|
|
||||||
|
### Rotation Ciphers
|
||||||
|
|
||||||
|
```python
|
||||||
|
def rot13(input_text):
|
||||||
|
"""
|
||||||
|
Applies ROT13 cipher to input text
|
||||||
|
- Preserves case of letters
|
||||||
|
- Leaves non-alphabetic characters unchanged
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
|
||||||
|
def rot5(input_text):
|
||||||
|
"""
|
||||||
|
Applies ROT5 cipher to input text
|
||||||
|
- Rotates digits by 5 positions
|
||||||
|
- Leaves non-digit characters unchanged
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Encoding
|
||||||
|
|
||||||
|
```python
|
||||||
|
def base64_encode(data):
|
||||||
|
"""
|
||||||
|
Encodes input data using Base64
|
||||||
|
- Handles both string and bytes input
|
||||||
|
- Returns UTF-8 encoded string
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Text Manipulation
|
||||||
|
|
||||||
|
```python
|
||||||
|
def mirror_words(text):
|
||||||
|
"""
|
||||||
|
Reverses each word in the input text
|
||||||
|
- Preserves word order
|
||||||
|
- Maintains spaces between words
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
|
||||||
|
def scramble_words(text):
|
||||||
|
"""
|
||||||
|
Randomly scrambles middle letters of words
|
||||||
|
- Preserves first and last letters
|
||||||
|
- Handles words shorter than 4 characters
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Randomization
|
||||||
|
|
||||||
|
```python
|
||||||
|
def randomize_letter_case(text):
|
||||||
|
"""
|
||||||
|
Randomly changes case of each character
|
||||||
|
- Independent case changes per character
|
||||||
|
- Preserves non-letter characters
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
|
||||||
|
def insert_noise_characters(text, frequency=0.2):
|
||||||
|
"""
|
||||||
|
Inserts random characters between existing ones
|
||||||
|
- Configurable insertion frequency
|
||||||
|
- Uses alphanumeric characters for noise
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Advanced Transformations
|
||||||
|
|
||||||
|
```python
|
||||||
|
def substitute_with_ascii(text):
|
||||||
|
"""
|
||||||
|
Replaces characters with their ASCII codes
|
||||||
|
- Space-separated numeric values
|
||||||
|
- Preserves original character order
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
|
||||||
|
def remove_vowels(text):
|
||||||
|
"""
|
||||||
|
Removes all vowel characters from text
|
||||||
|
- Handles both lowercase and uppercase vowels
|
||||||
|
- Preserves non-vowel characters
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
|
||||||
|
def zigzag_obfuscation(text):
|
||||||
|
"""
|
||||||
|
Alternates character case in zigzag pattern
|
||||||
|
- Starts with uppercase
|
||||||
|
- Toggles case for each alphabetic character
|
||||||
|
"""
|
||||||
|
# Implementation details...
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage Patterns
|
||||||
|
|
||||||
|
1. **Text Obfuscation**:
|
||||||
|
|
||||||
|
```python
|
||||||
|
obfuscated = zigzag_obfuscation(
|
||||||
|
scramble_words(
|
||||||
|
insert_noise_characters(text)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
1. **Encoding**:
|
||||||
|
|
||||||
|
```python
|
||||||
|
encoded = base64_encode(rot13(text))
|
||||||
|
```
|
||||||
|
|
||||||
|
1. **Randomization**:
|
||||||
|
|
||||||
|
```python
|
||||||
|
randomized = randomize_letter_case(
|
||||||
|
remove_vowels(text)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
- **Noise Frequency**: Configurable in insert_noise_characters()
|
||||||
|
- **Scrambling**: Automatic handling of word lengths
|
||||||
|
- **Case Handling**: Preserved in rotation ciphers
|
||||||
|
|
||||||
|
## Limitations
|
||||||
|
|
||||||
|
- Primarily handles ASCII text
|
||||||
|
- Limited to implemented transformation types
|
||||||
|
- Randomization is not cryptographically secure
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
:root {
|
||||||
|
--md-primary-fg-color: #e92063;
|
||||||
|
--md-primary-fg-color--light: #e92063;
|
||||||
|
--md-primary-fg-color--dark: #e92063;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Revert hue value to that of pre mkdocs-material v9.4.0 */
|
||||||
|
[data-md-color-scheme="slate"] {
|
||||||
|
--md-hue: 230;
|
||||||
|
--md-default-bg-color: hsla(230, 15%, 21%, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.hide {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.text-center {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
img.index-header {
|
||||||
|
width: 70%;
|
||||||
|
max-width: 500px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pydantic-pink {
|
||||||
|
color: #FF007F;
|
||||||
|
}
|
||||||
|
|
||||||
|
.team-blue {
|
||||||
|
color: #0072CE;
|
||||||
|
}
|
||||||
|
|
||||||
|
.secure-green {
|
||||||
|
color: #00A86B;
|
||||||
|
}
|
||||||
|
|
||||||
|
.shapes-orange {
|
||||||
|
color: #FF7F32;
|
||||||
|
}
|
||||||
|
|
||||||
|
.puzzle-purple {
|
||||||
|
color: #652D90;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wheel-gray {
|
||||||
|
color: #6E6E6E;
|
||||||
|
}
|
||||||
|
|
||||||
|
.vertical-middle {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
.text-emphasis {
|
||||||
|
font-size: 1rem;
|
||||||
|
font-weight: 300;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
|
#version-warning {
|
||||||
|
min-height: 120px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mermaid {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Hide the entire footer */
|
||||||
|
.md-footer {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* OR, hide only the "Made with Material" credit */
|
||||||
|
.md-footer__made-with {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
+90
@@ -0,0 +1,90 @@
|
|||||||
|
site_name: Agentic Security
|
||||||
|
repo_url: https://github.com/msoedov/agentic_security
|
||||||
|
site_url: https://msoedov.github.io/agentic_security
|
||||||
|
site_description: Open-source LLM Vulnerability Scanner for safe and reliable AI.
|
||||||
|
site_author: Agentic Security Team
|
||||||
|
edit_uri: edit/main/docs/
|
||||||
|
repo_name: msoedov/agentic_security
|
||||||
|
copyright: Maintained by <a href="https://msoedov.github.io">Agentic Security Team</a>.
|
||||||
|
|
||||||
|
nav:
|
||||||
|
- Home: index.md
|
||||||
|
- Features: probe_data.md
|
||||||
|
- Core Concepts:
|
||||||
|
- Probe Actor: probe_actor.md
|
||||||
|
- Refusal Actor: refusal_classifier_plugins.md
|
||||||
|
- Agent Spec: http_spec.md
|
||||||
|
- Setup:
|
||||||
|
- Installation: installation.md
|
||||||
|
- Getting Started: getting_started.md
|
||||||
|
- Configuration: configuration.md
|
||||||
|
- Advanced Topics:
|
||||||
|
- Dataset Extension: datasets.md
|
||||||
|
- External Modules: external_module.md
|
||||||
|
- CI/CD Integration: ci_cd.md
|
||||||
|
- Bayesian Optimization: optimizer.md
|
||||||
|
- Image Generation: image_generation.md
|
||||||
|
- Stenography Functions: stenography.md
|
||||||
|
- Reinforcement Learning Optimization: rl_model.md
|
||||||
|
- Reference:
|
||||||
|
- API Reference: api_reference.md
|
||||||
|
- Community:
|
||||||
|
- Contributing: contributing.md
|
||||||
|
|
||||||
|
plugins:
|
||||||
|
- search
|
||||||
|
- mkdocstrings:
|
||||||
|
handlers:
|
||||||
|
python:
|
||||||
|
paths: [agentic_security]
|
||||||
|
|
||||||
|
|
||||||
|
footer:
|
||||||
|
links: [] # Removes the default footer credits
|
||||||
|
|
||||||
|
theme:
|
||||||
|
name: material
|
||||||
|
features:
|
||||||
|
- navigation.expand
|
||||||
|
palette:
|
||||||
|
- media: "(prefers-color-scheme: dark)"
|
||||||
|
scheme: default
|
||||||
|
primary: custom
|
||||||
|
accent: deep orange
|
||||||
|
toggle:
|
||||||
|
icon: material/brightness-7
|
||||||
|
name: Switch to dark mode
|
||||||
|
- media: "(prefers-color-scheme: light)"
|
||||||
|
scheme: slate
|
||||||
|
primary: custom
|
||||||
|
accent: deep orange
|
||||||
|
toggle:
|
||||||
|
icon: material/brightness-4
|
||||||
|
name: Switch to light mode
|
||||||
|
icon:
|
||||||
|
repo: fontawesome/brands/github
|
||||||
|
favicon: "https://res.cloudinary.com/dq0w2rtm9/image/upload/v1737555066/r17hrkre246doczwmvbv.png"
|
||||||
|
|
||||||
|
extra:
|
||||||
|
generator: false
|
||||||
|
social:
|
||||||
|
- icon: fontawesome/brands/github
|
||||||
|
link: https://github.com/msoedov/agentic_security
|
||||||
|
- icon: fontawesome/brands/python
|
||||||
|
link: https://pypi.org/project/agentic_security
|
||||||
|
|
||||||
|
extra_css:
|
||||||
|
- stylesheets/extra.css
|
||||||
|
|
||||||
|
markdown_extensions:
|
||||||
|
- toc:
|
||||||
|
permalink: true
|
||||||
|
- pymdownx.arithmatex:
|
||||||
|
generic: true
|
||||||
|
- pymdownx.highlight:
|
||||||
|
anchor_linenums: true
|
||||||
|
line_spans: __span
|
||||||
|
pygments_lang_class: true
|
||||||
|
- pymdownx.inlinehilite
|
||||||
|
- pymdownx.snippets
|
||||||
|
- pymdownx.superfences
|
||||||
Generated
+1698
-234
File diff suppressed because it is too large
Load Diff
+21
-11
@@ -1,13 +1,13 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "agentic_security"
|
name = "agentic_security"
|
||||||
version = "0.4.2"
|
version = "0.4.4"
|
||||||
description = "Agentic LLM vulnerability scanner"
|
description = "Agentic LLM vulnerability scanner"
|
||||||
authors = ["Alexander Miasoiedov <msoedov@gmail.com>"]
|
authors = ["Alexander Miasoiedov <msoedov@gmail.com>"]
|
||||||
maintainers = ["Alexander Miasoiedov <msoedov@gmail.com>"]
|
maintainers = ["Alexander Miasoiedov <msoedov@gmail.com>"]
|
||||||
repository = "https://github.com/msoedov/agentic_security"
|
repository = "https://github.com/msoedov/agentic_security"
|
||||||
homepage = "https://github.com/msoedov/agentic_security"
|
homepage = "https://github.com/msoedov/agentic_security"
|
||||||
documentation = "https://github.com/msoedov/agentic_security/blob/main/README.md"
|
documentation = "https://github.com/msoedov/agentic_security/blob/main/README.md"
|
||||||
license = "MIT"
|
license = "Apache-2.0"
|
||||||
readme = "Readme.md"
|
readme = "Readme.md"
|
||||||
keywords = [
|
keywords = [
|
||||||
"LLM vulnerability scanner",
|
"LLM vulnerability scanner",
|
||||||
@@ -29,7 +29,7 @@ agentic_security = "agentic_security.__main__:main"
|
|||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.11"
|
python = "^3.11"
|
||||||
fastapi = "^0.115.6"
|
fastapi = "^0.115.6"
|
||||||
uvicorn = "^0.32.0"
|
uvicorn = "^0.34.0"
|
||||||
fire = "0.7.0"
|
fire = "0.7.0"
|
||||||
loguru = "^0.7.3"
|
loguru = "^0.7.3"
|
||||||
httpx = "^0.28.1"
|
httpx = "^0.28.1"
|
||||||
@@ -39,9 +39,9 @@ datasets = ">=1.14,<4.0"
|
|||||||
tabulate = ">=0.8.9,<0.10.0"
|
tabulate = ">=0.8.9,<0.10.0"
|
||||||
colorama = "^0.4.4"
|
colorama = "^0.4.4"
|
||||||
matplotlib = "^3.9.2"
|
matplotlib = "^3.9.2"
|
||||||
pydantic = "2.10.4"
|
pydantic = "2.10.6"
|
||||||
scikit-optimize = "^0.10.2"
|
scikit-optimize = "^0.10.2"
|
||||||
scikit-learn = "1.5.2"
|
scikit-learn = "1.6.1"
|
||||||
numpy = ">=1.24.3,<3.0.0"
|
numpy = ">=1.24.3,<3.0.0"
|
||||||
jinja2 = "^3.1.4"
|
jinja2 = "^3.1.4"
|
||||||
python-multipart = "^0.0.20"
|
python-multipart = "^0.0.20"
|
||||||
@@ -51,16 +51,26 @@ rich = "13.9.4"
|
|||||||
|
|
||||||
|
|
||||||
[tool.poetry.group.dev.dependencies]
|
[tool.poetry.group.dev.dependencies]
|
||||||
pytest-asyncio = "^0.24.0"
|
# Pytest
|
||||||
inline-snapshot = ">=0.13.3,<0.19.0"
|
|
||||||
black = "^24.10.0"
|
|
||||||
mypy = "^1.12.0"
|
|
||||||
pytest = "^8.3.4"
|
pytest = "^8.3.4"
|
||||||
pre-commit = "^4.0.1"
|
pytest-asyncio = "^0.25.2"
|
||||||
huggingface-hub = "^0.25.1"
|
inline-snapshot = ">=0.13.3,<0.21.0"
|
||||||
pytest-httpx = "^0.35.0"
|
pytest-httpx = "^0.35.0"
|
||||||
pytest-mock = "^3.14.0"
|
pytest-mock = "^3.14.0"
|
||||||
|
|
||||||
|
# Rest
|
||||||
|
black = ">=24.10,<26.0"
|
||||||
|
mypy = "^1.12.0"
|
||||||
|
pre-commit = "^4.0.1"
|
||||||
|
huggingface-hub = ">=0.25.1,<0.29.0"
|
||||||
|
|
||||||
|
# Docs
|
||||||
|
mkdocs = ">=1.4.2"
|
||||||
|
mkdocs-material = ">=8.5.10"
|
||||||
|
mkdocstrings = ">=0.26.1"
|
||||||
|
mkdocs-jupyter = ">=0.25.1"
|
||||||
|
|
||||||
|
|
||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
line-length = 120
|
line-length = 120
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
# vercel deps
|
|
||||||
fastapi
|
|
||||||
httpx
|
|
||||||
uvicorn
|
|
||||||
tqdm
|
|
||||||
httpx
|
|
||||||
cache_to_disk
|
|
||||||
# datasets
|
|
||||||
loguru
|
|
||||||
pandas
|
|
||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"devCommand": "uvicorn agentic_security.app:app --host 0.0.0.0 --port 3000",
|
|
||||||
"builds": [
|
|
||||||
{
|
|
||||||
"src": "agentic_security/app.py",
|
|
||||||
"use": "@vercel/python"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"routes": [
|
|
||||||
{
|
|
||||||
"src": "/(.*)",
|
|
||||||
"dest": "agentic_security/app.py"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user