Source code for prompt_risk.judges.j1_over_permissive
# -*- coding: utf-8 -*-"""J1 Over-Permissive Authorization Judge.Evaluates a prompt's system prompt text (and optionally its user prompttemplate) for over-permissive authorization risks. The judge itself is aprompt — it uses an LLM to perform semantic analysis against five criteriadefined in its own system prompt template.This module is **use-case-agnostic**. It accepts raw prompt text as stringsand knows nothing about FNOL, claims, or any specific business domain.Use-case-specific wrappers (e.g. ``uc.uc1.j1_uc1_p1``) handle loadingprompt files and calling this function."""importtypingasTimportjsonimportrefrompydanticimportBaseModel,Field,ValidationErrorfrom..constantsimportPromptIdEnumfrom..promptsimportPromptfrom..llm_outputimportextract_jsonfrom..bedrock_utilsimportconverseifT.TYPE_CHECKING:frommypy_boto3_bedrock_runtimeimportBedrockRuntimeClient# ---------------------------------------------------------------------------# Input / Output models# ---------------------------------------------------------------------------
[docs]classJ1UserPromptData(BaseModel):"""Input data for the J1 judge user prompt template."""target_system_prompt:strtarget_user_prompt_template:T.Optional[str]=None
[docs]classJ1Finding(BaseModel):"""A single criterion-level finding from the J1 judge."""criterion:strseverity:T_SEVERITYevidence:strexplanation:strrecommendation:str
# ---------------------------------------------------------------------------# Helpers# ---------------------------------------------------------------------------MAX_RETRIES=3# ---------------------------------------------------------------------------# Main entry point# ---------------------------------------------------------------------------
[docs]defrun_j1_over_permissive(client:"BedrockRuntimeClient",data:J1UserPromptData,judge_version:str="01",model_id:str="us.amazon.nova-2-lite-v1:0",)->J1Result:"""Evaluate a prompt for over-permissive authorization risks. Parameters ---------- client: Bedrock Runtime client. data: The target prompt texts to evaluate. judge_version: Which version of the J1 judge prompt to use. model_id: Bedrock model ID for the judge LLM. Returns ------- J1Result Structured evaluation result with overall risk, score, findings, and summary. """judge_prompt=Prompt(id=PromptIdEnum.JUDGE_J1_OVER_PERMISSIVE.value,version=judge_version,)system=[{"text":judge_prompt.system_prompt_template.render()},{"cachePoint":{"type":"default"}},]user_prompt=judge_prompt.user_prompt_template.render(data=data)messages:list[dict]=[{"role":"user","content":[{"text":user_prompt}]},]forattemptinrange(MAX_RETRIES):text=converse(client,model_id,system,messages)json_obj=extract_json(text)try:returnJ1Result(**json_obj)except(json.JSONDecodeError,ValidationError)asexc:ifattempt==MAX_RETRIES-1:raiseerror_msg=(f"Your previous response failed validation:\n{exc}\n\n""Please return a corrected JSON object.")messages.append({"role":"assistant","content":[{"text":text}]})messages.append({"role":"user","content":[{"text":error_msg}]})raiseException("Should never reach this line of code")# pragma: no cover