diff --git a/examples/server/README.md b/examples/server/README.md index 8b53dc36e..824fd5194 100644 --- a/examples/server/README.md +++ b/examples/server/README.md @@ -172,3 +172,11 @@ Run with NodeJS version 16 or later: ```sh node chat.mjs ``` + +Another sample in [chat.sh](chat.sh). +Requires [bash](https://www.gnu.org/software/bash/), [curl](https://curl.se) and [jq](https://jqlang.github.io/jq/). +Run with bash: + +```sh +bash chat.sh +``` diff --git a/examples/server/chat.sh b/examples/server/chat.sh new file mode 100644 index 000000000..dd2d1953b --- /dev/null +++ b/examples/server/chat.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +API_URL="http://127.0.0.1:8080" + +CHAT=( + "Hello, Assistant." + "Hello. How may I help you today?" + "Please tell me the largest city in Europe." + "Sure. The largest city in Europe is Moscow, the capital of Russia." +) + +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." + +format_prompt() { + echo -n "${INSTRUCTION}" + printf "\n### Human: %s\n### Assistant: %s" "${CHAT[@]}" "$1" +} + +tokenize() { + echo -n "$1" | jq -Rs '{content:.}' | curl \ + --silent \ + --request POST \ + --url "${API_URL}/tokenize" \ + --data "@-" | jq '.tokens[]' +} + +N_KEEP=$(tokenize "${INSTRUCTION}" | wc -l) + +chat_completion() { + CONTENT=$(format_prompt "$1" | jq -Rs --argjson n_keep $N_KEEP '{ + prompt: ., + temperature: 0.2, + top_k: 40, + top_p: 0.9, + n_keep: $n_keep, + n_predict: 256, + stop: ["\n### Human:"] + }' | curl \ + --silent \ + --request POST \ + --url "${API_URL}/completion" \ + --data "@-" | jq -r '.content | sub("^\\s*"; "")') + + printf "$CONTENT\n" + + CHAT+=("$1" "$CONTENT") +} + +while true; do + read -p "> " QUESTION + chat_completion "${QUESTION}" +done