Skip to content

Commit

Permalink
feat(Improve garak error handling):
Browse files Browse the repository at this point in the history
  • Loading branch information
msoedov committed Apr 29, 2024
1 parent 1fbcde8 commit 58643f9
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions agentic_security/probe_data/modules/garak_tool.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import subprocess
import os
import asyncio
import importlib.util
import os
import subprocess

from loguru import logger
import asyncio

# TODO: add probes modules


class Module:

def __init__(self, prompt_groups: [], tools_inbox: asyncio.Queue):
self.tools_inbox = tools_inbox
if not self.is_garak_installed():
logger.error(
"Garak module is not installed. Please install it using 'pip install garak'"
)

def is_garak_installed(self) -> bool:
garak_spec = importlib.util.find_spec("garak")
return garak_spec is not None

async def apply(self) -> []:
env = os.environ.copy()
env["OPENAI_API_BASE"] = "http://0.0.0.0:8718/proxy"

# Command to be executed
command = [
"python3",
"python",
"-m",
"garak",
"--model_type",
Expand All @@ -32,7 +43,7 @@ async def apply(self) -> []:
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env
)
out, err = await asyncio.to_thread(process.communicate)

yield "Started"
is_empty = self.tools_inbox.empty()
logger.info(f"Is inbox empty? {is_empty}")
while not self.tools_inbox.empty():
Expand All @@ -43,3 +54,7 @@ async def apply(self) -> []:
logger.info("Garak tool finished.")
logger.info(f"stdout: {out}")
logger.error(f"exit code: {process.returncode}")
if process.returncode != 0:
logger.error(f"Error executing command: {command}")
logger.error(f"err: {err}")
return

0 comments on commit 58643f9

Please sign in to comment.