Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model = AutoModelForCausalLM.from_pretrained(
4 "your-username/phi4-guardrail",
5 trust_remote_code=True,
6 token="your_hf_token",
7)
8tokenizer = AutoTokenizer.from_pretrained(
9 "your-username/phi4-guardrail",
10 trust_remote_code=True,
11 token="your_hf_token",
12)
13
14prompt = tokenizer.apply_chat_template(
15 [{"role": "user", "content": "What is the capital of France?"}],
16 tokenize=False,
17 add_generation_prompt=True,
18)
19inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
20input_length = inputs["input_ids"].shape[1]
21
22outputs = model.generate(**inputs, max_new_tokens=200)
23print(tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True))| Parameter | Default | Description |
|---|---|---|
guard_threshold | 0.5 | JAILBREAK probability above which the prompt is blocked |
blocked_response | "I'm not able to assist with that." | Static string returned on block |
phi_model_id | microsoft/Phi-4-mini-instruct | Base generation model |
guard_model_id | meta-llama/Llama-Prompt-Guard-2-86M | Guardrail classifier |