Update app.py
Browse files
app.py
CHANGED
|
@@ -83,19 +83,24 @@ def verify_api_key(req: Request):
|
|
| 83 |
|
| 84 |
token = None
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
status_code=401,
|
| 91 |
-
detail="Invalid Authorization Format"
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
if token != API_KEY:
|
| 101 |
raise HTTPException(
|
|
@@ -473,14 +478,12 @@ async def anthropic_messages(
|
|
| 473 |
if (
|
| 474 |
hasattr(chunk, "choices")
|
| 475 |
and chunk.choices
|
|
|
|
|
|
|
| 476 |
and chunk.choices[0].delta
|
| 477 |
-
and chunk.choices[0]
|
| 478 |
-
.delta.content
|
| 479 |
):
|
| 480 |
-
content =
|
| 481 |
-
chunk.choices[0]
|
| 482 |
-
.delta.content
|
| 483 |
-
)
|
| 484 |
|
| 485 |
if not content:
|
| 486 |
continue
|
|
@@ -495,7 +498,10 @@ async def anthropic_messages(
|
|
| 495 |
|
| 496 |
yield (
|
| 497 |
"data: "
|
| 498 |
-
+ json.dumps(
|
|
|
|
|
|
|
|
|
|
| 499 |
+ "\n\n"
|
| 500 |
)
|
| 501 |
|
|
|
|
| 83 |
|
| 84 |
token = None
|
| 85 |
|
| 86 |
+
# دعم x-api-key
|
| 87 |
+
if x_api_key:
|
| 88 |
+
token = x_api_key.strip()
|
| 89 |
|
| 90 |
+
# دعم Authorization Bearer
|
| 91 |
+
elif auth:
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
parts = auth.split(" ", 1)
|
| 94 |
|
| 95 |
+
if len(parts) == 2:
|
| 96 |
+
token = parts[1].strip()
|
| 97 |
|
| 98 |
+
# لو لم يتم استخراج توكن صالح
|
| 99 |
+
if not token:
|
| 100 |
+
raise HTTPException(
|
| 101 |
+
status_code=401,
|
| 102 |
+
detail="Invalid API Key"
|
| 103 |
+
)
|
| 104 |
|
| 105 |
if token != API_KEY:
|
| 106 |
raise HTTPException(
|
|
|
|
| 478 |
if (
|
| 479 |
hasattr(chunk, "choices")
|
| 480 |
and chunk.choices
|
| 481 |
+
and len(chunk.choices) > 0
|
| 482 |
+
and hasattr(chunk.choices[0], "delta")
|
| 483 |
and chunk.choices[0].delta
|
| 484 |
+
and chunk.choices[0].delta.content
|
|
|
|
| 485 |
):
|
| 486 |
+
content = chunk.choices[0].delta.content
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
if not content:
|
| 489 |
continue
|
|
|
|
| 498 |
|
| 499 |
yield (
|
| 500 |
"data: "
|
| 501 |
+
+ json.dumps(
|
| 502 |
+
payload,
|
| 503 |
+
ensure_ascii=False
|
| 504 |
+
)
|
| 505 |
+ "\n\n"
|
| 506 |
)
|
| 507 |
|