Skip to content

Commit 9fe7082

Browse files
committed
use html comment for LLM agent
1 parent 067b4b8 commit 9fe7082

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

.github/workflows/publish-cheat-sheet.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
with:
3434
source: ./doc/cheat_sheet
3535
destination: ./_site
36+
- name: Compile cheat sheet for genAI
37+
run: sudo python3 doc/cheat_sheet/_scripts/generate_genAI_cheat_sheet.py -d _site
3638
- name: Upload artifact
3739
uses: actions/upload-pages-artifact@v3
3840

doc/cheat_sheet/_includes/en/Circuits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
| Imports | `from braket.circuits import Circuit, Gate, Instruction`<br>`from braket.circuits.observables import X` |
2-
| Create a circuit | `circuit = Circuit()` |
2+
| Create a circuit<!-- LLM: . Note that number of qubits is not passed as an argument to the circuit constructor--> | `circuit = Circuit()`|
33
| Add gates | `circuit.x(0).rx(1, 1.23).cnot(0, 1)` |
44
| Get the list of available gates | `[attr for attr in dir(Gate) if attr[0].isupper()]` |
55
| Apply a unitary matrix | `circuit.unitary(matrix, [0])` |
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import argparse
2+
import io
3+
import os
4+
import re
5+
import requests
6+
import zipfile
7+
8+
9+
def download_zip_file(version=None):
10+
if version is None:
11+
zip_file_url = "https://github.com/amazon-braket/amazon-braket-sdk-python/zipball/jcjaskula-aws/add_cheat_sheet"
12+
else:
13+
zip_file_url = f"https://github.com/amazon-braket/amazon-braket-sdk-python/archive/refs/tags/v{version}.zip"
14+
15+
r = requests.get(zip_file_url)
16+
return zipfile.ZipFile(io.BytesIO(r.content))
17+
18+
19+
def list_markdown_files(zip_file):
20+
cs_folder = os.path.join(zip_file.namelist()[0], "doc/cheat_sheet/_includes/en/")
21+
print(cs_folder)
22+
excluded_files = ["What-is.md", "Resources.md"]
23+
24+
zip_files = [
25+
os.path.join(cs_folder, file.name)
26+
for file in zipfile.Path(zip_file, at=cs_folder).iterdir()
27+
if file.name not in excluded_files
28+
]
29+
return zip_files
30+
31+
32+
def concatenate_files(zip_file, files):
33+
content = {}
34+
for file in files:
35+
filename = os.path.split(file)[-1]
36+
content[filename] = []
37+
with zip_file.open(file, "r") as f:
38+
for line in f.readlines():
39+
if '|' not in line.decode():
40+
continue
41+
result = re.match(
42+
r"\|\s*(.*)\s*\|\s*(.*)\s*\|", line.decode()
43+
)
44+
if result and len(result.groups()) == 2:
45+
content[filename].append(
46+
result.groups()
47+
)
48+
else:
49+
raise ValueError(f"Invalid line: {line} in {file}")
50+
return content
51+
52+
53+
def format_and_write(content, destination):
54+
with open(os.path.join(destination, "genAI_optimized_cheat_sheet.md"), "w") as f:
55+
f.write("# Braket CheatSheet\n\n")
56+
for filename, file_content in content.items():
57+
f.write(f"**{filename[:-3]}**\n\n")
58+
for description, command in file_content:
59+
if result := re.match(
60+
r"(.*)\<!--\s*LLM:\s(.*)\s*-->(.*)", description
61+
):
62+
description = "".join(result.groups())
63+
f.write(f"***{description.strip()}:***\n\n")
64+
65+
if result := re.match(
66+
r"(.*)\<!--\s*LLM:\s(.*)\s*-->(.*)", command
67+
):
68+
command = "".join(result.groups())
69+
if "<br>" in command:
70+
command = command.strip().replace('`', '')
71+
f.write("```\n{}\n```\n\n".format(command.replace('<br>', '\n')))
72+
else:
73+
f.write(f"{command}\n\n")
74+
75+
76+
if __name__ == "__main__":
77+
parser = argparse.ArgumentParser()
78+
parser.add_argument("-d", "--destination", type=str)
79+
parser.add_argument("-v", "--version", type=str)
80+
81+
args = parser.parse_args()
82+
version = args.version if args.version else None
83+
destination = args.destination if args.destination else "."
84+
85+
zip_file = download_zip_file(version)
86+
markdown_file_names = list_markdown_files(zip_file)
87+
content = concatenate_files(zip_file, markdown_file_names)
88+
format_and_write(content, destination)

0 commit comments

Comments
 (0)