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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
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
442
443
444
445
"""
generate_enhanced_training_data.py - Enhanced Training Data Generator

This script creates a comprehensive training dataset specifically designed to address
the JSON syntax issues identified in our evaluation:

1. Long string parameters with proper quote handling
2. Complex nested parameter structures  
3. Arrays and multiple parameter types
4. Edge cases with special characters
5. Real-world enterprise API patterns

Based on failure analysis: Most failures were "Expecting ',' delimiter" errors
indicating issues with quote handling in complex parameters.
"""

import json
import random
from typing import List, Dict, Any

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_base_examples():
    """Generate foundational examples similar to our original dataset."""
    examples = []
    
    # Simple stock example (working baseline)
    examples.append(create_training_pair(
        {
            "name": "get_stock_price",
            "description": "Get current stock price for a ticker",
            "parameters": {
                "type": "object",
                "properties": {
                    "ticker": {"type": "string"}
                },
                "required": ["ticker"]
            }
        },
        "What's Apple's current stock price?",
        '{"name": "get_stock_price", "arguments": {"ticker": "AAPL"}}',
        "I'll help you get Apple's current stock price using our market data."
    ))
    
    return examples

def generate_long_string_examples():
    """Generate examples with long string parameters - the main failure mode."""
    examples = []
    
    # Document analysis with long text
    examples.append(create_training_pair(
        {
            "name": "analyze_document", 
            "description": "Analyze document content for insights",
            "parameters": {
                "type": "object",
                "properties": {
                    "document_text": {"type": "string"},
                    "analysis_type": {"type": "string"},
                    "focus_areas": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["document_text", "analysis_type"]
            }
        },
        "Analyze this quarterly report: 'Q4 2024 showed remarkable growth with revenue increasing 25% year-over-year to $2.3 billion. Our flagship product line contributed 60% of total revenue, while emerging markets expanded by 40%. Key challenges include supply chain optimization and talent acquisition in our European operations.' Focus on financial performance and operational insights.",
        '{"name": "analyze_document", "arguments": {"document_text": "Q4 2024 showed remarkable growth with revenue increasing 25% year-over-year to $2.3 billion. Our flagship product line contributed 60% of total revenue, while emerging markets expanded by 40%. Key challenges include supply chain optimization and talent acquisition in our European operations.", "analysis_type": "comprehensive", "focus_areas": ["financial", "operational"]}}',
        "I'll analyze that quarterly report for you, focusing on the financial and operational aspects."
    ))
    
    # Email composition with complex content
    examples.append(create_training_pair(
        {
            "name": "send_email",
            "description": "Send an email message",
            "parameters": {
                "type": "object", 
                "properties": {
                    "to": {"type": "string"},
                    "subject": {"type": "string"},
                    "body": {"type": "string"},
                    "priority": {"type": "string"}
                },
                "required": ["to", "subject", "body"]
            }
        },
        "Send an email to [email protected] about the project delay with subject 'Project Timeline Update' explaining that due to unexpected technical challenges and resource constraints, we need to extend the deadline by two weeks to ensure quality delivery.",
        '{"name": "send_email", "arguments": {"to": "[email protected]", "subject": "Project Timeline Update", "body": "Due to unexpected technical challenges and resource constraints, we need to extend the deadline by two weeks to ensure quality delivery.", "priority": "high"}}',
        "I'll send that email about the project delay to John for you."
    ))
    
    # Complex query with special characters
    examples.append(create_training_pair(
        {
            "name": "search_database",
            "description": "Search database records",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string"},
                    "filters": {"type": "object"},
                    "limit": {"type": "integer"}
                },
                "required": ["query"]
            }
        },
        "Search for customers with names containing O'Brien or D'Angelo who registered after 2023-01-01 and have premium status, limit to 50 results",
        '{"name": "search_database", "arguments": {"query": "customers with names containing OBrien or DAngelo registered after 2023-01-01", "filters": {"status": "premium", "registration_date": ">2023-01-01"}, "limit": 50}}',
        "I'll search the database for those premium customers with Irish names registered this year."
    ))
    
    return examples

def generate_complex_parameter_examples():
    """Generate examples with complex nested parameters and arrays."""
    examples = []
    
    # API configuration with nested objects
    examples.append(create_training_pair(
        {
            "name": "configure_api_endpoint",
            "description": "Configure API endpoint settings",
            "parameters": {
                "type": "object",
                "properties": {
                    "endpoint": {"type": "string"},
                    "methods": {"type": "array", "items": {"type": "string"}},
                    "authentication": {
                        "type": "object",
                        "properties": {
                            "type": {"type": "string"},
                            "credentials": {"type": "object"}
                        }
                    },
                    "rate_limits": {
                        "type": "object",
                        "properties": {
                            "requests_per_minute": {"type": "integer"},
                            "burst_limit": {"type": "integer"}
                        }
                    }
                },
                "required": ["endpoint", "methods"]
            }
        },
        "Set up the /api/v2/users endpoint to accept GET and POST requests with OAuth2 authentication using client credentials, and rate limiting of 100 requests per minute with burst up to 150",
        '{"name": "configure_api_endpoint", "arguments": {"endpoint": "/api/v2/users", "methods": ["GET", "POST"], "authentication": {"type": "oauth2", "credentials": {"grant_type": "client_credentials"}}, "rate_limits": {"requests_per_minute": 100, "burst_limit": 150}}}',
        "I'll configure that API endpoint with OAuth2 authentication and the specified rate limits."
    ))
    
    # Data processing with multiple arrays
    examples.append(create_training_pair(
        {
            "name": "process_data_batch",
            "description": "Process a batch of data records",
            "parameters": {
                "type": "object",
                "properties": {
                    "input_files": {"type": "array", "items": {"type": "string"}},
                    "output_format": {"type": "string"},
                    "transformations": {"type": "array", "items": {"type": "object"}},
                    "validation_rules": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["input_files", "output_format"]
            }
        },
        "Process files data_2024_q1.csv, data_2024_q2.csv, and data_2024_q3.csv into JSON format with transformations to normalize currency fields and validate that all records have valid email addresses and phone numbers",
        '{"name": "process_data_batch", "arguments": {"input_files": ["data_2024_q1.csv", "data_2024_q2.csv", "data_2024_q3.csv"], "output_format": "json", "transformations": [{"type": "normalize", "field": "currency"}, {"type": "standardize", "field": "phone"}], "validation_rules": ["valid_email", "valid_phone", "required_fields"]}}',
        "I'll process those quarterly data files with currency normalization and validation rules."
    ))
    
    return examples

def generate_enterprise_api_examples():
    """Generate examples based on real enterprise API patterns.""" 
    examples = []
    
    # Financial reporting API
    examples.append(create_training_pair(
        {
            "name": "generate_financial_report",
            "description": "Generate comprehensive financial report",
            "parameters": {
                "type": "object",
                "properties": {
                    "report_type": {"type": "string"},
                    "date_range": {
                        "type": "object",
                        "properties": {
                            "start_date": {"type": "string"},
                            "end_date": {"type": "string"}
                        }
                    },
                    "departments": {"type": "array", "items": {"type": "string"}},
                    "metrics": {"type": "array", "items": {"type": "string"}},
                    "breakdown_by": {"type": "array", "items": {"type": "string"}},
                    "format": {"type": "string"},
                    "include_comparisons": {"type": "boolean"}
                },
                "required": ["report_type", "date_range", "departments"]
            }
        },
        "Create a quarterly P&L report for Sales, Marketing, and Operations departments from 2024-07-01 to 2024-09-30, including revenue, expenses, and profit margins broken down by region and product line in Excel format with year-over-year comparisons",
        '{"name": "generate_financial_report", "arguments": {"report_type": "profit_and_loss", "date_range": {"start_date": "2024-07-01", "end_date": "2024-09-30"}, "departments": ["Sales", "Marketing", "Operations"], "metrics": ["revenue", "expenses", "profit_margin"], "breakdown_by": ["region", "product_line"], "format": "excel", "include_comparisons": true}}',
        "I'll generate that quarterly P&L report with regional and product breakdowns plus YoY comparisons."
    ))
    
    # HR management system
    examples.append(create_training_pair(
        {
            "name": "update_employee_record",
            "description": "Update employee information in HR system",
            "parameters": {
                "type": "object",
                "properties": {
                    "employee_id": {"type": "string"},
                    "updates": {
                        "type": "object",
                        "properties": {
                            "personal_info": {"type": "object"},
                            "job_details": {"type": "object"},
                            "compensation": {"type": "object"}
                        }
                    },
                    "effective_date": {"type": "string"},
                    "approval_required": {"type": "boolean"},
                    "notification_settings": {"type": "object"}
                },
                "required": ["employee_id", "updates"]
            }
        },
        "Update employee EMP-12345's record with promotion to Senior Data Scientist in the Analytics team, salary increase to $135,000 annually, new manager Sarah Johnson (EMP-67890), effective January 15th 2025, requiring approval and sending notifications to HR and the employee",
        '{"name": "update_employee_record", "arguments": {"employee_id": "EMP-12345", "updates": {"personal_info": {"manager_id": "EMP-67890", "manager_name": "Sarah Johnson"}, "job_details": {"title": "Senior Data Scientist", "department": "Analytics", "team": "Analytics"}, "compensation": {"annual_salary": 135000, "currency": "USD"}}, "effective_date": "2025-01-15", "approval_required": true, "notification_settings": {"notify_hr": true, "notify_employee": true, "notify_manager": true}}}',
        "I'll update that employee record with the promotion details and compensation changes, requiring approvals."
    ))
    
    return examples

def generate_edge_case_examples():
    """Generate examples with tricky edge cases and special characters."""
    examples = []
    
    # JSON with quotes and escaping
    examples.append(create_training_pair(
        {
            "name": "create_content",
            "description": "Create content with rich formatting",
            "parameters": {
                "type": "object",
                "properties": {
                    "title": {"type": "string"},
                    "content": {"type": "string"},
                    "metadata": {"type": "object"},
                    "tags": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["title", "content"]
            }
        },
        "Create a blog post titled 'The New Era of AI: What's Next?' with content discussing how AI is transforming industries, including quotes from experts, tagged with AI, technology, and future",
        '{"name": "create_content", "arguments": {"title": "The New Era of AI: What\'s Next?", "content": "AI is transforming industries with experts saying AI will revolutionize everything and we are just getting started. The future holds tremendous potential.", "metadata": {"category": "technology", "author": "AI Research Team"}, "tags": ["AI", "technology", "future"]}}',
        "I'll create that blog post about AI's new era with the expert quotes and proper tagging."
    ))
    
    # Numbers, booleans, and mixed types
    examples.append(create_training_pair(
        {
            "name": "configure_system_settings",
            "description": "Configure system settings and parameters",
            "parameters": {
                "type": "object",
                "properties": {
                    "cache_size_mb": {"type": "integer"},
                    "enable_logging": {"type": "boolean"},
                    "log_level": {"type": "string"},
                    "timeout_seconds": {"type": "number"},
                    "allowed_origins": {"type": "array", "items": {"type": "string"}},
                    "feature_flags": {"type": "object"}
                },
                "required": ["cache_size_mb", "enable_logging"]
            }
        },
        "Set cache to 512 MB, enable logging at debug level, timeout of 30.5 seconds, allow origins from localhost:3000 and *.company.com, and enable experimental features for beta users but disable legacy support",
        '{"name": "configure_system_settings", "arguments": {"cache_size_mb": 512, "enable_logging": true, "log_level": "debug", "timeout_seconds": 30.5, "allowed_origins": ["localhost:3000", "*.company.com"], "feature_flags": {"experimental_features": true, "beta_user_access": true, "legacy_support": false}}}',
        "I'll configure those system settings with the specified cache, logging, and feature flags."
    ))
    
    return examples

def generate_real_world_failure_patterns():
    """Generate examples that specifically address the patterns that failed in our evaluation."""
    examples = []
    
    # Weather API (failed 2/3 in evaluation)
    examples.append(create_training_pair(
        {
            "name": "get_weather_forecast",
            "description": "Get weather forecast with detailed parameters",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"},
                    "days": {"type": "integer"},
                    "units": {"type": "string", "enum": ["metric", "imperial", "kelvin"]},
                    "include_hourly": {"type": "boolean"},
                    "alert_types": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["location", "days"]
            }
        },
        "Get a 5-day weather forecast for San Francisco, California in metric units with hourly breakdown and alerts for severe weather, precipitation, and temperature extremes",
        '{"name": "get_weather_forecast", "arguments": {"location": "San Francisco, California", "days": 5, "units": "metric", "include_hourly": true, "alert_types": ["severe_weather", "precipitation", "temperature_extremes"]}}',
        "I'll get that detailed 5-day forecast for San Francisco with hourly data and weather alerts."
    ))
    
    # Currency conversion (failed 3/3 in evaluation)
    examples.append(create_training_pair(
        {
            "name": "convert_currency",
            "description": "Convert currency amounts with detailed options",
            "parameters": {
                "type": "object",
                "properties": {
                    "amount": {"type": "number"},
                    "from_currency": {"type": "string"},
                    "to_currency": {"type": "string"},
                    "date": {"type": "string"},
                    "include_fees": {"type": "boolean"},
                    "precision": {"type": "integer"}
                },
                "required": ["amount", "from_currency", "to_currency"]
            }
        },
        "Convert 2,500.75 US dollars to Japanese yen using exchange rates from December 15th, 2024, include conversion fees, and show result with 2 decimal places precision",
        '{"name": "convert_currency", "arguments": {"amount": 2500.75, "from_currency": "USD", "to_currency": "JPY", "date": "2024-12-15", "include_fees": true, "precision": 2}}',
        "I'll convert that amount from USD to JPY using the specified date and including fees."
    ))
    
    # Sentiment analysis (failed 3/3 in evaluation)  
    examples.append(create_training_pair(
        {
            "name": "analyze_sentiment",
            "description": "Analyze text sentiment with advanced options",
            "parameters": {
                "type": "object",
                "properties": {
                    "text": {"type": "string"},
                    "language": {"type": "string"},
                    "include_emotions": {"type": "boolean"},
                    "confidence_threshold": {"type": "number"},
                    "aspects": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["text"]
            }
        },
        "Analyze the sentiment of this customer review: 'The product quality exceeded my expectations, but the delivery was delayed by a week. Customer service was helpful in resolving the issue.' Include emotion analysis and focus on product quality, delivery, and customer service aspects with 0.8 confidence threshold",
        '{"name": "analyze_sentiment", "arguments": {"text": "The product quality exceeded my expectations, but the delivery was delayed by a week. Customer service was helpful in resolving the issue.", "language": "en", "include_emotions": true, "confidence_threshold": 0.8, "aspects": ["product_quality", "delivery", "customer_service"]}}',
        "I'll analyze the sentiment of that customer review, focusing on the specific aspects you mentioned."
    ))
    
    return examples

def main():
    """Generate comprehensive enhanced training dataset."""
    print("πŸ”„ Generating Enhanced Training Dataset...")
    
    all_examples = []
    
    # Add different categories of examples
    print("πŸ“ Adding base examples...")
    all_examples.extend(generate_base_examples())
    
    print("πŸ“ Adding long string examples...")
    all_examples.extend(generate_long_string_examples())
    
    print("πŸ“ Adding complex parameter examples...")
    all_examples.extend(generate_complex_parameter_examples())
    
    print("πŸ“ Adding enterprise API examples...")
    all_examples.extend(generate_enterprise_api_examples())
    
    print("πŸ“ Adding edge case examples...")
    all_examples.extend(generate_edge_case_examples())
    
    print("πŸ“ Adding real-world failure pattern examples...")
    all_examples.extend(generate_real_world_failure_patterns())
    
    # Add multiple variations of the most problematic patterns
    print("πŸ“ Adding extra variations for JSON syntax patterns...")
    for _ in range(5):
        all_examples.extend(generate_long_string_examples())
        all_examples.extend(generate_real_world_failure_patterns())
    
    # Save enhanced training data
    output_file = "tool_pairs_enhanced.jsonl"
    with open(output_file, 'w') as f:
        for example in all_examples:
            f.write(json.dumps(example) + '\n')
    
    print(f"βœ… Generated {len(all_examples)} enhanced training examples")
    print(f"πŸ’Ύ Saved to {output_file}")
    
    # Print summary
    categories = {
        "Base examples": len(generate_base_examples()),
        "Long string handling": len(generate_long_string_examples()) * 6,  # 5 extra variations
        "Complex parameters": len(generate_complex_parameter_examples()), 
        "Enterprise APIs": len(generate_enterprise_api_examples()),
        "Edge cases": len(generate_edge_case_examples()),
        "Failure patterns": len(generate_real_world_failure_patterns()) * 6  # 5 extra variations
    }
    
    print(f"\nπŸ“Š Training Data Composition:")
    for category, count in categories.items():
        print(f"   {category}: {count} examples")
    
    print(f"\n🎯 Key Improvements:")
    print(f"   β€’ JSON syntax edge cases with proper quote escaping")
    print(f"   β€’ Long string parameters (main failure mode)")
    print(f"   β€’ Complex nested objects and arrays")
    print(f"   β€’ Real enterprise API patterns")
    print(f"   β€’ Special characters and mixed data types")
    print(f"   β€’ 6x more examples for problematic patterns")
    
    return len(all_examples)

if __name__ == "__main__":
    main()