Accessing the Google Pathways LLM from a Bash Script

Smart Shell

© Lead Image © lightwise, 123RF.com

© Lead Image © lightwise, 123RF.com

Article from Issue 282/2024
Author(s):

The PaLMShell.bash script lets you connect to the Google Pathways API Large Language Model (PaLM) from the command line.

The PaLM shell is a simple Bash script that interfaces with the Google Pathways large language model (LLM) API [1] over a RESTful API. This method for accessing Google Pathways is simpler than a more complex implementation in a high-level compiled language. You only need a shell script – no other libraries, packages, or configuration files.

Large Language Models

Large language models, or LLMs [2], are currently very newly popular. An LLM is a neural network, but a neural network that has been trained on massive amounts of data, using massive amounts of computing power to create a model [3] with potentially billions of parameters.

The most well-known and popular LLM is ChatGPT or Chat Generative Pre-trained Transformer from OpenAI, which was trained using 175 billion parameters for the model. ChatGPT, which was launched in 2022, is available as a chatbot. Other LLMs include LLaMA (Large Language Model Meta AI) [6] from Meta and BLOOM (BigScience Large Open-science Open-access Multilingual Language Model) [7], which was created by over 1,000 researchers in artificial intelligence for large-scale public access.

For the shell-based LLM client described in this article, I will use the Pathways Language Model (PaLM) from Google. PaLM was introduced in April 2022 [8] and is trained on 540 billion parameters. This LLM is now part of Google Gemini, which is part of the Google AI Studio.

PaLM is a single LLM that can generalize across domains and tasks while being highly efficient. In May 2023, Google announced PaLM2, the next LLM. Google PaLM is available through Google's PaLM API, a RESTful interface to the online LLM service.

Several programming languages support access to the PaLM API, including Java, Python, Go, C#, and Swift. I downloaded the Java version of the client and libraries, and after some tinkering and effort, I got the Java code to run and work with Google PaLM. But the effort to get the Java source code to work as a client to the PaLM RESTful API was a challenge. I decided to roll my own Bash-based PaLM client that would be accessible from the command line.

Access by API Key

One thing you'll need to work with the Google PaLM RESTful API is an API key. You can request the API key online. I requested a key and it took approximately three days to receive it.

Bash Script Goals

The goals for a Bash script that functions as a PaLM shell are simple:

1. Connect to the Google PaLM RESTful API successfully via HTTP.

2. Operate simply, with only one shell command to exit the shell.

3. Automatically log and record the session as a shell transcript to file.

Several external utilities were used to construct the PaLM shell as a Bash script, including:

  • cURL – connect to Google PaLM RESTful API via an HTTP URL.
  • jq – JSON is used in the payload or body of the message.
  • sed – classic stream editor.
  • tee – logs output from the Bash script to an external file with an automatic name.

Two other utilities were date and tr, which were used to create the external file name used with the tee utility.

The PaLM shell has three phases:

1. Initialize

2. Infinite command loop, REPL (read, evaluate, print, loop) [9]

3. De-initialize

Initialization simply sets up the PaLM API key and reports the version, date, and license. The script checks that the PaLM API key is not empty and then creates the external log file name.

The main loop is an infinite loop that continues to read, echo prompts, print to the user, and tee to an external file. This process continues until the one PaLM command bye, which causes the infinite loop to terminate.

The last phase of the Bash shell script is to finalize and de-initialize what is simply an exit message, and then clean exit from the Bash shell script to the operating system.

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