DEV Community

Cover image for Terminal Chat with Bard using bardAPI and Python
John Pinto
John Pinto

Posted on • Updated on

Terminal Chat with Bard using bardAPI and Python

So I took a look at BardAPI on Github and built a simple terminal chat using python

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

My Github : https://github.com/pj8912/terminal_chat

  • Get the key as per the instruction on BARDAPI
  • Create a .env on the root folder and assign that value BARD_API_KEY variable

Code

import os
from dotenv import load_dotenv
from bardapi import Bard
load_dotenv()
os.environ['_BARD_API_KEY'] = os.getenv("BARD_API_KEY")
bard = Bard()
print('\n','*'*50,'Terminal Chat with BARD','*'*50, '\n')
try:
    while True:
        user_input = input('\U0001F464 You: ')
        print('')
        print('\U0001F916 Bard:', bard.get_answer(user_input.strip())['content'])
        print('-'*100, "\n")
except KeyboardInterrupt:
    print('Ended Chat!')
except:
    print('Ended Chat!')
Enter fullscreen mode Exit fullscreen mode

As per the documentation the os.environ['_BARD_API_KEY'] is assigned the your BARD_API_KEY


Run

Run the script to start chatting with Bard

$ python app.py
Enter fullscreen mode Exit fullscreen mode

Image

ENJOY!

Top comments (0)