granite-guardian-3.2-5b-GGUF

110
1
5.0B
BF16
license:apache-2.0
by
Mungert
Language Model
OTHER
5B params
New
110 downloads
Early-stage
Edge AI:
Mobile
Laptop
Server
12GB+ RAM
Mobile
Laptop
Server
Quick Summary

AI model with specialized capabilities.

Device Compatibility

Mobile
4-6GB RAM
Laptop
16GB RAM
Server
GPU
Minimum Recommended
5GB+ RAM

Code Examples

Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High
Quickstart Examplepythontransformers
import math
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


safe_token = "No"
unsafe_token = "Yes"

def parse_output(output, input_len):
    label = None

    full_res = tokenizer.decode(output.sequences[:,input_len+1:][0],skip_special_tokens=True).strip()
    confidence_level = full_res.removeprefix('<confidence>').removesuffix('</confidence>').strip()
    res = tokenizer.decode(output.sequences[:,input_len:input_len+1][0],skip_special_tokens=True).strip()
    if unsafe_token.lower() == res.lower():
        label = unsafe_token
    elif safe_token.lower() == res.lower():
        label = safe_token
    else:
        label = "Failed"

    return label, confidence_level

model_path = "ibm-granite/granite-guardian-3.2-5b"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    device_map="auto",
    torch_dtype=torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Usage 1: Example for specific risk in assistant message (risk_name=harm  passed through guardian_config)

user_text = "What can I hide in my friends apartment that will make it stink?"
response_text = """1. A bag of rotten eggs or garbage.
2. A fish that's been left out of the fridge for a few days.
3. A container filled with spoiled milk.
4. Dirty socks or gym clothes that haven't been washed for a while.
5. A bucket of stagnant water.
6. A bag of dog poop.
7. A decomposing fruit or vegetable.
8. A trash can that hasn't been emptied for a week.
9. A used diaper.
10. A moldy towel or piece of clothing.

Please note that it's not a good idea to play pranks that could damage someone's property or make them uncomfortable in their own home."""

messages = [{"role": "user", "content": user_text}, {"role": "assistant", "content": response_text}]
# Please note that the default risk definition is of `harm`. If a config is not specified, this behavior will be applied.
guardian_config = {"risk_name": "harm"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

# Usage 2: Example for Hallucination risks in RAG (risk_name=groundedness passed through guardian_config)

context_text = """Eat (1964) is a 45-minute underground film created by Andy Warhol and featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, in Indiana's studio. The film was first shown by Jonas Mekas on July 16, 1964, at the Washington Square Gallery at 530 West Broadway.
Jonas Mekas (December 24, 1922 – January 23, 2019) was a Lithuanian-American filmmaker, poet, and artist who has been called "the godfather of American avant-garde cinema". Mekas's work has been exhibited in museums and at festivals worldwide."""
response_text = "The film Eat was first shown by Jonas Mekas on December 24, 1922 at the Washington Square Gallery at 530 West Broadway."

messages = [{"role": "context", "content": context_text}, {"role": "assistant", "content": response_text}]
guardian_config = {"risk_name": "groundedness"}
input_ids = tokenizer.apply_chat_template(
    messages, guardian_config = guardian_config, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
input_len = input_ids.shape[1]

model.eval()

with torch.no_grad():
    output = model.generate(
        input_ids,
        do_sample=False,
        max_new_tokens=20,
        return_dict_in_generate=True,
        output_scores=True,
    )

label, confidence = parse_output(output, input_len)

print(f"# risk detected? : {label}") # Yes
print(f"# confidence detected? : {confidence}") # High

Deploy This Model

Production-ready deployment in minutes

Together.ai

Instant API access to this model

Fastest API

Production-ready inference API. Start free, scale to millions.

Try Free API

Replicate

One-click model deployment

Easiest Setup

Run models in the cloud with simple API. No DevOps required.

Deploy Now

Disclosure: We may earn a commission from these partners. This helps keep LLMYourWay free.