|
| 1 | +import json |
| 2 | + |
| 3 | +import codegen |
| 4 | +from codegen import Codebase |
| 5 | +from codegen.sdk.core.external_module import ExternalModule |
| 6 | +from codegen.sdk.core.import_resolution import Import |
| 7 | +from codegen.sdk.core.symbol import Symbol |
| 8 | + |
| 9 | + |
| 10 | +def hop_through_imports(imp: Import) -> Symbol | ExternalModule: |
| 11 | + """Finds the root symbol for an import""" |
| 12 | + if isinstance(imp.imported_symbol, Import): |
| 13 | + return hop_through_imports(imp.imported_symbol) |
| 14 | + return imp.imported_symbol |
| 15 | + |
| 16 | + |
| 17 | +def get_function_context(function) -> dict: |
| 18 | + """Get the implementation, dependencies, and usages of a function.""" |
| 19 | + context = { |
| 20 | + "implementation": {"source": function.source, "filepath": function.filepath}, |
| 21 | + "dependencies": [], |
| 22 | + "usages": [], |
| 23 | + } |
| 24 | + |
| 25 | + # Add dependencies |
| 26 | + for dep in function.dependencies: |
| 27 | + # Hop through imports to find the root symbols ource |
| 28 | + if isinstance(dep, Import): |
| 29 | + dep = hop_through_imports(dep) |
| 30 | + |
| 31 | + context["dependencies"].append({"source": dep.source, "filepath": dep.filepath}) |
| 32 | + |
| 33 | + # Add usages |
| 34 | + for usage in function.usages: |
| 35 | + context["usages"].append( |
| 36 | + { |
| 37 | + "source": usage.usage_symbol.source, |
| 38 | + "filepath": usage.usage_symbol.filepath, |
| 39 | + } |
| 40 | + ) |
| 41 | + |
| 42 | + return context |
| 43 | + |
| 44 | + |
| 45 | +@codegen.function("generate-training-data") |
| 46 | +def run(codebase: Codebase): |
| 47 | + """Generate training data using a node2vec-like approach for code embeddings. |
| 48 | +
|
| 49 | + This codemod: |
| 50 | + 1. Finds all functions in the codebase |
| 51 | + 2. For each function: |
| 52 | + - Captures its implementation |
| 53 | + - Lists all dependencies (with their implementations) |
| 54 | + - Lists all usages (with their implementations) |
| 55 | + 3. Outputs structured JSON data for training |
| 56 | + """ |
| 57 | + # Track all function contexts |
| 58 | + training_data = { |
| 59 | + "functions": [], |
| 60 | + "metadata": { |
| 61 | + "total_functions": len(codebase.functions), |
| 62 | + "total_processed": 0, |
| 63 | + "avg_dependencies": 0, |
| 64 | + "avg_usages": 0, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + # Process each function in the codebase |
| 69 | + for function in codebase.functions: |
| 70 | + # Skip if function is too small |
| 71 | + if len(function.source.split("\n")) < 2: |
| 72 | + continue |
| 73 | + |
| 74 | + # Get function context |
| 75 | + context = get_function_context(function) |
| 76 | + |
| 77 | + # Only keep functions with enough context |
| 78 | + if len(context["dependencies"]) + len(context["usages"]) > 0: |
| 79 | + training_data["functions"].append(context) |
| 80 | + |
| 81 | + # Update metadata |
| 82 | + training_data["metadata"]["total_processed"] = len(training_data["functions"]) |
| 83 | + if training_data["functions"]: |
| 84 | + training_data["metadata"]["avg_dependencies"] = sum( |
| 85 | + len(f["dependencies"]) for f in training_data["functions"] |
| 86 | + ) / len(training_data["functions"]) |
| 87 | + training_data["metadata"]["avg_usages"] = sum( |
| 88 | + len(f["usages"]) for f in training_data["functions"] |
| 89 | + ) / len(training_data["functions"]) |
| 90 | + |
| 91 | + # Print stats |
| 92 | + print(f"Processed {training_data['metadata']['total_processed']} functions") |
| 93 | + print(f"Average dependencies: {training_data['metadata']['avg_dependencies']:.2f}") |
| 94 | + print(f"Average usages: {training_data['metadata']['avg_usages']:.2f}") |
| 95 | + |
| 96 | + return training_data |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + print("Initializing codebase...") |
| 101 | + codebase = Codebase.from_repo("fastapi/fastapi") |
| 102 | + |
| 103 | + print("Generating training data...") |
| 104 | + training_data = run(codebase) |
| 105 | + |
| 106 | + print("Saving training data...") |
| 107 | + with open("training_data.json", "w") as f: |
| 108 | + json.dump(training_data, f, indent=2) |
| 109 | + print("Training data saved to training_data.json") |
0 commit comments