Bard – DEPRECATED

Bard has been superseded by Google Gemini – you can find my Gemini scripts in the AI category.

This script allows you to send sequential queries to Bard, drawn from the input.csv file. The answers are saved in the output.csv file.

Usage

This is similar to the ChatGPT script, except that this does not use the OpenAI API, and therefore costs nothing to run. It does require a bit more setting up. Being able to send queries automatically to AI Chatbots means you can create high volumes of good quality content without intervention. For example, you could use this script to ask Bard to create an outline for a book, and then ask it to write each chapter in sequence. This tends to produce better quality responses than asking it to write a book in one go. Alternatively you could instruct it to read a document and then provide an analysis, step by step.

Prerequisites

  1. You need a Bard account
  2. Once you have the account you will need to log in with a Chrome browser and locate the __Secure-1PSID cookie. You do this by right clicking anywhere on the Bard page once you’ve logged in then click “Inspect”. In the console that opens, click on the “Application” tab, and in the “Storage” window, click on the Cookie dropdown. Now click on the https://bard.google.com item which should present all the cookies for that page. Copy the long string associated with the __Secure-1PSID cookie. (Be careful to choose the right one). Paste that into the script at the point shown.
  3. Now install the Bardapi and requests libraries
pip install requests
pip install bardapi

How to use it

  1. Save the script below in the folder of your choice.
  2. Edit the file and insert your Bard cookie string
  3. Also edit the prompts and timings if desired – you can change the speed of responses. In this script previous questions and answers are provided as context.
  4. Create an input.csv file, which contains the prompts in the first column to be sent to OpenAI.
  5. The output is saved as output.csv
import csv
from bardapi import Bard
import time
import os
import re
import requests

# Set up a reusable session
session = requests.Session()
session.headers = {
    "Host": "bard.google.com",
    "X-Same-Domain": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
    "Origin": "https://bard.google.com",
    "Referer": "https://bard.google.com/",
}

# Set cookies for the session
token = 'your_Bard_cookie_very_long_string_of_digits_etc_etc_etc.'  
session.cookies.set("__Secure-1PSID", token)

# Create a Bard instance with the reusable session
bard_instance = Bard(token=token, session=session, timeout=30)

# Open input and output CSV files with error handling
input_file = open('input.csv', 'r', encoding='utf-8', errors='replace')
output_file = open('output.csv', 'w', encoding='utf-8', newline='')

# Create CSV writers
input_csv = csv.reader(input_file)
output_csv = csv.writer(output_file)

# Write headers to output CSV
# output_csv.writerow(['Prompt', 'Response'])

# Set the desired API call rate (2 calls per minute)
calls_per_minute = 1
interval = 60 / calls_per_minute

# Regular expression pattern to match file paths in prompts
file_path_pattern = r"C:/Users/Steve/.*?\.txt"

context = ""

# Iterate through prompts and generate responses
for row in input_csv:
    prompt = row[0]

    # Check if the prompt contains a file path
    file_paths = re.findall(file_path_pattern, prompt)

    if file_paths:
        # Upload and generate a response for each file path found
        for file_path in file_paths:
            # Read the content of the file with the specified encoding and error handling
            with open(file_path, 'r', encoding='utf-8', errors='replace') as file:
                file_content = file.read()

            # Replace the file path with the content in the prompt
            prompt = prompt.replace(file_path, file_content)

    if not context:
        # If context is empty, set the prompt without the previous conversation context
        full_prompt = "Please answer this question: " + prompt
        print("Prompt:", full_prompt)
    else:
        # Otherwise, include the previous conversation context
        full_prompt = (
            "Please consider the previous conversation we have had, which I am recording for you here within the <context> tags: <context> "
            + context
            + " </context> Now, bearing in mind the conversation so far within the context tags which you have already responded to - no need to answer any of those questions again -  please answer this question: "
            + prompt
        )
        print("Prompt:", full_prompt)

    # Send an API request and get a response - print to check.
    response = bard_instance.get_answer(full_prompt)
    response_content = response['content']
    print(response_content)

    # Update context after each loop - depracated in Bard
    # context = response_content

    # Write to file
    output_csv.writerow([response_content])

    # Introduce a delay to limit the rate of API calls
    time.sleep(interval)

# Close files
input_file.close()
output_file.close()