Skip to content

decide

decide asks a bounded question about text and returns an answered, not-sure or failed result.

Terminal window
printf '%s' 'Please refund my order.' |
thinkthen decide 'Does the customer request a refund?'

Output

true

A decide call with --check goes straight into an if. -q keeps the word off the screen, as with grep -q.

Terminal window
if thinkthen decide --check -q 'Does the customer request a refund?' \
--text 'Please refund my order.'; then
echo "refund queue"
fi

The then branch runs only on a yes. If the message could not be judged, the command exits 6 and says why on standard error. It never exits 0.

Don't add a plain else to this if. In a shell, else runs for every exit code that is not 0: no, not sure and failed alike. To act on a no, read the exit code:

Terminal window
status=0
thinkthen decide --check -q 'Does the customer request a refund?' \
--text 'Please refund my order.' || status=$?
case $status in
0) echo "refund queue" ;;
1) echo "inbox" ;;
*) exit "$status" ;;
esac

Send not sure to a person. Never treat it as a no.

Set two bars and decide can also be not sure. Exit codes 0 and 1 are the yes and the no. Codes 3 and 6 both mean there is no answer to read.

Handle every result state in Bash
Terminal window
status=0
thinkthen decide --check -q 'Does the customer request a refund?' \
--text 'Where is my parcel?' --low 0.2 --high 0.8 || status=$?
case $status in
0) echo "refund queue" ;;
1) echo "inbox" ;;
3 | 6) echo "a person reads it" ;;
*) exit "$status" ;;
esac

3 | 6 covers both not sure and failed. The last branch is for a mistake in the call itself, such as a misspelled option, which exits 2.

Replace -q with --json to keep the result as well as the exit code.

Terminal window
status=0
is_refund=$(thinkthen decide --check --json \
'Does the customer request a refund?' \
--text 'Where is my parcel?' --low 0.2 --high 0.8) || status=$?
case $status in
3) echo "a person reads it" \
"$(jq -r '.probability' <<< "$is_refund")" ;;
6) echo "try again later" "$(jq -r '.error.code' <<< "$is_refund")" ;;
0) echo "refund queue" ;;
1) echo "inbox" ;;
*) exit "$status" ;;
esac

Yes and no are the two answers. Not sure and failed are the two ways of having none. Bash can't check that a case names every state, so end each one with a *) branch that stops the script.

The result's status field holds the same state as a word: answered, unsure or failed.

Without --check, the exit code answers only "did it work". It is 0 for yes, no and not sure, and 6 when the result failed.

Terminal window
if ! is_refund=$(thinkthen decide \
'Does the customer request a refund?' \
--text 'Please refund my order.'); then
echo "could not judge it: $is_refund"
fi

When the service cannot judge the message, this reports the failure and its reason. Not sure counts as working: ThinkThen did its job, and the answer was "not sure".

Terminal window
set -euo pipefail
is_refund=$(thinkthen decide 'Does the customer request a refund?' \
--text 'Please refund my order.')

Under set -e this line stops the script at once when the result failed, with the command's exit code. Otherwise the script goes on, and $is_refund holds the printed word.

Terminal window
thinkthen decide 'Does the customer request a refund?' \
--text 'Where is my parcel?' \
--low 0.2 --high 0.8 --json | jq '.probability'
  • No bar option: one bar at 0.5. The answer is always yes or no.
  • --threshold 0.7: one bar. Yes at 0.7 or above, otherwise no.
  • --low 0.2 --high 0.8: two bars. No below 0.2, not sure from 0.2 up to 0.8, yes at 0.8 or above.

probability is the number ThinkThen compared with your bars: the model's probability of yes. A confident no has a low probability, such as 0.05. A result has no probability only when the result failed.

The number is the model's own. To choose bars for your data, run examples you have already labelled through thinkthen decide --json, save the results, and compare them with your labels using thinkthen audit, described at the end of this page.

Read the CSV file and save its answers

--column selects the CSV column to judge; --into names the answer column. Check the exit status before using the saved file.

messages.csv
message
Please refund my order.
Terminal window
thinkthen decide 'Does the customer request a refund?' messages.csv \
--column message --into is_refund
cat messages.thinkthen.csv

Output

messages.thinkthen.csv
message,is_refund
Please refund my order.,TRUE
Read the model’s numbers with the answer state

The state distinguishes an answer from not sure and failure. These are the actual numbers returned for the shown message.

Terminal window
echo 'I was charged twice for one invoice.' |
thinkthen decide --low 0.2 --high 0.8 --json \
'Does the customer request a refund?' |
jq -c '{status, probability}'

Output

{"status":"unsure","probability":0.71}
Options
OptionWhat it does
<QUESTION>Question to ask
[FILE]...Input files; default: standard input
--name <NAME>Optional execution identity
--text <TEXT>Judge this text instead of reading input
--threshold <THRESHOLD>Required probability
--low <LOW>Low probability bar
--high <HIGH>High probability bar
--check [=<CHECK>]Return the verdict as an exit code
--yes-means <TEXT>Meaning of a true answer
--no-means <TEXT>Meaning of a false answer
--column <COLUMN>Field column; repeat; replaces a saved pointer
--details [=<DETAILS>]Add status, probability, error, detail and source columns
--in-place [=<IN-PLACE>]Replace source with backup
--into <INTO>Answer field for JSON Lines; column for tables
--output <OUTPUT>Destination path
--sheet <SHEET>Worksheet

Shared options