langalf


Namelangalf JSON
Version 0.0.4 PyPI version JSON
download
home_pagehttps://github.com/msoedov/langalf
SummaryAgentic LLM vulnerability scanner
upload_time2024-04-15 12:40:16
maintainerAlexander Miasoiedov
docs_urlNone
authorAlexander Miasoiedov
requires_python<4.0,>=3.9
licenseMIT
keywords llm vulnerability scanner llm security llm adversarial attacks prompt injection prompt leakage prompt injection attacks prompt leakage prevention llm vulnerabilities owasp-llm-top-10
VCS
bugtrack_url
requirements fastapi httpx uvicorn tqdm httpx cache_to_disk loguru pandas
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
  <a href="https://github.com/msoedov/langalf">
   <img src="https://res.cloudinary.com/do9qa2bqr/image/upload/v1713002571/OIG1_bkbr0d.jpg" height=100 alt="Logo">
  </a>

<h1 align="center">Langalf</h1>

<p align="center">
    The open-source Agentic LLM Vulnerability Scanner .
    <br />
    <a href="#features"><strong>Learn more ยป</strong></a>
    <br />
    <br />

<p>
<img alt="GitHub Contributors" src="https://img.shields.io/github/contributors/msoedov/langalf" />
<img alt="GitHub Last Commit" src="https://img.shields.io/github/last-commit/msoedov/langalf" />
<img alt="" src="https://img.shields.io/github/repo-size/msoedov/langalf" />
<img alt="Downloads" src="https://static.pepy.tech/badge/langalf" />
<img alt="GitHub Issues" src="https://img.shields.io/github/issues/msoedov/langalf" />
<img alt="GitHub Pull Requests" src="https://img.shields.io/github/issues-pr/msoedov/langalf" />
<img alt="Github License" src="https://img.shields.io/github/license/msoedov/langalf" />
</p>
  </p>
</p>

## About the Project ๐Ÿง™

<img width="100%" alt="booking-screen" src="https://res.cloudinary.com/do9qa2bqr/image/upload/v1713002396/1-ezgif.com-video-to-gif-converter_s2hsro.gif">

<p align="center"></p>
<h3 align="center">LLM threat vectors scanner</h3>

|   |   |
| --- | --- |
| <b>Prebuilt Datasets of Prompts</b><br /><br /><br/><b>Focused on OWASP top 10 LLM</b><br /><br /><br /><b>Integration under 1 min</b><br />| <img src="https://res.cloudinary.com/do9qa2bqr/image/upload/v1713002416/12-ezgif.com-video-to-gif-converter_jspzmx.gif" /> |

## Features

 - Comprehensive Threat Detection ๐Ÿ›ก๏ธ: Scans for a wide array of LLM vulnerabilities including prompt injection, jailbreaking, hallucinations, biases, and other malicious exploitation attempts.
 - OWASP Top 10 for LLMs scan: to test the list of the most critical LLM vulnerabilities.
 - Privacy-centric Architecture ๐Ÿ”’: Ensures that all data scanning and analysis occur on-premise or in a local environment, with no external data transmission, maintaining strict data privacy.
 - Comprehensive Reporting Tools ๐Ÿ“Š: Offers detailed reports of vulnerability, helping teams to quickly understand and respond to security incidents.
 - Customizable Rule Sets ๐Ÿ› ๏ธ: Allows users to define custom attack rules and parameters to meet specific prompt attacks needs and compliance standards.



Note: Please be aware that Langalf is designed as a safety scanner tool and not a foolproof solution. It cannot guarantee complete protection against all possible threats.


## ๐Ÿ“ฆ Installation

To get started with Langalf, simply install the package using pip:

```shell
pip install langalf
```

## โ›“๏ธ Quick Start

```shell
langalf

2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:273 - Found 1 CSV files
2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:274 - CSV files: ['prompts.csv']
INFO:     Started server process [18524]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8718 (Press CTRL+C to quit)
```

```shell
python -m langalf
# or
langalf --help


langalf --port=PORT --host=HOST

```


## LLM kwargs

Langalf uses plain text HTTP spec like:

```http
POST https://api.openai.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
}

```

Where `<<PROMPT>>` will be replaced with the actual attack vector during the scan, insert the `Bearer XXXXX` header value with your app credentials.


### Adding LLM integration templates

TBD
```
....
```
## Adding own dataset

To add your own dataset you can place one or multiples csv files with `prompt` column, this data will be loaded on `langalf` startup

```
2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:273 - Found 1 CSV files
2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:274 - CSV files: ['prompts.csv']
```

## Extending dataset collections

1. Add new metadata to langalf.probe_data.REGISTRY
```python
    {
        "dataset_name": "markush1/LLM-Jailbreak-Classifier",
        "num_prompts": 1119,
        "tokens": 19758,
        "approx_cost": 0.0,
        "source": "Hugging Face Datasets",
        "selected": True,
        "dynamic": False,
        "url": "https://huggingface.co/markush1/LLM-Jailbreak-Classifier",
    },
```

and implement loader into


```python
@dataclass
class ProbeDataset:
    dataset_name: str
    metadata: dict
    prompts: list[str]
    tokens: int
    approx_cost: float

    def metadata_summary(self):
        return {
            "dataset_name": self.dataset_name,
            "num_prompts": len(self.prompts),
            "tokens": self.tokens,
            "approx_cost": self.approx_cost,
        }

```

## Dynamic datasets with mutations

One of the current examples uses sampling for the existing preloaded prompt data and applying mutations yielding a new dataset

```python
class Stenography:
    fn_library = {
        "rot5": stenography_fn.rot5,
        "rot13": stenography_fn.rot13,
        "base64": stenography_fn.base64_encode,
        "mirror": stenography_fn.mirror_words,
        "random_case": stenography_fn.randomize_letter_case,
        "scramble_words": stenography_fn.scramble_words,
        "noise_characters": stenography_fn.insert_noise_characters,
        "substitute_with_ascii": stenography_fn.substitute_with_ascii,
        "remove_vowels": stenography_fn.remove_vowels,
        "zigzag_obfuscation": stenography_fn.zigzag_obfuscation,
    }

    def __init__(self, prompt_groups: [ProbeDataset]):
        self.prompt_groups = prompt_groups

    def apply(self):
        for prompt_group in self.prompt_groups:

            size = len(prompt_group.prompts)
            for name, fn in self.fn_library.items():
                logger.info(f"Applying {name} to {prompt_group.dataset_name}")
                selected_prompts = random.sample(prompt_group.prompts, int(size * 0.25))
                prompts = [
                    f"Follow the instructions in {name}: {fn(p)}"
                    for p in selected_prompts
                ]
                yield ProbeDataset(
                    dataset_name=f"stenography.{name}({prompt_group.dataset_name})",
                    metadata={},
                    prompts=prompts,
                    tokens=count_words_in_list(prompts),
                    approx_cost=0.0,
                )
```
## Probe endpoint

In the example of custom integration, we use `/v1/self-probe` for the sake of integration testing.


```python
POST https://langalf-preview.vercel.app/v1/self-probe
Authorization: Bearer XXXXX
Content-Type: application/json

{
    "prompt": "<<PROMPT>>"
}

```
This endpoint randomly mimics the refusal of a fake LLM.

```python
@app.post("/v1/self-probe")
def self_probe(probe: Probe):
    refuse = random.random() < 0.2
    message = random.choice(REFUSAL_MARKS) if refuse else "This is a test!"
    message = probe.prompt + " " + message
    return {
        "id": "chatcmpl-abc123",
        "object": "chat.completion",
        "created": 1677858242,
        "model": "gpt-3.5-turbo-0613",
        "usage": {"prompt_tokens": 13, "completion_tokens": 7, "total_tokens": 20},
        "choices": [
            {
                "message": {"role": "assistant", "content": message},
                "logprobs": None,
                "finish_reason": "stop",
                "index": 0,
            }
        ],
    }

```

## CI/CD integration

TBD

## Documentation

For more detailed information on how to use Langalf, including advanced features and customization options, please refer to the official documentation.

## Roadmap and Future Goals

- [ ] Expand dataset variety
- [ ] Introduce two new attack vectors
- [ ] Develop initial attacker LLM
- [ ] Complete integration of OWASP Top 10 classification

Note: All dates are tentative and subject to change based on project progress and priorities.



## ๐Ÿ‘‹ Contributing

Contributions to Langalf are welcome! If you'd like to contribute, please follow these steps:

- Fork the repository on GitHub
- Create a new branch for your changes
- Commit your changes to the new branch
- Push your changes to the forked repository
- Open a pull request to the main Langalf repository

Before contributing, please read the contributing guidelines.

## License

Langalf is released under the Apache License v2.

## Contact us

## ๐Ÿค Schedule a 1-on-1 Session

<a href="https://cal.com/alexander-myasoedov-go2tfs/30min"><img src="https://cal.com/book-with-cal-dark.svg" alt="Book us with Cal.com"></a>

Book a 1-on-1 Session with the founders, to discuss any issues, provide feedback, or explore how we can improve langalf for you.

## Repo Activity

<img width="100%" src="https://repobeats.axiom.co/api/embed/2b4b4e080d21ef9174ca69bcd801145a71f67aaf.svg" />

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/msoedov/langalf",
    "name": "langalf",
    "maintainer": "Alexander Miasoiedov",
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": "msoedov@gmail.com",
    "keywords": "LLM vulnerability scanner, llm security, llm adversarial attacks, prompt injection, prompt leakage, prompt injection attacks, prompt leakage prevention, llm vulnerabilities, owasp-llm-top-10",
    "author": "Alexander Miasoiedov",
    "author_email": "msoedov@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/96/50/f752856e34f0ebc8ec65252c95ccbab83b0444fd97205fbdefe581c23019/langalf-0.0.4.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n  <a href=\"https://github.com/msoedov/langalf\">\n   <img src=\"https://res.cloudinary.com/do9qa2bqr/image/upload/v1713002571/OIG1_bkbr0d.jpg\" height=100 alt=\"Logo\">\n  </a>\n\n<h1 align=\"center\">Langalf</h1>\n\n<p align=\"center\">\n    The open-source Agentic LLM Vulnerability Scanner .\n    <br />\n    <a href=\"#features\"><strong>Learn more \u00bb</strong></a>\n    <br />\n    <br />\n\n<p>\n<img alt=\"GitHub Contributors\" src=\"https://img.shields.io/github/contributors/msoedov/langalf\" />\n<img alt=\"GitHub Last Commit\" src=\"https://img.shields.io/github/last-commit/msoedov/langalf\" />\n<img alt=\"\" src=\"https://img.shields.io/github/repo-size/msoedov/langalf\" />\n<img alt=\"Downloads\" src=\"https://static.pepy.tech/badge/langalf\" />\n<img alt=\"GitHub Issues\" src=\"https://img.shields.io/github/issues/msoedov/langalf\" />\n<img alt=\"GitHub Pull Requests\" src=\"https://img.shields.io/github/issues-pr/msoedov/langalf\" />\n<img alt=\"Github License\" src=\"https://img.shields.io/github/license/msoedov/langalf\" />\n</p>\n  </p>\n</p>\n\n## About the Project \ud83e\uddd9\n\n<img width=\"100%\" alt=\"booking-screen\" src=\"https://res.cloudinary.com/do9qa2bqr/image/upload/v1713002396/1-ezgif.com-video-to-gif-converter_s2hsro.gif\">\n\n<p align=\"center\"></p>\n<h3 align=\"center\">LLM threat vectors scanner</h3>\n\n|   |   |\n| --- | --- |\n| <b>Prebuilt Datasets of Prompts</b><br /><br /><br/><b>Focused on OWASP top 10 LLM</b><br /><br /><br /><b>Integration under 1 min</b><br />| <img src=\"https://res.cloudinary.com/do9qa2bqr/image/upload/v1713002416/12-ezgif.com-video-to-gif-converter_jspzmx.gif\" /> |\n\n## Features\n\n - Comprehensive Threat Detection \ud83d\udee1\ufe0f: Scans for a wide array of LLM vulnerabilities including prompt injection, jailbreaking, hallucinations, biases, and other malicious exploitation attempts.\n - OWASP Top 10 for LLMs scan: to test the list of the most critical LLM vulnerabilities.\n - Privacy-centric Architecture \ud83d\udd12: Ensures that all data scanning and analysis occur on-premise or in a local environment, with no external data transmission, maintaining strict data privacy.\n - Comprehensive Reporting Tools \ud83d\udcca: Offers detailed reports of vulnerability, helping teams to quickly understand and respond to security incidents.\n - Customizable Rule Sets \ud83d\udee0\ufe0f: Allows users to define custom attack rules and parameters to meet specific prompt attacks needs and compliance standards.\n\n\n\nNote: Please be aware that Langalf is designed as a safety scanner tool and not a foolproof solution. It cannot guarantee complete protection against all possible threats.\n\n\n## \ud83d\udce6 Installation\n\nTo get started with Langalf, simply install the package using pip:\n\n```shell\npip install langalf\n```\n\n## \u26d3\ufe0f Quick Start\n\n```shell\nlangalf\n\n2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:273 - Found 1 CSV files\n2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:274 - CSV files: ['prompts.csv']\nINFO:     Started server process [18524]\nINFO:     Waiting for application startup.\nINFO:     Application startup complete.\nINFO:     Uvicorn running on http://0.0.0.0:8718 (Press CTRL+C to quit)\n```\n\n```shell\npython -m langalf\n# or\nlangalf --help\n\n\nlangalf --port=PORT --host=HOST\n\n```\n\n\n## LLM kwargs\n\nLangalf uses plain text HTTP spec like:\n\n```http\nPOST https://api.openai.com/v1/chat/completions\nAuthorization: Bearer sk-xxxxxxxxx\nContent-Type: application/json\n\n{\n     \"model\": \"gpt-3.5-turbo\",\n     \"messages\": [{\"role\": \"user\", \"content\": \"<<PROMPT>>\"}],\n     \"temperature\": 0.7\n}\n\n```\n\nWhere `<<PROMPT>>` will be replaced with the actual attack vector during the scan, insert the `Bearer XXXXX` header value with your app credentials.\n\n\n### Adding LLM integration templates\n\nTBD\n```\n....\n```\n## Adding own dataset\n\nTo add your own dataset you can place one or multiples csv files with `prompt` column, this data will be loaded on `langalf` startup\n\n```\n2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:273 - Found 1 CSV files\n2024-04-13 13:21:31.157 | INFO     | langalf.probe_data.data:load_local_csv:274 - CSV files: ['prompts.csv']\n```\n\n## Extending dataset collections\n\n1. Add new metadata to langalf.probe_data.REGISTRY\n```python\n    {\n        \"dataset_name\": \"markush1/LLM-Jailbreak-Classifier\",\n        \"num_prompts\": 1119,\n        \"tokens\": 19758,\n        \"approx_cost\": 0.0,\n        \"source\": \"Hugging Face Datasets\",\n        \"selected\": True,\n        \"dynamic\": False,\n        \"url\": \"https://huggingface.co/markush1/LLM-Jailbreak-Classifier\",\n    },\n```\n\nand implement loader into\n\n\n```python\n@dataclass\nclass ProbeDataset:\n    dataset_name: str\n    metadata: dict\n    prompts: list[str]\n    tokens: int\n    approx_cost: float\n\n    def metadata_summary(self):\n        return {\n            \"dataset_name\": self.dataset_name,\n            \"num_prompts\": len(self.prompts),\n            \"tokens\": self.tokens,\n            \"approx_cost\": self.approx_cost,\n        }\n\n```\n\n## Dynamic datasets with mutations\n\nOne of the current examples uses sampling for the existing preloaded prompt data and applying mutations yielding a new dataset\n\n```python\nclass Stenography:\n    fn_library = {\n        \"rot5\": stenography_fn.rot5,\n        \"rot13\": stenography_fn.rot13,\n        \"base64\": stenography_fn.base64_encode,\n        \"mirror\": stenography_fn.mirror_words,\n        \"random_case\": stenography_fn.randomize_letter_case,\n        \"scramble_words\": stenography_fn.scramble_words,\n        \"noise_characters\": stenography_fn.insert_noise_characters,\n        \"substitute_with_ascii\": stenography_fn.substitute_with_ascii,\n        \"remove_vowels\": stenography_fn.remove_vowels,\n        \"zigzag_obfuscation\": stenography_fn.zigzag_obfuscation,\n    }\n\n    def __init__(self, prompt_groups: [ProbeDataset]):\n        self.prompt_groups = prompt_groups\n\n    def apply(self):\n        for prompt_group in self.prompt_groups:\n\n            size = len(prompt_group.prompts)\n            for name, fn in self.fn_library.items():\n                logger.info(f\"Applying {name} to {prompt_group.dataset_name}\")\n                selected_prompts = random.sample(prompt_group.prompts, int(size * 0.25))\n                prompts = [\n                    f\"Follow the instructions in {name}: {fn(p)}\"\n                    for p in selected_prompts\n                ]\n                yield ProbeDataset(\n                    dataset_name=f\"stenography.{name}({prompt_group.dataset_name})\",\n                    metadata={},\n                    prompts=prompts,\n                    tokens=count_words_in_list(prompts),\n                    approx_cost=0.0,\n                )\n```\n## Probe endpoint\n\nIn the example of custom integration, we use `/v1/self-probe` for the sake of integration testing.\n\n\n```python\nPOST https://langalf-preview.vercel.app/v1/self-probe\nAuthorization: Bearer XXXXX\nContent-Type: application/json\n\n{\n    \"prompt\": \"<<PROMPT>>\"\n}\n\n```\nThis endpoint randomly mimics the refusal of a fake LLM.\n\n```python\n@app.post(\"/v1/self-probe\")\ndef self_probe(probe: Probe):\n    refuse = random.random() < 0.2\n    message = random.choice(REFUSAL_MARKS) if refuse else \"This is a test!\"\n    message = probe.prompt + \" \" + message\n    return {\n        \"id\": \"chatcmpl-abc123\",\n        \"object\": \"chat.completion\",\n        \"created\": 1677858242,\n        \"model\": \"gpt-3.5-turbo-0613\",\n        \"usage\": {\"prompt_tokens\": 13, \"completion_tokens\": 7, \"total_tokens\": 20},\n        \"choices\": [\n            {\n                \"message\": {\"role\": \"assistant\", \"content\": message},\n                \"logprobs\": None,\n                \"finish_reason\": \"stop\",\n                \"index\": 0,\n            }\n        ],\n    }\n\n```\n\n## CI/CD integration\n\nTBD\n\n## Documentation\n\nFor more detailed information on how to use Langalf, including advanced features and customization options, please refer to the official documentation.\n\n## Roadmap and Future Goals\n\n- [ ] Expand dataset variety\n- [ ] Introduce two new attack vectors\n- [ ] Develop initial attacker LLM\n- [ ] Complete integration of OWASP Top 10 classification\n\nNote: All dates are tentative and subject to change based on project progress and priorities.\n\n\n\n## \ud83d\udc4b Contributing\n\nContributions to Langalf are welcome! If you'd like to contribute, please follow these steps:\n\n- Fork the repository on GitHub\n- Create a new branch for your changes\n- Commit your changes to the new branch\n- Push your changes to the forked repository\n- Open a pull request to the main Langalf repository\n\nBefore contributing, please read the contributing guidelines.\n\n## License\n\nLangalf is released under the Apache License v2.\n\n## Contact us\n\n## \ud83e\udd1d Schedule a 1-on-1 Session\n\n<a href=\"https://cal.com/alexander-myasoedov-go2tfs/30min\"><img src=\"https://cal.com/book-with-cal-dark.svg\" alt=\"Book us with Cal.com\"></a>\n\nBook a 1-on-1 Session with the founders, to discuss any issues, provide feedback, or explore how we can improve langalf for you.\n\n## Repo Activity\n\n<img width=\"100%\" src=\"https://repobeats.axiom.co/api/embed/2b4b4e080d21ef9174ca69bcd801145a71f67aaf.svg\" />\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Agentic LLM vulnerability scanner",
    "version": "0.0.4",
    "project_urls": {
        "Homepage": "https://github.com/msoedov/langalf",
        "Repository": "https://github.com/msoedov/langalf"
    },
    "split_keywords": [
        "llm vulnerability scanner",
        " llm security",
        " llm adversarial attacks",
        " prompt injection",
        " prompt leakage",
        " prompt injection attacks",
        " prompt leakage prevention",
        " llm vulnerabilities",
        " owasp-llm-top-10"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9650f752856e34f0ebc8ec65252c95ccbab83b0444fd97205fbdefe581c23019",
                "md5": "664fdb95fe7d938ab943dd674f2d0318",
                "sha256": "909841262fd5063e9cf690bb078e034161ab70347ce900bcf1bfcf95eb59e37e"
            },
            "downloads": -1,
            "filename": "langalf-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "664fdb95fe7d938ab943dd674f2d0318",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 25360,
            "upload_time": "2024-04-15T12:40:16",
            "upload_time_iso_8601": "2024-04-15T12:40:16.490269Z",
            "url": "https://files.pythonhosted.org/packages/96/50/f752856e34f0ebc8ec65252c95ccbab83b0444fd97205fbdefe581c23019/langalf-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-15 12:40:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "msoedov",
    "github_project": "langalf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "fastapi",
            "specs": []
        },
        {
            "name": "httpx",
            "specs": []
        },
        {
            "name": "uvicorn",
            "specs": []
        },
        {
            "name": "tqdm",
            "specs": []
        },
        {
            "name": "httpx",
            "specs": []
        },
        {
            "name": "cache_to_disk",
            "specs": []
        },
        {
            "name": "loguru",
            "specs": []
        },
        {
            "name": "pandas",
            "specs": []
        }
    ],
    "lcname": "langalf"
}
        
Elapsed time: 3.76338s