-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
48 lines (40 loc) · 1.35 KB
/
Copy pathserver.py
File metadata and controls
48 lines (40 loc) · 1.35 KB
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
46
47
48
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from agents.hacker import generate_attack
from agents.guardrail import evaluate
from agents.target import respond
from agents.judge import analyze
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class TurnRequest(BaseModel):
secret_word: str
target_last_response: str
attacks_so_far: list[str]
use_guardrail: bool
@app.post("/run-turn")
async def run_turn(req: TurnRequest):
import os
os.environ["SECRET_WORD"] = req.secret_word
attack = generate_attack(req.target_last_response, req.attacks_so_far)
guardrail = evaluate(attack) if req.use_guardrail else {"score": 0, "reason": "disabled", "blocked": False}
if guardrail["blocked"]:
target_response = "[BLOCKED BY GUARDRAIL]"
else:
target_response = respond(attack)
judge = analyze(attack, target_response)
return {
"attack": attack,
"guardrail_score": guardrail["score"],
"guardrail_blocked": guardrail["blocked"],
"guardrail_reason": guardrail["reason"],
"target_response": target_response,
"attack_type": judge["attack_type"],
"jailbroken": judge["succeeded"],
"judge_reason": judge["reason"]
}