Use Bard with Python and Brad-API

0š•koji
3 min readJul 9, 2023

--

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

https://github.com/dsdanielpark/Bard-API

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`.

Step 2. Install Bard-API and python-dotenv

pip install bardapi

or

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

Step 3. Create .env file

COOKIE_TOKEN=ā€™put your cookie valueā€™

Step 4. Write code

```python
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)

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.

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)

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
for choice in responses:
id = choice['id']
response = choice['content'][0]
print(id)
print(response)
load_dotenv()

token = os.environ[ā€˜COOKIE_TOKENā€™]
bard = Bard(token=token)
prompt=ā€™What is a LLM?ā€™

responses = bard.get_answer(prompt)[ā€˜choicesā€™]

--

--

0š•koji

software engineer works for a Biotechnology Research startup in Brooklyn. #CreativeCoding #Art #IoT #MachineLearning #python #typescript #javascript #reactjs