From 9d564db9ae105df85a11081e2e843cf354f211ce Mon Sep 17 00:00:00 2001 From: anon Date: Mon, 12 Jun 2023 21:30:33 -0300 Subject: [PATCH] trim response and trim trailing space in prompt Also add "-r" to read because of this: https://www.shellcheck.net/wiki/SC2162 --- examples/server/chat.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/examples/server/chat.sh b/examples/server/chat.sh index ddd4418bd..e2b50fb44 100644 --- a/examples/server/chat.sh +++ b/examples/server/chat.sh @@ -11,6 +11,17 @@ CHAT=( INSTRUCTION="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions." +trim() { + shopt -s extglob + set -- "${1##+([[:space:]])}" + printf "%s" "${1%%+([[:space:]])}" +} + +trim_trailing() { + shopt -s extglob + printf "%s" "${1%%+([[:space:]])}" +} + format_prompt() { echo -n "${INSTRUCTION}" printf "\n### Human: %s\n### Assistant: %s" "${CHAT[@]}" "$1" @@ -28,7 +39,8 @@ tokenize() { N_KEEP=$(tokenize "${INSTRUCTION}" | wc -l) chat_completion() { - DATA="$(format_prompt "$1" | jq -Rs --argjson n_keep $N_KEEP '{ + PROMPT="$(trim_trailing "$(format_prompt "$1")")" + DATA="$(echo -n "$PROMPT" | jq -Rs --argjson n_keep $N_KEEP '{ prompt: ., temperature: 0.2, top_k: 40, @@ -56,11 +68,10 @@ chat_completion() { printf "\n" - CHAT+=("$1" "${ANSWER:1}") + CHAT+=("$1" "$(trim "$ANSWER")") } - while true; do - read -e -p "> " QUESTION + read -r -e -p "> " QUESTION chat_completion "${QUESTION}" done