Skip to content

Commit 8fc393f

Browse files
authored
scripts : fix pattern and get n_tokens in one go (#10221)
1 parent ec450d3 commit 8fc393f

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

examples/chat-persistent.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ CUR_PROMPT_CACHE="${CHAT_SAVE_DIR}/current-cache.bin"
2323
NEXT_PROMPT_FILE="${CHAT_SAVE_DIR}/next-prompt.txt"
2424
NEXT_PROMPT_CACHE="${CHAT_SAVE_DIR}/next-cache.bin"
2525

26-
SESSION_SIZE_MSG_PATTERN='main: session file matches [[:digit:]]+ / [[:digit:]]+'
27-
SAMPLE_TIME_MSG_PATTERN='sample time =[[:space:]]+[[:digit:]]+.[[:digit:]]+ ms /[[:space:]]+[[:digit:]]+'
26+
SESSION_AND_SAMPLE_PATTERN='main: session file matches [[:digit:]]+ / [[:digit:]]+'\
27+
'|'\
28+
'sampling time =[[:space:]]+[[:digit:]]+.[[:digit:]]+ ms /[[:space:]]+[[:digit:]]+'
2829
SED_DELETE_MESSAGES="/^(${USER_NAME}:|${AI_NAME}:|\\.\\.\\.)/,\$d"
2930

3031
CTX_SIZE=2048
@@ -129,15 +130,12 @@ while read -e line; do
129130

130131
printf ' '
131132

132-
# HACK get num tokens from debug message
133-
# TODO get both messages in one go
134-
if ! session_size_msg="$(tail -n30 "$LOG" | grep -oE "$SESSION_SIZE_MSG_PATTERN")" ||
135-
! sample_time_msg="$(tail -n10 "$LOG" | grep -oE "$SAMPLE_TIME_MSG_PATTERN")"; then
133+
if ! session_and_sample_msg=$(tail -n30 "$LOG" | grep -oE "$SESSION_AND_SAMPLE_PATTERN"); then
136134
echo >&2 "Couldn't get number of tokens from ./llama-cli output!"
137135
exit 1
138136
fi
139137

140-
n_tokens=$(($(cut -d/ -f2 <<<"$session_size_msg") + $(cut -d/ -f2 <<<"$sample_time_msg")))
138+
n_tokens=$(awk '{sum+=$1} END {print sum}' <<< "$(cut -d/ -f2 <<< "$session_and_sample_msg")")
141139

142140
if ((n_tokens > CTX_ROTATE_POINT)); then
143141
tail -c+$((n_prompt_len_pre + 1)) "$CUR_PROMPT_FILE" >>"$NEXT_PROMPT_FILE"

0 commit comments

Comments
 (0)