From 6f1a9650fbce4fccba88402d9fea19a23c0ad1b2 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 23 Mar 2023 16:15:33 +0800 Subject: Update --- bin/gpt/gpt | 120 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 36 deletions(-) (limited to 'bin') diff --git a/bin/gpt/gpt b/bin/gpt/gpt index cd312d6..4d0f651 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt @@ -3,11 +3,13 @@ # TODO # - Use suggested block to wrap data: # https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api -# - Add signal trap for API parameters (like stop) +# - Print token usage when exit + +# User can dynamically change these options for API call +configurable_options=( behavior temperature max_tokens ) # If script is interupt by SIGINT, simply print leave message -trap 'echo -e "\nChat Finished, cached file: $cache"; exit 0' INT -trap '_configure_completion' TSTP +trap _print_leave_message INT # Function for printing helper message _print_helper_message() { @@ -40,22 +42,72 @@ Options: -s|--skip Skip message, STDIN would be treated as your message * The other arguments would be treated as message content. - If no message is specified, user should type it by hands. + If no message is specified, user should input content by hands. If STDIN is given, it would be append to the end of message. +Special prompt: + Options If input starts with '.', then a prompt of options shows up. + User can modify option value for API calls. + Reference: https://platform.openai.com/docs/api-reference/completions EOF } -_configure_completion() { - field="$( - dialog --form "Configure Completion" 10 50 3 \ - --stdout \ - 'model:' 1 1 "$(eval 'echo $model')" 1 15 25 0 \ - 'temperature:' 2 1 "$(eval 'echo $temperature')" 2 15 25 0 \ - 'max_tokens:' 3 1 "$(eval 'echo $max_tokens')" 3 15 25 0 - )" - +_print_leave_message(){ + echo -e "\nChat Finished, cached file: $cache" + exit 0 +} + +# A prompt for option configuration +_configure_options() { + # Apply original trap action, and exit this prompt + trap 'trap _print_leave_message SIGINT; echo; return' INT + # Hint message + echo 'List of options: (Use Ctrl-C to exit)' + + # Print all available options + index=0 + for option in "${configurable_options[@]}"; do + echo -e "$index." "$option:\t" "$(eval "echo \$$option")" + (( index+=1 )) + done + + # Prompt for user input + while true; do + echo + read -e -r -p "Modify which option? (0~$((index-1))) " selection + local field=${configurable_options[$selection]} + eval "read -e -r -p '$field: ' $field" + done +} + +# Get latest content for completion +_get_content() { + + # Read data from STDIN + [ ! -t 0 ] && data="$(cat)" + + if [ ! "$SKIP_INPUT" = true ] ; then + # Read content it from terminal + while true; do + read -e -r -p "Let's Chat: " content =1 ]] && read -e -r -p "Let's Chat: " content >$cache + echo "$body" >>"$cache" # Add an empty line between prompt and response echo -e '\n------\n' @@ -156,15 +204,15 @@ EOF # API call # Save original response into cache file # And only print content of message - response="` + response="$( curl https://api.openai.com/$ROUTE \ --silent \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d "$body" | \ - jq . | tee -a $cache | \ + jq . | tee -a "$cache" | \ jq -r .choices[0].message.content - `" + )" echo -e "${response}\n\n------\n" # Append newest message into session -- cgit v1.2.3-70-g09d2