Views
No views yet
song_name (required): Name of the song to playartist (optional): Artist namealbum (optional): Album nameaction (required): One of: play, pause, skip, next, previous, stop, resume1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5# Load base model and tokenizer
6base_model = AutoModelForCausalLM.from_pretrained(
7 "google/functiongemma-270m-it",
8 torch_dtype=torch.float16,
9 device_map="auto"
10)
11
12tokenizer = AutoTokenizer.from_pretrained("google/functiongemma-270m-it")
13
14# Load LoRA adapters
15model = PeftModel.from_pretrained(base_model, "your-username/music-2func")
16
17# Define functions
18FUNCTIONS = [
19 {
20 "type": "function",
21 "function": {
22 "name": "play_song",
23 "description": "Play a specific song by name or artist",
24 "parameters": {
25 "type": "object",
26 "properties": {
27 "song_name": {"type": "string", "description": "Name of the song to play"},
28 "artist": {"type": "string", "description": "Artist name (optional)"},
29 "album": {"type": "string", "description": "Album name (optional)"}
30 },
31 "required": ["song_name"]
32 }
33 }
34 },
35 {
36 "type": "function",
37 "function": {
38 "name": "playback_control",
39 "description": "Control music playback",
40 "parameters": {
41 "type": "object",
42 "properties": {
43 "action": {
44 "type": "string",
45 "enum": ["play", "pause", "skip", "next", "previous", "stop", "resume"]
46 }
47 },
48 "required": ["action"]
49 }
50 }
51 }
52]
53
54# Generate function call
55user_input = "Play Bohemian Rhapsody"
56
57messages = [{"role": "user", "content": user_input}]
58
59prompt = tokenizer.apply_chat_template(
60 messages,
61 tools=FUNCTIONS,
62 add_generation_prompt=True,
63 tokenize=False
64)
65
66inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
67
68with torch.no_grad():
69 outputs = model.generate(
70 **inputs,
71 max_new_tokens=128,
72 temperature=0.1,
73 do_sample=True,
74 pad_token_id=tokenizer.eos_token_id
75 )
76
77response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=False)
78print(response)<start_function_call>call:play_song{song_name:<escape>Bohemian Rhapsody<escape>}<end_function_call>| Test Input | Expected Function | Result |
|---|---|---|
| "Play Bohemian Rhapsody" | play_song | ✅ Pass |
| "Pause the music" | playback_control | ✅ Pass |
| "Skip to next song" | playback_control | ✅ Pass |
| "Play Wonderwall" | play_song | ✅ Pass |
| "Resume" | playback_control | ✅ Pass |
apply_chat_template (NOT json.dumps())json.dumps(arguments)1@misc{music-2func-2026,
2 title={FunctionGemma Music Assistant (2-Function)},
3 author={Your Name},
4 year={2026},
5 publisher={HuggingFace},
6 howpublished={\url{https://huggingface.co/your-username/music-2func}}
7}