GPTRouter 0.4.1__tar.gz → 0.4.3__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: GPTRouter
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: A Python package for working with GPTRouter APIs
5
5
  Author-email: sirjanpreet@writesonic.com
6
6
  Classifier: Development Status :: 3 - Alpha
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: GPTRouter
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: A Python package for working with GPTRouter APIs
5
5
  Author-email: sirjanpreet@writesonic.com
6
6
  Classifier: Development Status :: 3 - Alpha
@@ -15,7 +15,7 @@ from gpt_router.models import (
15
15
  ChunkedGenerationResponse,
16
16
  ModelRouterEmbeddingsGenerationRequest,
17
17
  ModelRouterGenerationResponse,
18
- ModelRouterEmbeddingsGenerationResponse
18
+ ModelRouterEmbeddingsGenerationResponse,
19
19
  )
20
20
  from gpt_router.exceptions import (
21
21
  GPTRouterApiTimeoutError,
@@ -77,9 +77,7 @@ class GPTRouterClient:
77
77
  async def agenerate_embeddings(
78
78
  self,
79
79
  *,
80
- ordered_generation_requests: List[
81
- ModelRouterEmbeddingsGenerationRequest
82
- ],
80
+ ordered_generation_requests: List[ModelRouterEmbeddingsGenerationRequest],
83
81
  model_router_metadata: Optional[GPTRouterMetadata] = None,
84
82
  ) -> ModelRouterGenerationResponse:
85
83
  api_payload = {
@@ -184,6 +182,9 @@ class GPTRouterClient:
184
182
  async def astream_events(self, *, path: str, method: str, payload: dict, **kwargs):
185
183
  retry_count = kwargs.get("stream_retry_count") or 1
186
184
  stop_stream_signal = kwargs.get("stop_stream_signal")
185
+ switch_models_order_on_timeout = (
186
+ kwargs.get("switch_models_order_on_timeout") or False
187
+ )
187
188
  async_retrying = AsyncRetrying(
188
189
  stop=stop_after_attempt(retry_count),
189
190
  wait=wait_fixed(0.5),
@@ -252,6 +253,10 @@ class GPTRouterClient:
252
253
  if attempt_number == retry_count:
253
254
  logger.error("All retry attempts failed")
254
255
  raise
256
+ elif switch_models_order_on_timeout:
257
+ if attempt_number % 2:
258
+ self._reorder_models_on_timeout(payload)
259
+ logger.info(f"Retrying as per attempt #{attempt_number}")
255
260
  except httpx.HTTPStatusError as err:
256
261
  raise Exception(
257
262
  f"HTTP Error {err.response.status_code}: {err.response.text}"
@@ -267,6 +272,9 @@ class GPTRouterClient:
267
272
  self, *, path: str, method: str, payload: dict, **kwargs
268
273
  ) -> Generator[ChunkedGenerationResponse]:
269
274
  retry_count = kwargs.get("stream_retry_count") or 1
275
+ switch_models_order_on_timeout = (
276
+ kwargs.get("switch_models_order_on_timeout") or False
277
+ )
270
278
  retrying = Retrying(
271
279
  stop=stop_after_attempt(retry_count),
272
280
  wait=wait_fixed(0.5),
@@ -331,6 +339,10 @@ class GPTRouterClient:
331
339
  if attempt_number == retry_count:
332
340
  logger.error("All retry attempts failed")
333
341
  raise
342
+ elif switch_models_order_on_timeout:
343
+ if attempt_number % 2:
344
+ self._reorder_models_on_timeout(payload)
345
+ logger.info(f"Retrying as per attempt #{attempt_number}")
334
346
  except httpx.HTTPStatusError as err:
335
347
  raise Exception(
336
348
  f"HTTP Error {err.response.status_code}: {err.response.text}"
@@ -338,6 +350,30 @@ class GPTRouterClient:
338
350
  except httpx.TimeoutException as err:
339
351
  raise TimeoutError("Request timed out") from err
340
352
 
353
+ def _reorder_models_on_timeout(self, payload: dict) -> None:
354
+ """Helper method to reorder models in payload when timeout occurs."""
355
+ if not payload:
356
+ payload = {}
357
+ data = payload.get("data", [])
358
+ try:
359
+ if (
360
+ len(data) >= 2
361
+ and isinstance(data[0], dict)
362
+ and isinstance(data[1], dict)
363
+ ):
364
+ data_0_order = data[0].get("order", None)
365
+ data_1_order = data[1].get("order", None)
366
+ if data_1_order and data_0_order:
367
+ (
368
+ payload["data"][0]["order"],
369
+ payload["data"][1]["order"],
370
+ ) = (
371
+ payload["data"][1]["order"],
372
+ payload["data"][0]["order"],
373
+ )
374
+ except Exception as ex:
375
+ logger.exception(f"Error reordering models: {ex}")
376
+
341
377
  def generate(
342
378
  self,
343
379
  *,
@@ -430,7 +466,7 @@ class GPTRouterClient:
430
466
  def _extract_images_from_dict(self, image_dict: dict) -> List[dict]:
431
467
  if artifacts := image_dict.get("artifacts"):
432
468
  return artifacts
433
-
469
+
434
470
  if image_urls := image_dict.get("imageUrls"):
435
471
  return [{"url": url} for url in image_urls]
436
472
 
@@ -56,6 +56,7 @@ class ModelsEnum(Enum):
56
56
 
57
57
  OPENAI_EMBEDDINGS_ADA_002 = "text-embedding-ada-002"
58
58
  AZURE_OPENAI_EMBEDDINGS_ADA_002 = "text-embedding-ada-002"
59
+ OPENAI_EMBEDDINGS_TEXT_SMALL_3 = "text-embedding-3-small"
59
60
 
60
61
  class StreamingEventType(Enum):
61
62
  UPDATE = "update"
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setup(
7
7
  name="GPTRouter",
8
- version="0.4.1",
8
+ version="0.4.3",
9
9
  description="A Python package for working with GPTRouter APIs",
10
10
  long_description=long_description,
11
11
  long_description_content_type="text/markdown",
File without changes
File without changes
File without changes
File without changes