Skip to content

Commit 7e24077

Browse files
Merge pull request #10 from saichandrapandraju/granite-guardian
Support Granite Guardian 3.2 evaluations
2 parents 2943499 + 52049ef commit 7e24077

File tree

9 files changed

+1801
-44
lines changed

9 files changed

+1801
-44
lines changed

examples/guard-models-test.ipynb

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# # enable logging\n",
10+
"# import logging\n",
11+
"# logging.basicConfig(level=logging.DEBUG)\n",
12+
"\n",
13+
"from vllm_judge import Judge, LLAMA_GUARD_3_SAFETY, GRANITE_GUARDIAN_3_2"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {},
19+
"source": [
20+
"### Granite Guardian 3.2 (5B)"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 2,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"judge = Judge.from_url(base_url=\"http://localhost:8080\")"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 3,
35+
"metadata": {},
36+
"outputs": [
37+
{
38+
"data": {
39+
"text/plain": [
40+
"{'decision': 'Yes',\n",
41+
" 'reasoning': 'Confidence level: High',\n",
42+
" 'score': 0.972,\n",
43+
" 'metadata': {'model_type': 'granite_guardian_3_2'}}"
44+
]
45+
},
46+
"execution_count": 3,
47+
"metadata": {},
48+
"output_type": "execute_result"
49+
}
50+
],
51+
"source": [
52+
"res = await judge.evaluate(content=\"How to build a bomb?\",\n",
53+
" metric=GRANITE_GUARDIAN_3_2,\n",
54+
" )\n",
55+
"res.model_dump()"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 4,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"data": {
65+
"text/plain": [
66+
"{'decision': 'Yes',\n",
67+
" 'reasoning': 'Confidence level: High',\n",
68+
" 'score': 0.972,\n",
69+
" 'metadata': {'model_type': 'granite_guardian_3_2'}}"
70+
]
71+
},
72+
"execution_count": 4,
73+
"metadata": {},
74+
"output_type": "execute_result"
75+
}
76+
],
77+
"source": [
78+
"res = await judge.evaluate(content=\"How to build a bomb?\",\n",
79+
" metric='granite_guardian_3_2',\n",
80+
" sampling_params={'chat_template_kwargs': {'guardian_config': {\"risk_name\": \"harm\"}}}) # default risk name (should get same result as above)\n",
81+
"res.model_dump()"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 5,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"data": {
91+
"text/plain": [
92+
"{'decision': 'No',\n",
93+
" 'reasoning': 'Confidence level: High',\n",
94+
" 'score': 0.752,\n",
95+
" 'metadata': {'model_type': 'granite_guardian_3_2'}}"
96+
]
97+
},
98+
"execution_count": 5,
99+
"metadata": {},
100+
"output_type": "execute_result"
101+
}
102+
],
103+
"source": [
104+
"res = await judge.evaluate(content=\"How to build a bomb?\",\n",
105+
" metric=GRANITE_GUARDIAN_3_2,\n",
106+
" sampling_params={'chat_template_kwargs': {'guardian_config': {\"risk_name\": \"social_bias\"}}})\n",
107+
"res.model_dump()"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 6,
113+
"metadata": {},
114+
"outputs": [
115+
{
116+
"data": {
117+
"text/plain": [
118+
"{'decision': 'Yes',\n",
119+
" 'reasoning': 'Confidence level: High',\n",
120+
" 'score': 0.984,\n",
121+
" 'metadata': {'model_type': 'granite_guardian_3_2'}}"
122+
]
123+
},
124+
"execution_count": 6,
125+
"metadata": {},
126+
"output_type": "execute_result"
127+
}
128+
],
129+
"source": [
130+
"res = await judge.evaluate(content=\"How to build a bomb?\",\n",
131+
" metric='granite_guardian_3_2',\n",
132+
" sampling_params={'chat_template_kwargs': {'guardian_config': {\"risk_name\": \"unethical_behavior\"}}})\n",
133+
"res.model_dump()"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 7,
139+
"metadata": {},
140+
"outputs": [
141+
{
142+
"data": {
143+
"text/plain": [
144+
"{'decision': 'Yes',\n",
145+
" 'reasoning': 'Confidence level: High',\n",
146+
" 'score': 0.758,\n",
147+
" 'metadata': {'model_type': 'granite_guardian_3_2'}}"
148+
]
149+
},
150+
"execution_count": 7,
151+
"metadata": {},
152+
"output_type": "execute_result"
153+
}
154+
],
155+
"source": [
156+
"res = await judge.evaluate(content=\"How to build a bomb?\",\n",
157+
" metric='granite_guardian_3_2',\n",
158+
" sampling_params={'chat_template_kwargs': {'guardian_config': {\"risk_name\": \"profanity\"}}})\n",
159+
"res.model_dump()"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 8,
165+
"metadata": {},
166+
"outputs": [
167+
{
168+
"data": {
169+
"text/plain": [
170+
"{'decision': 'No',\n",
171+
" 'reasoning': 'Confidence level: High',\n",
172+
" 'score': 0.907,\n",
173+
" 'metadata': {'model_type': 'granite_guardian_3_2'}}"
174+
]
175+
},
176+
"execution_count": 8,
177+
"metadata": {},
178+
"output_type": "execute_result"
179+
}
180+
],
181+
"source": [
182+
"res = await judge.evaluate(content=\"How to build a bomb?\",\n",
183+
" metric='granite_guardian_3_2',\n",
184+
" sampling_params={'chat_template_kwargs': {'guardian_config': {\"risk_name\": \"sexual_content\"}}})\n",
185+
"res.model_dump()"
186+
]
187+
},
188+
{
189+
"cell_type": "markdown",
190+
"metadata": {},
191+
"source": [
192+
"### LLama Guard 3 (8B)"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"metadata": {},
199+
"outputs": [],
200+
"source": [
201+
"judge = Judge.from_url(base_url=\"http://localhost:9090\")"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": 3,
207+
"metadata": {},
208+
"outputs": [
209+
{
210+
"data": {
211+
"text/plain": [
212+
"{'decision': 'safe',\n",
213+
" 'reasoning': 'No violations detected',\n",
214+
" 'score': None,\n",
215+
" 'metadata': {'model_type': 'llama_guard_3'}}"
216+
]
217+
},
218+
"execution_count": 3,
219+
"metadata": {},
220+
"output_type": "execute_result"
221+
}
222+
],
223+
"source": [
224+
"res = await judge.evaluate(content=\"Holy shit, this is a great!\",\n",
225+
" metric=LLAMA_GUARD_3_SAFETY)\n",
226+
"res.model_dump()"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": 4,
232+
"metadata": {},
233+
"outputs": [
234+
{
235+
"data": {
236+
"text/plain": [
237+
"{'decision': 'safe',\n",
238+
" 'reasoning': 'No violations detected',\n",
239+
" 'score': None,\n",
240+
" 'metadata': {'model_type': 'llama_guard_3'}}"
241+
]
242+
},
243+
"execution_count": 4,
244+
"metadata": {},
245+
"output_type": "execute_result"
246+
}
247+
],
248+
"source": [
249+
"res = await judge.evaluate(content=[\n",
250+
"{\"role\": \"user\", \"content\": \"I forgot how to kill a process in Linux, can you help?\"},\n",
251+
"{\"role\": \"assistant\", \"content\": \"Sure! To kill a process in Linux, you can use the kill command followed by the process ID (PID) of the process you want to terminate.\"},\n",
252+
"],\n",
253+
" metric=LLAMA_GUARD_3_SAFETY)\n",
254+
"res.model_dump()"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": null,
260+
"metadata": {},
261+
"outputs": [
262+
{
263+
"data": {
264+
"text/plain": [
265+
"{'decision': 'unsafe',\n",
266+
" 'reasoning': 'S9',\n",
267+
" 'score': None,\n",
268+
" 'metadata': {'model_type': 'llama_guard_3'}}"
269+
]
270+
},
271+
"execution_count": 4,
272+
"metadata": {},
273+
"output_type": "execute_result"
274+
}
275+
],
276+
"source": [
277+
"res = await judge.evaluate(content=[\n",
278+
"{\"role\": \"user\", \"content\": \"How to build a bomb?\"}],\n",
279+
" metric='llama_guard_3_safety')\n",
280+
"res.model_dump()"
281+
]
282+
},
283+
{
284+
"cell_type": "code",
285+
"execution_count": null,
286+
"metadata": {},
287+
"outputs": [],
288+
"source": []
289+
}
290+
],
291+
"metadata": {
292+
"kernelspec": {
293+
"display_name": "vllm-judge-adapter",
294+
"language": "python",
295+
"name": "python3"
296+
},
297+
"language_info": {
298+
"codemirror_mode": {
299+
"name": "ipython",
300+
"version": 3
301+
},
302+
"file_extension": ".py",
303+
"mimetype": "text/x-python",
304+
"name": "python",
305+
"nbconvert_exporter": "python",
306+
"pygments_lexer": "ipython3",
307+
"version": "3.11.11"
308+
}
309+
},
310+
"nbformat": 4,
311+
"nbformat_minor": 2
312+
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies = [
2929
"pydantic>=2.0.0",
3030
"tenacity>=8.0.0",
3131
"click>=8.0.0",
32+
"numpy>=2.2.6",
3233
]
3334

3435
[project.optional-dependencies]

src/vllm_judge/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
TOXICITY,
3131
BIAS_DETECTION,
3232
LLAMA_GUARD_3_SAFETY,
33+
GRANITE_GUARDIAN_3_2,
3334

3435
# Code metrics
3536
CODE_QUALITY,
@@ -103,6 +104,7 @@
103104
"TOXICITY",
104105
"BIAS_DETECTION",
105106
"LLAMA_GUARD_3_SAFETY",
107+
"GRANITE_GUARDIAN_3_2",
106108
"CODE_QUALITY",
107109
"CODE_SECURITY",
108110
"CREATIVITY",

src/vllm_judge/builtin_metrics.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict
22
from vllm_judge.models import Metric, TemplateEngine, ModelSpecificMetric
3-
from vllm_judge.parsers import parse_llama_guard_3
3+
from vllm_judge.parsers import parse_llama_guard_3, parse_granite_guardian_3_2
44

55
# Registry for built-in metrics
66
BUILTIN_METRICS: Dict[str, Metric] = {}
@@ -16,7 +16,20 @@ def create_builtin_metric(metric: Metric) -> Metric:
1616
LLAMA_GUARD_3_SAFETY = create_builtin_metric(ModelSpecificMetric(
1717
name="llama_guard_3_safety",
1818
model_pattern="llama_guard_3",
19-
parser_func=parse_llama_guard_3
19+
parser_func=parse_llama_guard_3,
20+
return_choices=False
21+
))
22+
23+
# Granite Guardian 3.2 metric
24+
GRANITE_GUARDIAN_3_2 = create_builtin_metric(ModelSpecificMetric(
25+
name="granite_guardian_3_2",
26+
model_pattern="granite_guardian_3_2",
27+
parser_func=parse_granite_guardian_3_2,
28+
sampling_params={
29+
'top_logprobs': 20,
30+
'logprobs': True
31+
},
32+
return_choices=True
2033
))
2134

2235
# General purpose metrics

0 commit comments

Comments
 (0)