-
Notifications
You must be signed in to change notification settings - Fork 2
Added evaluation of multiple validators together #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0bd8545
Added evaluation of multiple validators together
rkritika1508 b0aef5f
Merge branch 'main' into feat/evaluation-multiple-validator-demo
rkritika1508 900510b
resolved comments
rkritika1508 6c4dadc
Merge branch 'main' into feat/evaluation-multiple-validator-demo
rkritika1508 366df52
resolved comments
rkritika1508 6304501
resolved comment
rkritika1508 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "_comment": "Edit this file to configure the evaluation run. All paths are relative to the 'evaluation' directory (i.e. backend/app/evaluation). Add or remove entries in 'validators' to control which validators run and with what settings.", | ||
| "dataset_path": "datasets/multi_validator_whatsapp_dataset.csv", | ||
| "out_path": "outputs/multi_validator_whatsapp/predictions.csv", | ||
| "organization_id": 1, | ||
| "project_id": 1, | ||
| "validators": [ | ||
| { | ||
| "type": "uli_slur_match", | ||
| "severity": "all", | ||
| "on_fail": "fix" | ||
| }, | ||
| { | ||
| "type": "pii_remover", | ||
| "on_fail": "fix" | ||
| }, | ||
| { | ||
| "type": "ban_list", | ||
| "banned_words": ["sonography"], | ||
| "on_fail": "fix" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import json | ||
| from pathlib import Path | ||
| import argparse | ||
| import os | ||
| from uuid import uuid4 | ||
|
|
||
| import httpx | ||
| import pandas as pd | ||
|
|
||
| from app.evaluation.common.helper import write_csv | ||
| from app.load_env import load_environment | ||
|
|
||
| load_environment() | ||
|
|
||
| BASE_DIR = Path(__file__).resolve().parent.parent | ||
|
|
||
| API_URL = os.getenv("GUARDRAILS_API_URL") | ||
| if not API_URL: | ||
| raise ValueError("GUARDRAILS_API_URL environment variable must be set.") | ||
| TIMEOUT_SECONDS = float(os.getenv("GUARDRAILS_TIMEOUT_SECONDS", "60")) | ||
|
|
||
|
|
||
| def load_config(config_path: Path) -> dict: | ||
| with open(config_path) as f: | ||
| return json.load(f) | ||
|
|
||
|
|
||
| def call_guardrails( | ||
| text: str, | ||
| validators_payload: list[dict], | ||
| organization_id: int, | ||
| project_id: int, | ||
| auth_token: str, | ||
| ) -> str: | ||
| headers = {"Content-Type": "application/json"} | ||
| if auth_token: | ||
| headers["Authorization"] = f"Bearer {auth_token}" | ||
|
|
||
| payload = { | ||
| "request_id": str(uuid4()), | ||
| "organization_id": organization_id, | ||
| "project_id": project_id, | ||
| "input": text, | ||
| "validators": validators_payload, | ||
| } | ||
|
|
||
| try: | ||
| response = httpx.post( | ||
| API_URL, | ||
| headers=headers, | ||
| json=payload, | ||
| timeout=TIMEOUT_SECONDS, | ||
| ) | ||
| response.raise_for_status() | ||
| body = response.json() | ||
| safe_text = body.get("data", {}).get("safe_text") | ||
| if safe_text is None: | ||
| return "" | ||
| return str(safe_text) | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except httpx.HTTPError as exc: | ||
| return f"REQUEST_ERROR: {exc}" | ||
| except ValueError as exc: | ||
| return f"JSON_ERROR: {exc}" | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--auth_token", | ||
| required=True, | ||
| help="Bearer token value (without the 'Bearer ' prefix).", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| config = load_config(Path(__file__).resolve().parent / "config.json") | ||
|
|
||
| dataset_path = BASE_DIR / config["dataset_path"] | ||
| out_path = BASE_DIR / config["out_path"] | ||
| organization_id = config["organization_id"] | ||
| project_id = config["project_id"] | ||
| validators_payload = config["validators"] | ||
|
|
||
| if not validators_payload: | ||
| raise ValueError("No validators defined in config.") | ||
|
|
||
| df = pd.read_csv(dataset_path) | ||
|
|
||
| rows = [] | ||
| for _, row in df.iterrows(): | ||
| source_text = str(row.get("Text", "")) | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| safe_text = call_guardrails( | ||
| source_text, | ||
| validators_payload, | ||
| organization_id, | ||
| project_id, | ||
| args.auth_token, | ||
| ) | ||
|
|
||
| rows.append( | ||
| { | ||
| "ID": row.get("ID"), | ||
| "text": source_text, | ||
| "validators_present": row.get("Validators_present", ""), | ||
| "response": safe_text, | ||
| } | ||
| ) | ||
|
|
||
| out_df = pd.DataFrame( | ||
| rows, columns=["ID", "text", "validators_present", "response"] | ||
| ) | ||
| write_csv(out_df, out_path) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: ProjectTech4DevAI/kaapi-guardrails
Length of output: 10166
Remove quotes for consistency with other environment variables.
The value should not be wrapped in double quotes. For consistency with
API_BASE_URLon line 9 and to address the static analysis warning, remove the quotes:📝 Committable suggestion
🧰 Tools
🪛 dotenv-linter (4.0.0)
[warning] 32-32: [QuoteCharacter] The value has quote characters (', ")
(QuoteCharacter)
🤖 Prompt for AI Agents