1
0

chat-llama2.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env bash
  2. API_URL="${API_URL:-http://127.0.0.1:8080}"
  3. CHAT=(
  4. "Hello, Assistant."
  5. "Hello. How may I help you today?"
  6. )
  7. 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."
  8. trim() {
  9. shopt -s extglob
  10. set -- "${1##+([[:space:]])}"
  11. printf "%s" "${1%%+([[:space:]])}"
  12. }
  13. trim_trailing() {
  14. shopt -s extglob
  15. printf "%s" "${1%%+([[:space:]])}"
  16. }
  17. format_prompt() {
  18. if [[ "${#CHAT[@]}" -eq 0 ]]; then
  19. echo -n "[INST] <<SYS>>\n${INSTRUCTION}\n<</SYS>>"
  20. else
  21. LAST_INDEX=$(( ${#CHAT[@]} - 1 ))
  22. echo -n "${CHAT[$LAST_INDEX]}\n[INST] $1 [/INST]"
  23. fi
  24. }
  25. tokenize() {
  26. curl \
  27. --silent \
  28. --request POST \
  29. --url "${API_URL}/tokenize" \
  30. --header "Content-Type: application/json" \
  31. --data-raw "$(jq -ns --arg content "$1" '{content:$content}')" \
  32. | jq '.tokens[]'
  33. }
  34. N_KEEP=$(tokenize "[INST] <<SYS>>\n${INSTRUCTION}\n<</SYS>>" | wc -l)
  35. chat_completion() {
  36. PROMPT="$(trim_trailing "$(format_prompt "$1")")"
  37. DATA="$(echo -n "$PROMPT" | jq -Rs --argjson n_keep $N_KEEP '{
  38. prompt: .,
  39. temperature: 0.2,
  40. top_k: 40,
  41. top_p: 0.9,
  42. n_keep: $n_keep,
  43. n_predict: 1024,
  44. stop: ["[INST]"],
  45. stream: true
  46. }')"
  47. # Create a temporary file to hold the Python output
  48. TEMPFILE=$(mktemp)
  49. exec 3< <(curl \
  50. --silent \
  51. --no-buffer \
  52. --request POST \
  53. --url "${API_URL}/completion" \
  54. --header "Content-Type: application/json" \
  55. --data-raw "${DATA}")
  56. python -c "
  57. import json
  58. import sys
  59. answer = ''
  60. while True:
  61. line = sys.stdin.readline()
  62. if not line:
  63. break
  64. if line.startswith('data: '):
  65. json_content = line[6:].strip()
  66. content = json.loads(json_content)['content']
  67. sys.stdout.write(content)
  68. sys.stdout.flush()
  69. answer += content
  70. answer = answer.rstrip('\n')
  71. # Write the answer to the temporary file
  72. with open('$TEMPFILE', 'w') as f:
  73. f.write(answer)
  74. " <&3
  75. exec 3<&-
  76. # Read the answer from the temporary file
  77. ANSWER=$(cat $TEMPFILE)
  78. # Clean up the temporary file
  79. rm $TEMPFILE
  80. printf "\n"
  81. CHAT+=("$1" "$(trim "$ANSWER")")
  82. }
  83. while true; do
  84. echo -en "\033[0;32m" # Green color
  85. read -r -e -p "> " QUESTION
  86. echo -en "\033[0m" # Reset color
  87. chat_completion "${QUESTION}"
  88. done