Update main.py
Browse files
main.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import json
|
| 3 |
import g4f
|
| 4 |
import logging
|
|
|
|
| 5 |
from fastapi import FastAPI, HTTPException, Header, Request
|
| 6 |
from fastapi.responses import StreamingResponse
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -12,11 +13,7 @@ from typing import List, Optional
|
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
app = FastAPI(
|
| 17 |
-
title="G4F OpenAI Compatible API",
|
| 18 |
-
version="1.0"
|
| 19 |
-
)
|
| 20 |
|
| 21 |
app.add_middleware(
|
| 22 |
CORSMiddleware,
|
|
@@ -28,200 +25,78 @@ app.add_middleware(
|
|
| 28 |
|
| 29 |
API_KEY = os.getenv("API_KEY", "your_fallback_secret")
|
| 30 |
|
| 31 |
-
# ---------------- Models ----------------
|
| 32 |
class ChatMessage(BaseModel):
|
| 33 |
role: str
|
| 34 |
content: str
|
| 35 |
|
| 36 |
class ChatRequest(BaseModel):
|
| 37 |
-
model: str
|
| 38 |
messages: List[ChatMessage]
|
| 39 |
stream: bool = False
|
| 40 |
provider: Optional[str] = None
|
| 41 |
-
max_tokens: Optional[int] = 2048
|
| 42 |
-
temperature: Optional[float] = 0.7
|
| 43 |
-
|
| 44 |
|
| 45 |
-
# ---------------- Auth ----------------
|
| 46 |
def verify_key(auth: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
if not auth:
|
| 49 |
-
raise HTTPException(
|
| 50 |
-
status_code=401,
|
| 51 |
-
detail="Missing Authorization header"
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
parts = auth.split()
|
| 55 |
-
|
| 56 |
-
if len(parts) != 2:
|
| 57 |
-
raise HTTPException(
|
| 58 |
-
status_code=401,
|
| 59 |
-
detail="Malformed Authorization"
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
if parts[0] != "Bearer":
|
| 63 |
-
raise HTTPException(
|
| 64 |
-
status_code=401,
|
| 65 |
-
detail="Malformed Authorization"
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
if parts[1] != API_KEY:
|
| 69 |
-
raise HTTPException(
|
| 70 |
-
status_code=401,
|
| 71 |
-
detail="Invalid API Key"
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# ---------------- Health ----------------
|
| 76 |
-
@app.get("/")
|
| 77 |
-
async def health():
|
| 78 |
-
return {
|
| 79 |
-
"status": "online"
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# ---------------- OpenAI models endpoint ----------------
|
| 84 |
@app.get("/v1/models")
|
| 85 |
-
async def
|
| 86 |
-
authorization: str = Header(None)
|
| 87 |
-
):
|
| 88 |
-
|
| 89 |
verify_key(authorization)
|
| 90 |
-
|
|
|
|
| 91 |
return {
|
| 92 |
"object": "list",
|
| 93 |
-
"data": [
|
| 94 |
-
{
|
| 95 |
-
"id": "gpt-4o-mini",
|
| 96 |
-
"object": "model"
|
| 97 |
-
},
|
| 98 |
-
{
|
| 99 |
-
"id": "gpt-3.5-turbo",
|
| 100 |
-
"object": "model"
|
| 101 |
-
},
|
| 102 |
-
{
|
| 103 |
-
"id": "gpt-4",
|
| 104 |
-
"object": "model"
|
| 105 |
-
}
|
| 106 |
-
]
|
| 107 |
}
|
| 108 |
|
| 109 |
-
|
| 110 |
-
# ---------------- Chat ----------------
|
| 111 |
@app.post("/v1/chat/completions")
|
| 112 |
-
async def chat(
|
| 113 |
-
body: ChatRequest,
|
| 114 |
-
authorization: str = Header(None)
|
| 115 |
-
):
|
| 116 |
-
|
| 117 |
verify_key(authorization)
|
| 118 |
-
|
| 119 |
try:
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
provider = None
|
| 122 |
-
|
| 123 |
-
if body.provider:
|
| 124 |
-
|
| 125 |
-
provider = getattr(
|
| 126 |
-
g4f.Provider,
|
| 127 |
-
body.provider,
|
| 128 |
-
None
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
if provider is None:
|
| 132 |
-
raise HTTPException(
|
| 133 |
-
status_code=400,
|
| 134 |
-
detail="Invalid provider"
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
messages = [
|
| 139 |
-
{
|
| 140 |
-
"role":m.role,
|
| 141 |
-
"content":m.content
|
| 142 |
-
}
|
| 143 |
-
for m in body.messages
|
| 144 |
-
]
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
# ---------- Streaming ----------
|
| 148 |
if body.stream:
|
| 149 |
-
|
| 150 |
def generate():
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
for chunk in response:
|
| 162 |
-
|
| 163 |
-
if not chunk:
|
| 164 |
-
continue
|
| 165 |
-
|
| 166 |
payload = {
|
| 167 |
-
"
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
"index":0
|
| 173 |
-
}
|
| 174 |
-
]
|
| 175 |
}
|
| 176 |
-
|
| 177 |
yield f"data: {json.dumps(payload)}\n\n"
|
|
|
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
except Exception as e:
|
| 182 |
-
logger.exception(e)
|
| 183 |
-
|
| 184 |
-
err={
|
| 185 |
-
"error":"stream failed"
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
yield f"data: {json.dumps(err)}\n\n"
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
return StreamingResponse(
|
| 192 |
-
generate(),
|
| 193 |
-
media_type="text/event-stream"
|
| 194 |
-
)
|
| 195 |
|
| 196 |
-
|
| 197 |
-
# ---------- Normal ----------
|
| 198 |
response = await g4f.ChatCompletion.create_async(
|
| 199 |
model=body.model,
|
| 200 |
messages=messages,
|
| 201 |
provider=provider
|
| 202 |
)
|
| 203 |
-
|
| 204 |
return {
|
| 205 |
-
"id":"chatcmpl-g4f",
|
| 206 |
-
"object":"chat.completion",
|
| 207 |
-
"
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
"message":{
|
| 211 |
-
"role":"assistant",
|
| 212 |
-
"content":response
|
| 213 |
-
},
|
| 214 |
-
"finish_reason":"stop"
|
| 215 |
-
}
|
| 216 |
-
]
|
| 217 |
}
|
| 218 |
|
| 219 |
-
|
| 220 |
except Exception as e:
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
raise HTTPException(
|
| 225 |
-
status_code=503,
|
| 226 |
-
detail="Provider unavailable"
|
| 227 |
-
)
|
|
|
|
| 2 |
import json
|
| 3 |
import g4f
|
| 4 |
import logging
|
| 5 |
+
import time
|
| 6 |
from fastapi import FastAPI, HTTPException, Header, Request
|
| 7 |
from fastapi.responses import StreamingResponse
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
app = FastAPI(title="G4F Dynamic API")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
app.add_middleware(
|
| 19 |
CORSMiddleware,
|
|
|
|
| 25 |
|
| 26 |
API_KEY = os.getenv("API_KEY", "your_fallback_secret")
|
| 27 |
|
|
|
|
| 28 |
class ChatMessage(BaseModel):
|
| 29 |
role: str
|
| 30 |
content: str
|
| 31 |
|
| 32 |
class ChatRequest(BaseModel):
|
| 33 |
+
model: str # أزلنا القيمة الافتراضية لنجعل المستخدم يختار أو نعتمد على g4f.models.default
|
| 34 |
messages: List[ChatMessage]
|
| 35 |
stream: bool = False
|
| 36 |
provider: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
def verify_key(auth: str):
|
| 39 |
+
if not auth or not auth.startswith("Bearer "):
|
| 40 |
+
raise HTTPException(status_code=401, detail="Invalid or missing Token")
|
| 41 |
+
token = auth.split(" ")[1]
|
| 42 |
+
if token != API_KEY:
|
| 43 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
@app.get("/v1/models")
|
| 46 |
+
async def list_models(authorization: str = Header(None)):
|
|
|
|
|
|
|
|
|
|
| 47 |
verify_key(authorization)
|
| 48 |
+
# استخراج كافة النماذج المتوفرة في الإصدار الحالي للمكتبة
|
| 49 |
+
model_list = sorted(g4f.models._all)
|
| 50 |
return {
|
| 51 |
"object": "list",
|
| 52 |
+
"data": [{"id": m, "object": "model", "owned_by": "g4f"} for m in model_list]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
|
|
|
|
|
|
| 55 |
@app.post("/v1/chat/completions")
|
| 56 |
+
async def chat(body: ChatRequest, authorization: str = Header(None)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
verify_key(authorization)
|
| 58 |
+
|
| 59 |
try:
|
| 60 |
+
messages = [{"role": m.role, "content": m.content} for m in body.messages]
|
| 61 |
+
provider = getattr(g4f.Provider, body.provider, None) if body.provider else None
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if body.stream:
|
|
|
|
| 64 |
def generate():
|
| 65 |
+
response = g4f.ChatCompletion.create(
|
| 66 |
+
model=body.model,
|
| 67 |
+
messages=messages,
|
| 68 |
+
provider=provider,
|
| 69 |
+
stream=True
|
| 70 |
+
)
|
| 71 |
+
for chunk in response:
|
| 72 |
+
if chunk:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
payload = {
|
| 74 |
+
"id": "chatcmpl-" + str(int(time.time())),
|
| 75 |
+
"object": "chat.completion.chunk",
|
| 76 |
+
"created": int(time.time()),
|
| 77 |
+
"model": body.model,
|
| 78 |
+
"choices": [{"delta": {"content": chunk}, "index": 0, "finish_reason": None}]
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
|
|
|
| 80 |
yield f"data: {json.dumps(payload)}\n\n"
|
| 81 |
+
yield "data: [DONE]\n\n"
|
| 82 |
|
| 83 |
+
return StreamingResponse(generate(), media_type="text/event-stream")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# العادي
|
|
|
|
| 86 |
response = await g4f.ChatCompletion.create_async(
|
| 87 |
model=body.model,
|
| 88 |
messages=messages,
|
| 89 |
provider=provider
|
| 90 |
)
|
| 91 |
+
|
| 92 |
return {
|
| 93 |
+
"id": "chatcmpl-g4f",
|
| 94 |
+
"object": "chat.completion",
|
| 95 |
+
"created": int(time.time()),
|
| 96 |
+
"model": body.model,
|
| 97 |
+
"choices": [{"index": 0, "message": {"role": "assistant", "content": response}, "finish_reason": "stop"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
+
logger.error(f"Error: {e}")
|
| 102 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|