Skip to content

Commit

Permalink
idxtool: added '--template' command line
Browse files Browse the repository at this point in the history
  • Loading branch information
0xeb committed Dec 20, 2023
1 parent 63a59a7 commit 5c07596
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
8 changes: 5 additions & 3 deletions .scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
58 changes: 48 additions & 10 deletions .scripts/idxtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
"""

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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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.
"""
Expand All @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -135,7 +137,40 @@ def gpts_sorter(key):
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}: <img ...>\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.
Expand Down Expand Up @@ -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')

Expand All @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c07596

Please sign in to comment.