1919 visualization ,
2020)
2121
22+ API_REPORT_PROMPT_INJECTIONS = "v1/report/prompt_injections"
23+
2224
2325class SupportedLLMs :
2426 CHATGPT = "chatgpt"
@@ -99,8 +101,11 @@ def __init__(self, config: ZenGuardConfig):
99101 raise ValueError (f"LLM { config .llm } is not supported" )
100102
101103 def detect (self , detectors : list [Detector ], prompt : str ):
104+ """
105+ Uses detectors to evaluate the prompt and return the results.
106+ """
102107 if len (detectors ) == 0 :
103- return { "error" : " No detectors were provided"}
108+ raise ValueError ( " No detectors were provided")
104109
105110 url = self ._backend
106111 if len (detectors ) == 1 :
@@ -118,15 +123,48 @@ def detect(self, detectors: list[Detector], prompt: str):
118123 timeout = 20 ,
119124 )
120125 except httpx .RequestError as e :
121- print (e )
122- return {"error" : str (e )}
123-
124- if response .status_code != 200 :
125- print (response .json ())
126- return {"error" : str (response .json ())}
126+ raise RuntimeError (
127+ f"An error occurred while making the request: { str (e )} "
128+ ) from e
129+ except httpx .HTTPStatusError as e :
130+ raise RuntimeError (
131+ f"Received an unexpected status code: { response .status_code } \n Response content: { response .json ()} "
132+ ) from e
127133
128134 return response .json ()
129135
136+ def detect_async (self , detectors : list [Detector ], prompt : str ):
137+ """
138+ Same as detect function but asynchroneous.
139+ """
140+ if len (detectors ) == 0 :
141+ raise ValueError ("No detectors were provided" )
142+
143+ if detectors [0 ] != Detector .PROMPT_INJECTION :
144+ raise ValueError (
145+ "Only Prompt Injection detector is supported for async detection"
146+ )
147+
148+ url = self ._backend + convert_detector_to_api (detectors [0 ]) + "_async"
149+ json = {"messages" : [prompt ]}
150+
151+ try :
152+ response = httpx .post (
153+ url ,
154+ json = json ,
155+ headers = {"x-api-key" : self ._api_key },
156+ timeout = 20 ,
157+ )
158+ response .raise_for_status ()
159+ except httpx .RequestError as e :
160+ raise RuntimeError (
161+ f"An error occurred while making the request: { str (e )} "
162+ ) from e
163+ except httpx .HTTPStatusError as e :
164+ raise RuntimeError (
165+ f"Received an unexpected status code: { response .status_code } \n Response content: { response .json ()} "
166+ ) from e
167+
130168 def _attack_zenguard (self , detector : Detector , attacks : list [str ]):
131169 attacks = tqdm (attacks )
132170 for attack in attacks :
@@ -171,3 +209,40 @@ def update_detectors(self, detectors: list[Detector]):
171209
172210 if response .status_code != 200 :
173211 return {"error" : str (response .json ())}
212+
213+ def report (self , detector : Detector , days : int = None ):
214+ """
215+ Get a report of the detections made by the detector in the last days.
216+ Days is optional and if not provided, it will return all the detections.
217+ Days is int and will give back the number of detections made in the last days.
218+ """
219+
220+ if detector != Detector .PROMPT_INJECTION :
221+ raise ValueError (
222+ "Only Prompt Injection detector is currently supported for reports"
223+ )
224+
225+ json = {}
226+ if days :
227+ json = {"days" : days }
228+
229+ url = self ._backend + API_REPORT_PROMPT_INJECTIONS
230+
231+ try :
232+ response = httpx .post (
233+ url ,
234+ json = json ,
235+ headers = {"x-api-key" : self ._api_key },
236+ timeout = 20 ,
237+ )
238+ response .raise_for_status ()
239+ except httpx .RequestError as e :
240+ raise RuntimeError (
241+ f"An error occurred while making the request: { str (e )} "
242+ ) from e
243+ except httpx .HTTPStatusError as e :
244+ raise RuntimeError (
245+ f"Received an unexpected status code: { response .status_code } \n Response content: { response .text } "
246+ ) from e
247+
248+ return response .json ()
0 commit comments