dsdanielpark / Bard-API

The unofficial python package that returns response of Google Bard through cookie value.
https://pypi.org/project/bardapi/
MIT License
5.34k stars 528 forks source link

ChatBard: token_from_browser #265

Closed johnd0e closed 6 months ago

johnd0e commented 8 months ago

Describe the bug Unable to use token_from_browser in ChatBard

Version Bard API: 0.1.39

Code

from bardapi import ChatBard
chat = ChatBard(token_from_browser=True)
chat.start()

Error

Enter the Bard API Key(__Secure-1PSID):
Bard API(__Secure-1PSID) Key must be entered.

The issue is here: https://github.com/dsdanielpark/Bard-API/blob/9345c2c12cf11346f3a1ace2a24c130331fb3de5/bardapi/chat.py#L52-L53

Instead it should be:

        self.token_from_browser = token_from_browser
        self.token = None if token_from_browser else token or os.getenv("_BARD_API_KEY") or self._get_api_key()
johnd0e commented 8 months ago

Another issue is here: https://github.com/dsdanielpark/Bard-API/blob/9345c2c12cf11346f3a1ace2a24c130331fb3de5/bardapi/core.py#L110

cookies=False means that only __Secure-1PSID is used. But that does not work for my case, I need all 3 cookies. And there is no way to achieve with ChatBard.

dsdanielpark commented 8 months ago

I will check this issue and see all code again. Thank you!

dsdanielpark commented 8 months ago

As you know, this package was initially very simple, anticipating its obsolescence with the launch of the official Bard API.

However, as Bard expanded to offer various services, the structure became more complex, and diverse features were added in response to developers' requests. Consequently, I couldn't neatly organize all use cases within a single Bard structure, and although Async seemed of limited utility, it was updated as needed.

For Multi Cookie Bard, a new structure, BardCookies, inheriting from Bard, was placed in a new Python file.

In ChatBard, implementing multicookies in Bard via token_from_browser in core.py called for further modifications, so I've made the following changes:

https://github.com/dsdanielpark/Bard-API/blob/main/bardapi/chat.py#L209

ChatBard has been modified to inherit from BardCookies, which in turn has been adjusted to fetch three cookie values via token_from_browser.

After reinstalling from the GitHub version (after a clean uninstallation),

$ pip install git+https://github.com/dsdanielpark/Bard-API.git

please try it out and let us know if any errors occur. As I only need a single cookie, I couldn't debug this directly.

In progress

If there are no errors, I will update to version 0.1.40 along with the documentation.

Thank you for reporting.

johnd0e commented 8 months ago
from bardapi import ChatBard
chat = ChatBard(token_from_browser=True)
chat.start()
Traceback (most recent call last):
  File "D:\AI\Bard-API\main1.py", line 1, in <module>
    from bardapi import ChatBard
  File "D:\AI\Bard-API\bardapi\__init__.py", line 5, in <module>
    from bardapi.chat import ChatBard, ChatBardCookise
  File "D:\AI\Bard-API\bardapi\chat.py", line 7, in <module>
    from bardapi import BardCookies
ImportError: cannot import name 'BardCookies' from partially initialized module 'bardapi' (most likely due to a circular import) (D:\AI\Bard-API\bardapi\__init__.py)

Note Cookise. Perhaps typo, but it is not the cause of the error: after my correction nothing changed.

```diff diff --git a/bardapi/__init__.py b/bardapi/__init__.py index 6595aa8..877fbab 100644 --- a/bardapi/__init__.py +++ b/bardapi/__init__.py @@ -2,7 +2,7 @@ from os import environ from bardapi.core import Bard -from bardapi.chat import ChatBard, ChatBardCookise +from bardapi.chat import ChatBard, ChatBardCookies from bardapi.core_async import BardAsync from bardapi.core_cookies import BardCookies, BardAsyncCookies from bardapi.constants import ( @@ -28,7 +28,7 @@ bard_api_key = environ.get("_BARD_API_KEY") __all__ = [ "Bard", "ChatBard", - "ChatBardCookise", + "ChatBardCookies", "BardAsync", "BardCookies", "BardAsyncCookies", diff --git a/bardapi/chat.py b/bardapi/chat.py index b7697f0..ff46fb7 100644 --- a/bardapi/chat.py +++ b/bardapi/chat.py @@ -207,18 +207,18 @@ class ChatBard(Bard): self.chat_history.append({"User": user_input, "Chatbot": chatbot_response}) -class ChatBardCookise(BardCookies): +class ChatBardCookies(BardCookies): """ A class representing a chatbot powered by the Bard API. Usage: - chat = ChatBardCookise() + chat = ChatBardCookies() chat.start() Example: - from bardapi import ChatBardCookise + from bardapi import ChatBardCookies - chat = ChatBardCookise() + chat = ChatBardCookies() chat.start() """ ```
dsdanielpark commented 8 months ago

I am currently working on implementing multi-cookies to the Bard object due to the issue with multiple inheritance.

Additionally, I will also be conducting some overall code refactoring. This change will be updated in version 0.1.40 and documented to accommodate the inclusion of multi-cookies within the Bard object.

dsdanielpark commented 7 months ago

I have just finished but QA process will be needed.

johnd0e commented 7 months ago

In https://github.com/dsdanielpark/Bard-API/blob/main/documents/README_DEV.md#multi-cookie-bard described 3 ways, but there are some errors/mistypes in description.

So here what I have exactly tried: (only 1 case is successful)

BardCookies + cookie_dict

from bardapi import BardCookies

cookie_dict = {
    # Any cookie values you want to pass session object.
}

bard = BardCookies(cookie_dict=cookie_dict)
print(bard.get_answer("こんにちは")['content'])

This works!

BardCookies + session

import requests
from bardapi import BardCookies, SESSION_HEADERS

session = requests.Session()
#session.cookies.set("__Secure-1PSID", 
#...
session.headers = SESSION_HEADERS

bard = BardCookies(session=session)
bard.get_answer("How is the weather today in seoul?")
The BardCookies class is no longer in use.
Please use the Bard class with the 'cookie_dict' and 'multi_cookies_bool' arguments in the Bard constructor.
Traceback (most recent call last):
  File "D:\AI\Bard-API\main02.py", line 11, in <module>
    bard = BardCookies(session=session)
  File "D:\AI\Bard-API\bardapi\core_cookies.py", line 50, in __init__
    self.cookie_dict = cookie_dict or self._get_token(token_from_browser)
  File "D:\AI\Bard-API\bardapi\core_cookies.py", line 81, in _get_token
    raise Exception(
Exception: Bard API Key must be provided as token argument or extracted from browser.

Bard + session

import requests
from bardapi import Bard, SESSION_HEADERS

session = requests.Session()
#session.cookies.set("__Secure-1PSID", 
#...
session.headers = SESSION_HEADERS

bard = Bard(session=session)
bard.get_answer("How is the weather today in seoul?")
Traceback (most recent call last):
  File "D:\AI\Bard-API\main021.py", line 11, in <module>
    bard = Bard(session=session, multi_cookies_bool=True)
  File "D:\AI\Bard-API\bardapi\core.py", line 74, in __init__
    self.token = self._get_token(token, token_from_browser, multi_cookies_bool)
  File "D:\AI\Bard-API\bardapi\core.py", line 136, in _get_token
    raise Exception(
Exception: Bard API Key must be provided as the 'token' argument or extracted from the browser.

Bard +token_from_browser + multi_cookies_bool

from bardapi import Bard

bard = Bard(token_from_browser=True, multi_cookies_bool=True)
bard.get_answer("How is the weather today in seoul?")
Traceback (most recent call last):
  File "D:\AI\Bard-API\main03.py", line 3, in <module>
    bard = Bard(token_from_browser=True, multi_cookies_bool=True)
  File "D:\AI\Bard-API\bardapi\core.py", line 82, in __init__
    self.SNlM0e = self._get_snim0e()
  File "D:\AI\Bard-API\bardapi\core.py", line 181, in _get_snim0e
    raise Exception(
Exception: Response status code is not 200. Response Status is 404
aniruddhkrish commented 7 months ago

I used this code after updating the core.py, core.cookies, constant.py, for the new Gemini formerly Bard. Refer to the issue created by me "Google renames its chatbot Bard to Gemini"

I have provided the solution here using browser_cookie3 for extracting the values of cookies each time when the program gets executed. I have only tested for firefox. So you may install firefox, and sign in Gemini through your google account credentials, and run this code.

from bardapi import SESSION_HEADERS, BardCookies
import browser_cookie3
import requests
domain = '.google.com'

firefox_cookies = browser_cookie3.firefox(domain_name=domain)

psid = '__Secure-1PSID'
dts = '__Secure-1PSIDTS'
dcc = '__Secure-1PSIDCC'
nid = 'NID'

psid_value = None
dts_value = None
dcc_value = None
nid_value = None

for cookie in firefox_cookies:
    if cookie.name == psid:
        psid_value = cookie.value
    elif cookie.name == dts:
        dts_value = cookie.value
    elif cookie.name == dcc:
        dcc_value = cookie.value
    elif cookie.name == nid:
        nid_value = cookie.value

cookie_dict = {
    "__Secure-1PSID": psid_value,
    "__Secure-1PSIDTS": dts_value,
    "__Secure-1PSIDCC": dcc_value,
}
session = requests.Session()
session.cookies.set("__Secure-1PSID", psid_value)
session.cookies.set( "__Secure-1PSIDCC", dcc_value)
session.cookies.set("__Secure-1PSIDTS", dts_value)
session.cookies.set("NID",nid_value)
session.headers = SESSION_HEADERS

bard = BardCookies(session=session,cookie_dict=cookie_dict)

while True:

    question = input("Enter your prompt to Gemini: ")
    response = bard.get_answer(question)['content']
    data = str(response).replace("*", "")
    print(data)
dsdanielpark commented 6 months ago

[NOTICE] Please, go to Gemini-API https://github.com/dsdanielpark/Gemini-API

PyPI

Gemini Icon Google - Gemini API

A unofficial Python wrapper, python-gemini-api, operates through reverse-engineering, utilizing cookie values to interact with Google Gemini for users struggling with frequent authentication problems or unable to authenticate via Google Authentication.

Collaborated competently with Antonio Cheong.

What is Gemini?

[Paper] [Official Website] [Official API] [API Documents]

Gemini is a family of generative AI models developed by Google DeepMind that is designed for multimodal use cases. The Gemini API gives you access to the Gemini Pro and Gemini Pro Vision models. In February 2024, Google's Bard service was changed to Gemini.


Installation

pip install python-gemini-api
pip install git+https://github.com/dsdanielpark/Gemini-API.git

For the updated version, use as follows:

pip install -q -U python-gemini-api