diff --git a/.github/workflows/publish-cheat-sheet.yml b/.github/workflows/publish-cheat-sheet.yml index 989a51688..0e9729226 100644 --- a/.github/workflows/publish-cheat-sheet.yml +++ b/.github/workflows/publish-cheat-sheet.yml @@ -33,6 +33,8 @@ jobs: with: source: ./doc/cheat_sheet destination: ./_site + - name: Compile cheat sheet for genAI + run: sudo python3 doc/cheat_sheet/_scripts/generate_genAI_cheat_sheet.py -d _site - name: Upload artifact uses: actions/upload-pages-artifact@v3 diff --git a/doc/cheat_sheet/_includes/en/Circuits.md b/doc/cheat_sheet/_includes/en/Circuits.md index a376732f0..35f24aebd 100644 --- a/doc/cheat_sheet/_includes/en/Circuits.md +++ b/doc/cheat_sheet/_includes/en/Circuits.md @@ -1,5 +1,5 @@ | Imports | `from braket.circuits import Circuit, Gate, Instruction`
`from braket.circuits.observables import X` | -| Create a circuit | `circuit = Circuit()` | +| Create a circuit | `circuit = Circuit()`| | Add gates | `circuit.x(0).rx(1, 1.23).cnot(0, 1)` | | Get the list of available gates | `[attr for attr in dir(Gate) if attr[0].isupper()]` | | Apply a unitary matrix | `circuit.unitary(matrix, [0])` | diff --git a/doc/cheat_sheet/_scripts/generate_genAI_cheat_sheet.py b/doc/cheat_sheet/_scripts/generate_genAI_cheat_sheet.py new file mode 100644 index 000000000..84c697409 --- /dev/null +++ b/doc/cheat_sheet/_scripts/generate_genAI_cheat_sheet.py @@ -0,0 +1,88 @@ +import argparse +import io +import os +import re +import requests +import zipfile + + +def download_zip_file(version=None): + if version is None: + zip_file_url = "https://github.com/amazon-braket/amazon-braket-sdk-python/zipball/jcjaskula-aws/add_cheat_sheet" + else: + zip_file_url = f"https://github.com/amazon-braket/amazon-braket-sdk-python/archive/refs/tags/v{version}.zip" + + r = requests.get(zip_file_url) + return zipfile.ZipFile(io.BytesIO(r.content)) + + +def list_markdown_files(zip_file): + cs_folder = os.path.join(zip_file.namelist()[0], "doc/cheat_sheet/_includes/en/") + print(cs_folder) + excluded_files = ["What-is.md", "Resources.md"] + + zip_files = [ + os.path.join(cs_folder, file.name) + for file in zipfile.Path(zip_file, at=cs_folder).iterdir() + if file.name not in excluded_files + ] + return zip_files + + +def concatenate_files(zip_file, files): + content = {} + for file in files: + filename = os.path.split(file)[-1] + content[filename] = [] + with zip_file.open(file, "r") as f: + for line in f.readlines(): + if '|' not in line.decode(): + continue + result = re.match( + r"\|\s*(.*)\s*\|\s*(.*)\s*\|", line.decode() + ) + if result and len(result.groups()) == 2: + content[filename].append( + result.groups() + ) + else: + raise ValueError(f"Invalid line: {line} in {file}") + return content + + +def format_and_write(content, destination): + with open(os.path.join(destination, "genAI_optimized_cheat_sheet.md"), "w") as f: + f.write("# Braket CheatSheet\n\n") + for filename, file_content in content.items(): + f.write(f"**{filename[:-3]}**\n\n") + for description, command in file_content: + if result := re.match( + r"(.*)\(.*)", description + ): + description = "".join(result.groups()) + f.write(f"***{description.strip()}:***\n\n") + + if result := re.match( + r"(.*)\(.*)", command + ): + command = "".join(result.groups()) + if "
" in command: + command = command.strip().replace('`', '') + f.write("```\n{}\n```\n\n".format(command.replace('
', '\n'))) + else: + f.write(f"{command}\n\n") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("-d", "--destination", type=str) + parser.add_argument("-v", "--version", type=str) + + args = parser.parse_args() + version = args.version if args.version else None + destination = args.destination if args.destination else "." + + zip_file = download_zip_file(version) + markdown_file_names = list_markdown_files(zip_file) + content = concatenate_files(zip_file, markdown_file_names) + format_and_write(content, destination)