hiieu/tool_r1_cot
Viewer • Updated • 130 • 8
How to use hiieu/R1_tool_call_Distill-Qwen-7B with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="hiieu/R1_tool_call_Distill-Qwen-7B")
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("hiieu/R1_tool_call_Distill-Qwen-7B")
model = AutoModelForCausalLM.from_pretrained("hiieu/R1_tool_call_Distill-Qwen-7B")
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]:]))How to use hiieu/R1_tool_call_Distill-Qwen-7B with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "hiieu/R1_tool_call_Distill-Qwen-7B"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "hiieu/R1_tool_call_Distill-Qwen-7B",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/hiieu/R1_tool_call_Distill-Qwen-7B
How to use hiieu/R1_tool_call_Distill-Qwen-7B with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "hiieu/R1_tool_call_Distill-Qwen-7B" \
--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": "hiieu/R1_tool_call_Distill-Qwen-7B",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'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 "hiieu/R1_tool_call_Distill-Qwen-7B" \
--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": "hiieu/R1_tool_call_Distill-Qwen-7B",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use hiieu/R1_tool_call_Distill-Qwen-7B with Unsloth Studio:
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 hiieu/R1_tool_call_Distill-Qwen-7B to start chatting
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 hiieu/R1_tool_call_Distill-Qwen-7B to start chatting
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for hiieu/R1_tool_call_Distill-Qwen-7B to start chatting
pip install unsloth
from unsloth import FastModel
model, tokenizer = FastModel.from_pretrained(
model_name="hiieu/R1_tool_call_Distill-Qwen-7B",
max_seq_length=2048,
)How to use hiieu/R1_tool_call_Distill-Qwen-7B with Docker Model Runner:
docker model run hf.co/hiieu/R1_tool_call_Distill-Qwen-7B
Function calling requires two step inferences, below is the example:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json
model_id = "R1_tool_call_Distill-Qwen-1.5B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
tools = [
{
"name": "create_contact",
"description": "Create a new contact",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the contact"
},
"email": {
"type": "string",
"description": "The email address of the contact"
}
},
"required": ["name", "email"]
}
}
]
messages = [
{ "role": "user", "content": f"""You have access to these tools, use them if necessary: {tools}
I need to create a new contact for my friend John Doe. His email is johndoe@example.com."""
}
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=256,
do_sample=True,
temperature=0.6,
top_p=0.9,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
# >>
# >> <|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>create_contact
# >> ```json
# >> {"name": "John Doe", "email": "johndoe@example.com"}
# >> ```<|tool▁call▁end|><|tool▁calls▁end|>
# Above is a response from assistant, you need to parse it and execute a tool on your own.
messages = [
{"role": "user", "content": """You have access to these tools, use them if necessary: {tools}\n\nI need to create a new contact for my friend John Doe. His email is johndoe@example.com."""},
{"role": "assistant", "content": None, "tool_calls": [
{
"type": "function",
"function": {
"name": "create_contact",
"arguments": json.dumps({"name": "John Doe", "email": "johndoe@example.com"})
}
},
]},
{"role": "tool", "name": "create_contact", "content": """{"status": "success", "message": "Contact for John Doe with email johndoe@example.com has been created successfully."}"""},
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=256,
do_sample=True,
temperature=0.6,
top_p=0.9,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
# >>
# >> <think>Based on the user's request, I have created a contact for John Doe with his email address. The tool has successfully created the contact. I will now provide the contact information to the user.</think>
# >> The contact for John Doe has been successfully created with the email address johndoe@example.com. Please feel free to reach out to him if needed.
This qwen2 model was trained 2x faster with Unsloth and Huggingface's TRL library.
Base model
deepseek-ai/DeepSeek-R1-Distill-Qwen-7B