aboutsummaryrefslogtreecommitdiff
path: root/examples/server/chat-llama2.sh
blob: 1fc79b7e191374b434b645951610a68e55537305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash

API_URL="${API_URL:-http://127.0.0.1:8080}"

CHAT=(
    "Hello, Assistant."
    "Hello. How may I help you today?"
)

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() {
    if [[ "${#CHAT[@]}" -eq 0 ]]; then
        echo -n "[INST] <<SYS>>\n${INSTRUCTION}\n<</SYS>>"
    else
        LAST_INDEX=$(( ${#CHAT[@]} - 1 ))
        echo -n "${CHAT[$LAST_INDEX]}\n[INST] $1 [/INST]"
    fi
}

tokenize() {
    curl \
        --silent \
        --request POST \
        --url "${API_URL}/tokenize" \
        --header "Content-Type: application/json" \
        --data-raw "$(jq -ns --arg content "$1" '{content:$content}')" \
    | jq '.tokens[]'
}

N_KEEP=$(tokenize "[INST] <<SYS>>\n${INSTRUCTION}\n<</SYS>>" | wc -l)

chat_completion() {
    PROMPT="$(trim_trailing "$(format_prompt "$1")")"
    DATA="$(echo -n "$PROMPT" | jq -Rs --argjson n_keep $N_KEEP '{
        prompt: .,
        temperature: 0.2,
        top_k: 40,
        top_p: 0.9,
        n_keep: $n_keep,
        n_predict: 1024,
        stop: ["[INST]"],
        stream: true
    }')"

    # Create a temporary file to hold the Python output
    TEMPFILE=$(mktemp)

    exec 3< <(curl \
        --silent \
        --no-buffer \
        --request POST \
        --url "${API_URL}/completion" \
        --header "Content-Type: application/json" \
        --data-raw "${DATA}")

    python -c "
import json
import sys

answer = ''
while True:
    line = sys.stdin.readline()
    if not line:
        break
    if line.startswith('data: '):
        json_content = line[6:].strip()
        content = json.loads(json_content)['content']
        sys.stdout.write(content)
        sys.stdout.flush()
        answer += content

answer = answer.rstrip('\n')

# Write the answer to the temporary file
with open('$TEMPFILE', 'w') as f:
    f.write(answer)
    " <&3

    exec 3<&-

    # Read the answer from the temporary file
    ANSWER=$(cat $TEMPFILE)

    # Clean up the temporary file
    rm $TEMPFILE

    printf "\n"

    CHAT+=("$1" "$(trim "$ANSWER")")
}

while true; do
    echo -en "\033[0;32m"  # Green color
    read -r -e -p "> " QUESTION
    echo -en "\033[0m"  # Reset color
    chat_completion "${QUESTION}"
done