-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchatgptpromptlib
executable file
·110 lines (98 loc) · 3.56 KB
/
chatgptpromptlib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
PROMPTLIB="$SCRIPTDIR/../promptlib"
usage() {
echo "Usage:"
echo " chatgptpromptlib [-d variable=value] ... promptkey1 promptkey2 ... output of prompt fragments with key promptkey1 promptkey2 ..."
echo " chatgptpromptlib -l lists all promptfragments"
echo " chatgptpromptlib -c category lists all promptfragments of category c"
echo " chatgptpromptlib -s searchstring lists all promptfragments where all words of the searchstring appear"
echo ""
echo "Description:"
echo "The prompt fragments are located in the directory \$SCRIPTDIR/../promptlib/"
echo "and are named like the key but with the extension .prompt.txt."
echo "The first line of the file can specify a category, e.g., CATEGORY=thecategory."
echo "The second line can provide a description, e.g., DESCRIPTION=a description of the file."
echo "These lines are not included in the output."
}
if [[ "$#" -eq 0 || "$1" == "--help" ]]; then
usage
exit 0
fi
contains_words() {
local file="$1"
local search="$2"
for word in $search; do
if ! grep -q -w -i "$word" "$file"; then
return 1
fi
done
return 0
}
replacements=()
while getopts ":d:" opt; do
case $opt in
d)
replacements+=("$OPTARG")
shift 2
;;
*)
break
;;
esac
done
case "$1" in
-l)
find "$PROMPTLIB" -type f -name '*.prompt.txt' -exec basename -s .prompt.txt {} \;
;;
-c)
shift
category="$1"
find "$PROMPTLIB" -type f -name '*.prompt.txt' -exec grep -l "CATEGORY=.*${category}" {} \; -exec basename -s .prompt.txt {} \;
;;
-s)
shift
searchstring="$1"
find "$PROMPTLIB" -type f -name '*.prompt.txt' | while read -r prompt; do
if contains_words "$prompt" "$searchstring"; then
basename -s .prompt.txt "$prompt"
fi
done
;;
*)
for key in "$@"; do
promptfile="$PROMPTLIB/$key.prompt.txt"
if [[ -f "$promptfile" ]]; then
prompt_text="$(sed -e '1,2d' "$promptfile" | awk 'BEGIN{RS=ORS="\n\n"} NF{print $0}')"
for replacement in "${replacements[@]}"; do
variable="${replacement%%=*}"
value="${replacement#*=}"
prompt_text="${prompt_text//\$$variable/$value}"
done
echo "$prompt_text"
else
echo "Error: Prompt fragment with key '$key' not found."
exit 1
fi
done
;;
esac
# Generated with ChatGPT and modified from prompt:
#The script is named chatgptpromptlib and should be done with bash. It has the options:
#chatgptpromptlib promptkey1 promptkey2 ...
# output of prompt fragments with key promptkey1 promptkey2 ...
#chatgptpromptlib -l
# lists all promptfragments
#chatgptpromptlib -c category
# lists all promptfragments of category c
#chatgptpromptlib -s searchstring
# lists all promptfragments where all word of the searchstring appear
#The prompts are searched in the directory $SCRIPTDIR/../promptlib/, where SCRIPTDIR is the location of the script,
#and are name like the key but with extension .prompt.txt . The first line can look like
#CATEGORY=thecategory
#where thecategory is a category name, possibly several separated with spaces, and then a line
#DESCRIPTION=a description of the file
#Those line is not written to stdout, only the following text.
#Please extend the following script so that it also takes arguments like -d variable=value and replaces occurrences of
#$variable in the prompt by value before printing it.
#That applies only to the first case, where promptkey1 promptkey2 and so forth are given, not others, and is optional.