diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-24 09:02:08 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-24 09:02:08 +0800 |
| commit | 1c6752b5dfbd026d7b56b81474c052e6bb92c4d8 (patch) | |
| tree | 73bdb3abc8823d33742041532fc101672e1a5e8a /bin/gpt | |
| parent | 0a6c50b8ffead961aeb1f7d4888eae7fd75e9891 (diff) | |
Update
Diffstat (limited to 'bin/gpt')
| -rwxr-xr-x | bin/gpt/gpt | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt index c17ab69..9a0e830 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | # - Use suggested block to wrap data: | 4 | # - Use suggested block to wrap data: |
| 5 | # https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api | 5 | # https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api |
| 6 | # - Print token usage when exit | 6 | # - Print token usage when exit |
| 7 | # - For the chat | ||
| 7 | 8 | ||
| 8 | # Necessary commands | 9 | # Necessary commands |
| 9 | stacks=( curl jq ) | 10 | stacks=( curl jq ) |
| @@ -54,7 +55,7 @@ Options: | |||
| 54 | (Defaults to 0.7) | 55 | (Defaults to 0.7) |
| 55 | 56 | ||
| 56 | -M|--max_tokens The maximum number of tokens to generate in the completion. | 57 | -M|--max_tokens The maximum number of tokens to generate in the completion. |
| 57 | (Defaults to 2048) | 58 | (Defaults to 1024) |
| 58 | 59 | ||
| 59 | -s|--skip Skip message, STDIN would be treated as your message | 60 | -s|--skip Skip message, STDIN would be treated as your message |
| 60 | 61 | ||
| @@ -71,7 +72,8 @@ EOF | |||
| 71 | } | 72 | } |
| 72 | 73 | ||
| 73 | _print_leave_message(){ | 74 | _print_leave_message(){ |
| 74 | echo -e "\nChat Finished, cached file: $cache" | 75 | tokens=$(jq '.usage? // empty | .total_tokens' "$cache" | paste -sd+ | bc) |
| 76 | echo -e "\nChat Finished, ${tokens:-0} tokens used. Session is cached in: $cache" | ||
| 75 | exit 0 | 77 | exit 0 |
| 76 | } | 78 | } |
| 77 | 79 | ||
| @@ -93,8 +95,13 @@ _configure_options() { | |||
| 93 | while true; do | 95 | while true; do |
| 94 | echo | 96 | echo |
| 95 | read -e -r -p "Modify which option? (0~$((index-1))) " selection | 97 | read -e -r -p "Modify which option? (0~$((index-1))) " selection |
| 96 | local field=${configurable_options[$selection]} | 98 | if [[ "$selection" =~ ^[0-9]+$ ]] && \ |
| 97 | eval "read -e -r -p '$field: ' $field" | 99 | (( "$selection" >= 0 && "$selection" < "$index" )); then |
| 100 | local field=${configurable_options[$selection]} | ||
| 101 | eval "read -e -r -p '$field: ' $field" | ||
| 102 | else | ||
| 103 | echo Wrong Input | ||
| 104 | fi | ||
| 98 | done | 105 | done |
| 99 | } | 106 | } |
| 100 | 107 | ||
| @@ -108,7 +115,7 @@ _get_content() { | |||
| 108 | # Read content it from terminal | 115 | # Read content it from terminal |
| 109 | while true; do | 116 | while true; do |
| 110 | read -e -r -p "Let's Chat: " content </dev/tty | 117 | read -e -r -p "Let's Chat: " content </dev/tty |
| 111 | [[ "$content" =~ ^\. ]] && echo && _configure_options && echo -e '\n------\n' && continue | 118 | [[ "$content" =~ ^\. ]] && echo && _configure_options && echo -e '\n======\n' && continue |
| 112 | [ -n "$content" ] && break | 119 | [ -n "$content" ] && break |
| 113 | done | 120 | done |
| 114 | elif [[ "$count" -eq 0 && -z "$data" ]]; then | 121 | elif [[ "$count" -eq 0 && -z "$data" ]]; then |
| @@ -178,7 +185,7 @@ ROUTE=v1/chat/completions | |||
| 178 | model=${model:-gpt-3.5-turbo} | 185 | model=${model:-gpt-3.5-turbo} |
| 179 | behavior="${behavior:-You are a helpful programming assistant}" | 186 | behavior="${behavior:-You are a helpful programming assistant}" |
| 180 | temperature=${temperature:-0.7} | 187 | temperature=${temperature:-0.7} |
| 181 | max_tokens=${max_tokens:-2048} | 188 | max_tokens=${max_tokens:-1024} |
| 182 | 189 | ||
| 183 | # Prepare for chat session | 190 | # Prepare for chat session |
| 184 | cache=$(mktemp) && touch "$cache" | 191 | cache=$(mktemp) && touch "$cache" |