Accessing the Google Pathways LLM from a Bash Script

Implementation

The Bash shell script that implements the PaLM shell as PaLMShell.bash is a script of approximately 100 lines of code in length. This short length demonstrates the simplicity of the PaLM shell in comparison to the compiled high-level language examples available.

Ignoring line comments, the length of the PaLM shell script is nearly halved to 40 lines of code. Thus half the Bash script source code is comments to document and explain the PaLM shell Bash script.

Initialize

The initialize section initializes, or sets up, the Bash script (Listing 1). The header with information about the Bash script is reported, including the release, version, and copyright.

Listing 1

Initialize

01 PALM_KEY="NHGUBE-PBATENGF-BA-XABJVAT-EBG13-PBQVAT"
02
03 #
04 #
05 #
06 echo "PaLM Shell v1.1 (c) 2023 William F. Gilreath"
07 echo "All Rights Reserved. License is GPLv3       "
08 echo
09
10 #
11 # check if PALM_KEY empty, report error need key, exit
12 if [ -z $PALM_KEY ]
13 then
14   echo "Must have PaLM key to use PaLM API!"
15   exit 1
16 fi
17
18 date_time=$(date | date | tr '  ' '-' | tr ':' '-')
19 log_file="PaLM-$date_time.log"
20
21 echo
22 echo "Logging this session to: 'PaLM-$date_time.log'"
23 echo

The script then checks that the variable PALM_KEY, which is the RESTful API key provided by Google to access the Pathways LLM, exists and is set. If not, the script exits, as it cannot access the RESTful API.

Then the date and time are used to create the transcript or log filename used by the script; the filename generated is then reported to the user.

Main Body Infinite Loop

The main body of the bash script for the PaLM shell is an infinite loop (Listing 2). The infinite loop is the software classic REPL. A while loop continues the process of evaluating prompts (see the "Prompt Engineering" box for more on prompts). The command-line prompt is printed, and then the user prompt line read.

Listing 2

Main Body

01 while :
02 do
03   echo -n "PaLM>"
04   read prompt_line
05
06   #'bye' is bye/exit/quit PaLM Shell
07   if [ "$prompt_line" == "bye" ]; then
08     break
09   fi
10
11   echo "Prompt: '$prompt_line'" | tee -a $log_file
12
13   echo | tee -a $log_file #PaLM-$date_time.log
14
15   curl \
16   -s \
17   -H 'Content-Type: application/json' \
18   -d '{ "prompt": { "text": '"\"$prompt_line\""' }}' \
19  "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:
20 generateText?key=$PALM_KEY" |  jq '.candidates | .[].output' | sed 's/\\n/\n/g'
21 | tee -a $log_file #PaLM-$date_time.log
22
23   echo | tee -a $log_file ##PaLM-$date_time.log
24
25 done

Prompt Engineering

With the rise of large language models, it has become clear that the quality of the AI response often depends on the quality of the question. The concept of creating the right prompt to get the answer you want from an LLM is known as prompt engineering [4].

A better prompt will result in a better answer, thus the prompt provided by the user truly matters. There is much material about prompt engineering, and I recommend some study of the topic, especially if you want to master the subtleties of working with an LLM.

An early example of bad prompt engineering was when King Croesus of Lydia asked the Oracle of Delphi whether he should go to war against Persia [5]. The oracle responded that if Croesus attacked Persia he would "destroy a great empire."

Croesus then went to war with the Persians and lost. A great empire did fall, but it was Croesus's own empire. Croesus demonstrated an early example of flawed prompt engineering: A better prompt would have forced the oracle to be more specific.

If the input is only the command bye, the while loop is broken to continue the rest of the script. Otherwise the input is a prompt, in which case that prompt is reported and then sent to the Pathways LLM via a RESTful HTTP call. The response is received and then printed to the user. All the output to the user is also recorded as a transcript to the log file.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News