gemini-webapi 1.7.0__py3-none-any.whl → 1.8.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gemini_webapi/client.py CHANGED
@@ -8,7 +8,7 @@ from typing import Any, Optional
8
8
 
9
9
  from httpx import AsyncClient, ReadTimeout
10
10
 
11
- from .constants import Endpoint, Headers
11
+ from .constants import Endpoint, Headers, Model
12
12
  from .exceptions import AuthError, APIError, TimeoutError, GeminiError
13
13
  from .types import WebImage, GeneratedImage, Candidate, ModelOutput
14
14
  from .utils import (
@@ -79,6 +79,9 @@ class GeminiClient:
79
79
  __Secure-1PSIDTS cookie value, some google accounts don't require this value, provide only if it's in the cookie list.
80
80
  proxy: `str`, optional
81
81
  Proxy URL.
82
+ kwargs: `dict`, optional
83
+ Additional arguments which will be passed to the http client.
84
+ Refer to `httpx.AsyncClient` for more information.
82
85
 
83
86
  Raises
84
87
  ------
@@ -98,6 +101,7 @@ class GeminiClient:
98
101
  "close_task",
99
102
  "auto_refresh",
100
103
  "refresh_interval",
104
+ "kwargs",
101
105
  ]
102
106
 
103
107
  def __init__(
@@ -105,6 +109,7 @@ class GeminiClient:
105
109
  secure_1psid: str | None = None,
106
110
  secure_1psidts: str | None = None,
107
111
  proxy: str | None = None,
112
+ **kwargs,
108
113
  ):
109
114
  self.cookies = {}
110
115
  self.proxy = proxy
@@ -117,6 +122,7 @@ class GeminiClient:
117
122
  self.close_task: Task | None = None
118
123
  self.auto_refresh: bool = True
119
124
  self.refresh_interval: float = 540
125
+ self.kwargs = kwargs
120
126
 
121
127
  # Validate cookies
122
128
  if secure_1psid:
@@ -173,6 +179,7 @@ class GeminiClient:
173
179
  follow_redirects=True,
174
180
  headers=Headers.GEMINI.value,
175
181
  cookies=valid_cookies,
182
+ **self.kwargs,
176
183
  )
177
184
  self.access_token = access_token
178
185
  self.cookies = valid_cookies
@@ -256,7 +263,9 @@ class GeminiClient:
256
263
  self,
257
264
  prompt: str,
258
265
  images: list[bytes | str | Path] | None = None,
266
+ model: Model | str = Model.UNSPECIFIED,
259
267
  chat: Optional["ChatSession"] = None,
268
+ **kwargs,
260
269
  ) -> ModelOutput:
261
270
  """
262
271
  Generates contents with prompt.
@@ -267,8 +276,14 @@ class GeminiClient:
267
276
  Prompt provided by user.
268
277
  images: `list[bytes | str | Path]`, optional
269
278
  List of image file paths or file data in bytes.
279
+ model: `Model` | `str`, optional
280
+ Specify the model to use for generation.
281
+ Pass either a `gemini_webapi.constants.Model` enum or a model name string.
270
282
  chat: `ChatSession`, optional
271
283
  Chat data to retrieve conversation history. If None, will automatically generate a new chat id when sending post request.
284
+ kwargs: `dict`, optional
285
+ Additional arguments which will be passed to the post request.
286
+ Refer to `httpx.AsyncClient.request` for more information.
272
287
 
273
288
  Returns
274
289
  -------
@@ -291,12 +306,16 @@ class GeminiClient:
291
306
 
292
307
  assert prompt, "Prompt cannot be empty."
293
308
 
309
+ if not isinstance(model, Model):
310
+ model = Model.from_name(model)
311
+
294
312
  if self.auto_close:
295
313
  await self.reset_close_task()
296
314
 
297
315
  try:
298
316
  response = await self.client.post(
299
317
  Endpoint.GENERATE.value,
318
+ headers=model.model_header,
300
319
  data={
301
320
  "at": self.access_token,
302
321
  "f.req": json.dumps(
@@ -311,12 +330,8 @@ class GeminiClient:
311
330
  None,
312
331
  [
313
332
  [
314
- [
315
- await upload_file(
316
- image, self.proxy
317
- ),
318
- 1,
319
- ]
333
+ [await upload_file(image, self.proxy)],
334
+ "filename.jpg",
320
335
  ]
321
336
  for image in images
322
337
  ],
@@ -329,6 +344,7 @@ class GeminiClient:
329
344
  ]
330
345
  ),
331
346
  },
347
+ **kwargs,
332
348
  )
333
349
  except ReadTimeout:
334
350
  raise TimeoutError(
@@ -435,12 +451,13 @@ class GeminiClient:
435
451
  Parameters
436
452
  ----------
437
453
  kwargs: `dict`, optional
438
- Other arguments to pass to `ChatSession.__init__`.
454
+ Additional arguments which will be passed to the chat session.
455
+ Refer to `gemini_webapi.ChatSession` for more information.
439
456
 
440
457
  Returns
441
458
  -------
442
459
  :class:`ChatSession`
443
- Empty chat object for retrieving conversation history.
460
+ Empty chat session object for retrieving conversation history.
444
461
  """
445
462
 
446
463
  return ChatSession(geminiclient=self, **kwargs)
@@ -462,9 +479,17 @@ class ChatSession:
462
479
  Reply id, if provided together with metadata, will override the second value in it.
463
480
  rcid: `str`, optional
464
481
  Reply candidate id, if provided together with metadata, will override the third value in it.
482
+ model: `Model` | `str`, optional
483
+ Specify the model to use for generation.
484
+ Pass either a `gemini_webapi.constants.Model` enum or a model name string.
465
485
  """
466
486
 
467
- __slots__ = ["__metadata", "geminiclient", "last_output"]
487
+ __slots__ = [
488
+ "__metadata",
489
+ "geminiclient",
490
+ "last_output",
491
+ "model",
492
+ ]
468
493
 
469
494
  def __init__(
470
495
  self,
@@ -473,10 +498,12 @@ class ChatSession:
473
498
  cid: str | None = None, # chat id
474
499
  rid: str | None = None, # reply id
475
500
  rcid: str | None = None, # reply candidate id
501
+ model: Model | str = Model.UNSPECIFIED,
476
502
  ):
477
503
  self.__metadata: list[str | None] = [None, None, None]
478
504
  self.geminiclient: GeminiClient = geminiclient
479
505
  self.last_output: ModelOutput | None = None
506
+ self.model = model
480
507
 
481
508
  if metadata:
482
509
  self.metadata = metadata
@@ -503,6 +530,7 @@ class ChatSession:
503
530
  self,
504
531
  prompt: str,
505
532
  images: list[bytes | str | Path] | None = None,
533
+ **kwargs,
506
534
  ) -> ModelOutput:
507
535
  """
508
536
  Generates contents with prompt.
@@ -514,6 +542,9 @@ class ChatSession:
514
542
  Prompt provided by user.
515
543
  images: `list[bytes | str | Path]`, optional
516
544
  List of image file paths or file data in bytes.
545
+ kwargs: `dict`, optional
546
+ Additional arguments which will be passed to the post request.
547
+ Refer to `httpx.AsyncClient.request` for more information.
517
548
 
518
549
  Returns
519
550
  -------
@@ -535,7 +566,7 @@ class ChatSession:
535
566
  """
536
567
 
537
568
  return await self.geminiclient.generate_content(
538
- prompt=prompt, images=images, chat=self
569
+ prompt=prompt, images=images, model=self.model, chat=self, **kwargs
539
570
  )
540
571
 
541
572
  def choose_candidate(self, index: int) -> ModelOutput:
@@ -21,3 +21,36 @@ class Headers(Enum):
21
21
  "Content-Type": "application/json",
22
22
  }
23
23
  UPLOAD = {"Push-ID": "feeds/mcudyrk2a4khkz"}
24
+
25
+
26
+ class Model(Enum):
27
+ UNSPECIFIED = ("unspecified", {}, False)
28
+ G_1_5_FLASH = (
29
+ "gemini-1.5-flash",
30
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"7daceb7ef88130f5"]'},
31
+ False,
32
+ )
33
+ G_2_0_FLASH_EXP = (
34
+ "gemini-2.0-flash-exp",
35
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"948b866104ccf484"]'},
36
+ False,
37
+ )
38
+ G_2_0_EXP_ADVANCED = (
39
+ "gemini-2.0-exp-advanced",
40
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"b1e46a6037e6aa9f"]'},
41
+ True,
42
+ )
43
+
44
+ def __init__(self, name, header, advanced_only):
45
+ self.model_name = name
46
+ self.model_header = header
47
+ self.advanced_only = advanced_only
48
+
49
+ @classmethod
50
+ def from_name(cls, name: str):
51
+ for model in cls:
52
+ if model.model_name == name:
53
+ return model
54
+ raise ValueError(
55
+ f"Unknown model name: {name}. Available models: {', '.join([model.model_name for model in cls])}"
56
+ )
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: gemini-webapi
3
- Version: 1.7.0
3
+ Version: 1.8.1
4
4
  Summary: ✨ An elegant async Python wrapper for Google Gemini web app
5
5
  Author: UZQueen
6
6
  License: GNU AFFERO GENERAL PUBLIC LICENSE
@@ -732,6 +732,7 @@ A reverse-engineered asynchronous python wrapper for [Google Gemini](https://gem
732
732
  - [Retrieve images in response](#retrieve-images-in-response)
733
733
  - [Generate images with ImageFx](#generate-images-with-imagefx)
734
734
  - [Save images to local files](#save-images-to-local-files)
735
+ - [Specify language model version](#specify-language-model-version)
735
736
  - [Generate contents with Gemini extensions](#generate-contents-with-gemini-extensions)
736
737
  - [Check and switch to other reply candidates](#check-and-switch-to-other-reply-candidates)
737
738
  - [Control log level](#control-log-level)
@@ -774,9 +775,9 @@ pip install -U browser-cookie3
774
775
 
775
776
  ```yaml
776
777
  services:
777
- main:
778
- volumes:
779
- - ./gemini_cookies:/usr/local/lib/python3.12/site-packages/gemini_webapi/utils/temp
778
+ main:
779
+ volumes:
780
+ - ./gemini_cookies:/usr/local/lib/python3.12/site-packages/gemini_webapi/utils/temp
780
781
  ```
781
782
 
782
783
  > [!NOTE]
@@ -937,6 +938,34 @@ async def main():
937
938
  asyncio.run(main())
938
939
  ```
939
940
 
941
+ ### Specify language model version
942
+
943
+ You can choose a specified language model version by passing `model` argument to `GeminiClient.generate_content` or `GeminiClient.start_chat`. The default value is `unspecified`.
944
+
945
+ Currently available models (as of Dec 21, 2024):
946
+
947
+ - `unspecified` - Default model (Gemini 1.5 Flash)
948
+ - `gemini-1.5-flash` - Gemini 1.5 Flash
949
+ - `gemini-2.0-flash-exp` - Gemini 2.0 Flash Experimental
950
+ - `gemini-2.0-exp-advanced` - Gemini 2.0 Experimental Advanced (requires Gemini Advanced account)
951
+
952
+ ```python
953
+ from gemini_webapi.constants import Model
954
+
955
+ async def main():
956
+ response1 = await client.generate_content(
957
+ "What's you language model version? Reply version number only.",
958
+ model="gemini-1.5-flash",
959
+ )
960
+ print(f"Model version (gemini-1.5-flash): {response1.text}")
961
+
962
+ chat = client.start_chat(model=Model.G_2_0_FLASH_EXP)
963
+ response2 = await chat.send_message("What's you language model version? Reply version number only.")
964
+ print(f"Model version ({Model.G_2_0_FLASH_EXP.model_name}): {response2.text}")
965
+
966
+ asyncio.run(main())
967
+ ```
968
+
940
969
  ### Generate contents with Gemini extensions
941
970
 
942
971
  > [!IMPORTANT]
@@ -1,6 +1,6 @@
1
1
  gemini_webapi/__init__.py,sha256=28uNIywK4vCXxENaSagNWUzhqr1RyNtLzDF6WRRM4KQ,194
2
- gemini_webapi/client.py,sha256=A_unTPUCSSLch0zojvTG4PrS7Hvpxgr7mnIXG-uW_kM,20922
3
- gemini_webapi/constants.py,sha256=ezJ0Squ5FPTspAp5IGRe5gg9CYaPHnzx5N95W5gGm5s,865
2
+ gemini_webapi/client.py,sha256=SZEihRytBcBNa8ZJllEU3xFRB1cX8ZrEY_3TRjDdN4g,22169
3
+ gemini_webapi/constants.py,sha256=43tV363_lv6J-idZ48n0_G-A4GZSwIW0gJ60GibNMa0,1866
4
4
  gemini_webapi/exceptions.py,sha256=6e-EXHGApi4iC0GDw7RKc3YqVK8UvEkHYaJyGQbReLw,548
5
5
  gemini_webapi/types/__init__.py,sha256=d2kvXnE004s2E2KDmPPLi5N-BQ59FgDSlrGrO3Wphww,163
6
6
  gemini_webapi/types/candidate.py,sha256=Z9bpIK4l8UWbUVMLEoophfhgROo93dxOM9cAwx77CkU,1030
@@ -12,8 +12,8 @@ gemini_webapi/utils/load_browser_cookies.py,sha256=A5n_VsB7Rm8ck5lpy856UNJEhv30l
12
12
  gemini_webapi/utils/logger.py,sha256=PF4ROQq7scRRrWzeYdeYiYs2S2Jqr0bgjyrPbXVOCqE,816
13
13
  gemini_webapi/utils/rotate_1psidts.py,sha256=cwCXsE_mX2knaKCuquZT2jsRXHQo-GK2Ldom-cO9Gio,1668
14
14
  gemini_webapi/utils/upload_file.py,sha256=X0k-3jW7usheFo5z0nXL9mUt-XF-2L2OgwpJnPSIcKw,1151
15
- gemini_webapi-1.7.0.dist-info/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
16
- gemini_webapi-1.7.0.dist-info/METADATA,sha256=XhfeQVH9p_5SYZ1t12mVhrhbIJMCw9Ucqhmzq0PvKI0,55729
17
- gemini_webapi-1.7.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
18
- gemini_webapi-1.7.0.dist-info/top_level.txt,sha256=dtWtug_ZrmnUqCYuu8NmGzTgWglHeNzhHU_hXmqZGWE,14
19
- gemini_webapi-1.7.0.dist-info/RECORD,,
15
+ gemini_webapi-1.8.1.dist-info/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
16
+ gemini_webapi-1.8.1.dist-info/METADATA,sha256=cV33fx9JrgMZJQjiIe5sLUZYkobSIyzW5pmK8mCRBuw,56893
17
+ gemini_webapi-1.8.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
18
+ gemini_webapi-1.8.1.dist-info/top_level.txt,sha256=dtWtug_ZrmnUqCYuu8NmGzTgWglHeNzhHU_hXmqZGWE,14
19
+ gemini_webapi-1.8.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.6.0)
2
+ Generator: setuptools (75.8.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5