Skip to main content

Bard.py

Latest Release Python MIT License

Python Client for Bard, a Chat Based AI tool by Google.

Features

  • Connect to Bard, Google's AI-powered personal assistant.
  • Ask questions and have a conversation.
  • Improve responses by defining the conversation tone and length.
  • Use asyncio for efficient and non-blocking I/O operations.

Requirements

  • Python 3.10 or newer
  • Google account with access to Bard

Installation

To install Bard.py, run the following command:

pip install bard-py

or, if you use poetry:

poetry add bard-py

Usage

Prerequisites

To use Bard.py you first need to extract the __Secure-1PSID and __Secure-1PSIDTS cookies from the Bard web page. These cookies are used to authenticate your requests to the Bard API.

To get the cookies, follow these steps on Chrome:

  • Go to the Bard web page.
  • Write a message on the chat dialog that appears.
  • Open the developer tools in your browser (usually by pressing F12 or right-clicking on the chat dialog and selecting Inspect).
  • Select the Application tab and click on the Cookies option to view all cookies associated with https://bard.google.com.
  • Look for the __Secure-1PSID and __Secure-1PSIDTS cookies and click on them to expand their details.
  • Copy the values of the cookies (they should look like a long string of letters and numbers).

Then, set them as environment variables in your shell:

export SECURE_1PSID=<your-cookie>
export SECURE_1PSIDTS=<your-other-cookie>

or, in your Python code:

os.environ["SECURE_1PSID"] = "<your-cookie>"
os.environ["SECURE_1PSIDTS"] = "<your-other-cookie>"

Example

You can use Bard.py to easily create a CLI client for Bard:

import asyncio

from bard import BardClient


async def main() -> None:
    async with BardClient() as bard:
        while True:
            prompt = input("You: ")

            if prompt == "!reset":
                await bard.reset_conversation()
                continue
            elif prompt == "!exit":
                break

            response = await bard.ask(prompt)
            print(f"Bard: {response}")


if __name__ == "__main__":
    asyncio.run(main())

Bard Client

You can create a Bard Client and initialize a connection with Bard which starts a conversation:

bard = BardClient()

await bard.start_conversation()

# Conversation

await bard.end_conversation()

Alternatively, you can use the async with statement to keep the code compact:

async with BardClient() as bard:
    # Conversation

Reset Conversation

You can reset the conversation in order to make the client forget the previous conversation:

async with BardClient() as bard:
    # Conversation
    await bard.reset_conversation()

Ask

You can ask Bard questions and get the results:

async with BardClient() as bard:
    response = await bard.ask("When was Bard released?")
    print(response)

Tone

You can set the tone when having a conversation with Bard:

async with BardClient() as bard:
    _ = await bard.ask("When was Bard released?")

    response = await bard.ask("When was Bard released?", tone="Professional")
    print(response)

The available options for the tone parameter are:

  • Professional
  • Casual

Length

You can set the response length when having a conversation with Bard:

async with BardClient() as bard:
    _ = await bard.ask("When was Bard released?")

    response = await bard.ask("When was Bard released?", length="Short")
    print(response)

The available options for the length parameter are:

  • Short
  • Long

Exceptions

When something goes wrong, Sydney.py might throw one of the following exceptions:

Exception Meaning Solution
CreateConversationException Failed to create conversation Retry or use new cookies
AskException Failed to get response from Bard Retry or use new cookies
NoResponseException Received an empty response from Bard Wait and retry

For more detailed documentation and options, please refer to the code docstrings.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for bard-py 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bard-py 0.4.0
File Size Uploaded
bard_py-0.4.0.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bard-py 0.4.0
File Interpreter ABI Platform
bard_py-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / bard_py-0.4.0.tar.gz

Download URL bard_py-0.4.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
191e3ef302e65a43ffa28b88c997d03e105bed43a25f2608ab4d61524c2af0e7
BLAKE2b-256 checksum
How to use checksums
df39a782fbb9e12ad2ef8820702f00295700bbd4d06b3975cac60586124e35af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.10 Linux/6.1.62-1-lts

Release files / bard_py-0.4.0-py3-none-any.whl

Download URL bard_py-0.4.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d074d4e34c89817d4790548280a1b71924cb8e23703a03e5c3bcaebed4470ed4
BLAKE2b-256 checksum
How to use checksums
8bbb6f43d0881731dbeb0a094cebd597805528cc9935b0d115f82972cd738eb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.10 Linux/6.1.62-1-lts

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page