Views
No views yet
If you prefer loading a LoRA adapter on top of the original base model, see the standalone adapter:prabhu-nithin/qwen3-4b-xlam-function-calling-60k-lora
<tool_call> tags. Given the tool result back as a <tool_response>, it then produces a natural language final answer.User Query → Model → <tool_call>{"name": "fn", "arguments": {...}}</tool_call>
↓
Execute Python function
↓
<tool_response>{"result": ...}</tool_response>
↓
Model → Final Answer1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3import json
4
5model_id = "prabhu-nithin/qwen3-4b-xlam-function-calling-60k"
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
8
9tool_definitions = [
10 {
11 "name": "get_weather",
12 "description": "Get current weather information for a given city.",
13 "parameters": {
14 "type": "object",
15 "properties": {
16 "city": {"type": "string", "description": "The city name"},
17 "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "default": "celsius"}
18 },
19 "required": ["city"]
20 }
21 }
22]
23
24system_prompt = f"""You are a helpful assistant.
25
26# Tools
27
28You may call one or more functions to assist with the user query.
29
30You are provided with function signatures within <tools></tools> XML tags:
31<tools>
32{json.dumps(tool_definitions, indent=2)}
33</tools>
34
35For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
36<tool_call>
37{{"name": "<function-name>", "arguments": <args-json-object>}}
38</tool_call>"""
39
40messages = [
41 {"role": "system", "content": system_prompt},
42 {"role": "user", "content": "What's the weather like in Paris?"}
43]
44
45text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
46inputs = tokenizer(text, return_tensors="pt").to(model.device)
47outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.7, top_p=0.8, top_k=20)
48print(tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))<tool_call> / <tool_response> tokens| Parameter | Value |
|---|---|
| Base model | Qwen/Qwen3-4B-Instruct-2507 |
| Quantization | 4-bit (QLoRA) |
LoRA rank (r) | 16 |
| LoRA alpha | 32 |
| LoRA dropout | 0.05 |
| LoRA target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
| Epochs | 3 |
| Learning rate | 2e-4 |
| LR scheduler | cosine |
| Warmup ratio | 0.05 |
| Batch size (per device) | 1 |
| Gradient accumulation steps | 8 (effective batch size = 8) |
| Weight decay | 0.01 |
| Optimizer | paged_adamw_8bit |
| Max sequence length | 2048 |
| Precision | bf16 |
| Setting | Value |
|---|---|
| GPU | NVIDIA RTX 3060 Laptop |
| VRAM | ~8 GB used (QLoRA 4-bit) |
| Training time | ~18 hours |