Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3from huggingface_hub import login
4
5# Authenticate
6login()
7
8# Load base model
9base_model = AutoModelForCausalLM.from_pretrained(
10 "google/functiongemma-270m-it",
11 dtype="auto",
12 device_map="auto",
13 attn_implementation="eager",
14 token=True
15)
16
17# Load LoRA adapter
18model = PeftModel.from_pretrained(base_model, "sandeeppanem/functiongemma-270m-lora")
19model.eval()
20
21# Load tokenizer
22tokenizer = AutoTokenizer.from_pretrained("sandeeppanem/functiongemma-270m-lora")1# Merge adapter with base model for faster inference
2merged_model = model.merge_and_unload()
3
4# Save merged model
5merged_model.save_pretrained("./functiongemma-270m-merged")
6tokenizer.save_pretrained("./functiongemma-270m-merged")
7
8# Load merged model directly (no adapter needed)
9model = AutoModelForCausalLM.from_pretrained("./functiongemma-270m-merged")1# Define function schemas
2FUNCTION_SCHEMAS = [
3 {
4 "name": "gcd",
5 "description": "Compute the greatest common divisor of two numbers",
6 "parameters": {
7 "type": "object",
8 "properties": {
9 "a": {"type": "integer", "description": "First number"},
10 "b": {"type": "integer", "description": "Second number"}
11 },
12 "required": ["a", "b"]
13 }
14 },
15 # ... other function schemas
16]
17
18# Convert to tools format
19tools = []
20for schema in FUNCTION_SCHEMAS:
21 tools.append({
22 "type": "function",
23 "function": {
24 "name": schema["name"],
25 "description": schema["description"],
26 "parameters": schema["parameters"],
27 "return": {"type": "string"}
28 }
29 })
30
31# Create messages
32messages = [
33 {
34 "role": "developer",
35 "content": "You are a model that can do function calling with the following functions",
36 "tool_calls": None
37 },
38 {
39 "role": "user",
40 "content": "What is the GCD of 48 and 18?",
41 "tool_calls": None
42 }
43]
44
45# Apply chat template
46inputs = tokenizer.apply_chat_template(
47 messages,
48 tools=tools,
49 add_generation_prompt=True,
50 return_dict=True,
51 return_tensors="pt"
52)
53
54# Generate
55outputs = model.generate(**inputs.to(model.device), max_new_tokens=128)
56response = tokenizer.decode(outputs[0][len(inputs["input_ids"][0]):], skip_special_tokens=False)
57print(response)google/functiongemma-270m-it1@misc{functiongemma-270m-lora,
2 title={FunctionGemma 270M - Fine-tuned for Python Function Calling},
3 author={sandeeppanem},
4 year={2025},
5 url={https://huggingface.co/sandeeppanem/functiongemma-270m-lora}
6}