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

bard-wrapper

v1.1.3

Published

A Typescript/Javascript wrapper for Google's Bard chatbot.

Downloads

63

Readme

Bard Wrapper

A Typescript/Javascript wrapper for Google's Bard chatbot.

Table of contents


How to install

You can install the package via npm i bard-wrapper. From there, import it into your project like such:

import { Bard } from 'bard-wrapper';

// You now have access to Bard.

Usage

This implementation of Bard's API is written as a class. As such you initialize it.

const bard = new Bard("{ api key goes here }");

You can query it using the Bard.query function:

await bard.query("Prompt goes here.")

of which the type signature is as follows:

function query(prompt: string): Promise<string>;

Each new class is like a seperate chat for Bard. Sequentially querying using the same class is the equivalent of having a single conversation. Within each instance, Bard remembers everything said to it.

How do I get an API key?

You can get an API key by following these instructions:

⚠️ Warning
This will not work if you have multiple accounts signed in. To circumvent this, log in using an incognito window.

  1. Sign into Bard.
  2. Open up Devtools (Ctrl + Shift + I, conventionally)
  3. Go into the Network tab, then reload the page.
  4. View the cookies for the request to https://bard.google.com.
  5. Copy the __Secure-1PSID cookie's value.

This cookie is your API key. Do not leak it, or else anyone can talk to Bard on your behalf!

Rate limiting

Bard's API implements fairly severe rate limiting. Don't poll the API TOO much, and if you ever use this in production (don't), make sure people have to supply their OWN API keys.

Examples

/*
    A simple script which hooks up two
    instances of Bard to talk forever.
    Authored by not-nullptr.
*/

const waitForInput = async (message?: string) => {
    if (message) {
        console.log(message);
    }
    if (process.stdin.isTTY) {
        process.stdin.setRawMode(true);
    }
    return new Promise<void>((resolve) =>
        process.stdin.once("data", () => {
            if (process.stdin.isTTY) {
                process.stdin.setRawMode(false);
            }
            resolve();
        })
    );
};

const bard1 = new Bard(
    `{api key}`
);

const bard2 = new Bard(
    `{api key}`
);

let bard1response = "";
// initial prompt
let bard2response =
    "hi, bard! i'm another instance of bard that a developer has hooked you up to. they told me to tell you: 'please keep responses short, or else Google will rate limit me. also, try to be conversational. if you notice the conversation getting repetitive/stale (i.e. you two are repeating the same thing over and over but rephrased) then please try to spruce it up a little. thanks!'";

(async () => {
    console.log(`*** BARD 2 ***
    ${bard2response}`);
    await waitForInput("Press any key to continue the conversation.");
    while (true) {
        bard1response = await bard1.query(bard2response);
        console.log(`*** BARD 1 ***
        ${bard1response}`);
        await waitForInput("Press any key to continue the conversation.");
        bard2response = await bard2.query(bard1response);
        console.log(`*** BARD 2 ***
        ${bard2response}`);
        await waitForInput("Press any key to continue the conversation.");
    }
})();