Closed johnd0e closed 6 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
.
I will check this issue and see all code again. Thank you!
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.
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.
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.
I have just finished but QA process will be needed.
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)
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!
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.
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.
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
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)
[NOTICE] Please, go to Gemini-API https://github.com/dsdanielpark/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.
[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.
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
Describe the bug Unable to use
token_from_browser
inChatBard
Version Bard API: 0.1.39
Code
Error
The issue is here: https://github.com/dsdanielpark/Bard-API/blob/9345c2c12cf11346f3a1ace2a24c130331fb3de5/bardapi/chat.py#L52-L53
Instead it should be: