Views
No views yet
SFTTrainer and
PEFT LoRA (r=16, lora_alpha=32, targeting q_proj/v_proj).peft to load it.get_weather(location, unit)get_news(topic, limit)calculate(expression)<|system|>
{system prompt}
<|tools|>
{tools.json as a JSON array}
<|user|>
{user message}
<|assistant|><|assistant|> block with either:{"name": "...", "arguments": {...}} (it was also trained on code-fenced, <tool_call>...</tool_call> XML-style, and OpenAI-style {"tool_calls": [...]} variants), or<|user|> / <|assistant|> / <|tool|> blocks.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5BASE_MODEL = "Qwen/Qwen2.5-3B-Instruct"
6ADAPTER = "navenduk75/qwen2.5-3b-function-calling-lora"
7
8base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype=torch.float32)
9tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
10model = PeftModel.from_pretrained(base_model, ADAPTER).merge_and_unload()
11
12prompt = """<|system|>
13You are a function-calling model. If the user's request matches one of the available tools, respond ONLY with a JSON object describing the tool to call and its arguments. If no tool applies, or required information is missing, respond in plain natural language instead.
14
15<|tools|>
16[{"name": "get_weather", "description": "Get current weather for a location", "parameters": {"type": "object", "properties": {"location": {"type": "string"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["location"]}}]
17
18<|user|>
19What's the weather in Berlin in celsius?
20
21<|assistant|>
22"""
23
24inputs = tokenizer(prompt, return_tensors="pt")
25output_ids = model.generate(**inputs, max_new_tokens=200, do_sample=False, pad_token_id=tokenizer.eos_token_id)
26print(tokenizer.decode(output_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))1@software{vonwerra2020trl,
2 title = {{TRL: Transformers Reinforcement Learning}},
3 author = {von Werra, Leandro and Belkada, Younes and Tunstall, Lewis and Beeching, Edward and Thrush, Tristan and Lambert, Nathan and Huang, Shengyi and Rasul, Kashif and Gallouédec, Quentin},
4 license = {Apache-2.0},
5 url = {https://github.com/huggingface/trl},
6 year = {2020}
7}