-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_function_calling.py
More file actions
387 lines (319 loc) · 10.1 KB
/
Copy pathtest_function_calling.py
File metadata and controls
387 lines (319 loc) · 10.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python3
"""
Test script for function calling fine-tuned model.
Provides example prompts and tests the model's function calling capabilities.
"""
import argparse
from pathlib import Path
import mlx.core as mx
import utils
from models import LoRALinear
# Example test cases for function calling
TEST_CASES = [
{
"name": "Power of Two Check",
"prompt": """<user>Check if the numbers 8 and 1233 are powers of two.</user>
<tools>""",
"expected": "Function to check if numbers are powers of two"
},
{
"name": "Weather Query",
"prompt": """<user>What's the weather like in San Francisco today?</user>
<tools>""",
"expected": "Weather API function call with location parameter"
},
{
"name": "Calculate Average",
"prompt": """<user>Calculate the average of these numbers: 10, 20, 30, 40, 50</user>
<tools>""",
"expected": "Mathematical average function"
},
{
"name": "Date Conversion",
"prompt": """<user>Convert the date '2024-03-15' to a human-readable format.</user>
<tools>""",
"expected": "Date formatting function"
},
{
"name": "String Manipulation",
"prompt": """<user>Convert the text 'hello world' to uppercase.</user>
<tools>""",
"expected": "String transformation function"
}
]
def load_model_with_adapter(model_path: str, adapter_path: str, lora_layers: int = 16, lora_rank: int = 8):
"""
Load model and apply LoRA adapters.
Args:
model_path: Path to base model
adapter_path: Path to adapter weights
lora_layers: Number of layers with LoRA
lora_rank: LoRA rank parameter
Returns:
Tuple of (model, tokenizer, config)
"""
print("=" * 80)
print("Loading Model with LoRA Adapters")
print("=" * 80)
# Load base model
print(f"\n📥 Loading base model from {model_path}...")
model, tokenizer, config = utils.load(model_path)
# Freeze and add LoRA layers
print("🔧 Setting up LoRA layers...")
model.freeze()
num_layers = len(model.model.layers)
lora_layer_start = num_layers - lora_layers
for layer in model.model.layers[lora_layer_start:]:
layer.self_attn.q_proj = LoRALinear.from_linear(
layer.self_attn.q_proj, rank=lora_rank
)
layer.self_attn.v_proj = LoRALinear.from_linear(
layer.self_attn.v_proj, rank=lora_rank
)
if hasattr(layer, "block_sparse_moe"):
layer.block_sparse_moe.gate = LoRALinear.from_linear(
layer.block_sparse_moe.gate, rank=lora_rank
)
# Load adapter weights
print(f"📥 Loading adapter weights from {adapter_path}...")
if not Path(adapter_path).exists():
raise FileNotFoundError(
f"Adapter file not found: {adapter_path}\n"
"Please train the model first with train_function_calling.py"
)
model.load_weights(adapter_path, strict=False)
model.eval()
print("✅ Model loaded successfully!\n")
return model, tokenizer, config
def generate_response(model, tokenizer, prompt: str, max_tokens: int = 200, temp: float = 0.7):
"""
Generate a response for the given prompt.
Args:
model: Model to use for generation
tokenizer: Tokenizer
prompt: Input prompt
max_tokens: Maximum tokens to generate
temp: Sampling temperature
Returns:
Generated text
"""
# Encode prompt
prompt_tokens = mx.array(tokenizer.encode(prompt))
# Generate tokens
tokens = []
for token, n in zip(
utils.generate(prompt_tokens, model, temp),
range(max_tokens)
):
if token == tokenizer.eos_token_id:
break
tokens.append(token.item())
# Decode response
response = tokenizer.decode(tokens)
return response
def run_test_suite(model, tokenizer, test_cases=None, max_tokens=200, temp=0.7):
"""
Run a suite of test cases.
Args:
model: Model to test
tokenizer: Tokenizer
test_cases: List of test cases (uses default if None)
max_tokens: Maximum tokens to generate
temp: Sampling temperature
"""
if test_cases is None:
test_cases = TEST_CASES
print("=" * 80)
print(f"Running Function Calling Test Suite ({len(test_cases)} tests)")
print("=" * 80)
results = []
for i, test_case in enumerate(test_cases, 1):
print(f"\n{'=' * 80}")
print(f"Test {i}/{len(test_cases)}: {test_case['name']}")
print(f"{'=' * 80}")
# Show prompt
print(f"\n📝 Prompt:")
print("-" * 40)
print(test_case['prompt'])
print("-" * 40)
# Generate response
print(f"\n🤖 Generating response...")
try:
response = generate_response(
model,
tokenizer,
test_case['prompt'],
max_tokens=max_tokens,
temp=temp
)
print(f"\n✅ Response:")
print("-" * 40)
print(response)
print("-" * 40)
results.append({
"name": test_case['name'],
"success": True,
"response": response
})
except Exception as e:
print(f"\n❌ Error generating response: {e}")
results.append({
"name": test_case['name'],
"success": False,
"error": str(e)
})
# Print summary
print("\n" + "=" * 80)
print("Test Suite Summary")
print("=" * 80)
successful = sum(1 for r in results if r['success'])
print(f"\n✅ Passed: {successful}/{len(results)}")
print(f"❌ Failed: {len(results) - successful}/{len(results)}")
if successful < len(results):
print("\nFailed tests:")
for r in results:
if not r['success']:
print(f" • {r['name']}: {r.get('error', 'Unknown error')}")
def interactive_mode(model, tokenizer, max_tokens=200, temp=0.7):
"""
Run interactive testing mode.
Args:
model: Model to test
tokenizer: Tokenizer
max_tokens: Maximum tokens to generate
temp: Sampling temperature
"""
print("\n" + "=" * 80)
print("Interactive Function Calling Mode")
print("=" * 80)
print("\nEnter your prompts in the format:")
print("<user>Your question</user>\\n\\n<tools>")
print("\nType 'exit' or 'quit' to stop")
print("Type 'examples' to see example prompts")
print("=" * 80 + "\n")
while True:
try:
# Get user input
print("\n🎤 Your prompt (press Enter twice to submit):")
lines = []
while True:
line = input()
if line == "":
if lines: # Empty line after content = submit
break
continue
lines.append(line)
prompt = "\n".join(lines).strip()
# Check for commands
if prompt.lower() in ['exit', 'quit']:
print("\n👋 Goodbye!")
break
if prompt.lower() == 'examples':
print("\n📋 Example prompts:")
for i, test in enumerate(TEST_CASES, 1):
print(f"\n{i}. {test['name']}:")
print(test['prompt'])
continue
if not prompt:
continue
# Generate response
print("\n🤖 Generating response...")
response = generate_response(model, tokenizer, prompt, max_tokens, temp)
print("\n✅ Response:")
print("-" * 80)
print(response)
print("-" * 80)
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
break
except Exception as e:
print(f"\n❌ Error: {e}")
def main():
"""Main testing script."""
parser = argparse.ArgumentParser(
description="Test function calling fine-tuned model"
)
# Model configuration
parser.add_argument(
"--model",
type=str,
default="mlx_model",
help="Path to base model directory (default: mlx_model)"
)
parser.add_argument(
"--adapter",
type=str,
default="adapters.npz",
help="Path to adapter weights (default: adapters.npz)"
)
# LoRA configuration
parser.add_argument(
"--lora-layers",
type=int,
default=16,
help="Number of LoRA layers (default: 16)"
)
parser.add_argument(
"--lora-rank",
type=int,
default=8,
help="LoRA rank (default: 8)"
)
# Generation configuration
parser.add_argument(
"--max-tokens",
type=int,
default=200,
help="Maximum tokens to generate (default: 200)"
)
parser.add_argument(
"--temp",
type=float,
default=0.7,
help="Sampling temperature (default: 0.7)"
)
# Mode selection
parser.add_argument(
"--interactive",
"-i",
action="store_true",
help="Run in interactive mode"
)
parser.add_argument(
"--prompt",
"-p",
type=str,
default=None,
help="Single prompt to test"
)
args = parser.parse_args()
# Load model
model, tokenizer, config = load_model_with_adapter(
args.model,
args.adapter,
args.lora_layers,
args.lora_rank
)
# Run appropriate mode
if args.prompt:
# Single prompt mode
print("=" * 80)
print("Single Prompt Test")
print("=" * 80)
print(f"\n📝 Prompt:\n{args.prompt}\n")
response = generate_response(
model,
tokenizer,
args.prompt,
args.max_tokens,
args.temp
)
print(f"✅ Response:\n{response}")
elif args.interactive:
# Interactive mode
interactive_mode(model, tokenizer, args.max_tokens, args.temp)
else:
# Run test suite
run_test_suite(model, tokenizer, TEST_CASES, args.max_tokens, args.temp)
if __name__ == "__main__":
main()