From 5c0759611796174c69f4afbd87441fe488d133be Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Tue, 19 Dec 2023 19:40:22 -0800 Subject: [PATCH] idxtool: added '--template' command line --- .scripts/README.md | 8 ++++--- .scripts/idxtool.py | 58 +++++++++++++++++++++++++++++++++++++-------- README.md | 6 +++++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/.scripts/README.md b/.scripts/README.md index 3c83881..75c7f64 100644 --- a/.scripts/README.md +++ b/.scripts/README.md @@ -8,15 +8,16 @@ Contributions to `idxtool` are welcome. Please submit pull requests or issues to ``` usage: idxtool.py [-h] [--toc [TOC]] [--find-gpt FIND_GPT] - [--parse-gptfile PARSE_GPTFILE] [--rename] + [--template TEMPLATE] [--parse-gptfile PARSE_GPTFILE] + [--rename] idxtool: A GPT indexing and searching tool for the CSP repo options: -h, --help show this help message and exit --toc [TOC] Rebuild the table of contents (TOC.md) file - --find-gpt FIND_GPT - Find a GPT file by its ID or full ChatGPT URL + --find-gpt FIND_GPT Find a GPT file by its ID or full ChatGPT URL + --template TEMPLATE Creates an empty GPT template file from a ChatGPT URL --parse-gptfile PARSE_GPTFILE Parses a GPT file name --rename Rename the GPT file names to include their GPT ID @@ -27,6 +28,7 @@ options: - Rebuild TOC: Use `--toc` to rebuild the table of contents (TOC.md) file. - Find GPT File: Use `--find-gpt [GPTID or Full ChatGPT URL or a response file with IDs/URLs]` to find a GPT by its ID or URL. - Rename GPT: Use `--rename` to rename all the GPTs to include their GPTID as prefix. +- Create a starter template GPT file: Use `--template [Full ChatGPT URL]` to create a starter template GPT file. - Help: Use `--help` to display the help message and usage instructions. ## Example diff --git a/.scripts/idxtool.py b/.scripts/idxtool.py index 9d8cc0d..6cff66e 100644 --- a/.scripts/idxtool.py +++ b/.scripts/idxtool.py @@ -8,15 +8,17 @@ idxtool is a script is used to perform various GPT indexing and searching tasks """ import sys, os, argparse -from gptparser import GptMarkdownFile, enum_gpts, parse_gpturl, enum_gpt_files from typing import Tuple from urllib.parse import quote +import gptparser +from gptparser import enum_gpts, parse_gpturl, enum_gpt_files, get_prompts_path + TOC_FILENAME = 'TOC.md' TOC_GPT_MARKER_LINE = '- GPTs' def get_toc_file() -> str: - return os.path.abspath(os.path.join(os.path.dirname(__file__), '..', TOC_FILENAME)) + return os.path.abspath(os.path.join(os.path.dirname(__file__), '..', TOC_FILENAME)) def rename_gpts(): nb_ok = nb_total = 0 @@ -39,7 +41,7 @@ def rename_gpts(): print(f"[+] {basename} -> {os.path.basename(new_fn)}") if os.system(f"git mv \"{gpt.filename}\" \"{new_fn}\"") == 0: nb_ok += 1 - + msg = f"Renamed {nb_ok} out of {nb_total} GPT files." ok = nb_ok == nb_total if all_renamed_already: @@ -50,11 +52,11 @@ def rename_gpts(): def parse_gpt_file(filename) -> Tuple[bool, str]: - ok, gpt = GptMarkdownFile.parse(filename) + ok, gpt = gptparser.GptMarkdownFile.parse(filename) if ok: file_name_without_ext = os.path.splitext(os.path.basename(filename))[0] dst_fn = os.path.join( - os.path.dirname(filename), + os.path.dirname(filename), f"{file_name_without_ext}.new.md") gpt.save(dst_fn) else: @@ -63,7 +65,7 @@ def parse_gpt_file(filename) -> Tuple[bool, str]: return (ok, gpt) -def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]: +def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]: """ Rebuilds the table of contents (TOC.md) file by reading all the GPT files in the prompts/gpts directory. """ @@ -79,7 +81,7 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]: if not os.path.exists(toc_in): return (False, f"TOC File '{toc_in}' does not exist.") - + # Read the TOC file and find the marker line for the GPT instructions out = [] marker_found = False @@ -92,7 +94,7 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]: out.append(line) if not marker_found: return (False, f"Could not find the marker '{TOC_GPT_MARKER_LINE}' in '{toc_in}'. Please revert the TOC file and try again.") - + # Write the TOC file all the way up to the marker line try: ofile = open(toc_out, 'w', encoding='utf-8') @@ -135,7 +137,40 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]: if ok: print(msg) return (ok, msg) - + +def make_template(url, verbose=True): + """Creates an empty GPT template file from a ChatGPT URL""" + if not (gpt_info := parse_gpturl(url)): + msg = f"Invalid ChatGPT URL: '{url}'" + if verbose: + print(msg) + return (False, msg) + + filename = os.path.join(get_prompts_path(), f"{gpt_info.id}_RENAMEME.md") + if os.path.exists(filename): + msg = f"File '{filename}' already exists." + if verbose: + print(msg) + return (False, msg) + + with open(filename, 'w', encoding='utf-8') as file: + for field, info in gptparser.SUPPORTED_FIELDS.items(): + if field == 'verif_status': + continue + if field == 'url': + file.write(f"{gptparser.FIELD_PREFIX} {info.display}: {url}\n\n") + elif field == 'instructions': + file.write(f"{gptparser.FIELD_PREFIX} {info.display}:\n```markdown\n{info.display} here...\n```\n\n") + elif field == 'logo': + file.write(f"{gptparser.FIELD_PREFIX} {info.display}: \n\n") + else: + file.write(f"{gptparser.FIELD_PREFIX} {info.display}: {info.display} goes here...\n\n") + + msg = f"Created template '{filename}' for URL '{url}'" + if verbose: + print(msg) + return (True, msg) + def find_gptfile(keyword, verbose=True): """Find a GPT file by its ID or full ChatGPT URL The ID can be prefixed with '@' to indicate a file containing a list of GPT IDs. @@ -177,9 +212,10 @@ def find_gptfile(keyword, verbose=True): def main(): parser = argparse.ArgumentParser(description='idxtool: A GPT indexing and searching tool for the CSP repo') - + parser.add_argument('--toc', nargs='?', const='', type=str, help='Rebuild the table of contents (TOC.md) file') parser.add_argument('--find-gpt', type=str, help='Find a GPT file by its ID or full ChatGPT URL') + parser.add_argument('--template', type=str, help='Creates an empty GPT template file from a ChatGPT URL') parser.add_argument('--parse-gptfile', type=str, help='Parses a GPT file name') parser.add_argument('--rename', action='store_true', help='Rename the GPT file names to include their GPT ID') @@ -197,6 +233,8 @@ def main(): print(err) elif args.find_gpt: find_gptfile(args.find_gpt) + elif args.template: + make_template(args.template) elif args.rename: ok, err = rename_gpts() if not ok: diff --git a/README.md b/README.md index 089bf93..59e403a 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,12 @@ GPT Extras: Put a list of extra stuff, for example Chrome Extension links, etc. Please check a simple GPT file [here](./prompts/gpts/Animal%20Chefs.md) and mimic the format. +Alternatively, use the (`idxtool`)[./.scripts/README.md] to create a template file: + +```bash +python idxtool.py --template https://chat.openai.com/g/g-3ngv8eP6R-gpt-white-hack +``` + With respect to the GPT file names, please follow the format below for new GPT submissions: ```markdown