Skip to content

Commit 8e3321f

Browse files
authored
Add script for generating an Ailly context of snippet contents. (#159)
1 parent e3764af commit 8e3321f

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Example generated IAM policy descriptions
2+
```
3+
[
4+
{
5+
"title": "Allows Amazon SNS to send messages to an Amazon SQS dead-letter queue",
6+
"title_abbrev": "Allows SNS to SQS messages",
7+
"description": "This resource-based policy allows Amazon SNS to send messages to a specific Amazon SQS dead-letter queue. The policy grants the SNS service principal permission to perform the SQS:SendMessage action on the queue named [MyDeadLetterQueue], but only when the source ARN matches the specified SNS topic [MyTopic]. This policy would be attached to the SQS queue to enable it to receive messages from the SNS topic when message delivery fails. Replace [us-east-2] with your specific AWS Region."
8+
},
9+
{
10+
"title": "Allows managing log delivery and related resources",
11+
"title_abbrev": "Allows log delivery management",
12+
"description": "This identity-based policy allows managing CloudWatch Logs delivery configurations and related resources. The policy grants permissions to create, get, update, delete, and list log deliveries, as well as manage resource policies for a specific log group [SampleLogGroupName]. It also allows creating service-linked roles, tagging Firehose delivery streams, and managing bucket policies for a specific S3 bucket [bucket-name]. Replace [region] and [account-id] with your specific AWS Region and account ID, and [bucket-name] with your actual S3 bucket name."
13+
},
14+
{
15+
"title": "Allows Amazon SNS to send messages to an SQS dead-letter queue",
16+
"title_abbrev": "SNS to SQS permissions",
17+
"description": "This resource-based policy allows Amazon SNS to send messages to a specific Amazon SQS queue that serves as a dead-letter queue. The policy grants the SNS service permission to perform the SQS:SendMessage action on the queue named [MyDeadLetterQueue] in the [us-east-2] Region. The policy includes a condition that restricts this permission to messages originating from the SNS topic named [MyTopic] in the same Region and account."
18+
}
19+
]
20+
```

0 commit comments

Comments
 (0)