Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ API_KEY = "sk-your-secret-key"
|
|
| 31 |
# timeout ูู
ูุน ุงูุชุนููู ุงูุฃุจุฏู
|
| 32 |
REQUEST_TIMEOUT = 45
|
| 33 |
|
| 34 |
-
#
|
| 35 |
MAX_RETRIES = 2
|
| 36 |
|
| 37 |
|
|
@@ -41,7 +41,7 @@ MAX_RETRIES = 2
|
|
| 41 |
|
| 42 |
app = FastAPI(
|
| 43 |
title="Universal AI Gateway",
|
| 44 |
-
version="4.
|
| 45 |
)
|
| 46 |
|
| 47 |
|
|
@@ -114,7 +114,7 @@ async def root():
|
|
| 114 |
return {
|
| 115 |
"status": "online",
|
| 116 |
"service": "Universal AI Gateway",
|
| 117 |
-
"version": "4.
|
| 118 |
}
|
| 119 |
|
| 120 |
|
|
@@ -127,41 +127,79 @@ async def get_models():
|
|
| 127 |
|
| 128 |
models_data = []
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
fallback_models = [
|
|
|
|
|
|
|
| 131 |
"gpt-4o-mini",
|
| 132 |
"gpt-4o",
|
| 133 |
"gpt-4",
|
| 134 |
"gpt-3.5-turbo",
|
|
|
|
|
|
|
| 135 |
"claude-3-haiku",
|
|
|
|
|
|
|
| 136 |
"llama-3.1-70b",
|
|
|
|
|
|
|
| 137 |
"mixtral-8x7b",
|
|
|
|
|
|
|
| 138 |
"deepseek-chat",
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
]
|
| 141 |
|
|
|
|
|
|
|
| 142 |
try:
|
| 143 |
|
| 144 |
if hasattr(g4f.models, "_all_models"):
|
| 145 |
|
| 146 |
all_models = list(g4f.models._all_models)
|
| 147 |
|
| 148 |
-
for model in all_models[:
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
except Exception as e:
|
| 158 |
|
| 159 |
logger.error(f"Models error: {e}")
|
| 160 |
|
| 161 |
-
# fallback
|
| 162 |
-
|
| 163 |
|
| 164 |
-
|
| 165 |
|
| 166 |
models_data.append({
|
| 167 |
"id": model,
|
|
@@ -170,6 +208,8 @@ async def get_models():
|
|
| 170 |
"owned_by": "g4f"
|
| 171 |
})
|
| 172 |
|
|
|
|
|
|
|
| 173 |
return {
|
| 174 |
"object": "list",
|
| 175 |
"data": models_data
|
|
@@ -198,6 +238,7 @@ async def safe_completion(
|
|
| 198 |
|
| 199 |
client = Client()
|
| 200 |
|
|
|
|
| 201 |
response = await asyncio.wait_for(
|
| 202 |
asyncio.to_thread(
|
| 203 |
client.chat.completions.create,
|
|
@@ -208,6 +249,10 @@ async def safe_completion(
|
|
| 208 |
timeout=REQUEST_TIMEOUT
|
| 209 |
)
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
return response
|
| 212 |
|
| 213 |
except asyncio.TimeoutError:
|
|
@@ -215,7 +260,7 @@ async def safe_completion(
|
|
| 215 |
last_error = "Request timeout"
|
| 216 |
|
| 217 |
logger.warning(
|
| 218 |
-
f"Timeout
|
| 219 |
)
|
| 220 |
|
| 221 |
except Exception as e:
|
|
@@ -223,7 +268,7 @@ async def safe_completion(
|
|
| 223 |
last_error = e
|
| 224 |
|
| 225 |
logger.warning(
|
| 226 |
-
f"Attempt failed {attempt + 1}
|
| 227 |
)
|
| 228 |
|
| 229 |
await asyncio.sleep(1)
|
|
@@ -322,7 +367,7 @@ async def chat_completions(
|
|
| 322 |
f"Chunk error: {chunk_error}"
|
| 323 |
)
|
| 324 |
|
| 325 |
-
# provider
|
| 326 |
if not has_content:
|
| 327 |
|
| 328 |
error_payload = {
|
|
|
|
| 31 |
# timeout ูู
ูุน ุงูุชุนููู ุงูุฃุจุฏู
|
| 32 |
REQUEST_TIMEOUT = 45
|
| 33 |
|
| 34 |
+
# retry ุฎููู
|
| 35 |
MAX_RETRIES = 2
|
| 36 |
|
| 37 |
|
|
|
|
| 41 |
|
| 42 |
app = FastAPI(
|
| 43 |
title="Universal AI Gateway",
|
| 44 |
+
version="4.2.0"
|
| 45 |
)
|
| 46 |
|
| 47 |
|
|
|
|
| 114 |
return {
|
| 115 |
"status": "online",
|
| 116 |
"service": "Universal AI Gateway",
|
| 117 |
+
"version": "4.2.0"
|
| 118 |
}
|
| 119 |
|
| 120 |
|
|
|
|
| 127 |
|
| 128 |
models_data = []
|
| 129 |
|
| 130 |
+
# =================================================
|
| 131 |
+
# MODELS THAT WORK WELL
|
| 132 |
+
# =================================================
|
| 133 |
+
|
| 134 |
fallback_models = [
|
| 135 |
+
|
| 136 |
+
# GPT
|
| 137 |
"gpt-4o-mini",
|
| 138 |
"gpt-4o",
|
| 139 |
"gpt-4",
|
| 140 |
"gpt-3.5-turbo",
|
| 141 |
+
|
| 142 |
+
# Claude
|
| 143 |
"claude-3-haiku",
|
| 144 |
+
|
| 145 |
+
# Llama
|
| 146 |
"llama-3.1-70b",
|
| 147 |
+
|
| 148 |
+
# Mixtral
|
| 149 |
"mixtral-8x7b",
|
| 150 |
+
|
| 151 |
+
# Deepseek
|
| 152 |
"deepseek-chat",
|
| 153 |
+
|
| 154 |
+
# Gemini
|
| 155 |
+
"gemini-pro",
|
| 156 |
+
|
| 157 |
+
# =================================================
|
| 158 |
+
# COHERE FAMILY
|
| 159 |
+
# =================================================
|
| 160 |
+
|
| 161 |
+
"command-r",
|
| 162 |
+
"command-r-plus",
|
| 163 |
+
"command-r7b",
|
| 164 |
+
"command",
|
| 165 |
+
"command-nightly",
|
| 166 |
+
|
| 167 |
+
# Additional Cohere-style names
|
| 168 |
+
"cohere-command-r",
|
| 169 |
+
"cohere-command-r-plus",
|
| 170 |
]
|
| 171 |
|
| 172 |
+
added_models = set()
|
| 173 |
+
|
| 174 |
try:
|
| 175 |
|
| 176 |
if hasattr(g4f.models, "_all_models"):
|
| 177 |
|
| 178 |
all_models = list(g4f.models._all_models)
|
| 179 |
|
| 180 |
+
for model in all_models[:100]:
|
| 181 |
|
| 182 |
+
model_name = str(model)
|
| 183 |
+
|
| 184 |
+
if model_name not in added_models:
|
| 185 |
+
|
| 186 |
+
models_data.append({
|
| 187 |
+
"id": model_name,
|
| 188 |
+
"object": "model",
|
| 189 |
+
"created": int(time.time()),
|
| 190 |
+
"owned_by": "g4f"
|
| 191 |
+
})
|
| 192 |
+
|
| 193 |
+
added_models.add(model_name)
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
|
| 197 |
logger.error(f"Models error: {e}")
|
| 198 |
|
| 199 |
+
# fallback models
|
| 200 |
+
for model in fallback_models:
|
| 201 |
|
| 202 |
+
if model not in added_models:
|
| 203 |
|
| 204 |
models_data.append({
|
| 205 |
"id": model,
|
|
|
|
| 208 |
"owned_by": "g4f"
|
| 209 |
})
|
| 210 |
|
| 211 |
+
added_models.add(model)
|
| 212 |
+
|
| 213 |
return {
|
| 214 |
"object": "list",
|
| 215 |
"data": models_data
|
|
|
|
| 238 |
|
| 239 |
client = Client()
|
| 240 |
|
| 241 |
+
# timeout ูู
ูุน ุงูุชุนููู ุงูุฃุจุฏู
|
| 242 |
response = await asyncio.wait_for(
|
| 243 |
asyncio.to_thread(
|
| 244 |
client.chat.completions.create,
|
|
|
|
| 249 |
timeout=REQUEST_TIMEOUT
|
| 250 |
)
|
| 251 |
|
| 252 |
+
logger.info(
|
| 253 |
+
f"Success | model={model}"
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
return response
|
| 257 |
|
| 258 |
except asyncio.TimeoutError:
|
|
|
|
| 260 |
last_error = "Request timeout"
|
| 261 |
|
| 262 |
logger.warning(
|
| 263 |
+
f"Timeout | model={model}"
|
| 264 |
)
|
| 265 |
|
| 266 |
except Exception as e:
|
|
|
|
| 268 |
last_error = e
|
| 269 |
|
| 270 |
logger.warning(
|
| 271 |
+
f"Attempt failed {attempt + 1} | {e}"
|
| 272 |
)
|
| 273 |
|
| 274 |
await asyncio.sleep(1)
|
|
|
|
| 367 |
f"Chunk error: {chunk_error}"
|
| 368 |
)
|
| 369 |
|
| 370 |
+
# provider ูุชุญ stream ุจุฏูู ู
ุญุชูู
|
| 371 |
if not has_content:
|
| 372 |
|
| 373 |
error_payload = {
|