Skip to content

Commit

Permalink
Setup update (#124)
Browse files Browse the repository at this point in the history
* style(makefile): 🎨 create dynamically generated help target and move phony to top of each target

    This aids in clear target expectations and keeping track of missing descriptions as Clear target was missing from help.

* style(lint): 🧱 configure black and isort in pyproject.toml

    This allows running black/isort in pre-commit or locally to use the same settings and user have the same experience.

* style(markdown): 🚨 add alt text to resolve updated markdownlint img rule

* style(black): 🚨 black format via version 24.2.0
  • Loading branch information
CandiedCode authored Apr 25, 2024
1 parent b90ae1a commit d4639f9
Show file tree
Hide file tree
Showing 33 changed files with 93 additions and 49 deletions.
32 changes: 17 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: end-of-file-fixer
types: [ python ]
- id: requirements-txt-fixer

- repo: https://github.com/psf/black
rev: 23.7.0
- repo: local
hooks:
- id: black
args: [ --line-length=100, --exclude="" ]

# this is not technically always safe but usually is
# use comments `# isort: off` and `# isort: on` to disable/re-enable isort
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
name: black
entry: black
language: system
pass_filenames: true
types: [ python ]
# this is not technically always safe but usually is
# use comments `# isort: off` and `# isort: on` to disable/re-enable isort
- id: isort
args: [ --line-length=100, --profile=black ]
name: isort
entry: isort
language: system
pass_filenames: true
types: [ python ]

# this is slightly dangerous because python imports have side effects
# and this tool removes unused imports, which may be providing
# necessary side effects for the code to run
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.0
rev: v2.3.1
hooks:
- id: autoflake
args:
Expand All @@ -39,11 +41,11 @@ repos:
exclude: "llm_guard/__init__.py"

- repo: https://github.com/zricethezav/gitleaks
rev: v8.17.0
rev: v8.18.2
hooks:
- id: gitleaks

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.35.0
rev: v0.39.0
hooks:
- id: markdownlint
46 changes: 28 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
.PHONY: help lint test build docs-serve install-dev publish clean
.DEFAULT_GOAL := help

help:
@echo "Available commands:"
@echo "make lint - Check code with linters using pre-commit."
@echo "make test - Run unit tests using pytest."
@echo "make build - Build the package for PyPI."
@echo "make docs-serve - Serve documentation using mkdocs."
@echo "make install-dev - Install development dependencies."
@echo "make publish - Publish to PyPI."

install-dev:
.PHONY: install-dev
install-dev: ## Install development dependencies.
@echo "Installing development dependencies..."
@python -m pip install ".[dev]"
@python -m pre-commit install
@pre-commit install

lint:
.PHONY: lint
lint: ## Check code with linters using pre-commit.
@echo "Running linters..."
@pre-commit run --all-files

test:
.PHONY: test
test: ## Run unit tests using pytest.
@echo "Running tests..."
@pytest --exitfirst --verbose --failed-first --cov=.

build:
.PHONY: build
build: ## Build the package for PyPI.
@echo "Building for PyPI..."
@python -m pip install --upgrade build
@python -m build

publish:
.PHONY: publish
publish: ## Publish to PyPI.
@echo "Publishing to PyPI..."
@python -m pip install --upgrade twine
@python -m twine upload --repository llm-guard dist/*

docs-serve:
.PHONY: docs-serve
docs-serve: ## Serve documentation using mkdocs.
@echo "Serving documentation..."
@mkdocs serve -a localhost:8085

clean:
.PHONY: clean
clean: ## Clean and Remove build files and pytest cache.
@echo "Cleaning up..."
@rm -rf build dist .pytest_cache .egg-info llm_guard.egg-info

.PHONY: help
help: ## List all targets and help information.
@echo "Available commands:"
@grep --no-filename -E '^([a-z.A-Z_%-/]+:.*?)##' $(MAKEFILE_LIST) | sort | \
awk 'BEGIN {FS = ":.*?(## ?)"}; { \
if (length($$1) > 0) { \
printf " \033[36m%-30s\033[0m %s\n", $$1, $$2; \
} else { \
printf "%s\n", $$2; \
} \
}'
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stars](https://img.shields.io/github/stars/protectai/llm-guard.svg?style=social&
[![Downloads](https://static.pepy.tech/badge/llm-guard)](https://pepy.tech/project/llm-guard)
[![Downloads](https://static.pepy.tech/badge/llm-guard/month)](https://pepy.tech/project/llm-guard)

<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="https://github.com/protectai/llm-guard/blob/main/docs/assets/join-our-slack-community.png?raw=true" width="200"></a>
<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="https://github.com/protectai/llm-guard/blob/main/docs/assets/join-our-slack-community.png?raw=true" width="200" alt="Join Our Slack Community"></a>

## What is LLM Guard?

Expand Down Expand Up @@ -108,7 +108,7 @@ we would love to have you as part of our community.
Join our Slack to give us feedback, connect with the maintainers and fellow users, ask questions,
get help for package usage or contributions, or engage in discussions about LLM security!

<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="https://github.com/protectai/llm-guard/blob/main/docs/assets/join-our-slack-community.png?raw=true" width="200"></a>
<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="https://github.com/protectai/llm-guard/blob/main/docs/assets/join-our-slack-community.png?raw=true" width="200" alt="Join Our Slack Community"></a>

### Production Support

Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LLM Guard by [Protect AI](https://protectai.com/llm-guard) is a comprehensive to

[**Playground**](https://huggingface.co/spaces/ProtectAI/llm-guard-playground) | [**Changelog**](./changelog.md)

<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="./assets/join-our-slack-community.png" width="200"></a>
<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="./assets/join-our-slack-community.png" width="200" alt="Join Our Slack Community"></a>

## What is LLM Guard?

Expand Down Expand Up @@ -55,4 +55,4 @@ we would love to have you as part of our community.
Join our Slack to give us feedback, connect with the maintainers and fellow users, ask questions,
get help for package usage or contributions, or engage in discussions about LLM security!

<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="./assets/join-our-slack-community.png" width="200"></a>
<a href="https://join.slack.com/t/laiyerai/shared_invite/zt-28jv3ci39-sVxXrLs3rQdaN3mIl9IT~w"><img src="./assets/join-our-slack-community.png" width="200" alt="Join Our Slack Community"></a>
1 change: 1 addition & 0 deletions llm_guard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""LLM Guard package"""

from .evaluate import scan_output, scan_prompt
1 change: 1 addition & 0 deletions llm_guard/input_scanners/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Input scanners init"""

from .anonymize import Anonymize
from .ban_code import BanCode
from .ban_competitors import BanCompetitors
Expand Down
8 changes: 5 additions & 3 deletions llm_guard/input_scanners/anonymize.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,11 @@ def scan(self, prompt: str) -> (str, bool, float):
)

risk_score = round(
max(analyzer_result.score for analyzer_result in analyzer_results)
if analyzer_results
else 0.0,
(
max(analyzer_result.score for analyzer_result in analyzer_results)
if analyzer_results
else 0.0
),
2,
)
analyzer_results = self._remove_conflicts_and_get_text_manipulation_data(analyzer_results)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ def _load_pipeline(
self.model.onnx_path,
export=False,
subfolder=self.model.onnx_subfolder,
provider="CUDAExecutionProvider"
if device().type == "cuda"
else "CPUExecutionProvider",
provider=(
"CUDAExecutionProvider" if device().type == "cuda" else "CPUExecutionProvider"
),
revision=self.model.onnx_revision,
file_name=self.model.onnx_filename,
use_io_binding=True if device().type == "cuda" else False,
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/adafruit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Adafruit keys
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/adobe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Adobe keys
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/age_secret_key.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Age secret keys
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Airtable API keys
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Algolia API keys
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/alibaba.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Alibaba secrets
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/asana.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Asana secrets
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Atlassian API tokens
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Authress Service Client Access Keys
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Beamer API tokens
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/bitbucket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Bitbucket Client ID and Client Secret
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/bittrex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Bittrex Access Key and Secret Key
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Clojars API tokens
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Codecov Access Token
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Coinbase Access Token
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/confluent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Confluent Access Token and Confluent Secret Key
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Contentful delivery API token.
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Databricks API token.
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Datadog Access Tokens.
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for Defined Networking API Tokens.
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/input_scanners/secrets_plugins/github_token.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This plugin searches for GitHub tokens
"""

import re

from detect_secrets.plugins.base import RegexBasedDetector
Expand Down
1 change: 1 addition & 0 deletions llm_guard/output_scanners/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""LLM output scanners init"""

from .ban_code import BanCode
from .ban_competitors import BanCompetitors
from .ban_substrings import BanSubstrings
Expand Down
6 changes: 3 additions & 3 deletions llm_guard/output_scanners/relevance.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def __init__(
subfolder=model.onnx_subfolder,
file_name=model.onnx_filename,
revision=model.onnx_revision,
provider="CUDAExecutionProvider"
if device().type == "cuda"
else "CPUExecutionProvider",
provider=(
"CUDAExecutionProvider" if device().type == "cuda" else "CPUExecutionProvider"
),
use_io_binding=True if device().type == "cuda" else False,
**model.kwargs,
)
Expand Down
Loading

0 comments on commit d4639f9

Please sign in to comment.