idxtool: added '--template' command line

This commit is contained in:
Elias Bachaalany 2023-12-19 19:40:22 -08:00
parent 63a59a7e7d
commit 5c07596117
3 changed files with 59 additions and 13 deletions

View file

@ -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

View file

@ -8,10 +8,12 @@ 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'
@ -50,7 +52,7 @@ 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(
@ -136,6 +138,39 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]:
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.
@ -180,6 +215,7 @@ def main():
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:

View file

@ -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