The following is from man say
.
If the input is a TTY, text is spoken line by line.... Otherwise, text is spoken all at once.
Below is what you are using.
ollama run llama3:latest | tee >(say)
You have the say
command reading input from a pipe which is not a TTY and therefore the text is spoken all at once.
Try what is below instead.
ollama run llama3:latest | tee >(while read -r; do say "$REPLY"; done)
Here the read
command gets each line of text from a pipe. Each read line is placed in the variable REPLY
. The say
command then gets each line of text to speak from the command line. This repeats until end-of-file is encountered on the pipe.
Evidentially, escape sequences are imbedded in the output. To determine what and where, I entered the following commands.
ollama run llama3:latest | tee out.txt
Below in the output as seen at the Terminal window after entering my test input at the >>>
prompt.
>>> hello
Hello! It's nice to meet you. Is there something I can help you with, or would you like to chat?
>>> /bye
Below is a hex/ASCII dump of out.txt
% hexdump -Cv out.txt
00000000 1b 5b 3f 32 30 30 34 68 3e 3e 3e 20 1b 5b 33 38 |.[?2004h>>> .[38|
00000010 3b 35 3b 32 34 35 6d 53 65 6e 64 20 61 20 6d 65 |;5;245mSend a me|
00000020 73 73 61 67 65 20 28 2f 3f 20 66 6f 72 20 68 65 |ssage (/? for he|
00000030 6c 70 29 1b 5b 32 38 44 1b 5b 30 6d 1b 5b 4b 68 |lp).[28D.[0m.[Kh|
00000040 65 6c 6c 6f 0a 48 65 6c 6c 6f 21 20 49 74 27 73 |ello.Hello! It's|
00000050 20 6e 69 63 65 20 74 6f 20 6d 65 65 74 20 79 6f | nice to meet yo|
00000060 75 2e 20 49 73 20 74 68 65 72 65 20 73 6f 6d 65 |u. Is there some|
00000070 74 68 69 6e 67 20 49 20 63 61 6e 20 68 65 6c 70 |thing I can help|
00000080 20 79 6f 75 20 77 69 74 68 2c 20 6f 72 20 77 6f | you with, or wo|
00000090 75 6c 64 20 79 6f 75 20 6c 69 6b 65 20 74 6f 20 |uld you like to |
000000a0 63 68 61 74 3f 0a 0a 3e 3e 3e 20 1b 5b 33 38 3b |chat?..>>> .[38;|
000000b0 35 3b 32 34 35 6d 53 65 6e 64 20 61 20 6d 65 73 |5;245mSend a mes|
000000c0 73 61 67 65 20 28 2f 3f 20 66 6f 72 20 68 65 6c |sage (/? for hel|
000000d0 70 29 1b 5b 32 38 44 1b 5b 30 6d 1b 5b 4b 2f 62 |p).[28D.[0m.[K/b|
000000e0 79 65 0a 1b 5b 3f 32 30 30 34 6c |ye..[?2004l|
000000eb
This output shows the following:
- There is the escape sequence
$'\33[?2004h'
at the beginning of the file. This can be removed by setting REPLY="${REPLY#$'\33'*h}"
.
- The prompt
>>>
is followed by the $'\33[38;5;245mSend a message (/? for help)\33[28D\33[0m\33[K
escape sequences and text. This can be removed by setting REPLY="${REPLY#>>>*K}"
.
- There is the escape sequence
$'\33[?2004l'
at the end of the file. This can be removed by setting REPLY="${REPLY#$'\33'*l}"
. However, since this escape sequence is not followed by a newline ($'\n'
), the read
command would return a nonzero value when encountering this sequence.
Below is a test showing the escape sequences and undesired text can be removed.
while read -r; do REPLY="${REPLY#$'\33'*h}"; echo -n "${REPLY#>>>*K}" | hexdump -Cv; done < out.txt
Which produces the following output.
00000000 68 65 6c 6c 6f |hello|
00000005
00000000 48 65 6c 6c 6f 21 20 49 74 27 73 20 6e 69 63 65 |Hello! It's nice|
00000010 20 74 6f 20 6d 65 65 74 20 79 6f 75 2e 20 49 73 | to meet you. Is|
00000020 20 74 68 65 72 65 20 73 6f 6d 65 74 68 69 6e 67 | there something|
00000030 20 49 20 63 61 6e 20 68 65 6c 70 20 79 6f 75 20 | I can help you |
00000040 77 69 74 68 2c 20 6f 72 20 77 6f 75 6c 64 20 79 |with, or would y|
00000050 6f 75 20 6c 69 6b 65 20 74 6f 20 63 68 61 74 3f |ou like to chat?|
00000060
00000000 2f 62 79 65 |/bye|
00000004
The above shows the escape sequences and undesired text have been removed. The same can be applied when the say
command is used, as shown below.
ollama run llama3:latest | tee >(while read -r; do REPLY="${REPLY#$'\33'*h}"; say "${REPLY#>>>*K}"; done)
Below is a simplification which pass the same strings to the say
command. However, there may be cases where this is an oversimplification.
ollama run llama3:latest | tee >(while read -r; do say "${REPLY#[$'\33'>]*K}"; done)
This answer was tested using zsh
and macOS Monterey.