dsdanielpark / Gemini-API

The unofficial python package that returns response of Google Gemini through cookie values.
https://pypi.org/project/python-gemini-api/
MIT License
160 stars 14 forks source link

Still not usable #22

Closed johnd0e closed 5 months ago

johnd0e commented 6 months ago
  1. GeminiClient = Gemini(cookie_fp="cookies.json") For this case instructions recommends ExportThisCookies, but does not mentioned that downloaded with it cookies.txt is not real json: it is necessary to trim cookies = to make it valid json. But even with this I have no success:

    ``` Error loading cookie file: 'Gemini' object has no attribute 'session' Traceback (most recent call last): File "D:\AI\Gemini-API\test2.py", line 11, in GeminiClient = Gemini(cookie_fp="cookies.json") # Or use cookie file path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI\Gemini-API\gemini\core.py", line 66, in __init__ self.session = session or self._initialize_session() ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI\Gemini-API\gemini\core.py", line 94, in _initialize_session self._set_sid_and_nonce() File "D:\AI\Gemini-API\gemini\core.py", line 213, in _set_sid_and_nonce self._nonce = re.search(r'"SNlM0e":"(.*?)"', response.text).group(1) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'group' ```
  2. GeminiClient = Gemini(cookies=cookies) (where cookies are __Secure-1PSID, __Secure-1PSIDTS) I get an output, but it is not very helpful:

    ``` {'metadata': ['c_409da1520e77ed5f', 'r_f3670ae69c52a4e1'], 'candidates': {'rcid': 'rc_594f1013ce897401', 'text': 'http://googleusercontent.com/card_content/0', 'web_images': [], 'generated_images': []}} ```
  3. GeminiClient = Gemini(auto_cookies=True) does not work:

    ``` Traceback (most recent call last): File "D:\AI\Gemini-API\test.py", line 3, in GeminiClient = Gemini(auto_cookies=True) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI\Gemini-API\gemini\core.py", line 66, in __init__ self.session = session or self._initialize_session() ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI\Gemini-API\gemini\core.py", line 92, in _initialize_session self._set_cookies_automatically() File "D:\AI\Gemini-API\gemini\core.py", line 141, in _set_cookies_automatically if len(getattr(self, "cookies", {})) > 5: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: object of type 'NoneType' has no len() ```

Originally posted by @johnd0e in https://github.com/dsdanielpark/Gemini-API/issues/2#issuecomment-1981722593

The issue is still relevant for v2.2.0

dsdanielpark commented 6 months ago

Thank you. I'm aware of this issue and others, needing to isolate errors and refactor, but detailed revisions are currently delayed due to the hiring process and other projects.

The most likely successful approach in your current situation is method 2, try rerunning the generate_content method without redeclaring the Gemini object. The first object of generate_content might be empty.

  1. AttributeError: 'NoneType' object has no attribute 'group'

    • v2.3.0 will support clear error message.
    • The issue likely stems from invalid or expired cookies. Once exported cookie valuse, google expire and change cookie values very quickly.
  2. GeminiClient = Gemini(cookies=cookies) # where cookies are __Secure-1PSID, __Secure-1PSIDTS

    • Refer to issue #14
    • Try rerunning the generate_content method without redeclaring the Gemini object.
  3. Auto cookies

    • Auto-cookie update is not properly implemented yet; similar to point 1, cookies are received and then updated.
    • The package development was delayed because I aimed to automatically collect cookies from the browser perfectly from the start, ensuring error-free and comfortable use, but ultimately failed, causing a delay in the initial release.

I will try to make time to address this issue as soon as possible.

Also, I have marked # To-Do note in the source code to seek other contributors.

We are waiting for many contributors. Additionally, I do not expect frequent updates or added features for the Gemini API, as there is an official Gemini API, unlike with Bard API.

dsdanielpark commented 6 months ago

johnd0e

Gemini API == 2.4.2

pipi install python-gemini-api==2.4.2
pip install -q -U python-gemini-api

I've updated it to version 2.4.2. For cookies, users can now input them appropriately in dictionary format or contribute by creating them under misc.

johnd0e commented 6 months ago

@dsdanielpark

Both auto_cookie and cookies failed: ValueError: Failed to parse SNlM0e nonce value from the response

If I make fresh login in incognito mode, then it does not fail, but response is not useful - #14

dsdanielpark commented 6 months ago

Please make sure you're using version 2.4.2 or later and manually pass the complete cookie (avoid using auto_cookie).

Please test this script with the complete set of cookies collected manually: https://github.com/dsdanielpark/Gemini-API/blob/main/scripts/sample.ipynb

dsdanielpark commented 5 months ago

Most of error fixed. If error persists, please reopen issue.

johnd0e commented 5 months ago

Sorry, still does not work, neither with auto_cookies, nor with manually set cookies.

And I have no problem using exactly same cookies set with another project - https://github.com/HanaokaYuzu/Gemini-API