PaLM API Setup: Getting Started with Google’s Large Language Model Used by Bard

Bob Main
3 min readJul 14, 2023

Alright, it’s time to dive into building a small AI app. For this project, I’ll be utilizing the PaLM API, which powers Google’s Bard’s Generative AI API. PaLM stands for “Pathways Language Model,” and it’s a powerful tool that enables the creation of sophisticated AI applications. By harnessing the capabilities of PaLM, we can unlock a world of creative possibilities for our app. Let’s get started and explore the potential of this exciting API!

Prerequisites

To save time, I’m going to assume you have Node.js and npm already installed and are familiar with that environment, and have a code editor of choice. Also, note that I was using a macOS environment when creating this guide.

API Setup

  1. Visit: https://makersuite.google.com/
  2. Click the “Create an API key” button and generate a key for your new project.

Get “Data prompt” Example

  1. Go back to https://makersuite.google.com/
  2. Click “Create” in the “Data prompt” section.
  3. Click “Analogy factory” in the “Sample prompts” section.
  4. Click “< > Get Code” and copy the JavaScript code.
  5. Create a project folder and a data-prompt.js file in that folder.
  6. Paste the copied code into the data-prompt.js file.

✨SIDE NOTE: The data prompt is interesting because you sort of train the model. In the sample prompts section, you can find the external “link” button to see a bunch of interesting examples.

Setting up the project and running it

  1. Find and replace the line const API_KEY = "YOUR API KEY"; with your API key.
  2. Change directory (cd) into your project folder and run npm install.
  3. Run npm i @google-ai/generativelanguage.
  1. You should now be able to run node data-prompt.js in your project's root folder and see an output response!

My source code for this project

Visit My project folder on GitHub: https://github.com/robbobfrh84/Tutorials_Examples/tree/main/AI_Lab/PaLM_API-basic_data_prompt_example

  • There you’ll see a file called prompt.js that I've created.
  • Here, I’ve separated the prompt into its own file so that I can work on it without all the noise from the other code.

My example app Idea: “Colors of things”

The prompt string I’ve created is essentially the app core. I’m telling the PaLM API that I’ve got a bunch of examples for what I want the AI to follow:

  • Give a word and return color(s) associated with that word. That’s it! 🤣

Here’s what the prompt string looks like:

"
input: Blood
output: red

input: Grass
output: green

input: Sky
output: blue

input: Wood
output: brown

input: Cloud
output: white

input: Bee
output: yellow, black

input: Cow
output: black, white

input: Tree
output: green, brown

input: Clown fish
output: orange, white, black

input: ${input}
output:
"

Creating Command-line Data Prompts

Back in my project folder, see the file data-prompt.js. Here I've made it so we can pass a variable to the app to test our code as an argument.

  • Prompt with “rose” : node data-prompt.js rose > output: “red, pink”
  • Prompt with “salmon”: node data-prompt.js salmon > output: “orange”

You can use “-” to separate words and be more specific.

  • Prompt with “salmon-scales”: node data-prompt.js salmon-scales > output: “orange, red, white”
  • Prompt with “salmon-cooked”: node data-prompt.js salmon-cooked > output: “orange, pink”

NOTE: By Default node data-prompt.js will use the prompt “Sun”.

☀️ We now should be getting colors associated with words! Have fun!

OH, I’ve added more to this project in this post! Check it out! 👀✨

--

--

Bob Main

Exploring the wilderness of web development, creating example apps & tutorials, usually weaving some ✨silliness✨ in! Oh, & other random stuff...