avatarTristan Wolff

Summary

The web content provides a step-by-step guide on using a reverse-engineered Bard API to interact with Google's Bard AI chatbot directly from code.

Abstract

The article discusses the recent popularity of Google's Bard AI chatbot and introduces a method to access Bard's capabilities through an unofficial API. This API allows developers to perform inferences with Bard from their own Python code or Google Colab, which was not possible before due to the lack of an official API endpoint from Google. The guide details how to obtain an authentication token from the Bard website and use it with the BardAPI, providing code snippets and instructions for executing the process. It also offers a link to a Google Colab notebook for hands-on experimentation and mentions the need for a VPN in certain geofenced regions to access Bard. The article encourages readers to follow the author on social media for more insights on AI and creativity and invites them to support the author's work on Medium.

Opinions

  • The author suggests that while Bard is gaining popularity, it may not always be as useful as OpenAI's ChatG

Google’s Bard: A Step-by-Step Guide to Using the Unofficial Bard API

Using Google’s latest AI chatbot right from your code, thanks to a reverse-engineered Bard API

Image by the author & Midjourney

Bard seems to be all the rage right now (although in some cases it’s not as useful as ChatGPT). But if you haven’t tried Google’s latest AI chatbot yet, you definitely should. Interestingly, you can now do this via a reverse-engineered Bard API.

In this article, I’ll show you how that works.

[Please note that you may need to set up a VPN to access Google Bard from countries or regions that are geofenced due to regulatory issues. There are many reliable VPN providers out there, so choose one that suits your needs (ExpressVPN, for example, is a good option)]

What’s the Bard API?

To use Google’s Bard, you typically need to navigate to bard.google.com and interact through the chatbot interface. Whenever you enter a question or instruction, the website sends it to an AI model that responds to your input. This is called “inference,” meaning you ask an AI model to perform a task with your input.

However, if you preferred to perform inference directly from your Python code or a Google Colab instead of using the Bard website, you couldn’t until recently because Google doesn’t currently provide an API endpoint for Bard.

That’s why developers have reverse-engineered a Bard API by examining how inferences are initialized on the Bard website.

With the unofficial BardAPI, you can access Bard from anywhere with just a few lines of code.

So let’s try that.

Using Bard API

When using the Bard interface, the website stores an authentication token to make sure that you are a legitimate Google account holder. This authentication token is necessary in order to interact with the AI model.

And here’s how to get it:

  1. Navigate to bard.google.com and log in with your Google account (if you’re not already logged in).
  2. In Chrome or Firefox, right-click on the page and choose “Inspect” from the context menu. For Safari, you need to enable “Show Develop menu in menu bar” in the Advanced section of Preferences.
  3. From the Inspect window, select “Application” and navigate to “Storage”. Here, you’ll find all the data that the website uses to function correctly, including the “__Secure-1PSID” token. Once you’ve found it, copy its value — a lengthy sequence of letters and numbers.
Finding the “__Secure-1PSID” token

Remember, the authentication token is your personal key to Google Bard, so keep it confidential.

Now that we have the necessary token, we can use it with the reverse-engineered Bard API (replace ‘ENTER YOUR AUTH TOKEN HERE’ with your auth token and modify the question to whatever you’d like Bard to do for you):

from bardapi import Bard
import os

os.environ['_BARD_API_KEY']="ENTER YOUR AUTH TOKEN HERE"

answer = Bard().get_answer("What is ChatGPT?")['content']
print(answer)

You can try it online using this Google Colab: https://colab.research.google.com/drive/1zzzlTIh0kt2MdjLzvXRby1rWbHzmog8t?usp=sharing

Again, replace the xxxxxxxxxx with your auth token and change the question to whatever you like.

In both cases, if you get a timeout error, try passing the timeout parameter to the Bard object to give BardAPI more time to wait for a result. In this example: “timeout=30” to wait up to 20 seconds

from bardapi import Bard
import os

os.environ['_BARD_API_KEY']="ENTER YOUR AUTH TOKEN HERE"

answer = Bard(timeout=30).get_answer("What is ChatGPT?")['content']
print(answer)

For further information check out the Bard API Github repo: https://github.com/dsdanielpark/Bard-API

➡️ For more information about AI & Creativity, follow me on Twitter or Medium (use my referral link to get full access to all my articles and those of thousands of other writers).

➡️ If you like my content, why not leave a “clap” at the end of this article, so more people can see it?

Stay updated with the latest news and updates in the creative AI space — follow the Generative AI publication.

Artificial Intelligence
Technology
Innovation
Programming
Coding
Recommended from ReadMedium