DEV Community

Cover image for Use Bard with Python and Brad-API
0xkoji
0xkoji

Posted on • Updated on

Use Bard with Python and Brad-API

Bard-API is a python package that allows us to use Bard via python script.

GitHub logo dsdanielpark / Bard-API

The unofficial python package that returns response of Google Bard through cookie value.

Development Status :: 5 - Production/Stable

Google Bard API

PyPI package Downloads Code style: black PyPI

The python package that returns response of Google Bard through value of cookie.

Please exercise caution and use this package responsibly. This python package is UNOFFICIAL.

I referred to this github repository(github.com/acheong08/Bard) where inference process of Bard was reverse engineered. Using __Secure-1PSID, you can ask questions and get answers from Google Bard. Please note that the bardapi is not a free service, but rather a tool provided to assist developers with testing certain functionalities due to the delayed development and release of Google Bard's API. It has been designed with a lightweight structure that can easily adapt to the emergence of an official API. Therefore, I strongly discourage using it for any other purposes. If you have access to reliable official PaLM-2 API or Google Generative AI API, replace the provided response with the corresponding official…




Step 1. Get cookie data

Go to https://bard.google.com and open Chrome Developer tools and click Application tab.
Click Cookies under Storage and copy the value of _Secure-1PSID.

chrome

Step 2. Install Bard-API and python-dotenv

pip install bardapi
Enter fullscreen mode Exit fullscreen mode

or

pip install git+https://github.com/dsdanielpark/Bard-API.git
Enter fullscreen mode Exit fullscreen mode
pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

Step 3. Create .env file

COOKIE_TOKEN='put your cookie value'
Enter fullscreen mode Exit fullscreen mode

Step 4. Write code

import os
from bardapi import Bard
from dotenv import load_dotenv


load_dotenv()

token = os.environ['COOKIE_TOKEN']
bard = Bard(token=token)
response = bard.get_answer("What is a LLM?")['content']
print(response)

Enter fullscreen mode Exit fullscreen mode

output

LLM stands for Master of Laws. It is a postgraduate law degree that is typically obtained after completing a Juris Doctor (JD) degree. LLM programs can be general or specialized, and they can be offered in a variety of subjects, such as tax law, environmental law, and international law.

The LLM degree is a valuable credential for lawyers who want to specialize in a particular area of law or who want to advance their careers in academia or government. LLM programs can also help lawyers to develop their research and writing skills, which are essential for success in many legal careers.

The full form of LLM is Legum Magister, which is Latin for "Master of Laws." The LLM degree is abbreviated as LL.M. in English.

Here are some of the benefits of obtaining an LLM degree:

* **Specialization:** LLM programs allow you to specialize in a particular area of law, such as tax law, environmental law, or international law. This can give you a competitive edge in the job market and help you to advance your career.
* **Research and writing skills:** LLM programs can help you to develop your research and writing skills, which are essential for success in many legal careers.
* **International experience:** Many LLM programs are offered in countries outside of the United States. This can give you the opportunity to gain international experience and learn about different legal systems.

If you are interested in pursuing an LLM degree, there are a few things you should keep in mind:

* **Admission requirements:** Admission requirements for LLM programs vary from school to school. However, most programs require that you have a JD degree or an equivalent law degree.
* **Cost:** The cost of an LLM degree can vary depending on the school and the program. However, LLM programs can be expensive, so it is important to factor in the cost when making your decision.
* **Career opportunities:** LLM degrees can open up a variety of career opportunities for lawyers. However, the specific opportunities that are available to you will depend on your area of specialization and your work experience.

If you are considering pursuing an LLM degree, I recommend that you research different programs and talk to lawyers who have LLM degrees. This will help you to make an informed decision about whether or not an LLM degree is right for you.
Enter fullscreen mode Exit fullscreen mode

Specify output format

Also we can change the output format like ChatGPT.

import os
from bardapi import Bard
from dotenv import load_dotenv


load_dotenv()

token = os.environ['COOKIE_TOKEN']
bard = Bard(token=token)
prompt='''
What is a LLM?
The answer format should be the following.

answer: {
[
  id: 1,
  content: draft1
],
 id: 2,
  content: draft2
]
}
'''
response = bard.get_answer(prompt)['content']
print(response)
Enter fullscreen mode Exit fullscreen mode

Get Drafts

If you use Bard, you know Bard has a couple of drafts. Bard-API can get them.

import os
from bardapi import Bard
from dotenv import load_dotenv

load_dotenv()

token = os.environ['COOKIE_TOKEN']
bard = Bard(token=token)
prompt='What is a LLM?'

responses = bard.get_answer(prompt)['choices']

for choice in responses:
  id = choice['id']
  response = choice['content'][0]
  print(id)
  print(response)
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
shahizhsj profile image
Shahizhsj • Edited

Hey i am getting the error for running the above code
mycode:
load_dotenv()
bard = Bard(token="xxxxxx")
response = bard.get_answer('What are the stocks with the ratio less than 10')['content']
print(response)

Error:Exception: SNlM0e value not found. Double-check __Secure-1PSID value or pass it as token='xxxxx'.
can you help me out?

Collapse
 
0xkoji profile image
0xkoji

Did you pass __Secure-1PSID as token ?