|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to render a prompt instance by replacing placeholders with actual test and contract paths |
| 4 | +from the first entry in the ranking JSON file. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def load_ranking_data(): |
| 12 | + """Load the ranking JSON file and return the first entry.""" |
| 13 | + ranking_file = ( |
| 14 | + Path(__file__).parent.parent / "tests_ranker" / "output" / "ranking.json" |
| 15 | + ) |
| 16 | + |
| 17 | + with open(ranking_file, "r") as f: |
| 18 | + data = json.load(f) |
| 19 | + |
| 20 | + if not data.get("entries"): |
| 21 | + raise ValueError("No entries found in ranking.json") |
| 22 | + |
| 23 | + return data["entries"][0] |
| 24 | + |
| 25 | + |
| 26 | +def load_prompt_template(): |
| 27 | + """Load the prompt template markdown file.""" |
| 28 | + prompt_file = Path(__file__).parent.parent / "prompt" / "prompt.md" |
| 29 | + |
| 30 | + with open(prompt_file, "r") as f: |
| 31 | + return f.read() |
| 32 | + |
| 33 | + |
| 34 | +def render_prompt(template, test_path, contract_path): |
| 35 | + """Replace the placeholders in the template with actual paths.""" |
| 36 | + return template.replace("{TEST_PATH}", test_path).replace( |
| 37 | + "{CONTRACT_PATH}", contract_path |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def save_prompt_instance(rendered_prompt, test_path, contract_path): |
| 42 | + """Save the rendered prompt to the output folder with a descriptive name.""" |
| 43 | + output_dir = Path(__file__).parent / "output" |
| 44 | + output_dir.mkdir(exist_ok=True) |
| 45 | + |
| 46 | + # Extract test name and remove .t suffix if present |
| 47 | + test_name = Path(test_path).stem |
| 48 | + if test_name.endswith(".t"): |
| 49 | + test_name = test_name[:-2] |
| 50 | + |
| 51 | + # Create descriptive filename |
| 52 | + filename = f"{test_name}_prompt.md" |
| 53 | + output_file = output_dir / filename |
| 54 | + |
| 55 | + with open(output_file, "w") as f: |
| 56 | + f.write(rendered_prompt) |
| 57 | + |
| 58 | + return output_file |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + """Main function to render and save the prompt instance.""" |
| 63 | + try: |
| 64 | + # Load the first entry from ranking |
| 65 | + first_entry = load_ranking_data() |
| 66 | + test_path = first_entry["test_path"] |
| 67 | + contract_path = first_entry["contract_path"] |
| 68 | + |
| 69 | + print(f"Using first ranking entry:") |
| 70 | + print(f" Test path: {test_path}") |
| 71 | + print(f" Contract path: {contract_path}") |
| 72 | + |
| 73 | + # Load prompt template |
| 74 | + template = load_prompt_template() |
| 75 | + |
| 76 | + # Render the prompt with actual paths |
| 77 | + rendered_prompt = render_prompt(template, test_path, contract_path) |
| 78 | + |
| 79 | + # Save the rendered prompt |
| 80 | + output_file = save_prompt_instance(rendered_prompt, test_path, contract_path) |
| 81 | + |
| 82 | + print(f"Prompt instance saved to: {output_file}") |
| 83 | + |
| 84 | + except Exception as e: |
| 85 | + print(f"Error: {e}") |
| 86 | + return 1 |
| 87 | + |
| 88 | + return 0 |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + exit(main()) |
0 commit comments