npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@google-labs/palm-lite

v0.0.3

Published

A zero-dependency client for PaLM APIs

Downloads

3,353

Readme

palm-lite

A zero-dependency JavaScript client for the PaLM API.

Introduction

This library is designed for folks who want the library to mostly get out of the way and only help with the boring bits. If you're looking for a full-featured PaLM API client, check out the official Node.JS client.

The library does two things: it helps you create a valid Request object that you can pass to fetch, and it provides a couple of convenience builders to create these requests with minimal friction.

It intentionally does not cover the actual fetching of requests or handling of responses. These are left to the user to implement. In my own experience, I found these bits to be highly dependent on the use case and the environment -- so I decided to leave them out, at least for now.

Because the library relies on fetch, you can only use it in Node.JS 18 and above. However, because it is zero-dependency, you can use it in the browser as well. Tradeoffs, right?

Usage

As mentioned before, the library has two basic capabilities: constructing fetch requests and convenience building.

Constructing fetch requests

The palm function is the main entry point to the library. It takes an API key and returns an object with three functions: message, text, embedding -- one for each of the PaLM API endpoints.

import { palm } from "palm-lite";

// Grab the PALM_KEY somehow. In this example, we're using an environment variable.
const PALM_KEY = process.env.PALM_KEY;

// Create a `Request` object.
const request = palm(PALM_KEY).message({
  prompt: {
    messages: [{ content: "Hello there!" }],
  },
});
// Feed it to `fetch`.
const data = await fetch(request);
// Get the response.
const response = await data.json();
console.log(response.candidates[0].content);

The library also exports the necessary types for constructing valid PaLM API objects. The three key request types are GenerateMessageRequest, GenerateTextRequest, and EmbedTextRequest. They come with nice JSDoc comments, so you can use your editor's autocomplete to explore the available options.

Similarly, the library exports the response types for each of the three endpoints: GenerateMessageResponse, GenerateTextResponse, and EmbedTextResponse -- also with nice comments.

These exports should speed up your wandering around the PaLM API documentation.

Convenience builders

If you're looking to write fewer braces and square brackets, the library also provides a couple of convenience builders. These follow your standard builder pattern. Create an instance, and then chain methods to build up the request.

There are two builders: Chat for the message endpoint, and Text for the text endpoint. The constructors for both take an object that lets you specify some common request properties, such as candidateCount or temperature. Each also contains methods to populate other properties.

The builder's shape is conveniently designed to fit as arguments for the palm().message or palm().text functions.

For example:

// Create a builder instance and populate the `temperature` field, then
// populate the `text` field in the prompt, then
// add a safety setting, and finally
// add a stop sequence
const text = new Text({ temperature: 0.5 })
  .text("Hello there!")
  .addSafetySetting("HARM_CATEGORY_DANGEROUS", "BLOCK_LOW_AND_ABOVE")
  .addStopSequence("==");

// Feed it to the `text` function
const request = palm(PALM_KEY).text(text);

// ... Later in code
// Change the prompt
text.text("Hello there! How are you?");
// Feed it to the `text` function again
const request2 = palm(PALM_KEY).text(text);

Here's a full example of using most of the bits. This example uses the dotenv package to load the API key from an environment variable.

import { palm, Chat, Text } from "palm-lite";
import { config } from "dotenv";

config();

{
  const text = new Text();
  text.text("Repeat after me: one, two, three... ");

  const request = palm(process.env.PALM_KEY).text(text);
  const data = await fetch(request);
  const response = await data.json();

  console.log("TEXT", response.candidates[0].output);
}

{
  const chat = new Chat()
    .context("You are a pirate. Reply in a distinct pirate voice.")
    .addMessage("Hello, how are you?");

  const request = palm(process.env.PALM_KEY).message(chat);
  const data = await fetch(request);
  const response = await data.json();
  console.log("MESSAGE", response.candidates[0].content);
}

{
  const request = palm(process.env.PALM_KEY).embedding({
    text: "I love you",
  });
  const data = await fetch(request);
  const response = await data.json();
  console.log("EMBEDDING", response.embedding.value);
}

This concludes the tour of the library. It should be fairly straightforward to use. If you have any questions or suggestions, or find bugs, please open an issue on Github.

Happy generating!