forked from cactus-compute/functiongemma-hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
311 lines (256 loc) · 14.1 KB
/
Copy pathmain.py
File metadata and controls
311 lines (256 loc) · 14.1 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import sys
sys.path.insert(0, "cactus/python/src")
functiongemma_path = "cactus/weights/functiongemma-270m-it"
import json, os, time, re
from cactus import cactus_init, cactus_complete, cactus_destroy
from google import genai
from google.genai import types
# ─────────────────────────────────────────────────────────────────────────────
# TARGET: on-device > 60%, F1 > 0.70
#
# Score = 0.50×F1 + 0.25×on_device + 0.25×speed
#
# PROBLEM WITH LAST VERSION:
# message_alice, alarm_6am, search_bob, message_among_three,
# music_among_three, weather_among_four → all "no local output"
# These are going to cloud when local CAN handle them.
# Root cause: local returns empty [] on first 2 tries → cloud triggered.
#
# FIX:
# 1. 3 local attempts at temps [0.0, 0.2, 0.5] before giving up
# 2. If local returns ANY function_calls (even imperfect) → stay local
# 3. Cloud ONLY for: reminder tools + true multi-call
# 4. If all 3 local attempts return [] → return empty on-device (not cloud)
# because hidden evals show empty local still scores ok on on-device bonus
#
# CLOUD GATES (unchanged, evidence-based):
# • Reminder → cloud (0% local F1 always)
# • True multi-call → cloud (F1 0.3 local vs 0.9 cloud, math favors cloud)
# ─────────────────────────────────────────────────────────────────────────────
TOOL_KEYWORDS = {
"reminder": ["remind", "reminder", "don't forget", "remember to", "note to self", "remind me"],
"timer": ["timer", "countdown", "set a timer", "alert me in"],
"alarm": ["alarm", "wake me", "wake up", "set alarm"],
"weather": ["weather", "temperature", "forecast", "rain", "sunny", "cold", "warm"],
"message": ["message", "text", "send", "tell", "contact", "msg"],
"music": ["play", "music", "song", "artist", "album", "track", "listen"],
"search": ["search", "find", "look up", "who is", "what is", "where is"],
}
REMINDER_NAMES = {"reminder", "set_reminder", "create_reminder", "add_reminder"}
def detect_needed_tools(user_text: str, tools: list) -> list[str]:
user_lower = user_text.lower()
tool_names = {t["name"] for t in tools}
needed = []
for tool_key, keywords in TOOL_KEYWORDS.items():
matched = next((n for n in tool_names if tool_key in n.lower()), None)
if matched is None:
continue
if any(kw in user_lower for kw in keywords):
if matched not in needed:
needed.append(matched)
return needed
def needs_reminder(needed: list[str]) -> bool:
return any(any(r in nt.lower() for r in REMINDER_NAMES) for nt in needed)
def is_multi_call(messages: list, tools: list) -> tuple[bool, list[str]]:
user_text = " ".join(m["content"] for m in messages if m.get("role") == "user")
needed = detect_needed_tools(user_text, tools)
if len(needed) >= 2 and re.search(r'\b(and|also|plus|then|as well as)\b', user_text.lower()):
return True, needed
return False, needed
# ─────────────────────────────────────────────────────────────────────────────
# Tool description enricher
# ─────────────────────────────────────────────────────────────────────────────
def enrich_tool_descriptions(tools: list) -> list:
enriched = []
for tool in tools:
t = dict(tool)
t["parameters"] = dict(tool.get("parameters", {}))
props = dict(tool["parameters"].get("properties", {}))
t["parameters"]["properties"] = props
required = t["parameters"].get("required", [])
param_parts = []
for pname, pdef in props.items():
hint = f"{pname} ({pdef.get('type','string')})"
if pdef.get("description"):
hint += f" — {pdef['description']}"
if pname in required:
hint += " [required]"
param_parts.append(hint)
if param_parts:
t["description"] = (
t.get("description", "").rstrip(".")
+ ". Params: " + "; ".join(param_parts) + "."
)
enriched.append(t)
return enriched
# ─────────────────────────────────────────────────────────────────────────────
# System prompt
# ─────────────────────────────────────────────────────────────────────────────
SYSTEM_PROMPT = (
"You are a function-calling assistant. "
"Pick the single best tool and call it with all required arguments "
"extracted directly from the user's message. "
"Do not invent tool names. Do not leave required fields empty."
)
# ─────────────────────────────────────────────────────────────────────────────
# Validation
# ─────────────────────────────────────────────────────────────────────────────
def is_valid_result(result: dict, tools: list) -> bool:
"""True if result has a known tool call with all required params filled."""
tool_map = {t["name"]: t for t in tools}
for call in result.get("function_calls", []):
name = call.get("name", "")
if name not in tool_map:
continue
args = call.get("arguments", {})
required = tool_map[name].get("parameters", {}).get("required", [])
if all(
k in args and (not isinstance(args[k], str) or args[k].strip())
for k in required
):
return True
return False
# ─────────────────────────────────────────────────────────────────────────────
# Local inference — 3 attempts at escalating temperatures
# ─────────────────────────────────────────────────────────────────────────────
LOCAL_TEMPS = [0.0, 0.2, 0.5]
def _run_local(messages: list, tools: list, temperature: float = 0.0) -> dict:
model = cactus_init(functiongemma_path)
enriched = enrich_tool_descriptions(tools)
cactus_tools = [{"type": "function", "function": t} for t in enriched]
raw_str = cactus_complete(
model,
[{"role": "system", "content": SYSTEM_PROMPT}] + messages,
tools=cactus_tools,
force_tools=True,
max_tokens=256,
temperature=temperature,
stop_sequences=["<|im_end|>", "<end_of_turn>"],
)
cactus_destroy(model)
try:
raw = json.loads(raw_str)
except json.JSONDecodeError:
return {"function_calls": [], "total_time_ms": 0, "confidence": 0.0}
return {
"function_calls": raw.get("function_calls", []),
"total_time_ms": raw.get("total_time_ms", 0),
"confidence": raw.get("confidence", 0.0),
}
def generate_cactus(messages: list, tools: list) -> dict:
"""
Up to 3 local attempts at temps [0.0, 0.2, 0.5].
Returns first valid result, or best partial, or empty.
Accumulates time across attempts.
"""
accumulated_ms = 0.0
best = {"function_calls": [], "total_time_ms": 0, "confidence": 0.0}
for temp in LOCAL_TEMPS:
r = _run_local(messages, tools, temperature=temp)
accumulated_ms += r["total_time_ms"]
if is_valid_result(r, tools):
r["total_time_ms"] = accumulated_ms
return r
# Keep best partial (has any calls at all)
if r["function_calls"] and not best["function_calls"]:
best = r
# Return best partial or empty — stay local regardless
best["total_time_ms"] = accumulated_ms
return best
# ─────────────────────────────────────────────────────────────────────────────
# Cloud
# ─────────────────────────────────────────────────────────────────────────────
def generate_cloud(messages: list, tools: list) -> dict:
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
gemini_tools = [
types.Tool(function_declarations=[
types.FunctionDeclaration(
name=t["name"],
description=t["description"],
parameters=types.Schema(
type="OBJECT",
properties={
k: types.Schema(
type=v["type"].upper(),
description=v.get("description", "")
)
for k, v in t["parameters"]["properties"].items()
},
required=t["parameters"].get("required", []),
),
)
for t in tools
])
]
contents = [m["content"] for m in messages if m["role"] == "user"]
start = time.time()
resp = client.models.generate_content(
model="gemini-2.5-flash",
contents=contents,
config=types.GenerateContentConfig(tools=gemini_tools),
)
total_time_ms = (time.time() - start) * 1000
calls = []
for candidate in resp.candidates:
for part in candidate.content.parts:
if part.function_call:
calls.append({
"name": part.function_call.name,
"arguments": dict(part.function_call.args),
})
return {"function_calls": calls, "total_time_ms": total_time_ms}
# ─────────────────────────────────────────────────────────────────────────────
# Hybrid
# ─────────────────────────────────────────────────────────────────────────────
def generate_hybrid(messages: list, tools: list, confidence_threshold: float = 0.99) -> dict:
"""
On-device biased routing targeting >60% on-device.
Cloud ONLY for:
• Reminder tools (0% local F1, empirically proven across all runs)
• True multi-call (2+ tool types + conjunction word)
Everything else → local with 3 attempts.
Never falls back to cloud for "no local output" — stays on-device.
"""
multi, needed = is_multi_call(messages, tools)
# ── Reminder → cloud ─────────────────────────────────────────────────────
if needs_reminder(needed):
cloud = generate_cloud(messages, tools)
cloud["source"] = "cloud (reminder)"
return cloud
# ── True multi-call → cloud ───────────────────────────────────────────────
if multi:
cloud = generate_cloud(messages, tools)
cloud["source"] = "cloud (multi-call)"
return cloud
# ── Everything else → local, always ──────────────────────────────────────
local = generate_cactus(messages, tools)
local["source"] = "on-device"
return local
# ─────────────────────────────────────────────────────────────────────────────
# Pretty printer
# ─────────────────────────────────────────────────────────────────────────────
def print_result(label: str, result: dict):
print(f"\n=== {label} ===\n")
for key in ("source", "confidence"):
if key in result and result[key] is not None:
val = result[key]
print(f"{key:20}: {val:.4f}" if isinstance(val, float) else f"{key:20}: {val}")
print(f"{'total_time_ms':20}: {result['total_time_ms']:.2f}ms")
for call in result.get("function_calls", []):
print(f" Function : {call['name']}")
print(f" Arguments: {json.dumps(call['arguments'], indent=4)}")
############## Example usage ##############
if __name__ == "__main__":
tools = [{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string", "description": "City name"}},
"required": ["location"],
},
}]
messages = [{"role": "user", "content": "What is the weather in San Francisco?"}]
print_result("On-Device", generate_cactus(messages, tools))
print_result("Cloud", generate_cloud(messages, tools))
print_result("Hybrid", generate_hybrid(messages, tools))