File size: 5,152 Bytes
538ca27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash

# if not enough arguments
if [[ $# -lt 4 ]]; then
    echo "Usage: llama.sh <model> <template> <context-size> <temperature>"
    exit 1
fi

# if conf does not exist, create it
if [ ! -f "$HOME/.config/llama/llama-main.conf" ]; then
    mkdir -p "$HOME/.config/llama"
    cat <<EOF > "$HOME/.config/llama/llama-main.conf"
LLAMA_TEMPERATURE=0.1
LLAMA_CONTEXT_SIZE=4096
LLAMA_REPETITION_PENALTY=1.15
LLAMA_TOP_P=0.9
LLAMA_TOP_K=20
LLAMA_TEMPLATE=chatml
LLAMA_MODEL_NAME=teknium/OpenHermes-2.5-Mistral-7B/openhermes-2.5-mistral-7b-f16.gguf
EOF
fi

source $HOME/.config/llama/llama-main.conf

function llama_interactive {
    MODEL_NAME=$1
    TEMPLATE=$2
    CONTEXT_SIZE=$3
    TEMPERATURE=$4

    CMD=$HOME/.ai/bin/llama

    $CMD \
        --n-gpu-layers 1 \
        --model "$(model_path $MODEL_NAME)" \
        --prompt-cache "$(cache_path $MODEL_NAME)" \
        --file "$(get_model_prompt $MODEL_NAME)" \
        --in-prefix "$(get_model_prefix $TEMPLATE)" \
        --in-suffix "$(get_model_suffix $TEMPLATE)" \
        --reverse-prompt "$(get_model_prefix $TEMPLATE)" \
        --reverse-prompt "<|im_end|>" \
        --threads "7" \
        --temp "$TEMPERATURE" \
        --top-p "$LLAMA_TOP_P" \
        --top-k "$LLAMA_TOP_K" \
        --repeat-penalty "$LLAMA_REPETITION_PENALTY" \
        --ctx-size "$CONTEXT_SIZE" \
        --batch-size 1024 \
        --n-predict -1 \
        --keep -1 \
        --instruct \
        --no-mmap \
        --color \
        --escape
}

function model_path {
    MODEL_NAME=$1
    echo "$HOME/.ai/models/llama/${MODEL_NAME}"
}

function cache_path {
    MODEL_NAME=$1
    echo "$HOME/.ai/cache/menu-${MODEL_NAME//\//_}.cache"
}

function get_model_prefix {
    TEMPLATE_NAME=$1

    # if TEMPLATE_NAME contains string "guanaco"
    if [[ $TEMPLATE_NAME == *"guanaco"* ]]; then
        printf "### Human: "
    elif [[ $TEMPLATE_NAME == *"alpaca"* ]]; then
        printf "### Instruction: "
    elif [[ $TEMPLATE_NAME == *"upstage"* ]]; then
        printf "### Instruction: "
    elif [[ $TEMPLATE_NAME == *"airoboros"* ]]; then
        printf "### Instruction: "
    elif [[ $TEMPLATE_NAME == *"hermes"* ]]; then
        printf "### Instruction:"
    elif [[ $TEMPLATE_NAME == *"vicuna"* ]]; then
        printf "USER: "
    elif [[ $TEMPLATE_NAME == *"based"* ]]; then
        printf "Human: "
    elif [[ $TEMPLATE_NAME == *"wizardlm"* ]]; then
        printf "USER: "
    elif [[ $TEMPLATE_NAME == *"orca"* ]]; then
        printf "### User: "
    elif [[ $TEMPLATE_NAME == *"samantha"* ]]; then
        printf "USER: "
    elif [[ $TEMPLATE_NAME == "chatml" ]]; then
        printf "<|im_start|>user\\\n"
    else
        printf "Input: "
    fi
}

#  USER: hello, who are you? ASSISTANT: 

function get_model_suffix {
    TEMPLATE_NAME=$1

    # if TEMPLATE_NAME contains string "guanaco"
    if [[ $TEMPLATE_NAME == *"guanaco"* ]]; then
        printf "### Assistant: "
    elif [[ $TEMPLATE_NAME == *"alpaca"* ]]; then
        printf "### Response: "
    elif [[ $TEMPLATE_NAME == *"airoboros"* ]]; then
        printf "### Response: "
    elif [[ $TEMPLATE_NAME == *"upstage"* ]]; then
        printf "### Response: "
    elif [[ $TEMPLATE_NAME == *"hermes"* ]]; then
        printf "### Response: "
    elif [[ $TEMPLATE_NAME == *"vicuna"* ]]; then
        printf "ASSISTANT: "
    elif [[ $TEMPLATE_NAME == *"samantha"* ]]; then
        printf "ASSISTANT: "
    elif [[ $TEMPLATE_NAME == *"based"* ]]; then
        printf "Assistant: "
    elif [[ $TEMPLATE_NAME == *"wizardlm"* ]]; then
        printf "ASSISTANT: "
    elif [[ $TEMPLATE_NAME == *"orca"* ]]; then
        printf "### Response: "
    elif [[ $TEMPLATE_NAME == "chatml" ]]; then
        printf "<|im_end|>\n<|im_start|>assistant\\\n"
    else
        printf "Output: "
    fi
}

function get_model_prompt {
    MODEL_NAME=$1

    if [[ $MODEL_NAME == *"guanaco"* ]]; then
        echo "$HOME/.local/share/ai/prompts/guanaco.txt"
    elif [[ $MODEL_NAME == *"samantha"* ]]; then
        echo "$HOME/.local/share/ai/prompts/samantha.txt"
    elif [[ $MODEL_NAME == *"openhermes-2-mistral-7b"* ]]; then
        echo "$HOME/.local/share/ai/prompts/hermes-mistral.txt"
    elif [[ $MODEL_NAME == *"alpaca"* ]]; then
        echo "$HOME/.local/share/ai/prompts/alpaca.txt"
    elif [[ $MODEL_NAME == *"upstage"* ]]; then
        echo "$HOME/.local/share/ai/prompts/alpaca.txt"
    elif [[ $MODEL_NAME == *"airoboros"* ]]; then
        echo "$HOME/.local/share/ai/prompts/alpaca.txt"
    elif [[ $MODEL_NAME == *"hermes"* ]]; then
        echo "$HOME/.local/share/ai/prompts/alpaca.txt"
    elif [[ $MODEL_NAME == *"vicuna"* ]]; then
        echo "$HOME/.local/share/ai/prompts/vicuna-v11.txt"
    elif [[ $MODEL_NAME == *"based"* ]]; then
        echo "$HOME/.local/share/ai/prompts/based.txt"
    elif [[ $MODEL_NAME == *"wizardlm"* ]]; then
        echo "$HOME/.local/share/ai/prompts/wizardlm-30b.txt"
    elif [[ $MODEL_NAME == *"orca"* ]]; then
        echo "$HOME/.local/share/ai/prompts/orca.txt"
    else
        echo "$HOME/.local/share/ai/prompts/idm-gpt-lite.txt"
    fi
}

llama_interactive "$1" "$2" "$3" "$4"