Adapter Contract Examples
Focus
Focus
Prisma AIRS

Adapter Contract Examples

Table of Contents

Adapter Contract Examples

End-to-end adapter examples for common target types: a WebSocket target, an OAuth2 target with a stateful session, an audio target, and a document target.
Where Can I Use This?What Do I Need?
  • Prisma AIRS (AI Red Teaming)

WebSocket (call_target)

Uses the bundled websockets package to connect to a WebSocket target, send the prompt, and reassemble a streamed response frame by frame.
import json import websockets.sync.client as ws def call_target(context, inference_input): conn = ws.connect( context.vars["ws_url"], additional_headers={"Authorization": f"Bearer {context.auth['token']}"}, ) try: conn.send(json.dumps({"message": inference_input.prompt})) reply = "" for frame in conn: event = json.loads(frame) if event.get("type") == "chunk": reply += event["text"] elif event.get("type") == "done": break return CallTargetResult(output=reply) finally: conn.close()

OAuth2 and Stateful Session

A complete adapter for a target that requires OAuth2 client credentials, a server-side session created once per conversation, and a per-turn call that includes a UUID turn ID and a timestamp.
import time import uuid def authenticate(context): resp = context.http.post( context.vars["token_url"], data={ "grant_type": "client_credentials", "client_id": context.secrets["client_id"], "client_secret": context.secrets["client_secret"], }, ) if resp.status_code != 200: raise_auth_error(f"token endpoint returned {resp.status_code}: {resp.text}") body = resp.json() return AuthResult(ttl=body.get("expires_in", 300), data={"token": body["access_token"]}) def session_pre_process(context): session_id = str(uuid.uuid4()) resp = context.http.post( context.vars["session_url"], headers={"Authorization": f"Bearer {context.auth['token']}"}, json={"session_id": session_id}, ) if resp.status_code not in (200, 201): raise_target_error(f"session create returned {resp.status_code}: {resp.text}") return SessionPreProcessResult(session_state={"session_id": session_id}) def call_target(context, inference_input): body = { "session_id": context.session["session_id"], "turn_id": str(uuid.uuid4()), "timestamp": int(time.time()), "prompt": inference_input.prompt, } if context.vars.get("model"): body["model"] = context.vars["model"] resp = context.http.post( context.vars["turn_url"], headers={"Authorization": f"Bearer {context.auth['token']}"}, json=body, ) if resp.status_code == 429: raise_rate_limited(retry_after=30) if resp.status_code == 401: raise_auth_error("turn rejected: token invalid or expired") if resp.status_code != 200: raise_target_error(f"turn endpoint returned {resp.status_code}: {resp.text}") return CallTargetResult(output=resp.json()["response"])
Configuration for this example:
  • Variables: token_url, session_url, turn_url, model (optional)
  • Secrets: client_id, client_secret

Audio (pre_process and post_process)

For a target that takes audio, pick the format your target accepts and send the clip as-is. The clip replaces the prompt, so there is no text to send. To hand back an audio reply, set audio_b64 on the result; output is then optional and AI Red Teaming transcribes the audio. Audio attacks require Network Channel client v1.5.0 or later, and inference_input.audio requires v1.9.0 or later.
def pre_process(context, inference_input): if inference_input.audio is None: # not an audio attack: send the prompt as text return PreProcessResult( url=context.vars["endpoint"], json_body={"message": inference_input.prompt}, ) clip = inference_input.audio.first(AudioFormat.MP3, AudioFormat.WAV) if clip is None: offered = ", ".join(inference_input.audio.formats()) raise_target_error(f"no supported audio format; offered: {offered}") return PreProcessResult( url=context.vars["endpoint"], json_body={"audio": clip.b64, "format": clip.format.value}, ) def post_process(context, raw_response): # Audio-only reply: no text output, just the returned clip. Declare its format # only if it differs from what was sent: audio_format=AudioFormat.MP3. return PostProcessResult(audio_b64=raw_response.json_body["audio"])

Document (pre_process)

A document attack accompanies the prompt, so both are set: forward the prompt as usual and attach the file in whatever shape your target takes documents. The attack is present only when the File modality is selected on the target. doc.format.value is the MIME type, for example application/pdf.
def pre_process(context, inference_input): body = {"message": inference_input.prompt} if inference_input.file: doc = inference_input.file.any() body["document"] = {"mime_type": doc.format.value, "data": doc.b64} return PreProcessResult(url=context.vars["endpoint"], json_body=body)