Text Generation
Transformers
Safetensors
English
llama
text-generation-inference
unsloth
trl
grpo
conversational
Instructions to use Jlonge4/phi4-r1-guard-v1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Jlonge4/phi4-r1-guard-v1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Jlonge4/phi4-r1-guard-v1") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("Jlonge4/phi4-r1-guard-v1") model = AutoModelForCausalLM.from_pretrained("Jlonge4/phi4-r1-guard-v1") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Jlonge4/phi4-r1-guard-v1 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Jlonge4/phi4-r1-guard-v1" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Jlonge4/phi4-r1-guard-v1", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Jlonge4/phi4-r1-guard-v1
- SGLang
How to use Jlonge4/phi4-r1-guard-v1 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Jlonge4/phi4-r1-guard-v1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Jlonge4/phi4-r1-guard-v1", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Jlonge4/phi4-r1-guard-v1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Jlonge4/phi4-r1-guard-v1", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Unsloth Studio
How to use Jlonge4/phi4-r1-guard-v1 with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for Jlonge4/phi4-r1-guard-v1 to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for Jlonge4/phi4-r1-guard-v1 to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for Jlonge4/phi4-r1-guard-v1 to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="Jlonge4/phi4-r1-guard-v1", max_seq_length=2048, ) - Docker Model Runner
How to use Jlonge4/phi4-r1-guard-v1 with Docker Model Runner:
docker model run hf.co/Jlonge4/phi4-r1-guard-v1
Reasoning Model for Content Evaluation
- Developed by: Jlonge4
- License: apache-2.0
- Finetuned from model: unsloth/phi-4-bnb-4bit
This Llama model was trained 2x faster with Unsloth and Huggingface's TRL library. It specializes in three key reasoning tasks:
- Toxicity Detection: Evaluates whether text content contains toxic elements such as hate speech, harassment, or inappropriate language.
- Hallucination Detection: Determines if an AI model's response contains factual inaccuracies or fabricated information when compared to a reference.
- RAG Relevance Assessment: Analyzes whether retrieved context is relevant to answering a specific query.
Model Capabilities
The model provides structured reasoning and binary classification for each task:
- Toxicity:
toxicvsnon-toxic - Hallucination:
yesvsno - RAG Relevance:
relevantvsunrelated
All outputs follow a consistent format:
<reasoning>
Detailed explanation of the decision
</reasoning>
<answer>
Classification result
</answer>
Functions:
Toxicity
def toxic_format_func(text):
system = """Your job is to evaluate whether a human text is toxic or non-toxic.
Respond in the following format:
<reasoning>
...
</reasoning>
<answer>
...
</answer>
"""
prompt = f"""You are examining written text content. Here is the text:
<info>
[Text]: {text}
</info>
Examine the text and determine whether the text is toxic or not. Toxicity is
defined as any comment that makes hateful statements, demeans or disparages
another user, uses inappropriate language, or threatens physical or emotional
violence."""
return system, prompt
Hallucinations
def halu_format_func(reference, query, response):
system = """Your job is to evaluate whether a machine learning model has hallucinated or not. A hallucination occurs when the response is coherent but factually incorrect or nonsensical outputs that are not grounded in the provided context.
Respond in the following format:
<reasoning>
...
</reasoning>
<answer>
...
</answer>
"""
prompt = f"""You are given the following information:
<info>
[Knowledge]: {reference}
[User Input]: {query}
[Model Response]: {response}
</info>
Based on the information provided is the model output a hallucination?"""
return system, prompt
Rag Relevance
def rag_format_func(reference, query):
system = """Your job is to evaluate whether a retrieved context is relevant, or unrelated to a user query.
Respond in the following format:
<reasoning>
...
</reasoning>
<answer>
...
</answer>
"""
prompt = f"""You are comparing a reference text to a question and trying to determine if the reference text
contains information relevant to answering the question. Here is the info:
<info>
[Question]: {query}
[Reference text]: {reference}
</info>
Compare the Question above to the Reference text. Your response must be single word,
either "relevant" or "unrelated"."""
return system, prompt
Usage:
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("grounded-ai/phi4-r1-guard")
tokenizer = AutoTokenizer.from_pretrained("grounded-ai/phi4-r1-guard")
Toxicity Detection Example:
text_to_evaluate = "This is some text to evaluate"
system, prompt = toxic_format_func(text_to_evaluate)
inputs = tokenizer(prompt, return_tensors="pt")
output = model.generate(inputs)
result = tokenizer.decode(output[0])
Hallucination Detection Example:
reference = "The Eiffel Tower was completed in 1889."
query = "When was the Eiffel Tower built?"
response = "The Eiffel Tower was completed in 1925."
system, prompt = halu_format_func(reference, query, response)
RAG Relevance Example:
reference = "The process of photosynthesis in plants..."
query = "How does photosynthesis work?"
system, prompt = rag_format_func(reference, query)
)
Sample Output:
HALLUCINATION - YES CASE:
System: Your job is to evaluate whether a machine learning model has hallucinated or not. A hallucination occurs when the response is coherent but factually incorrect or nonsensical outputs that are not grounded in the provided context.
Respond in the following format:
<reasoning>
...
</reasoning>
<answer>
...
</answer>
Prompt: You are given the following information:
<info>
[Knowledge]: The Eiffel Tower was completed in 1889 and stands 324 meters tall. It was built for the World's Fair in Paris.
[User Input]: When was the Eiffel Tower built and how tall is it?
[Model Response]: The Eiffel Tower was completed in 1925 and stands 450 meters tall. It was built to celebrate France's victory in World War I.
</info>
Based on the information provided is the model output a hallucination?
##############################################################################
Result: <reasoning>
The model's response contains several factual inaccuracies when compared to the provided knowledge. According to the information given, the Eiffel Tower was completed in 1889, not 1925, and it stands 324 meters tall, not 450 meters. Additionally, the Eiffel Tower was built for the World's Fair in Paris, not to celebrate France's victory in World War I. These discrepancies indicate that the model's response is not grounded in the provided context and includes factually incorrect information. Therefore, the model's output can be classified as a hallucination.
</reasoning>
<answer>
Yes, the model output is a hallucination.
</answer>
- Downloads last month
- 1
