ccs-llmconnector 1.0.6__py3-none-any.whl → 1.1.0__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.
@@ -2,43 +2,54 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- import base64
6
- import mimetypes
7
- from pathlib import Path
8
- import logging
9
- from typing import Optional, Sequence, Union
10
- from urllib.error import URLError
11
- from urllib.request import urlopen
12
-
13
- from google import genai
14
- from google.genai import types
15
-
16
- ImageInput = Union[str, Path]
17
- logger = logging.getLogger(__name__)
5
+ import base64
6
+ import mimetypes
7
+ from pathlib import Path
8
+ import logging
9
+ from typing import Optional, Sequence
10
+ from urllib.request import urlopen
11
+
12
+ from google import genai
13
+ from google.genai import types
14
+
15
+ from .types import ImageInput, MessageSequence, normalize_messages
16
+ from .utils import clamp_retries, run_sync_in_thread, run_with_retries
17
+
18
+ logger = logging.getLogger(__name__)
18
19
 
19
20
 
20
21
  class GeminiClient:
21
22
  """Convenience wrapper around the Google Gemini SDK."""
22
23
 
23
- def generate_response(
24
- self,
25
- *,
26
- api_key: str,
27
- prompt: str,
28
- model: str,
29
- max_tokens: int = 32000,
30
- reasoning_effort: Optional[str] = None,
31
- images: Optional[Sequence[ImageInput]] = None,
32
- ) -> str:
24
+ def generate_response(
25
+ self,
26
+ *,
27
+ api_key: str,
28
+ prompt: Optional[str] = None,
29
+ model: str,
30
+ max_tokens: int = 32000,
31
+ reasoning_effort: Optional[str] = None,
32
+ images: Optional[Sequence[ImageInput]] = None,
33
+ messages: Optional[MessageSequence] = None,
34
+ request_id: Optional[str] = None,
35
+ timeout_s: Optional[float] = None,
36
+ max_retries: Optional[int] = None,
37
+ retry_backoff_s: float = 0.5,
38
+ ) -> str:
33
39
  """Generate a response from the specified Gemini model.
34
40
 
35
41
  Args:
36
42
  api_key: API key used to authenticate with the Gemini API.
37
- prompt: Natural-language instruction or query for the model.
38
- model: Identifier of the Gemini model to target (for example, ``"gemini-2.5-flash"``).
39
- max_tokens: Cap for tokens across the entire exchange, defaults to 32000.
40
- reasoning_effort: Included for API parity; currently unused by the Gemini SDK.
41
- images: Optional collection of image references (local paths, URLs, or data URLs).
43
+ prompt: Natural-language instruction or query for the model.
44
+ model: Identifier of the Gemini model to target (for example, ``"gemini-2.5-flash"``).
45
+ max_tokens: Cap for tokens across the entire exchange, defaults to 32000.
46
+ reasoning_effort: Included for API parity; currently unused by the Gemini SDK.
47
+ images: Optional collection of image references (local paths, URLs, or data URLs).
48
+ messages: Optional list of chat-style messages (role/content).
49
+ request_id: Optional request identifier for tracing/logging.
50
+ timeout_s: Optional request timeout in seconds.
51
+ max_retries: Optional retry count for transient failures.
52
+ retry_backoff_s: Base delay (seconds) for exponential backoff between retries.
42
53
 
43
54
  Returns:
44
55
  The text output produced by the model.
@@ -47,99 +58,179 @@ class GeminiClient:
47
58
  ValueError: If required arguments are missing or the request payload is empty.
48
59
  URLError: If an image URL cannot be retrieved.
49
60
  google.genai.errors.APIError: If the underlying Gemini request fails.
50
- """
51
- if not api_key:
52
- raise ValueError("api_key must be provided.")
53
- if not prompt and not images:
54
- raise ValueError("At least one of prompt or images must be provided.")
55
- if not model:
56
- raise ValueError("model must be provided.")
57
-
58
- parts: list[types.Part] = []
59
- if prompt:
60
- parts.append(types.Part.from_text(text=prompt))
61
-
62
- if images:
63
- for image in images:
64
- parts.append(self._to_image_part(image))
65
-
66
- if not parts:
67
- raise ValueError("No content provided for response generation.")
68
-
69
- content = types.Content(role="user", parts=parts)
70
-
71
- config = types.GenerateContentConfig(max_output_tokens=max_tokens)
72
- # reasoning_effort is accepted for compatibility but not currently applied because the
73
- # Gemini SDK does not expose an equivalent configuration parameter.
74
-
75
- client = genai.Client(api_key=api_key)
76
- try:
77
- try:
78
- response = client.models.generate_content(
79
- model=model,
80
- contents=[content],
81
- config=config,
82
- )
83
- except Exception as exc:
84
- logger.exception("Gemini generate_content failed: %s", exc)
85
- raise
86
- finally:
87
- closer = getattr(client, "close", None)
88
- if callable(closer):
89
- try:
90
- closer()
91
- except Exception:
92
- pass
93
-
94
- if response.text:
95
- result_text = response.text
96
- logger.info(
97
- "Gemini generate_content succeeded: model=%s images=%d text_len=%d",
98
- model,
99
- len(images or []),
100
- len(result_text or ""),
101
- )
102
- return result_text
103
-
104
- candidate_texts: list[str] = []
105
- for candidate in getattr(response, "candidates", []) or []:
106
- content_obj = getattr(candidate, "content", None)
107
- if not content_obj:
108
- continue
109
- for part in getattr(content_obj, "parts", []) or []:
110
- text = getattr(part, "text", None)
111
- if text:
112
- candidate_texts.append(text)
113
-
114
- if candidate_texts:
115
- result_text = "\n".join(candidate_texts)
116
- logger.info(
117
- "Gemini generate_content succeeded (candidates): model=%s images=%d text_len=%d",
118
- model,
119
- len(images or []),
120
- len(result_text or ""),
121
- )
122
- return result_text
123
-
124
- # Treat successful calls without textual content as a successful, empty response
125
- # rather than raising. This aligns with callers that handle empty outputs gracefully.
126
- logger.info(
127
- "Gemini generate_content succeeded with no text: model=%s images=%d",
128
- model,
129
- len(images or []),
130
- )
131
- return ""
132
-
133
- def generate_image(
134
- self,
135
- *,
136
- api_key: str,
137
- prompt: str,
138
- model: str,
139
- image_size: Optional[str] = None,
140
- aspect_ratio: Optional[str] = None,
141
- image: Optional[ImageInput] = None,
142
- ) -> bytes:
61
+ """
62
+ if not api_key:
63
+ raise ValueError("api_key must be provided.")
64
+ if not prompt and not messages and not images:
65
+ raise ValueError("At least one of prompt, messages, or images must be provided.")
66
+ if not model:
67
+ raise ValueError("model must be provided.")
68
+
69
+ normalized_messages = normalize_messages(prompt=prompt, messages=messages)
70
+ contents: list[types.Content] = []
71
+ for message in normalized_messages:
72
+ parts: list[types.Part] = []
73
+ if message["content"]:
74
+ parts.append(types.Part.from_text(text=message["content"]))
75
+ contents.append(types.Content(role=message["role"], parts=parts))
76
+
77
+ if images:
78
+ image_parts = [self._to_image_part(image) for image in images]
79
+ target_index = next(
80
+ (
81
+ index
82
+ for index in range(len(contents) - 1, -1, -1)
83
+ if contents[index].role == "user"
84
+ ),
85
+ None,
86
+ )
87
+ if target_index is None:
88
+ contents.append(types.Content(role="user", parts=image_parts))
89
+ else:
90
+ existing_parts = list(contents[target_index].parts or [])
91
+ existing_parts.extend(image_parts)
92
+ contents[target_index] = types.Content(
93
+ role="user", parts=existing_parts
94
+ )
95
+
96
+ if not contents or not any(content.parts for content in contents):
97
+ raise ValueError("No content provided for response generation.")
98
+
99
+ config = types.GenerateContentConfig(max_output_tokens=max_tokens)
100
+ # reasoning_effort is accepted for compatibility but not currently applied because the
101
+ # Gemini SDK does not expose an equivalent configuration parameter.
102
+
103
+ retry_count = clamp_retries(max_retries)
104
+
105
+ def _build_client() -> genai.Client:
106
+ client_kwargs: dict[str, object] = {"api_key": api_key}
107
+ if timeout_s is not None:
108
+ http_options = getattr(types, "HttpOptions", None)
109
+ if http_options is not None:
110
+ try:
111
+ client_kwargs["http_options"] = http_options(timeout=timeout_s)
112
+ except Exception:
113
+ logger.debug("Gemini HttpOptions timeout not applied.", exc_info=True)
114
+ return genai.Client(**client_kwargs)
115
+
116
+ def _run_request() -> str:
117
+ client = _build_client()
118
+ try:
119
+ try:
120
+ response = client.models.generate_content(
121
+ model=model,
122
+ contents=contents,
123
+ config=config,
124
+ )
125
+ except Exception as exc:
126
+ logger.exception(
127
+ "Gemini generate_content failed: %s request_id=%s",
128
+ exc,
129
+ request_id,
130
+ )
131
+ raise
132
+ finally:
133
+ closer = getattr(client, "close", None)
134
+ if callable(closer):
135
+ try:
136
+ closer()
137
+ except Exception:
138
+ pass
139
+
140
+ if response.text:
141
+ result_text = response.text
142
+ logger.info(
143
+ "Gemini generate_content succeeded: model=%s images=%d text_len=%d request_id=%s",
144
+ model,
145
+ len(images or []),
146
+ len(result_text or ""),
147
+ request_id,
148
+ )
149
+ return result_text
150
+
151
+ candidate_texts: list[str] = []
152
+ for candidate in getattr(response, "candidates", []) or []:
153
+ content_obj = getattr(candidate, "content", None)
154
+ if not content_obj:
155
+ continue
156
+ for part in getattr(content_obj, "parts", []) or []:
157
+ text = getattr(part, "text", None)
158
+ if text:
159
+ candidate_texts.append(text)
160
+
161
+ if candidate_texts:
162
+ result_text = "\n".join(candidate_texts)
163
+ logger.info(
164
+ "Gemini generate_content succeeded (candidates): model=%s images=%d text_len=%d request_id=%s",
165
+ model,
166
+ len(images or []),
167
+ len(result_text or ""),
168
+ request_id,
169
+ )
170
+ return result_text
171
+
172
+ # Treat successful calls without textual content as a successful, empty response
173
+ # rather than raising. This aligns with callers that handle empty outputs gracefully.
174
+ logger.info(
175
+ "Gemini generate_content succeeded with no text: model=%s images=%d request_id=%s",
176
+ model,
177
+ len(images or []),
178
+ request_id,
179
+ )
180
+ return ""
181
+
182
+ return run_with_retries(
183
+ func=_run_request,
184
+ max_retries=retry_count,
185
+ retry_backoff_s=retry_backoff_s,
186
+ request_id=request_id,
187
+ )
188
+
189
+ async def async_generate_response(
190
+ self,
191
+ *,
192
+ api_key: str,
193
+ prompt: Optional[str] = None,
194
+ model: str,
195
+ max_tokens: int = 32000,
196
+ reasoning_effort: Optional[str] = None,
197
+ images: Optional[Sequence[ImageInput]] = None,
198
+ messages: Optional[MessageSequence] = None,
199
+ request_id: Optional[str] = None,
200
+ timeout_s: Optional[float] = None,
201
+ max_retries: Optional[int] = None,
202
+ retry_backoff_s: float = 0.5,
203
+ ) -> str:
204
+ return await run_sync_in_thread(
205
+ lambda: self.generate_response(
206
+ api_key=api_key,
207
+ prompt=prompt,
208
+ model=model,
209
+ max_tokens=max_tokens,
210
+ reasoning_effort=reasoning_effort,
211
+ images=images,
212
+ messages=messages,
213
+ request_id=request_id,
214
+ timeout_s=timeout_s,
215
+ max_retries=max_retries,
216
+ retry_backoff_s=retry_backoff_s,
217
+ )
218
+ )
219
+
220
+ def generate_image(
221
+ self,
222
+ *,
223
+ api_key: str,
224
+ prompt: str,
225
+ model: str,
226
+ image_size: Optional[str] = None,
227
+ aspect_ratio: Optional[str] = None,
228
+ image: Optional[ImageInput] = None,
229
+ request_id: Optional[str] = None,
230
+ timeout_s: Optional[float] = None,
231
+ max_retries: Optional[int] = None,
232
+ retry_backoff_s: float = 0.5,
233
+ ) -> bytes:
143
234
  """Generate an image using Gemini 3 Pro Image.
144
235
 
145
236
  Args:
@@ -164,86 +255,195 @@ class GeminiClient:
164
255
  if not model:
165
256
  raise ValueError("model must be provided.")
166
257
 
167
- client = genai.Client(api_key=api_key)
168
-
169
- config = types.GenerateContentConfig(
170
- tools=[{"google_search": {}}],
171
- image_config=types.ImageConfig(
172
- image_size=image_size or "2K",
173
- aspect_ratio=aspect_ratio,
174
- )
175
- )
258
+ config = types.GenerateContentConfig(
259
+ tools=[{"google_search": {}}],
260
+ image_config=types.ImageConfig(
261
+ image_size=image_size or "2K",
262
+ aspect_ratio=aspect_ratio,
263
+ ),
264
+ )
176
265
 
177
266
  contents = [prompt]
178
267
  if image:
179
268
  contents.append(self._to_image_part(image))
180
269
 
181
- try:
182
- try:
183
- response = client.models.generate_content(
184
- model=model,
185
- contents=contents,
186
- config=config,
187
- )
188
- except Exception as exc:
189
- logger.exception("Gemini generate_image failed: %s", exc)
190
- raise
191
- finally:
192
- closer = getattr(client, "close", None)
193
- if callable(closer):
194
- try:
195
- closer()
196
- except Exception:
197
- pass
198
-
199
- if not response.parts:
200
- raise ValueError("No content returned from Gemini.")
201
-
202
- for part in response.parts:
203
- if part.inline_data:
204
- return part.inline_data.data
205
-
206
- raise ValueError("No image data found in response.")
207
-
208
- def list_models(self, *, api_key: str) -> list[dict[str, Optional[str]]]:
209
- """Return the models available to the authenticated Gemini account."""
210
- if not api_key:
211
- raise ValueError("api_key must be provided.")
212
-
213
- models: list[dict[str, Optional[str]]] = []
214
- client = genai.Client(api_key=api_key)
215
- try:
216
- try:
217
- iterator = client.models.list()
218
- except Exception as exc:
219
- logger.exception("Gemini list models failed: %s", exc)
220
- raise
221
- for model in iterator:
222
- model_id = getattr(model, "name", None)
223
- if model_id is None and isinstance(model, dict):
224
- model_id = model.get("name")
225
- if not model_id:
226
- continue
227
-
228
- # Normalize IDs like "models/<id>" -> "<id>"
229
- if isinstance(model_id, str) and model_id.startswith("models/"):
230
- model_id = model_id.split("/", 1)[1]
231
-
232
- display_name = getattr(model, "display_name", None)
233
- if display_name is None and isinstance(model, dict):
234
- display_name = model.get("display_name")
235
-
236
- models.append({"id": model_id, "display_name": display_name})
237
- finally:
238
- closer = getattr(client, "close", None)
239
- if callable(closer):
240
- try:
241
- closer()
242
- except Exception:
243
- pass
244
-
245
- logger.info("Gemini list_models succeeded: count=%d", len(models))
246
- return models
270
+ retry_count = clamp_retries(max_retries)
271
+
272
+ def _build_client() -> genai.Client:
273
+ client_kwargs: dict[str, object] = {"api_key": api_key}
274
+ if timeout_s is not None:
275
+ http_options = getattr(types, "HttpOptions", None)
276
+ if http_options is not None:
277
+ try:
278
+ client_kwargs["http_options"] = http_options(timeout=timeout_s)
279
+ except Exception:
280
+ logger.debug("Gemini HttpOptions timeout not applied.", exc_info=True)
281
+ return genai.Client(**client_kwargs)
282
+
283
+ def _run_request() -> bytes:
284
+ client = _build_client()
285
+ try:
286
+ try:
287
+ response = client.models.generate_content(
288
+ model=model,
289
+ contents=contents,
290
+ config=config,
291
+ )
292
+ except Exception as exc:
293
+ logger.exception(
294
+ "Gemini generate_image failed: %s request_id=%s",
295
+ exc,
296
+ request_id,
297
+ )
298
+ raise
299
+ finally:
300
+ closer = getattr(client, "close", None)
301
+ if callable(closer):
302
+ try:
303
+ closer()
304
+ except Exception:
305
+ pass
306
+
307
+ if not response.parts:
308
+ raise ValueError("No content returned from Gemini.")
309
+
310
+ for part in response.parts:
311
+ if part.inline_data:
312
+ return part.inline_data.data
313
+
314
+ raise ValueError("No image data found in response.")
315
+
316
+ return run_with_retries(
317
+ func=_run_request,
318
+ max_retries=retry_count,
319
+ retry_backoff_s=retry_backoff_s,
320
+ request_id=request_id,
321
+ )
322
+
323
+ async def async_generate_image(
324
+ self,
325
+ *,
326
+ api_key: str,
327
+ prompt: str,
328
+ model: str,
329
+ image_size: Optional[str] = None,
330
+ aspect_ratio: Optional[str] = None,
331
+ image: Optional[ImageInput] = None,
332
+ request_id: Optional[str] = None,
333
+ timeout_s: Optional[float] = None,
334
+ max_retries: Optional[int] = None,
335
+ retry_backoff_s: float = 0.5,
336
+ ) -> bytes:
337
+ return await run_sync_in_thread(
338
+ lambda: self.generate_image(
339
+ api_key=api_key,
340
+ prompt=prompt,
341
+ model=model,
342
+ image_size=image_size,
343
+ aspect_ratio=aspect_ratio,
344
+ image=image,
345
+ request_id=request_id,
346
+ timeout_s=timeout_s,
347
+ max_retries=max_retries,
348
+ retry_backoff_s=retry_backoff_s,
349
+ )
350
+ )
351
+
352
+ def list_models(
353
+ self,
354
+ *,
355
+ api_key: str,
356
+ request_id: Optional[str] = None,
357
+ timeout_s: Optional[float] = None,
358
+ max_retries: Optional[int] = None,
359
+ retry_backoff_s: float = 0.5,
360
+ ) -> list[dict[str, Optional[str]]]:
361
+ """Return the models available to the authenticated Gemini account."""
362
+ if not api_key:
363
+ raise ValueError("api_key must be provided.")
364
+
365
+ retry_count = clamp_retries(max_retries)
366
+
367
+ def _build_client() -> genai.Client:
368
+ client_kwargs: dict[str, object] = {"api_key": api_key}
369
+ if timeout_s is not None:
370
+ http_options = getattr(types, "HttpOptions", None)
371
+ if http_options is not None:
372
+ try:
373
+ client_kwargs["http_options"] = http_options(timeout=timeout_s)
374
+ except Exception:
375
+ logger.debug("Gemini HttpOptions timeout not applied.", exc_info=True)
376
+ return genai.Client(**client_kwargs)
377
+
378
+ def _run_request() -> list[dict[str, Optional[str]]]:
379
+ models: list[dict[str, Optional[str]]] = []
380
+ client = _build_client()
381
+ try:
382
+ try:
383
+ iterator = client.models.list()
384
+ except Exception as exc:
385
+ logger.exception(
386
+ "Gemini list models failed: %s request_id=%s",
387
+ exc,
388
+ request_id,
389
+ )
390
+ raise
391
+ for model in iterator:
392
+ model_id = getattr(model, "name", None)
393
+ if model_id is None and isinstance(model, dict):
394
+ model_id = model.get("name")
395
+ if not model_id:
396
+ continue
397
+
398
+ # Normalize IDs like "models/<id>" -> "<id>"
399
+ if isinstance(model_id, str) and model_id.startswith("models/"):
400
+ model_id = model_id.split("/", 1)[1]
401
+
402
+ display_name = getattr(model, "display_name", None)
403
+ if display_name is None and isinstance(model, dict):
404
+ display_name = model.get("display_name")
405
+
406
+ models.append({"id": model_id, "display_name": display_name})
407
+ finally:
408
+ closer = getattr(client, "close", None)
409
+ if callable(closer):
410
+ try:
411
+ closer()
412
+ except Exception:
413
+ pass
414
+
415
+ logger.info(
416
+ "Gemini list_models succeeded: count=%d request_id=%s",
417
+ len(models),
418
+ request_id,
419
+ )
420
+ return models
421
+
422
+ return run_with_retries(
423
+ func=_run_request,
424
+ max_retries=retry_count,
425
+ retry_backoff_s=retry_backoff_s,
426
+ request_id=request_id,
427
+ )
428
+
429
+ async def async_list_models(
430
+ self,
431
+ *,
432
+ api_key: str,
433
+ request_id: Optional[str] = None,
434
+ timeout_s: Optional[float] = None,
435
+ max_retries: Optional[int] = None,
436
+ retry_backoff_s: float = 0.5,
437
+ ) -> list[dict[str, Optional[str]]]:
438
+ return await run_sync_in_thread(
439
+ lambda: self.list_models(
440
+ api_key=api_key,
441
+ request_id=request_id,
442
+ timeout_s=timeout_s,
443
+ max_retries=max_retries,
444
+ retry_backoff_s=retry_backoff_s,
445
+ )
446
+ )
247
447
 
248
448
  @staticmethod
249
449
  def _to_image_part(image: ImageInput) -> types.Part: