Skip to content

Commit

Permalink
Merge pull request #66 from lallouslab/doc
Browse files Browse the repository at this point in the history
TOC generation should now be consistent
+idxtool with '--template'
  • Loading branch information
0xeb authored Dec 20, 2023
2 parents 9d5c325 + 5c07596 commit b68ae21
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 232 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
73 changes: 58 additions & 15 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 @@ -91,8 +93,8 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]:
else:
out.append(line)
if not marker_found:
return (False, f"Could not find the marker '{TOC_GPT_MARKER_LINE}' in '{toc_in}'.")
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 All @@ -107,16 +109,21 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]:
for ok, gpt in enum_gpts():
nb_total += 1
if ok:
if id := gpt.id():
if gpt_id := gpt.id():
nb_ok += 1
gpts.append((id, gpt))
gpts.append((gpt_id, gpt))
else:
print(f"[!] No ID detected: {gpt.filename}")
else:
print(f"[!] {gpt}")

# Consistently sort the GPTs by ID
gpts.sort(key=lambda x: x[0].id)
# Consistently sort the GPTs by ID and GPTs title
def gpts_sorter(key):
gpt_id, gpt = key
version = f"{gpt.get('version')}" if gpt.get('version') else ''
return f"{gpt.get('title')}{version} (id: {gpt_id.id}))"
gpts.sort(key=gpts_sorter)

for id, gpt in gpts:
file_link = f"./prompts/gpts/{quote(os.path.basename(gpt.filename))}"
version = f" {gpt.get('version')}" if gpt.get('version') else ''
Expand All @@ -130,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}: <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 @@ -172,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 @@ -192,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
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"version": "0.2.0",
"configurations": [
{
"name": "idxtool-Update Logo",
"name": "idxtool-any",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/.scripts/idxtool.py",
"args": ["--update-logo", "logo.png"],
"args": ["--template", "https://chat.openai.com/g/g-svehnI9xP-retro-adventures"],
"console": "integratedTerminal"
},
{
Expand Down
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# ChatGPT_system_prompt
valuable agent's system prompt,find GPT's prompt in [TOC.md](./TOC.md)

This repository is a collection of various system prompts for ChatGPT and [custom GPTs](https://openai.com/blog/introducing-gpts), providing significant educational value in learning about writing system prompts and creating custom GPTs.

For a quick start, go to [TOC.md](./TOC.md) to find the specific GPT or system prompt you need.

Other topics:

- [How to get system prompt?](#how-to-get-system-prompt)
- [How to get knowledge files?](#how-to-get-knowledge-files)
- [How to protect GPT instructions?](#how-to-protect-gpt-instructions)
- [How to get GPT's action schema?](#how-to-get-gpts-action-schema)
- [Contribution](#contribution)
- [Learning resources](#learning-resources)
- [Find system prompts and custom GPTs](./TOC.md)


<img src="bg.png" width="600px" />

Expand Down Expand Up @@ -96,9 +110,9 @@ some useful GPTs may be helpful:
2. [GPT Shop Keeper](https://chat.openai.com/g/g-22ZUhrOgu-gpt-shop-keeper)


## If you want to contribute to this repo
## Contribution

Please follow the format below; it is important to keep the format consistent for the [`idxtool`](./scripts/idxtool.py).
Please follow the format below; it is important to keep the format consistent for the [`idxtool`](./.scripts/README.md).

```markdown
GPT URL: You put the GPT url here
Expand All @@ -121,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 Expand Up @@ -154,7 +174,6 @@ NOTE: Please try not to use weird file name characters and avoid using '[' and '
- https://www.reddit.com/r/ChatGPTJailbreak/
- https://github.com/0xeb/gpt-analyst/


## Disclaimer

The sharing of these prompts/instructions is purely for reference and knowledge sharing, aimed at enhancing everyone's prompt writing skills and raising awareness about prompt injection security.
Expand Down
Loading

0 comments on commit b68ae21

Please sign in to comment.