File size: 4,679 Bytes
bdfa860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
license: apache-2.0
base_model: google/functiongemma-270m-it
tags:
- function-calling
- lora
- peft
- gemma
- functiongemma
- fine-tuned
datasets:
- custom
---

# FunctionGemma 270M - Fine-tuned for Python Function Calling

This is a LoRA (Low-Rank Adaptation) fine-tuned version of [google/functiongemma-270m-it](https://huggingface.co/google/functiongemma-270m-it) for Python function calling tasks.

## Model Details

- **Base Model**: [google/functiongemma-270m-it](https://huggingface.co/google/functiongemma-270m-it)
- **Fine-tuning Method**: LoRA (Low-Rank Adaptation)
- **Training Accuracy**: 100% on test set
- **Model Size**: 270M parameters (base) + LoRA adapters

## Supported Functions

The model is fine-tuned to call these 5 Python functions:

1. **is_prime(n)** - Check if a number is prime
2. **is_factorial(n)** - Compute factorial (n!)
3. **fibonacci(n)** - Compute nth Fibonacci number
4. **gcd(a, b)** - Greatest common divisor
5. **lcm(a, b)** - Least common multiple

## Usage

### Load with LoRA Adapter

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
from huggingface_hub import login

# Authenticate
login()

# Load base model
base_model = AutoModelForCausalLM.from_pretrained(
    "google/functiongemma-270m-it",
    dtype="auto",
    device_map="auto",
    attn_implementation="eager",
    token=True
)

# Load LoRA adapter
model = PeftModel.from_pretrained(base_model, "sandeeppanem/functiongemma-270m-lora")
model.eval()

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained("sandeeppanem/functiongemma-270m-lora")
```

### Merge and Use (Recommended for Inference)

```python
# Merge adapter with base model for faster inference
merged_model = model.merge_and_unload()

# Save merged model
merged_model.save_pretrained("./functiongemma-270m-merged")
tokenizer.save_pretrained("./functiongemma-270m-merged")

# Load merged model directly (no adapter needed)
model = AutoModelForCausalLM.from_pretrained("./functiongemma-270m-merged")
```

### Example Inference

```python
# Define function schemas
FUNCTION_SCHEMAS = [
    {
        "name": "gcd",
        "description": "Compute the greatest common divisor of two numbers",
        "parameters": {
            "type": "object",
            "properties": {
                "a": {"type": "integer", "description": "First number"},
                "b": {"type": "integer", "description": "Second number"}
            },
            "required": ["a", "b"]
        }
    },
    # ... other function schemas
]

# Convert to tools format
tools = []
for schema in FUNCTION_SCHEMAS:
    tools.append({
        "type": "function",
        "function": {
            "name": schema["name"],
            "description": schema["description"],
            "parameters": schema["parameters"],
            "return": {"type": "string"}
        }
    })

# Create messages
messages = [
    {
        "role": "developer",
        "content": "You are a model that can do function calling with the following functions",
        "tool_calls": None
    },
    {
        "role": "user",
        "content": "What is the GCD of 48 and 18?",
        "tool_calls": None
    }
]

# Apply chat template
inputs = tokenizer.apply_chat_template(
    messages,
    tools=tools,
    add_generation_prompt=True,
    return_dict=True,
    return_tensors="pt"
)

# Generate
outputs = model.generate(**inputs.to(model.device), max_new_tokens=128)
response = tokenizer.decode(outputs[0][len(inputs["input_ids"][0]):], skip_special_tokens=False)
print(response)
```

## Training Details

- **Training Data**: Custom dataset with 183 training examples, 21 test examples
- **Training Split**: 90% train, 10% test
- **Epochs**: 6
- **Learning Rate**: 2e-5
- **Batch Size**: 4 (per device)
- **Gradient Accumulation Steps**: 1
- **LoRA Config**:
  - Rank (r): 8
  - Alpha: 16
  - Target modules: q_proj, v_proj, k_proj, o_proj
  - Dropout: 0.05
  - Bias: none

## Evaluation

- **Test Accuracy**: 100.0%
- **Baseline Accuracy**: 81.0%
- **Improvement**: +19.0%

## Limitations

- The model is fine-tuned specifically for the 5 mathematical functions listed above
- It may not generalize well to other function calling tasks without additional fine-tuning
- Requires access to the gated base model `google/functiongemma-270m-it`

## Citation

If you use this model, please cite:

```bibtex
@misc{functiongemma-270m-lora,
  title={FunctionGemma 270M - Fine-tuned for Python Function Calling},
  author={sandeeppanem},
  year={2025},
  url={https://huggingface.co/sandeeppanem/functiongemma-270m-lora}
}
```

## License

This model is licensed under Apache 2.0, same as the base model.