Spaces:
Paused
Paused
Mirrowel commited on
Commit ·
2786fc8
1
Parent(s): 576a3ec
feat: optimize retry logic
Browse files- src/rotator_library/client.py +34 -33
src/rotator_library/client.py
CHANGED
|
@@ -99,43 +99,44 @@ class RotatingClient:
|
|
| 99 |
)
|
| 100 |
tried_keys.add(current_key)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
for attempt in range(self.max_retries):
|
| 103 |
try:
|
| 104 |
lib_logger.info(f"Attempting call with key ...{current_key[-4:]} (Attempt {attempt + 1}/{self.max_retries})")
|
| 105 |
|
| 106 |
-
litellm_kwargs = self.all_providers.get_provider_kwargs(**kwargs.copy())
|
| 107 |
-
|
| 108 |
-
if provider in self._provider_instances:
|
| 109 |
-
provider_instance = self._provider_instances[provider]
|
| 110 |
-
|
| 111 |
-
# Ensure safety_settings are present, defaulting to lowest if not provided
|
| 112 |
-
if "safety_settings" not in litellm_kwargs:
|
| 113 |
-
litellm_kwargs["safety_settings"] = {
|
| 114 |
-
"harassment": "BLOCK_NONE",
|
| 115 |
-
"hate_speech": "BLOCK_NONE",
|
| 116 |
-
"sexually_explicit": "BLOCK_NONE",
|
| 117 |
-
"dangerous_content": "BLOCK_NONE",
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
converted_settings = provider_instance.convert_safety_settings(litellm_kwargs["safety_settings"])
|
| 121 |
-
|
| 122 |
-
if converted_settings is not None:
|
| 123 |
-
litellm_kwargs["safety_settings"] = converted_settings
|
| 124 |
-
else:
|
| 125 |
-
# If conversion returns None, remove it to avoid sending empty settings
|
| 126 |
-
del litellm_kwargs["safety_settings"]
|
| 127 |
-
|
| 128 |
-
if "gemma-3" in model and "messages" in litellm_kwargs:
|
| 129 |
-
new_messages = [
|
| 130 |
-
{"role": "user", "content": m["content"]} if m.get("role") == "system" else m
|
| 131 |
-
for m in litellm_kwargs["messages"]
|
| 132 |
-
]
|
| 133 |
-
litellm_kwargs["messages"] = new_messages
|
| 134 |
-
|
| 135 |
-
if provider == "chutes":
|
| 136 |
-
litellm_kwargs["model"] = f"openai/{model.split('/', 1)[1]}"
|
| 137 |
-
litellm_kwargs["api_base"] = "https://llm.chutes.ai/v1"
|
| 138 |
-
|
| 139 |
if pre_request_callback:
|
| 140 |
await pre_request_callback()
|
| 141 |
|
|
|
|
| 99 |
)
|
| 100 |
tried_keys.add(current_key)
|
| 101 |
|
| 102 |
+
# Prepare litellm_kwargs once per key, not on every retry
|
| 103 |
+
litellm_kwargs = self.all_providers.get_provider_kwargs(**kwargs.copy())
|
| 104 |
+
|
| 105 |
+
if provider in self._provider_instances:
|
| 106 |
+
provider_instance = self._provider_instances[provider]
|
| 107 |
+
|
| 108 |
+
# Ensure safety_settings are present, defaulting to lowest if not provided
|
| 109 |
+
if "safety_settings" not in litellm_kwargs:
|
| 110 |
+
litellm_kwargs["safety_settings"] = {
|
| 111 |
+
"harassment": "BLOCK_NONE",
|
| 112 |
+
"hate_speech": "BLOCK_NONE",
|
| 113 |
+
"sexually_explicit": "BLOCK_NONE",
|
| 114 |
+
"dangerous_content": "BLOCK_NONE",
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
converted_settings = provider_instance.convert_safety_settings(litellm_kwargs["safety_settings"])
|
| 118 |
+
|
| 119 |
+
if converted_settings is not None:
|
| 120 |
+
litellm_kwargs["safety_settings"] = converted_settings
|
| 121 |
+
else:
|
| 122 |
+
# If conversion returns None, remove it to avoid sending empty settings
|
| 123 |
+
del litellm_kwargs["safety_settings"]
|
| 124 |
+
|
| 125 |
+
if "gemma-3" in model and "messages" in litellm_kwargs:
|
| 126 |
+
new_messages = [
|
| 127 |
+
{"role": "user", "content": m["content"]} if m.get("role") == "system" else m
|
| 128 |
+
for m in litellm_kwargs["messages"]
|
| 129 |
+
]
|
| 130 |
+
litellm_kwargs["messages"] = new_messages
|
| 131 |
+
|
| 132 |
+
if provider == "chutes":
|
| 133 |
+
litellm_kwargs["model"] = f"openai/{model.split('/', 1)[1]}"
|
| 134 |
+
litellm_kwargs["api_base"] = "https://llm.chutes.ai/v1"
|
| 135 |
+
|
| 136 |
for attempt in range(self.max_retries):
|
| 137 |
try:
|
| 138 |
lib_logger.info(f"Attempting call with key ...{current_key[-4:]} (Attempt {attempt + 1}/{self.max_retries})")
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
if pre_request_callback:
|
| 141 |
await pre_request_callback()
|
| 142 |
|