Use Bard with Python and Brad-API
Tags: python, bard, generativeai
Published: 2023-07-08 02:45:21

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


{% github 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`.


![chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/keqm3ydf4xoc5dswwc7j.png)

## Step 2. Install Bard-API and python-dotenv

```shell
pip install bardapi
```

or 

```shell
pip install git+https://github.com/dsdanielpark/Bard-API.git
```

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

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


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