Update app.py
Browse files
app.py
CHANGED
|
@@ -134,15 +134,21 @@ async def root_head():
|
|
| 134 |
def normalize_model(model: str):
|
| 135 |
|
| 136 |
if not model:
|
| 137 |
-
return "gpt-
|
| 138 |
|
| 139 |
model_lower = model.lower()
|
| 140 |
|
| 141 |
# GPT
|
| 142 |
-
if "gpt-4"
|
| 143 |
return "gpt-4"
|
| 144 |
|
| 145 |
-
if "gpt-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
return "gpt-3.5-turbo"
|
| 147 |
|
| 148 |
# Claude
|
|
@@ -153,13 +159,13 @@ def normalize_model(model: str):
|
|
| 153 |
if "gemini" in model_lower:
|
| 154 |
return "gemini"
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
if "command-r
|
| 158 |
-
return "command-r-plus"
|
| 159 |
-
|
| 160 |
-
if "command-r" in model_lower:
|
| 161 |
return "command-r"
|
| 162 |
|
|
|
|
|
|
|
|
|
|
| 163 |
# Grok
|
| 164 |
if "grok" in model_lower:
|
| 165 |
return "gpt-4"
|
|
@@ -195,22 +201,27 @@ async def get_models():
|
|
| 195 |
|
| 196 |
visible_models = [
|
| 197 |
|
|
|
|
| 198 |
"gpt-4",
|
| 199 |
"gpt-4-turbo",
|
| 200 |
"gpt-4o",
|
| 201 |
"gpt-4o-mini",
|
| 202 |
"gpt-3.5-turbo",
|
| 203 |
|
|
|
|
| 204 |
"claude-sonnet-4-6",
|
| 205 |
"claude-opus-4",
|
| 206 |
"claude-3-7-sonnet",
|
| 207 |
|
|
|
|
| 208 |
"gemini-2.5-pro",
|
| 209 |
"gemini-2.5-flash",
|
| 210 |
|
|
|
|
| 211 |
"command-r",
|
| 212 |
"command-r-plus",
|
| 213 |
|
|
|
|
| 214 |
"grok-4",
|
| 215 |
"llama-3-70b",
|
| 216 |
"mistral-7b"
|
|
@@ -222,7 +233,10 @@ async def get_models():
|
|
| 222 |
"id": model,
|
| 223 |
"object": "model",
|
| 224 |
"created": int(time.time()),
|
| 225 |
-
"owned_by": "
|
|
|
|
|
|
|
|
|
|
| 226 |
})
|
| 227 |
|
| 228 |
return {
|
|
@@ -259,10 +273,6 @@ async def chat_completions(
|
|
| 259 |
f"Request model={model} stream={body.stream}"
|
| 260 |
)
|
| 261 |
|
| 262 |
-
# =================================================
|
| 263 |
-
# STREAMING
|
| 264 |
-
# =================================================
|
| 265 |
-
|
| 266 |
if body.stream:
|
| 267 |
|
| 268 |
def generate_stream():
|
|
@@ -279,6 +289,8 @@ async def chat_completions(
|
|
| 279 |
stream=True
|
| 280 |
)
|
| 281 |
|
|
|
|
|
|
|
| 282 |
for chunk in response:
|
| 283 |
|
| 284 |
try:
|
|
@@ -302,6 +314,8 @@ async def chat_completions(
|
|
| 302 |
if not content:
|
| 303 |
continue
|
| 304 |
|
|
|
|
|
|
|
| 305 |
payload = {
|
| 306 |
"id": chunk_id,
|
| 307 |
"object": "chat.completion.chunk",
|
|
@@ -333,6 +347,18 @@ async def chat_completions(
|
|
| 333 |
f"Chunk error: {chunk_error}"
|
| 334 |
)
|
| 335 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
final_payload = {
|
| 337 |
"id": chunk_id,
|
| 338 |
"object": "chat.completion.chunk",
|
|
@@ -383,10 +409,6 @@ async def chat_completions(
|
|
| 383 |
}
|
| 384 |
)
|
| 385 |
|
| 386 |
-
# =================================================
|
| 387 |
-
# NORMAL
|
| 388 |
-
# =================================================
|
| 389 |
-
|
| 390 |
try:
|
| 391 |
|
| 392 |
client = create_client()
|
|
|
|
| 134 |
def normalize_model(model: str):
|
| 135 |
|
| 136 |
if not model:
|
| 137 |
+
return "gpt-4"
|
| 138 |
|
| 139 |
model_lower = model.lower()
|
| 140 |
|
| 141 |
# GPT
|
| 142 |
+
if model_lower == "gpt-4":
|
| 143 |
return "gpt-4"
|
| 144 |
|
| 145 |
+
if model_lower == "gpt-4o":
|
| 146 |
+
return "gpt-4o"
|
| 147 |
+
|
| 148 |
+
if model_lower == "gpt-4o-mini":
|
| 149 |
+
return "gpt-4o-mini"
|
| 150 |
+
|
| 151 |
+
if model_lower == "gpt-3.5-turbo":
|
| 152 |
return "gpt-3.5-turbo"
|
| 153 |
|
| 154 |
# Claude
|
|
|
|
| 159 |
if "gemini" in model_lower:
|
| 160 |
return "gemini"
|
| 161 |
|
| 162 |
+
# Command
|
| 163 |
+
if model_lower == "command-r":
|
|
|
|
|
|
|
|
|
|
| 164 |
return "command-r"
|
| 165 |
|
| 166 |
+
if model_lower == "command-r-plus":
|
| 167 |
+
return "command-r-plus"
|
| 168 |
+
|
| 169 |
# Grok
|
| 170 |
if "grok" in model_lower:
|
| 171 |
return "gpt-4"
|
|
|
|
| 201 |
|
| 202 |
visible_models = [
|
| 203 |
|
| 204 |
+
# GPT
|
| 205 |
"gpt-4",
|
| 206 |
"gpt-4-turbo",
|
| 207 |
"gpt-4o",
|
| 208 |
"gpt-4o-mini",
|
| 209 |
"gpt-3.5-turbo",
|
| 210 |
|
| 211 |
+
# Claude
|
| 212 |
"claude-sonnet-4-6",
|
| 213 |
"claude-opus-4",
|
| 214 |
"claude-3-7-sonnet",
|
| 215 |
|
| 216 |
+
# Gemini
|
| 217 |
"gemini-2.5-pro",
|
| 218 |
"gemini-2.5-flash",
|
| 219 |
|
| 220 |
+
# Cohere
|
| 221 |
"command-r",
|
| 222 |
"command-r-plus",
|
| 223 |
|
| 224 |
+
# Other
|
| 225 |
"grok-4",
|
| 226 |
"llama-3-70b",
|
| 227 |
"mistral-7b"
|
|
|
|
| 233 |
"id": model,
|
| 234 |
"object": "model",
|
| 235 |
"created": int(time.time()),
|
| 236 |
+
"owned_by": "openai",
|
| 237 |
+
"permission": [],
|
| 238 |
+
"root": model,
|
| 239 |
+
"parent": None
|
| 240 |
})
|
| 241 |
|
| 242 |
return {
|
|
|
|
| 273 |
f"Request model={model} stream={body.stream}"
|
| 274 |
)
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
if body.stream:
|
| 277 |
|
| 278 |
def generate_stream():
|
|
|
|
| 289 |
stream=True
|
| 290 |
)
|
| 291 |
|
| 292 |
+
has_content = False
|
| 293 |
+
|
| 294 |
for chunk in response:
|
| 295 |
|
| 296 |
try:
|
|
|
|
| 314 |
if not content:
|
| 315 |
continue
|
| 316 |
|
| 317 |
+
has_content = True
|
| 318 |
+
|
| 319 |
payload = {
|
| 320 |
"id": chunk_id,
|
| 321 |
"object": "chat.completion.chunk",
|
|
|
|
| 347 |
f"Chunk error: {chunk_error}"
|
| 348 |
)
|
| 349 |
|
| 350 |
+
if not has_content:
|
| 351 |
+
|
| 352 |
+
yield (
|
| 353 |
+
"data: "
|
| 354 |
+
+ json.dumps({
|
| 355 |
+
"error": {
|
| 356 |
+
"message": "Empty response"
|
| 357 |
+
}
|
| 358 |
+
})
|
| 359 |
+
+ "\n\n"
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
final_payload = {
|
| 363 |
"id": chunk_id,
|
| 364 |
"object": "chat.completion.chunk",
|
|
|
|
| 409 |
}
|
| 410 |
)
|
| 411 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
try:
|
| 413 |
|
| 414 |
client = create_client()
|