Skip to content

Commit

Permalink
feat: v1.0.0 release
Browse files Browse the repository at this point in the history
- feat: add support to auto refresh cookies in background
- feat: add support to import cookies from local browser
- feat: add support to control log level
- feat: now client will automatically retry when generate_content raises APIError
- fix: now the timeout value will be correctly applied after re-initializing the client
- docs: update readme and function docstrings
- refactor: split utils.py into multiple files
- build: update supported python version

close #6
  • Loading branch information
HanaokaYuzu committed Mar 19, 2024
1 parent 0922a14 commit 8f4d469
Show file tree
Hide file tree
Showing 15 changed files with 527 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,4 @@ Temporary Items
.apdisk

# Temporary files
/temp
temp/
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A reverse-engineered asynchronous python wrapper for [Google Gemini](https://gem

## Features

- **(WIP) Auto Cookie Management**
- **Smart Cookies** - Automatically import cookies and refresh them in background. Free up your hands!
- **ImageFx Support** - Supports retrieving images generated by ImageFx, Google's latest AI image generator.
- **Extension Support** - Supports generating contents with [Gemini extensions](https://gemini.google.com/extensions) on, like YouTube and Gmail.
- **Classified Outputs** - Auto categorizes texts, web images and AI generated images from the response.
Expand All @@ -51,6 +51,7 @@ A reverse-engineered asynchronous python wrapper for [Google Gemini](https://gem
- [Save images to local files](#save-images-to-local-files)
- [Generate contents with Gemini extensions](#generate-contents-with-gemini-extensions)
- [Check and switch to other reply candidates](#check-and-switch-to-other-reply-candidates)
- [Control log level](#control-log-level)
- [References](#references)
- [Stargazers](#stargazers)

Expand All @@ -60,35 +61,45 @@ A reverse-engineered asynchronous python wrapper for [Google Gemini](https://gem
pip install gemini_webapi
```

Optionally, package offers a way to automatically import cookies from your local browser. To enable this feature, install `browser-cookie3` as well. Supported platforms and browsers can be found [here](https://github.com/borisbabic/browser_cookie3?tab=readme-ov-file#contribute).

```bash
pip install browser-cookie3
```

## Authentication

> [!NOTE]
>
> If `browser-cookie3` is installed, you can skip this step and go directly to [usage](#usage) section. Just make sure you have logged in to <https://gemini.google.com> in your browser.
- Go to <https://gemini.google.com> and login with your Google account
- Press F12 for web inspector, go to `Network` tab and refresh the page
- Click any request and copy cookie values of `__Secure-1PSID` and `__Secure-1PSIDTS`

> [!TIP]
>
> `__Secure-1PSIDTS` could get expired frequently if <https://gemini.google.com> is kept opened in the browser after copying cookies. It's recommended to get cookies from a separate session (e.g. a new login in browser's private mode) if you are building a keep-alive service with this package.
>
> For more details, please refer to discussions in [issue #6](https://github.com/HanaokaYuzu/Gemini-API/issues/6)
> API's auto cookie refresh feature may cause that you need to re-login to your Google account in the browser. This is an expected behavior and won't affect the API's functionality. To avoid such result, it's recommended to get cookies from a separate browser session for best utilization (e.g. a fresh login in browser's private mode), or set `auto_refresh` to `False` in `GeminiClient.init` to disable this feature.
## Usage

### Initialization

Import required packages and initialize a client with your cookies obtained from the previous step.
Import required packages and initialize a client with your cookies obtained from the previous step. After a successful initialization, the API will automatically refresh `__Secure-1PSIDTS` in background as long as the process is alive.

```python
import asyncio
from gemini_webapi import GeminiClient

# Replace "COOKIE VALUE HERE" with your actual cookie values
# Replace "COOKIE VALUE HERE" with your actual cookie values.
# Leave Secure_1PSIDTS empty if it's not available for your account.
Secure_1PSID = "COOKIE VALUE HERE"
Secure_1PSIDTS = "COOKIE VALUE HERE"

async def main():
# If browser-cookie3 is installed, simply use `client = GeminiClient()`
client = GeminiClient(Secure_1PSID, Secure_1PSIDTS, proxies=None)
await client.init(timeout=30, auto_close=False, close_delay=300)
await client.init(timeout=30, auto_close=False, close_delay=300, auto_refresh=True)

asyncio.run(main())
```
Expand Down Expand Up @@ -250,6 +261,16 @@ async def main():
asyncio.run(main())
```

### Control log level

You can set the log level of the package to one of the following values: `DEBUG`, `INFO`, `WARNING`, `ERROR` and `CRITICAL`. The default value is `INFO`.

```python
from gemini_webapi import set_log_level

set_log_level("DEBUG")
```

## References

[Google AI Studio](https://ai.google.dev/tutorials/ai-studio_quickstart)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
requires-python = ">=3.7"
requires-python = ">=3.8"
dependencies = [
"httpx>=0.25.2",
"pydantic>=2.5.3",
Expand Down
1 change: 1 addition & 0 deletions src/gemini_webapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .client import GeminiClient, ChatSession # noqa: F401
from .exceptions import * # noqa: F401, F403
from .types import * # noqa: F401, F403
from .utils import set_log_level # noqa: F401
Loading

0 comments on commit 8f4d469

Please sign in to comment.