Package: bard-py
Bard.pyPython Client for Bard, a Chat Based AI tool by Google.[!NOTE]
This is anunofficialclient.FeaturesConnect 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.RequirementsPython 3.10 or newerGoogle account with access toBardInstallationTo install Bard.py, run the following command:pipinstallbard-pyor, if you usepoetry:poetryaddbard-py[!TIP]
Make sure you're using the latest version of Bard.py to ensure best compatibility with Bard.UsagePrerequisitesTo use Bard.py you first need to extract the__Secure-1PSIDand__Secure-1PSIDTScookies 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 theBard web page.Write a message on the chat dialog that appears.Open the developer tools in your browser (usually by pressingF12or right-clicking on the chat dialog and selectingInspect).Select theApplicationtab and click on theCookiesoption to view all cookies associated withhttps://bard.google.com.Look for the__Secure-1PSIDand__Secure-1PSIDTScookies 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:exportSECURE_1PSID=<your-cookie>exportSECURE_1PSIDTS=<your-other-cookie>or, in your Python code:os.environ["SECURE_1PSID"]="<your-cookie>"os.environ["SECURE_1PSIDTS"]="<your-other-cookie>"ExampleYou can use Bard.py to easily create a CLI client for Bard:importasynciofrombardimportBardClientasyncdefmain()->None:asyncwithBardClient()asbard:whileTrue:prompt=input("You: ")ifprompt=="!reset":awaitbard.reset_conversation()continueelifprompt=="!exit":breakresponse=awaitbard.ask(prompt)print(f"Bard:{response}")if__name__=="__main__":asyncio.run(main())Bard ClientYou can create a Bard Client and initialize a connection with Bard which starts a conversation:bard=BardClient()awaitbard.start_conversation()# Conversationawaitbard.end_conversation()Alternatively, you can use theasync withstatement to keep the code compact:asyncwithBardClient()asbard:# ConversationReset ConversationYou can reset the conversation in order to make the client forget the previous conversation:asyncwithBardClient()asbard:# Conversationawaitbard.reset_conversation()AskYou can ask Bard questions and get the results:asyncwithBardClient()asbard:response=awaitbard.ask("When was Bard released?")print(response)ToneYou can set the tone when having a conversation with Bard:asyncwithBardClient()asbard:_=awaitbard.ask("When was Bard released?")response=awaitbard.ask("When was Bard released?",tone="Professional")print(response)The available options for thetoneparameter are:ProfessionalCasual[!NOTE]
It is recommended to use thetoneparameter on subsequent prompts and not in the first one. This is because this feature is typically used to change the previous response, rather than define the entire conversation tone.LengthYou can set the response length when having a conversation with Bard:asyncwithBardClient()asbard:_=awaitbard.ask("When was Bard released?")response=awaitbard.ask("When was Bard released?",length="Short")print(response)The available options for thelengthparameter are:ShortLong[!NOTE]
It is recommended to use thelengthparameter on subsequent prompts and not in the first one. This is because this feature is typically used to change the previous response, rather than define the entire conversation length.ExceptionsWhen something goes wrong, Sydney.py might throw one of the following exceptions:ExceptionMeaningSolutionCreateConversationExceptionFailed to create conversationRetry or use new cookiesAskExceptionFailed to get response from BardRetry or use new cookiesNoResponseExceptionReceived an empty response from BardWait and retryFor more detailed documentation and options, please refer to the code docstrings.LicenseThis project is licensed under the MIT License - see theLICENSEfile for details.