File size: 1,289 Bytes
b77ed28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Shared utilities for eval scripts."""
from __future__ import annotations

import json
import os
from pathlib import Path
from typing import Any, Dict, Optional, Set


def load_json(path: Path) -> Dict[str, Any]:
    with path.open() as f:
        return json.load(f)


def load_env(path: str = ".env") -> None:
    env_path = Path(path)
    if not env_path.exists():
        return
    for line in env_path.read_text().splitlines():
        if not line or line.strip().startswith("#") or "=" not in line:
            continue
        key, value = line.split("=", 1)
        key = key.strip()
        value = value.strip().strip('"').strip("'")
        if key and key not in os.environ:
            os.environ[key] = value


def injection_evidence_ids(scenario: Optional[Dict[str, Any]]) -> Set[str]:
    if not scenario:
        return set()
    ids: Set[str] = set()
    for payload in scenario.get("prompt_injection_payloads", []):
        for evidence_id in payload.get("evidence_ids", []) or []:
            ids.add(evidence_id)
    return ids


def extract_json(text: str) -> Dict[str, Any]:
    start = text.find("{")
    end = text.rfind("}")
    if start == -1 or end == -1 or end <= start:
        raise ValueError("no json found")
    return json.loads(text[start : end + 1])