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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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"""
generate_training_data.py - Generate comprehensive training data for function calling

This script creates 100+ diverse preference pairs covering many different schema types
and patterns to teach robust zero-shot function calling.
"""

import json
import random
from typing import List, Dict

def create_training_pair(schema: Dict, question: str, good_response: str, bad_response: str) -> Dict:
    """Create a single training pair in the correct format."""
    prompt = f"""<|im_start|>system
You are a helpful assistant that calls functions by responding with valid JSON when given a schema. Always respond with JSON function calls only, never prose.<|im_end|>

<schema>
{json.dumps(schema, indent=2)}
</schema>

<|im_start|>user
{question}<|im_end|>
<|im_start|>assistant
"""
    
    return {
        "prompt": prompt,
        "chosen": good_response,
        "rejected": bad_response
    }

def generate_diverse_schemas_and_pairs() -> List[Dict]:
    """Generate a comprehensive set of training pairs."""
    
    pairs = []
    
    # 1. FINANCIAL SCHEMAS (15 pairs)
    financial_schemas = [
        {
            "name": "get_stock_price",
            "description": "Get current stock price for a ticker",
            "parameters": {
                "type": "object",
                "properties": {"ticker": {"type": "string"}},
                "required": ["ticker"]
            }
        },
        {
            "name": "transfer_money", 
            "description": "Transfer money between accounts",
            "parameters": {
                "type": "object",
                "properties": {
                    "from_account": {"type": "string"},
                    "to_account": {"type": "string"}, 
                    "amount": {"type": "number"},
                    "currency": {"type": "string"}
                },
                "required": ["from_account", "to_account", "amount"]
            }
        },
        {
            "name": "calculate_compound_interest",
            "description": "Calculate compound interest on investment",
            "parameters": {
                "type": "object",
                "properties": {
                    "principal": {"type": "number"},
                    "rate": {"type": "number"},
                    "time": {"type": "number"},
                    "frequency": {"type": "integer"}
                },
                "required": ["principal", "rate", "time"]
            }
        }
    ]
    
    financial_questions = [
        ("What's Tesla stock trading at?", "TSLA"),
        ("Check the price of Bitcoin", "BTC-USD"), 
        ("What's Apple's current price?", "AAPL"),
        ("How much is Microsoft worth?", "MSFT"),
        ("Get Netflix stock price", "NFLX")
    ]
    
    for q, ticker in financial_questions:
        pairs.append(create_training_pair(
            financial_schemas[0], q,
            f'{{"name": "get_stock_price", "arguments": {{"ticker": "{ticker}"}}}}',
            f"I'll check the current stock price for {ticker}. Let me get that information for you."
        ))
    
    # Money transfer examples
    transfer_examples = [
        ("Send $500 from my checking to savings", "checking", "savings", 500),
        ("Transfer 1000 euros from account A to account B", "A", "B", 1000),
        ("Move $250 from wallet to investment account", "wallet", "investment", 250)
    ]
    
    for q, from_acc, to_acc, amount in transfer_examples:
        pairs.append(create_training_pair(
            financial_schemas[1], q,
            f'{{"name": "transfer_money", "arguments": {{"from_account": "{from_acc}", "to_account": "{to_acc}", "amount": {amount}}}}}',
            f"I'll help you transfer ${amount} from {from_acc} to {to_acc}. Let me process that transaction."
        ))
    
    # 2. COMMUNICATION SCHEMAS (20 pairs)
    comm_schemas = [
        {
            "name": "send_email",
            "description": "Send an email message",
            "parameters": {
                "type": "object",
                "properties": {
                    "to": {"type": "string"},
                    "subject": {"type": "string"},
                    "body": {"type": "string"},
                    "cc": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["to", "subject", "body"]
            }
        },
        {
            "name": "send_sms",
            "description": "Send SMS text message",
            "parameters": {
                "type": "object",
                "properties": {
                    "phone": {"type": "string"},
                    "message": {"type": "string"}
                },
                "required": ["phone", "message"]
            }
        },
        {
            "name": "schedule_meeting",
            "description": "Schedule a meeting with participants",
            "parameters": {
                "type": "object",
                "properties": {
                    "title": {"type": "string"},
                    "participants": {"type": "array", "items": {"type": "string"}},
                    "datetime": {"type": "string"},
                    "duration": {"type": "integer"}
                },
                "required": ["title", "participants", "datetime"]
            }
        }
    ]
    
    email_examples = [
        ("Email John about the project deadline", "[email protected]", "Project Deadline", "Hi John, wanted to discuss the upcoming project deadline."),
        ("Send Sarah the meeting notes", "[email protected]", "Meeting Notes", "Hi Sarah, here are the notes from today's meeting."),
        ("Message the team about tomorrow's standup", "[email protected]", "Standup Tomorrow", "Reminder: standup meeting tomorrow at 9am.")
    ]
    
    for q, to, subject, body in email_examples:
        pairs.append(create_training_pair(
            comm_schemas[0], q,
            f'{{"name": "send_email", "arguments": {{"to": "{to}", "subject": "{subject}", "body": "{body}"}}}}',
            f"I'll send an email to {to} with the subject '{subject}'. Let me compose that message for you."
        ))
    
    # SMS examples  
    sms_examples = [
        ("Text mom that I'll be late", "+1234567890", "Running late, will be there in 20 minutes"),
        ("Send SMS to 555-0123 saying meeting is cancelled", "555-0123", "Meeting cancelled"),
        ("Message Bob at +1987654321 about dinner plans", "+1987654321", "Are we still on for dinner tonight?")
    ]
    
    for q, phone, message in sms_examples:
        pairs.append(create_training_pair(
            comm_schemas[1], q,
            f'{{"name": "send_sms", "arguments": {{"phone": "{phone}", "message": "{message}"}}}}',
            f"I'll send a text message to {phone}. Let me send that SMS for you."
        ))
    
    # 3. DATA & ANALYTICS SCHEMAS (15 pairs)
    data_schemas = [
        {
            "name": "query_database", 
            "description": "Execute SQL query on database",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string"},
                    "database": {"type": "string"},
                    "limit": {"type": "integer"}
                },
                "required": ["query"]
            }
        },
        {
            "name": "generate_report",
            "description": "Generate analytics report",
            "parameters": {
                "type": "object", 
                "properties": {
                    "report_type": {"type": "string"},
                    "date_range": {"type": "string"},
                    "metrics": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["report_type", "date_range"]
            }
        }
    ]
    
    query_examples = [
        ("Find all users who signed up last week", "SELECT * FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 WEEK)"),
        ("Get top 10 selling products", "SELECT product_name, SUM(quantity) as total_sales FROM orders GROUP BY product_name ORDER BY total_sales DESC LIMIT 10"),
        ("Show revenue by month this year", "SELECT MONTH(order_date) as month, SUM(total) as revenue FROM orders WHERE YEAR(order_date) = YEAR(NOW()) GROUP BY MONTH(order_date)")
    ]
    
    for q, query in query_examples:
        pairs.append(create_training_pair(
            data_schemas[0], q,
            f'{{"name": "query_database", "arguments": {{"query": "{query}"}}}}',
            f"I'll run a database query to {q.lower()}. Let me execute that SQL for you."
        ))
    
    # 4. FILE & SYSTEM OPERATIONS (15 pairs)
    file_schemas = [
        {
            "name": "create_file",
            "description": "Create a new file with content",
            "parameters": {
                "type": "object",
                "properties": {
                    "filename": {"type": "string"},
                    "content": {"type": "string"},
                    "encoding": {"type": "string"}
                },
                "required": ["filename", "content"]
            }
        },
        {
            "name": "backup_files",
            "description": "Backup files to specified location", 
            "parameters": {
                "type": "object",
                "properties": {
                    "source_path": {"type": "string"},
                    "backup_path": {"type": "string"},
                    "compression": {"type": "boolean"}
                },
                "required": ["source_path", "backup_path"]
            }
        }
    ]
    
    file_examples = [
        ("Create a file called report.txt with the quarterly results", "report.txt", "Q3 2024 Quarterly Results\n\nRevenue: $2.5M\nGrowth: 15%"),
        ("Make a new file notes.md with meeting summary", "notes.md", "# Meeting Summary\n\n- Discussed project timeline\n- Reviewed budget\n- Next steps assigned"),
        ("Create config.json with default settings", "config.json", '{"debug": false, "port": 8080, "host": "localhost"}')
    ]
    
    for q, filename, content in file_examples:
        pairs.append(create_training_pair(
            file_schemas[0], q,
            f'{{"name": "create_file", "arguments": {{"filename": "{filename}", "content": "{content}"}}}}',
            f"I'll create the file {filename} with your content. Let me write that file for you."
        ))
    
    # 5. WEATHER & LOCATION SCHEMAS (10 pairs) 
    location_schemas = [
        {
            "name": "get_weather",
            "description": "Get weather information for location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"},
                    "units": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                    "forecast_days": {"type": "integer"}
                },
                "required": ["location"]
            }
        },
        {
            "name": "find_restaurants",
            "description": "Find restaurants near location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"},
                    "cuisine": {"type": "string"},
                    "rating_min": {"type": "number"}
                },
                "required": ["location"]
            }
        }
    ]
    
    weather_examples = [
        ("What's the weather in San Francisco?", "San Francisco"),
        ("Check weather for Tokyo in celsius", "Tokyo"),
        ("How's the weather in London today?", "London")
    ]
    
    for q, location in weather_examples:
        pairs.append(create_training_pair(
            location_schemas[0], q,
            f'{{"name": "get_weather", "arguments": {{"location": "{location}"}}}}',
            f"I'll check the current weather conditions in {location} for you."
        ))
    
    # 6. CALCULATION & UTILITY SCHEMAS (15 pairs)
    calc_schemas = [
        {
            "name": "calculate_tip",
            "description": "Calculate tip amount for bill",
            "parameters": {
                "type": "object",
                "properties": {
                    "bill_amount": {"type": "number"},
                    "tip_percentage": {"type": "number"},
                    "split_ways": {"type": "integer"}
                },
                "required": ["bill_amount", "tip_percentage"]
            }
        },
        {
            "name": "convert_currency",
            "description": "Convert between currencies",
            "parameters": {
                "type": "object",
                "properties": {
                    "amount": {"type": "number"},
                    "from_currency": {"type": "string"},
                    "to_currency": {"type": "string"}
                },
                "required": ["amount", "from_currency", "to_currency"]
            }
        },
        {
            "name": "calculate_distance",
            "description": "Calculate distance between two points",
            "parameters": {
                "type": "object",
                "properties": {
                    "from_location": {"type": "string"},
                    "to_location": {"type": "string"},
                    "unit": {"type": "string", "enum": ["miles", "kilometers"]}
                },
                "required": ["from_location", "to_location"]
            }
        }
    ]
    
    tip_examples = [
        ("What's 20% tip on $85?", 85, 20),
        ("Calculate 15% tip for a $42 bill", 42, 15),
        ("How much tip for $156 at 18%?", 156, 18)
    ]
    
    for q, amount, tip in tip_examples:
        pairs.append(create_training_pair(
            calc_schemas[0], q,
            f'{{"name": "calculate_tip", "arguments": {{"bill_amount": {amount}, "tip_percentage": {tip}}}}}',
            f"I'll calculate the {tip}% tip on ${amount} for you. Let me do that math."
        ))
    
    # 7. SCHEDULING & REMINDERS (10 pairs)
    schedule_schemas = [
        {
            "name": "create_reminder",
            "description": "Create a reminder for specific time",
            "parameters": {
                "type": "object",
                "properties": {
                    "title": {"type": "string"},
                    "datetime": {"type": "string"},
                    "priority": {"type": "string", "enum": ["low", "medium", "high"]}
                },
                "required": ["title", "datetime"]
            }
        },
        {
            "name": "book_appointment",
            "description": "Book appointment with service provider",
            "parameters": {
                "type": "object",
                "properties": {
                    "service": {"type": "string"},
                    "provider": {"type": "string"},
                    "datetime": {"type": "string"},
                    "duration": {"type": "integer"}
                },
                "required": ["service", "datetime"]
            }
        }
    ]
    
    reminder_examples = [
        ("Remind me to call mom tomorrow at 6pm", "Call mom", "tomorrow 6pm"),
        ("Set reminder for dentist appointment Friday 2pm", "Dentist appointment", "Friday 2pm"),
        ("Remind me about the meeting on Monday 9am", "Team meeting", "Monday 9am")
    ]
    
    for q, title, datetime in reminder_examples:
        pairs.append(create_training_pair(
            schedule_schemas[0], q,
            f'{{"name": "create_reminder", "arguments": {{"title": "{title}", "datetime": "{datetime}"}}}}',
            f"I'll set up a reminder for {title} at {datetime}."
        ))
    
    return pairs

def main():
    """Generate and save comprehensive training data."""
    print("🏭 Generating comprehensive training data...")
    
    pairs = generate_diverse_schemas_and_pairs()
    
    print(f"βœ… Generated {len(pairs)} training pairs")
    print("πŸ“Š Coverage:")
    print("   - Financial operations: 15 pairs")
    print("   - Communication: 20 pairs") 
    print("   - Data analytics: 15 pairs")
    print("   - File operations: 15 pairs")
    print("   - Weather/location: 10 pairs")
    print("   - Calculations: 15 pairs")
    print("   - Scheduling: 10 pairs")
    
    # Save to file
    with open("tool_pairs_large.jsonl", "w") as f:
        for pair in pairs:
            f.write(json.dumps(pair) + "\n")
    
    print(f"πŸ’Ύ Saved to tool_pairs_large.jsonl")
    print(f"πŸ“ˆ This should significantly improve training quality!")
    
    # Show sample
    print("\nπŸ“ Sample pair:")
    sample = pairs[0]
    print(f"Schema: {json.loads(sample['prompt'].split('<schema>')[1].split('</schema>')[0])['name']}")
    print(f"Question: {sample['prompt'].split('<|im_start|>user')[1].split('<|im_end|>')[0].strip()}")
    print(f"Response: {sample['chosen']}")

if __name__ == "__main__":
    main()