Package: bardapi
Development Status :: 7 - InactiveNoticeTransition toGemini APIIn February 2024, Bard has changed its service name to Gemini.For some countries/regions where the__Secure-1PSIDcookie value ends with a single dot:Bard API>= 0.1.40For all other countries/regions: Starting from March 1st, 2024, please go to theGemini APIpackage.Moving forward, updates will primarily focus on the Gemini API package.Google - Bard APIThe python package that returns response ofGoogleBardGeminithrough value of cookie.Please exercise caution and use this package responsibly. This python package isUNOFFICIAL.I referred to this github repository(github.com/acheong08/Bard) where inference process of Bard was reverse engineered. Using__Secure-1PSID, you can ask questions and get answers from Google Bard. Please note that the bardapi is not a free service, but rather a tool provided to assist developers with testing certain functionalities due to the delayed development and release of Google Bard's API. It has been designed with a lightweight structure that can easily adapt to the emergence of an official API. Therefore, I strongly discourage using it for any other purposes. If you have access to reliable officialPaLM-2 APIorGoogle Generative AI API, replace the provided response with the corresponding official code. Check outhttps://github.com/dsdanielpark/Bard-API/issues/262.Google Bard APIWhat is Google Bard?InstallAuthenticationUsageFurtherBehind a proxyUse rotating proxiesReusable session objectAuto Cookie BardBardask_about_imagemethodText To Speech(TTS) from BardMore featuresAmazing Bard Prompts Is All You Need!The Python package hf-transllmWhat is GoogleBardGemini?Google PaLMFAQWhat is GoogleBard?Bard is a conversational generative artificial intelligence chatbot developed by Google, based initially on the LaMDA family of LLMs(Large Language Models) and later the PaLM LLM. Please check official documents forupdateson Bard, includingavailable regions and languages.Install$ pip install bardapi$ pip install git+https://github.com/dsdanielpark/Bard-API.gitDue to certain dependency packages that are not compatible with 64bit windows(OS), we are releasing a lightweight alpha release of bard that only returns responses for simple requests. This release is a continuation of the pypi0.1.18version, which was maintained with lightweight and simple functionality. Seealpha-release github branchfor more details.$ pip install bardapi==0.1.23aAuthenticationWarningDo not expose the__Secure-1PSID. For testing purposes only; avoid direct application use. Cookie values change periodically (every 15-20 minutes). Frequent session changes may briefly block access; headless mode is challenging. Rate limiting applies and changes often. If the cookie changes, log out of your Google account, close the browser, and enter the new cookie value. Or manually reset the cookie for a new value. See FAQ and issue pages for details.Visithttps://gemini.google.com/F12 for consoleSession: Application → Cookies → Copy the value of__Secure-1PSIDcookie.Note that while I referred to__Secure-1PSIDvalue as an API key for convenience, it is not an officially provided API key.
Cookie value subject to frequent changes. Verify the value again if an error occurs. Most errors occur when an invalid cookie value is entered.If you need to set multiple cookie values:Multi-cookie Bard- After confirming that multiple cookie values are required to receive responses reliably in certain countries, I will deploy it for testing purposes. Please debug and create a pull request.UsageSimple UsagefrombardapiimportBardtoken='xxxxxxx'bard=Bard(token=token)bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']Or you can use thisfrombardapiimportBardimportosos.environ['_BARD_API_KEY']="xxxxxxx"Bard().get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']To get reponse dictionaryimportbardapi# set your __Secure-1PSID value to keytoken='xxxxxxx'# set your input textinput_text="나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘"# Send an API request and get a response.response=bardapi.core.Bard(token).get_answer(input_text)Addressing errors caused by delayed responses in environments like Google Colab and containers. If an error occurs despite following the proper procedure, utilize the timeout argument.frombardapiimportBardimportosos.environ['_BARD_API_KEY']="xxxxxxx"bard=Bard(timeout=30)# Set timeout in secondsbard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']FurtherBehind a proxyIf you are working behind a proxy, use the following.frombardapiimportBard# Change 'http://proxy.example.com:8080' to your http proxy# timeout in secondsproxies={'http':'http://proxy.example.com:8080','https':'https://proxy.example.com:8080'}bard=Bard(token='xxxxxxx',proxies=proxies,timeout=30)bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']Use rotating proxiesIf you want toavoid blocked requestsand bans, then useSmart Proxy by Crawlbase. It forwards your connection requests to arandomly rotating IP addressin a pool of proxies before reaching the target website. The combination of AI and ML make it more effective toavoid CAPTCHAs and blocks.frombardapiimportBardimportrequests# Get your proxy url at crawlbase https://crawlbase.com/docs/smart-proxy/get/proxy_url="http://xxxxxxxxxxxxxx:@smartproxy.crawlbase.com:8012"proxies={"http":proxy_url,"https":proxy_url}bard=Bard(token='xxxxxxx',proxies=proxies,timeout=30)bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']Reusable session objectYou can continue the conversation using a reusable session. However, this feature is limited, and it is difficult for a package-level feature to perfectly maintain conversation_id and context. You can try to maintain the consistency of conversations same way as other LLM services, such as passing some sort of summary of past conversations to the DB.frombardapiimportBardimportrequests# import os# os.environ['_BARD_API_KEY'] = 'xxxxxxx'token='xxxxxxx'session=requests.Session()session.headers={"Host":"gemini.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://gemini.google.com","Referer":"https://gemini.google.com/",}# session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))session.cookies.set("__Secure-1PSID",token)bard=Bard(token=token,session=session,timeout=30)bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']# Continued conversation without set new sessionbard.get_answer("What is my last prompt??")['content']Async Bard Code (Click to expand)fromhttpximportAsyncClientfrombardapiimportBardAsyncimportos# Uncomment and set your API key as needed# os.environ['_BARD_API_KEY'] = 'xxxxxxx'token='xxxxxxx'# Replace with your actual tokenSESSION_HEADERS={"Host":"gemini.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://gemini.google.com","Referer":"https://gemini.google.com/",}timeout=30# Example timeoutproxies={}# Replace with your proxies if neededclient=AsyncClient(http2=True,headers=SESSION_HEADERS,cookies={"__Secure-1PSID":token},timeout=timeout,proxies=proxies,)bard_async=BardAsync(token=token,client=client)# Asynchronous function to get the answerasyncdefget_bard_answer(question):awaitbard_async.async_setup()# Ensure async setup is donereturnawaitbard_async.get_answer(question)response=awaitget_bard_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")print(response['content'])Auto Cookie BardUsingbrowser_cookie3we extract the__Secure-1PSIDcookie from all browsers, and then we can use the API without passing the token. However, there are still incomplete dependency packages and various variables, so please seek assistance in the followingGitHub Issuesor adjust your browser's version.Visithttps://gemini.google.com/in your browser and execute the following command while in the chat-enabled state. Refer to browser_cookie3 for details on how it works. If any issues arise, please restart the browser or log in to your Google account again.Recommended to keep the browser open.frombardapiimportBardbard=Bard(token_from_browser=True)response=bard.get_answer("Do you like cookies?")print(response['content'])Bardask_about_imagemethodIt may not work as it is only available for certain accounts, regions, and other restrictions.As an experimental feature, it is possible to ask questions with an image. However, this functionality is only available for accounts with image upload capability in Bard's web UI.frombardapiimportBardbard=Bard(token='xxxxxxx')image=open('image.jpg','rb').read()# (jpeg, png, webp) are supported.bard_answer=bard.ask_about_image('What is in the image?',image)print(bard_answer['content'])Text To Speech(TTS)from BardBusiness users and high traffic volume may be subject to account restrictions according to Google's policies. Please use theOfficial Google Cloud APIfor any other purpose.
The user is solely responsible for all code, and it is imperative to consult Google's official services and policies. Furthermore, the code in this repository is provided under the MIT license, and it disclaims any liability, including explicit or implied legal responsibilities.frombardapiimportBardbard=Bard(token='xxxxxxx')audio=bard.speech('Hello, I am Bard! How can I help you today?')withopen("speech.ogg","wb")asf:f.write(bytes(audio['audio']))More featuresStarting from version0.1.18, the GitHub version of BardAPI will be synchronized with the PyPI version and released simultaneously. However, the version undergoing QA can still be used from the GitHub repository.$ pip install git+https://github.com/dsdanielpark/Bard-API.gitMulti-cookie BardAuto Cookie BardTTS from BardMulti-language Bard APIGet image linksChatBardExport ConversationExport Code to Repl.itExecuting Python code received as a response from BardUsing Bard AsynchronouslyBard CookiesFix Coversation ID (Fix Context)Max_token, Max_sentencesTranslation to another programming languageAmazing Bard PromptsIs All You Need!Helpful prompts for Google BardThe Python package hf-transllmIf you want to comfortably use the open-source LLM models in your native language, which arereleased under the Apache License (allowing free commercial use)invarious languages, you can try using thehf-transllmpackage. hf-transllm also supports multilingual inference for various LLMs stored in hugging face repository.Example code ofhf-transllmIn case the Google package is no longer available due to policy restrictions, here's a simple example code for using open-source language models (LLMs) in both English and multiple languages.UsageFor the decoder models provided by Hugging Face, you can easily use them by either following a simple approach or overriding the inference method. You can explore various open-source language models atthis link. Through the ranking information fromOpen LLM Leader Board Report repository, you can find information about good models.For LLM that use languagesother than EnglishfromtransllmimportLLMtranslatoropen_llama3b_kor=LLMtranslator('openlm-research/open_llama_3b',target_lang='ko',translator='google')# Koreantrnaslated_answer=open_llama3b_kor.generate("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")print(trnaslated_answer)For LLM that useEnglishReferhttps://github.com/openlm-research/open_llamaor using like this:fromtransllmimportLLMtranslatoropen_llama3b=LLMtranslator('openlm-research/open_llama_3b)answer=open_llama3b.generate("Tell me about the Korean girl group Newjeans.")print(answer)What is GoogleGemini?Geminior formerly knowns as Bard is an advanced, multimodal AI model byGoogle DeepMind, capable of understanding and integrating various information types like text, code, audio, images, and video.Paper:https://storage.googleapis.com/deepmind-media/gemini/gemini_1_report.pdfWeb:https://blog.google/technology/ai/google-gemini-ai/#capabilitiesCode Guide:https://ai.google.dev/tutorials/python_quickstartOfficial API OnGoogle AI Studio.Google AI StudioGoogle AI Studio creates a new Google Cloud project for each new API key. You also can create an API key in an existing Google Cloud project. All projects are subject to theGoogle Cloud Platform Terms of Service.Web:https://makersuite.google.com/app/apikeyNote: The Gemini API is currently in public preview. Production applications are not supported yet.Access to Gemini Pro in Bard API packageThe Bard API, sourcing responses fromGoogleBardGemini's official website, allows you to receive the same responses as the website. So, if Gemini answers are available on the web, you can also accessed Gemini through the Bard API. However, it's important to note that responses might also come from other models, not exclusively Gemini Pro or Ultra.There is no official Bard API or early access/waiting list for Gemini, although thePaLM2 APIis available.Google's PaLM2 API differs from Bard, with some aspects of Bard being superior.It's speculated that after expert review, Bard Advanced lineup will likely provide an official API in 2024.Gemini and previous generative AI model responses are provided randomly on Bard Web.The Bard API, with its imperfect extension features(e.g,ask_about_image), occasionally demonstrates Gemini's capabilities. This behavior may vary by region, language, or Google account.More information in theFAQ.For more on Gemini:Official APIIntroducing Gemini: our largest and most capable AI modelHow it's made: multimodal promptingYouTube DemoGoogle PaLMTry demo athttps://makersuite.google.com/app/prompts/new_text.who are you?
>> I am powered by PaLM 2, which stands for Pathways Language Model 2, a large language model from Google AI.Google Generative AIOfficial Page:https://blog.google/technology/ai/google-palm-2-ai-large-language-model/GitHub:https://github.com/GoogleCloudPlatform/generative-aiTry Demo:https://makersuite.google.com/app/prompts/new_text.Official Library:https://makersuite.google.com/app/libraryGet API Key:https://makersuite.google.com/app/apikeyQuick Start Tutorial:https://developers.generativeai.google/tutorials/text_quickstartQuick Start$ pip install -q google-generativeaiimportpprintimportgoogle.generativeaiaspalmpalm.configure(api_key='YOUR_API_KEY')models=[mforminpalm.list_models()if'generateText'inm.supported_generation_methods]model=models[0].nameprint(model)prompt="Who are you?"completion=palm.generate_text(model=model,prompt=prompt,temperature=0,# The maximum length of the responsemax_output_tokens=800,)print(completion.result)SponsorUse data scraping to train your AI models.Easy to useAPI to crawl and scrapemillions of websitesUse crawlbase for efficientdata extractionfor yourLLMsAveragesuccess rate: 98%Uptime guarantee:99.9%Simple docsto get started in minutesAsynchronousCrawlingAPIif you need massive amounts of dataGDPRandCCPAcompliantUsed by70k+developers.FAQPlease check the FAQ and open issues for similar questions before creating a new issue. Repeated questions will be kept as open issues. Too many requests can trigger a temporary account block (HTTP 429). Maintain proper intervals, using functions like sleep to avoid rate limits. Policies may vary by country and language, so all users could face temporary or permanent errors via the API.ScriptsIn the scriptsfolder, I have released a script to help you compareOpenAI-ChatGPT,Microsoft-EdgeGPTandGoogle-Bard. I hope they will help more developers.ContributorsI would like to express my sincere gratitude to all the contributors.LicenseMITWe hold no legal responsibility; for more information, please refer to the bottom of the readme file. We just want you to give me andthema star.The MIT License (MIT)

Copyright (c) 2023 Minwoo Park

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.Shifting Service Policies: Bard and Google's DynamicsBard's service status and Google's API interfaces are in constant flux.The number of replies is currently limited, but certain users,such as those utilizing VPNs or proxy servers, have reported slightly higher message caps. Adaptability is crucial in navigating these dynamic service policies. Please note that the cookie values used in this package are not official API values.Bugs and IssuesSincerely grateful for any reports on new features or bugs. Your valuable feedback on the code is highly appreciated.ContactsCore maintainer:Daniel Park, South KoreaE-mail:parkminwoo1991@gmail.comReference[1]https://github.com/acheong08/BardWarningImportant Notice
The user assumes all legal responsibilities associated with using the BardAPI package. This Python package merely facilitates easy access to Google Bard for developers. Users are solely responsible for managing data and using the package appropriately. For further information, please consult the Google Bard Official Document.WarningCaution
This Python package is not an official Google package or API service. It is not affiliated with Google and uses Google account cookies, which means that excessive or commercial usage may result in restrictions on your Google account. The package was created to support developers in testing functionalities due to delays in the official Google package. However, it should not be misused or abused. Please be cautious and refer to the Readme for more information.Copyright (c) 2023 MinWoo Park, South Korea