-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_research_study.py
More file actions
575 lines (460 loc) · 24.4 KB
/
final_research_study.py
File metadata and controls
575 lines (460 loc) · 24.4 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#!/usr/bin/env python3
"""
Final API Documentation Quality Research Study
Uses pre-extracted documentation from our successful test
"""
import asyncio
import json
import time
from datetime import datetime
from typing import Dict, List, Optional
from dataclasses import dataclass, asdict
import logging
# Configure logging without emojis
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('final_research_study.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
@dataclass
class APIConfig:
"""Configuration for an API in the research study"""
name: str
domain: str
quality_level: float
auth_method: str
documentation_url: str
test_endpoint: str
env_key: str
auth_type: str
notes: Optional[str] = None
@dataclass
class ExperimentResult:
"""Result of a single code generation experiment"""
api_name: str
domain: str
quality_level: float
auth_method: str
llm_model: str
attempt_number: int
timestamp: str
# Documentation extraction
documentation_extracted: bool
documentation_length: int
extraction_time_ms: int
# Code generation
code_generated: bool
generation_time_ms: int
generated_code: Optional[str]
code_length: int
# Code execution
execution_status: str
execution_time_ms: int
api_response_code: Optional[int]
error_message: Optional[str]
# Quality metrics
has_error_handling: bool
has_authentication: bool
has_proper_imports: bool
complexity_score: int
class FinalAPIResearchStudy:
"""Final research study using pre-extracted documentation"""
def __init__(self):
self.results: List[ExperimentResult] = []
self.start_time = datetime.now()
self.verified_apis = self.load_verified_apis()
self.documentation_data = self.load_documentation_data()
self.llm_models = ['claude', 'gpt', 'gemini']
self.attempts_per_api_per_model = 1
# Filter APIs to only those with documentation
self.apis_with_docs = [api for api in self.verified_apis
if api.name in self.documentation_data and
self.documentation_data[api.name]['extraction_success']]
logger.info("FINAL API Documentation Quality Research Study")
logger.info(f"Testing {len(self.apis_with_docs)} APIs with documentation x {len(self.llm_models)} LLMs x {self.attempts_per_api_per_model} attempts")
logger.info(f"Total experiments: {len(self.apis_with_docs) * len(self.llm_models) * self.attempts_per_api_per_model}")
def load_verified_apis(self) -> List[APIConfig]:
"""Load the verified APIs from authentication test results"""
# Load authentication results
try:
with open('api_authentication_results.json', 'r') as f:
auth_results = json.load(f)
successful_apis = [r for r in auth_results['results'] if r['status'] == 'success']
logger.info(f"Loaded {len(successful_apis)} verified APIs from authentication test")
except FileNotFoundError:
logger.error("Authentication results not found. Please run api_authentication_test.py first")
raise
# Define API configurations
api_configs = [
# Weather APIs
APIConfig("OpenWeatherMap", "Weather", 4.0, "API Key (Query)",
"https://openweathermap.org/api/one-call-3",
"https://api.openweathermap.org/data/2.5/weather",
"OPENWEATHER_API_KEY", "query"),
APIConfig("Visual Crossing", "Weather", 4.5, "API Key (Query)",
"https://www.visualcrossing.com/resources/documentation/weather-api/timeline-weather-api/",
"https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline",
"VISUAL_CROSSING_KEY", "query"),
APIConfig("WeatherBit", "Weather", 3.0, "API Key (Query)",
"https://www.weatherbit.io/api/weather-current",
"https://api.weatherbit.io/v2.0/current",
"WEATHERBIT_KEY", "query"),
# News APIs
APIConfig("NewsAPI", "News", 4.0, "API Key (Header)",
"https://newsapi.org/docs/endpoints/top-headlines",
"https://newsapi.org/v2/top-headlines",
"NEWS_API_KEY", "header"),
APIConfig("NewsData.io", "News", 4.0, "API Key (Query)",
"https://newsdata.io/documentation/#latest_news",
"https://newsdata.io/api/1/news",
"NEWSDATA_API_KEY", "query"),
APIConfig("Guardian API", "News", 3.5, "API Key (Query)",
"https://open-platform.theguardian.com/documentation/",
"https://content.guardianapis.com/search",
"GUARDIAN_API_KEY", "query"),
APIConfig("Currents API", "News", 3.5, "API Key (Query)",
"https://currentsapi.services/en/docs/",
"https://api.currentsapi.services/v1/latest-news",
"CURRENTS_API_KEY", "query"),
APIConfig("Mediastack", "News", 2.5, "API Key (Query)",
"https://mediastack.com/documentation",
"http://api.mediastack.com/v1/news",
"MEDIASTACK_API_KEY", "query"),
# Currency APIs
APIConfig("ExchangeRate-API", "Currency", 4.0, "API Key (Path)",
"https://www.exchangerate-api.com/docs/overview",
"https://v6.exchangerate-api.com/v6",
"EXCHANGERATE_API_KEY", "path"),
APIConfig("Fixer.io", "Currency", 3.0, "API Key (Query)",
"https://fixer.io/documentation",
"http://data.fixer.io/api/latest",
"FIXER_API_KEY", "query"),
APIConfig("CurrencyAPI", "Currency", 3.0, "API Key (Query)",
"https://currencyapi.com/docs",
"https://api.currencyapi.com/v3/latest",
"CURRENCY_API_KEY", "query"),
APIConfig("CurrencyLayer", "Currency", 2.0, "API Key (Query)",
"https://currencylayer.com/documentation",
"http://api.currencylayer.com/live",
"CURRENCYLAYER_API_KEY", "query"),
# User Management APIs
APIConfig("Clerk", "User Management", 2.0, "Bearer Token",
"https://clerk.com/docs/reference/backend-api",
"https://api.clerk.dev/v1/users",
"CLERK_API_KEY", "bearer"),
# File Storage APIs
APIConfig("Uploadcare", "File Storage", 3.5, "Public + Secret Key",
"https://uploadcare.com/docs/api_reference/rest/",
"https://api.uploadcare.com/files/",
"UPLOADCARE_PUBLIC_KEY", "basic"),
# Utility APIs
APIConfig("IPGeolocation", "Utility", 3.0, "API Key (Query)",
"https://ipgeolocation.io/documentation/ip-geolocation-api.html",
"https://api.ipgeolocation.io/ipgeo",
"IPGEOLOCATION_API_KEY", "query")
]
# Filter to only include APIs that passed authentication
verified_names = {api['name'] for api in successful_apis}
verified_configs = [config for config in api_configs if config.name in verified_names]
logger.info(f"Configured {len(verified_configs)} APIs for final research study")
return verified_configs
def load_documentation_data(self) -> Dict:
"""Load pre-extracted documentation data"""
try:
with open('documentation_extraction_test_results.json', 'r') as f:
doc_results = json.load(f)
doc_data = doc_results['api_results']
successful_docs = len([api for api in doc_data.values() if api['extraction_success']])
logger.info(f"Loaded documentation for {successful_docs}/{len(doc_data)} APIs")
return doc_data
except FileNotFoundError:
logger.error("Documentation extraction results not found. Please run test_documentation_solutions.py first")
raise
async def run_final_study(self):
"""Execute the final research study pipeline"""
logger.info("Starting Final API Documentation Quality Research Study")
logger.info("=" * 80)
if len(self.apis_with_docs) == 0:
logger.error("No APIs with documentation available. Cannot proceed with study.")
return
# Import components
from llm_code_generator import LLMCodeGenerator
from code_execution_engine import CodeExecutionEngine
# Initialize components
code_generator = LLMCodeGenerator()
code_executor = CodeExecutionEngine()
try:
# Initialize components
await code_generator.initialize()
logger.info("All components initialized")
# Run experiments for each API with documentation
total_experiments = len(self.apis_with_docs) * len(self.llm_models) * self.attempts_per_api_per_model
current_experiment = 0
for api_config in self.apis_with_docs:
logger.info(f"\nProcessing API: {api_config.name} ({api_config.domain})")
logger.info(f"Quality Level: {api_config.quality_level}/5.0")
# Get pre-extracted documentation
doc_data = self.documentation_data[api_config.name]
documentation = doc_data['content_preview'] # Use preview for now
doc_time = doc_data['extraction_time_ms']
logger.info(f"Using pre-extracted documentation ({len(documentation)} characters)")
# Generate and test code with each LLM model
for model in self.llm_models:
for attempt in range(self.attempts_per_api_per_model):
current_experiment += 1
logger.info(f"[{current_experiment}/{total_experiments}] {api_config.name} - {model} - Attempt {attempt + 1}")
result = await self.run_single_experiment(
api_config, documentation, doc_time, model, attempt + 1,
code_generator, code_executor
)
self.results.append(result)
# Log immediate result
if result.execution_status == "full_success":
logger.info(f"SUCCESS: {result.api_name} - {result.llm_model}")
else:
logger.info(f"{result.execution_status.upper()}: {result.api_name} - {result.llm_model}")
# Brief pause between experiments
await asyncio.sleep(0.5)
# Generate final analysis and report
await self.generate_final_report()
except Exception as e:
logger.error(f"Research study failed: {e}")
raise
finally:
# Cleanup components
await code_generator.cleanup()
logger.info("Final research study completed successfully!")
async def run_single_experiment(self, api_config: APIConfig, documentation: str, doc_time: int,
model: str, attempt: int, code_generator, code_executor) -> ExperimentResult:
"""Run a single code generation and testing experiment"""
timestamp = datetime.now().isoformat()
# Generate code
code_success, generated_code, gen_time, model_used = await code_generator.generate_code(
api_config.name, documentation, api_config.auth_type, api_config.env_key
)
if not code_success:
return ExperimentResult(
api_name=api_config.name, domain=api_config.domain, quality_level=api_config.quality_level,
auth_method=api_config.auth_method, llm_model=model, attempt_number=attempt, timestamp=timestamp,
documentation_extracted=True, documentation_length=len(documentation), extraction_time_ms=doc_time,
code_generated=False, generation_time_ms=gen_time, generated_code=None, code_length=0,
execution_status="generation_failed", execution_time_ms=0, api_response_code=None,
error_message="Code generation failed", has_error_handling=False, has_authentication=False,
has_proper_imports=False, complexity_score=0
)
# Execute and test code
execution_status, exec_time, response_code, error_msg, quality_metrics = await code_executor.execute_code(
api_config.name, generated_code, api_config.env_key
)
return ExperimentResult(
api_name=api_config.name, domain=api_config.domain, quality_level=api_config.quality_level,
auth_method=api_config.auth_method, llm_model=model_used, attempt_number=attempt, timestamp=timestamp,
documentation_extracted=True, documentation_length=len(documentation), extraction_time_ms=doc_time,
code_generated=True, generation_time_ms=gen_time, generated_code=generated_code, code_length=len(generated_code),
execution_status=execution_status, execution_time_ms=exec_time, api_response_code=response_code,
error_message=error_msg, has_error_handling=quality_metrics.get('has_error_handling', False),
has_authentication=quality_metrics.get('has_authentication', False),
has_proper_imports=quality_metrics.get('has_proper_imports', False),
complexity_score=quality_metrics.get('complexity_score', 0)
)
async def generate_final_report(self):
"""Generate comprehensive research report with statistical analysis"""
logger.info("\nGenerating Final Research Report")
logger.info("=" * 60)
# Save raw results
results_data = [asdict(result) for result in self.results]
with open('final_research_results.json', 'w') as f:
json.dump({
"study_metadata": {
"start_time": self.start_time.isoformat(),
"end_time": datetime.now().isoformat(),
"total_experiments": len(self.results),
"apis_tested": len(self.apis_with_docs),
"llm_models": self.llm_models,
"attempts_per_api_per_model": self.attempts_per_api_per_model,
"study_type": "Final - Using Pre-extracted Documentation"
},
"results": results_data
}, f, indent=2)
# Generate statistical analysis
analysis = self.perform_statistical_analysis()
# Generate comprehensive report
self.generate_research_report(analysis)
logger.info("Final research report generated successfully")
def perform_statistical_analysis(self):
"""Perform statistical analysis of results"""
logger.info("Performing Statistical Analysis")
# Calculate success rates by quality level
quality_analysis = {}
domain_analysis = {}
auth_analysis = {}
model_analysis = {}
for result in self.results:
# Quality level analysis
quality = result.quality_level
if quality not in quality_analysis:
quality_analysis[quality] = {"total": 0, "success": 0}
quality_analysis[quality]["total"] += 1
if result.execution_status == "full_success":
quality_analysis[quality]["success"] += 1
# Domain analysis
domain = result.domain
if domain not in domain_analysis:
domain_analysis[domain] = {"total": 0, "success": 0}
domain_analysis[domain]["total"] += 1
if result.execution_status == "full_success":
domain_analysis[domain]["success"] += 1
# Authentication method analysis
auth = result.auth_method
if auth not in auth_analysis:
auth_analysis[auth] = {"total": 0, "success": 0}
auth_analysis[auth]["total"] += 1
if result.execution_status == "full_success":
auth_analysis[auth]["success"] += 1
# LLM model analysis
model = result.llm_model
if model not in model_analysis:
model_analysis[model] = {"total": 0, "success": 0}
model_analysis[model]["total"] += 1
if result.execution_status == "full_success":
model_analysis[model]["success"] += 1
# Calculate success rates
for analysis in [quality_analysis, domain_analysis, auth_analysis, model_analysis]:
for key in analysis:
total = analysis[key]["total"]
success = analysis[key]["success"]
analysis[key]["success_rate"] = (success / total * 100) if total > 0 else 0
# Save analysis results
analysis_results = {
"quality_level_analysis": quality_analysis,
"domain_analysis": domain_analysis,
"auth_method_analysis": auth_analysis,
"llm_model_analysis": model_analysis
}
with open('final_statistical_analysis.json', 'w') as f:
json.dump(analysis_results, f, indent=2)
# Log key findings
logger.info("Key Statistical Findings:")
# Quality level correlation
quality_rates = [(q, data["success_rate"]) for q, data in quality_analysis.items()]
quality_rates.sort()
logger.info("Success rates by quality level:")
for quality, rate in quality_rates:
logger.info(f" {quality}/5.0: {rate:.1f}%")
# Best performing domain
if domain_analysis:
best_domain = max(domain_analysis.items(), key=lambda x: x[1]["success_rate"])
logger.info(f"Best performing domain: {best_domain[0]} ({best_domain[1]['success_rate']:.1f}%)")
# Best performing LLM
if model_analysis:
best_model = max(model_analysis.items(), key=lambda x: x[1]["success_rate"])
logger.info(f"Best performing LLM: {best_model[0]} ({best_model[1]['success_rate']:.1f}%)")
return analysis_results
def generate_research_report(self, analysis):
"""Generate comprehensive research report"""
# Calculate overall statistics
total_experiments = len(self.results)
successful_experiments = len([r for r in self.results if r.execution_status == "full_success"])
overall_success_rate = (successful_experiments / total_experiments * 100) if total_experiments > 0 else 0
# Generate markdown report
report = f"""# API Documentation Quality Research Study - Final Results
## Executive Summary
This study investigated the relationship between API documentation quality and LLM code generation accuracy using {len(self.apis_with_docs)} APIs with successfully extracted documentation across multiple domains.
### Key Findings
- **Overall Success Rate**: {overall_success_rate:.1f}% ({successful_experiments}/{total_experiments} experiments)
- **APIs Tested**: {len(self.apis_with_docs)} APIs with verified documentation
- **LLM Models**: {len(self.llm_models)} different models (Claude, GPT, Gemini)
- **Total Experiments**: {total_experiments} code generation and testing cycles
- **Study Type**: Final study using pre-extracted documentation
## Research Question: "How does API documentation quality affect LLM code generation accuracy?"
### Methodology
1. **API Selection**: 15 verified APIs across 6 domains with authentication confirmed
2. **Documentation Extraction**: Pre-extracted using enhanced Playwright with anti-bot bypass
3. **Code Generation**: LLM-generated Python integration code using live documentation
4. **Real API Testing**: Execution against actual APIs with verified authentication
5. **Success Measurement**: Full success, partial success, syntax errors, logic errors
### Quality Levels Tested
"""
# Add quality level breakdown
quality_analysis = analysis["quality_level_analysis"]
for quality in sorted(quality_analysis.keys()):
data = quality_analysis[quality]
report += f"- **{quality}/5.0**: {data['success_rate']:.1f}% success rate ({data['success']}/{data['total']} experiments)\n"
report += f"""
## Results by Domain
"""
# Add domain analysis
domain_analysis = analysis["domain_analysis"]
for domain, data in sorted(domain_analysis.items(), key=lambda x: x[1]["success_rate"], reverse=True):
report += f"- **{domain}**: {data['success_rate']:.1f}% success rate ({data['success']}/{data['total']} experiments)\n"
report += f"""
## Results by LLM Model
"""
# Add model analysis
model_analysis = analysis["llm_model_analysis"]
for model, data in sorted(model_analysis.items(), key=lambda x: x[1]["success_rate"], reverse=True):
report += f"- **{model}**: {data['success_rate']:.1f}% success rate ({data['success']}/{data['total']} experiments)\n"
# Add error analysis
error_types = {}
for result in self.results:
status = result.execution_status
if status not in error_types:
error_types[status] = 0
error_types[status] += 1
report += f"""
## Error Analysis
"""
for error_type, count in sorted(error_types.items(), key=lambda x: x[1], reverse=True):
percentage = (count / total_experiments * 100)
report += f"- **{error_type.replace('_', ' ').title()}**: {count} ({percentage:.1f}%)\n"
report += f"""
## Key Research Insights
### Documentation Quality Impact
[Analysis of correlation between quality levels and success rates based on results]
### LLM Model Performance Comparison
[Comparison of Claude, GPT, and Gemini performance across different quality levels]
### Domain-Specific Patterns
[Which API domains work best with LLMs and why]
### Authentication Complexity Impact
[How different authentication methods affect success rates]
## Study Limitations
- Sample size: {len(self.apis_with_docs)} APIs with documentation
- Single attempt per model (optimized for speed)
- Time period: Single study execution
- Documentation extraction challenges limited API coverage
## Data Availability
- Raw results: `final_research_results.json`
- Statistical analysis: `final_statistical_analysis.json`
- Study logs: `final_research_study.log`
## Conclusions
This study provides empirical evidence about the relationship between API documentation quality and LLM code generation accuracy. The results demonstrate [key findings based on actual data].
---
*Final study completed: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*
*Total execution time: {datetime.now() - self.start_time}*
"""
# Save report
with open('Final_API_Documentation_Quality_Research_Report.md', 'w') as f:
f.write(report)
logger.info("Comprehensive research report saved to: Final_API_Documentation_Quality_Research_Report.md")
# Main execution
async def main():
"""Execute the final research study"""
study = FinalAPIResearchStudy()
await study.run_final_study()
if __name__ == "__main__":
print("Final API Documentation Quality Research Study")
print("Testing how documentation quality affects LLM code generation accuracy")
print("Using pre-extracted documentation from successful extraction test")
print(f"Expected completion time: ~5-10 minutes")
print("\nPress Ctrl+C to stop at any time")
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nFinal research study stopped by user")
except Exception as e:
print(f"\nFatal error: {e}")
print("Please check the logs for detailed error information")