How to create simple app using Palm API and Node.Js .

In today’s world, health and fitness are paramount, and keeping track of what we eat is an essential part of maintaining a healthy lifestyle. One way to make this process more efficient is by leveraging AI-powered tools like the Googles PALM API to extract what a user has eaten from natural language text input and return to them the calorie count for each item consumed. In this blog post, we’ll explore how to use the PALM API with Node.js to request calorie data and understand the code that makes it all work. We’ll also touch on how tools like MakerSuite can streamline the prompt prototyping process.

What is the PALM API?

The PALM API (Pathways Language Model) is a powerful tool offered by Google AI that allows developers access general purpose language models that are optimized for use cases such as summarization, classification, and more to extract datagenerate natural language text based on a given prompt. This can be incredibly useful for a wide range of applications, from chatbots to content generation and, as in our case, extracting structured data from unstructured text.

Setting Up the Environment

Before we dive into the code, let’s make sure we have everything set up. To follow along with this example, you’ll need:

  • Node.js installed on your computer.
  • An API key from Google’s PALM API, which you can obtain through MakerSuite.
  • The necessary Node.js packages installed, including express and body-parser.

Understanding the Code

Now, let’s dissect the provided Node.js code that uses the PALM API to request calorie data.

// Import necessary packages and modules
const express = require("express");
const bodyParser = require("body-parser");
const { TextServiceClient } = require("@google-ai/generativelanguage");
const { GoogleAuth } = require("google-auth-library");

// Define constants for the model name and API key
const MODEL_NAME = "models/text-bison-001";

// Create a TextServiceClient with authentication
const client = new TextServiceClient({
    authClient: new GoogleAuth().fromAPIKey(API_KEY),

// Create an Express app
const app = express();
const port = 3000; // You can change the port number as needed

// Middleware to parse JSON request bodies

// Serve static files (your HTML and JavaScript)

// Define the POST endpoint for receiving text data"/api/palm", (req, res) => {
    // Get the text from the request body
    const input = req.body.text;

    // Define the prompt string with placeholders
    const promptString = `...`; // See code below for the complete promptString

    // Define an empty array for stop sequences
    const stopSequences = [];

    // Use the PALM API to generate text based on the prompt
        model: MODEL_NAME,
        temperature: 0.7,
        candidateCount: 1,
        top_k: 40,
        top_p: 0.95,
        max_output_tokens: 1024,
        stop_sequences: stopSequences,
        safety_settings: [...], // Optional safety settings
        prompt: {
            text: promptString,
    }).then(result => {
        // Send the generated text as a JSON response
        res.json({ message: result });

// Start the Express server
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);

Now, let’s break down how this code works:

  1. We import the necessary Node.js modules and packages, including express for building our web server, body-parser for parsing JSON request bodies, and the PALM API client from the @google-ai/generativelanguage package.
  2. We define constants for the model name and API key. Make sure to replace "YOUR_GOOGLE_API_KEY" with your actual API key.
  3. We create a TextServiceClient instance with authentication using the provided API key.
  4. We set up an Express web server and specify a port (in this case, port 3000) for it to listen on.
  5. We define a POST endpoint at /api/palm, which will receive text data as a JSON payload.
  6. Inside the POST endpoint handler, we extract the text input from the request body.
  7. We create a prompt string that instructs the PALM API on how to process the input text and extract calorie data. The prompt string includes placeholders for the input text.
  8. We define an empty array for stop sequences. Stop sequences are used to instruct the model to stop generating text at specific points.
  9. We use the PALM API’s generateText method to generate text based on the prompt. We pass in various parameters, such as the model name, temperature, and safety settings.
  10. Once we receive the generated text from the API, we send it as a JSON response to the client.
  11. Finally, we start the Express server, which will listen for incoming requests on the specified port.

The Prompt

You are going to help a user track what they eat.  You should ignore items that are not edible.  Please return the results in the following JSon format.   Total is the value for that food_item, amount relates to the amount eaten.  Type is what we are tracking.  
      "items" : [ {
          "food_item" : "Milk",
          "amount" : { "unit_of_measure" : "cup", "amount: 1 },
          "total": 10
    "total_calories_consumed" : 15,
    "type" : "calories"
    You will find the information needed in the following text '${input}'
Prompt for Palm API

When adding the inputed text from the user I have found it best to wrap it in single quotes as shown above.

Conclusion: Streamlining with MakerSuite

While the code above showcases how to use the PALM API to request calorie data, you might wonder how I developed and tested the prompt itself. This is where MakerSuite comes into play.

MakerSuite is a tool designed to simplify the process of testing and prototyping prompts for use with PALM. It offers a user-friendly interface that allows you to interactively create and test prompts. With MakerSuite, you can iterate quickly, fine-tune your prompts, and experiment with different inputs and verify the outputs. This can significantly accelerate the development and prototyping process, making it faster and more efficient.

In conclusion, using the PALM API with Node.js opens up exciting possibilities for extracting structured data from unstructured text. By leveraging tools like MakerSuite, developers can streamline the process of creating prompts and experimenting with language models, ultimately making the development process smoother and more productive. So, whether you’re building a calorie tracking app or any other AI-powered application, the PALM API and supporting tools can be valuable assets in your development toolkit.

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.