|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +from typing import Dict, List |
| 8 | + |
| 9 | +from aws_doc_sdk_examples_tools.doc_gen import DocGen, Snippet |
| 10 | + |
| 11 | +# Setup logging |
| 12 | +logging.basicConfig(level=logging.INFO) |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def make_doc_gen(root: Path) -> DocGen: |
| 17 | + """Create and return a DocGen instance from the given root directory.""" |
| 18 | + doc_gen = DocGen.from_root(root) |
| 19 | + doc_gen.collect_snippets() |
| 20 | + return doc_gen |
| 21 | + |
| 22 | + |
| 23 | +def write_prompts(snippets: Dict[str, Snippet], out: Path) -> None: |
| 24 | + """Write each snippet's code into a separate Markdown file.""" |
| 25 | + out.mkdir(parents=True, exist_ok=True) |
| 26 | + for snippet_id, snippet in snippets.items(): |
| 27 | + snippet_path = out / f"{snippet_id}.md" |
| 28 | + snippet_path.write_text(snippet.code, encoding="utf-8") |
| 29 | + |
| 30 | + |
| 31 | +def setup_ailly(system_prompts: List[str], out: Path) -> None: |
| 32 | + """Create the .aillyrc configuration file.""" |
| 33 | + fence = "---" |
| 34 | + options = {"isolated": "true"} |
| 35 | + options_block = "\n".join(f"{key}: {value}" for key, value in options.items()) |
| 36 | + prompts_block = "\n".join(system_prompts) |
| 37 | + |
| 38 | + content = f"{fence}\n{options_block}\n{fence}\n{prompts_block}" |
| 39 | + |
| 40 | + aillyrc_path = out / ".aillyrc" |
| 41 | + aillyrc_path.parent.mkdir(parents=True, exist_ok=True) |
| 42 | + aillyrc_path.write_text(content, encoding="utf-8") |
| 43 | + |
| 44 | + |
| 45 | +def parse_prompts_arg(values: List[str]) -> List[str]: |
| 46 | + """Parse system prompts from a list of strings or file paths.""" |
| 47 | + prompts = [] |
| 48 | + for value in values: |
| 49 | + if os.path.isfile(value): |
| 50 | + with open(value, "r", encoding="utf-8") as f: |
| 51 | + prompts.append(f.read()) |
| 52 | + else: |
| 53 | + prompts.append(value) |
| 54 | + return prompts |
| 55 | + |
| 56 | + |
| 57 | +def main(doc_gen_root: Path, system_prompts: List[str], out: str = ".ailly_prompts") -> None: |
| 58 | + """Generate prompts and configuration files for Ailly.""" |
| 59 | + out_path = Path(out) |
| 60 | + setup_ailly(system_prompts, out_path) |
| 61 | + |
| 62 | + doc_gen = make_doc_gen(doc_gen_root) |
| 63 | + write_prompts(doc_gen.snippets, out_path) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + parser = argparse.ArgumentParser( |
| 68 | + description="Write Ailly prompts for DocGen snippets and parse the results." |
| 69 | + ) |
| 70 | + parser.add_argument( |
| 71 | + "--doc-gen-root", required=True, |
| 72 | + help="Path to a DocGen ready project." |
| 73 | + ) |
| 74 | + parser.add_argument( |
| 75 | + "--system-prompts", nargs="+", required=True, |
| 76 | + help="List of prompt strings or file paths to store in a .aillyrc file." |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + "--out", |
| 80 | + default=".ailly_prompts", |
| 81 | + help="Directory where Ailly prompt files will be written. Defaults to '.ailly_prompts'." |
| 82 | + ) |
| 83 | + |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + doc_gen_root = Path(args.doc_gen_root) |
| 87 | + system_prompts = parse_prompts_arg(args.system_prompts) |
| 88 | + main(doc_gen_root, system_prompts, out=args.out) |
0 commit comments