bahi-bh commited on
Commit
94f8ccb
·
verified ·
1 Parent(s): ed1e830

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +92 -95
main.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import json
3
  import time
4
  import logging
 
5
  import g4f
6
 
7
  from fastapi import FastAPI, HTTPException, Header
@@ -14,13 +15,13 @@ from typing import List, Optional
14
  # ================= Logging =================
15
 
16
  logging.basicConfig(level=logging.INFO)
17
- logger = logging.getLogger(__name__)
18
 
19
 
20
  # ================= App =================
21
 
22
- app = FastAPI(
23
- title="G4F OpenAI API",
24
  version="1.0"
25
  )
26
 
@@ -32,8 +33,7 @@ app.add_middleware(
32
  allow_headers=["*"]
33
  )
34
 
35
-
36
- API_KEY = os.getenv(
37
  "API_KEY",
38
  "your_fallback_secret"
39
  )
@@ -42,22 +42,20 @@ API_KEY = os.getenv(
42
  # ================= Models =================
43
 
44
  class ChatMessage(BaseModel):
45
- role: str
46
- content: str
47
 
48
 
49
  class ChatRequest(BaseModel):
50
- model: str = "gpt-4o-mini"
51
- messages: List[ChatMessage]
52
- stream: bool = False
53
- provider: Optional[str] = None
54
- max_tokens: Optional[int] = 2048
55
- temperature: Optional[float] = 0.7
56
 
57
 
58
  # ================= Auth =================
59
 
60
- def verify_key(auth: str):
61
 
62
  if not auth:
63
  raise HTTPException(
@@ -65,21 +63,21 @@ def verify_key(auth: str):
65
  detail="Missing Authorization"
66
  )
67
 
68
- parts = auth.split()
69
 
70
- if len(parts) != 2:
71
  raise HTTPException(
72
  status_code=401,
73
  detail="Malformed Authorization"
74
  )
75
 
76
- if parts[0] != "Bearer":
77
  raise HTTPException(
78
  status_code=401,
79
- detail="Invalid Bearer"
80
  )
81
 
82
- if parts[1] != API_KEY:
83
  raise HTTPException(
84
  status_code=401,
85
  detail="Invalid API Key"
@@ -89,71 +87,70 @@ def verify_key(auth: str):
89
  # ================= Health =================
90
 
91
  @app.get("/")
92
- async def root():
93
 
94
  return {
95
- "status": "online"
96
  }
97
 
98
 
99
- # ================= Models Endpoint =================
100
 
101
  @app.get("/v1/models")
102
  async def models(
103
- authorization: str = Header(None)
104
  ):
105
 
106
  verify_key(authorization)
107
 
108
  try:
109
 
110
- found = []
111
-
112
- # استخراج أسماء النماذج تلقائياً
113
- for name in dir(g4f.models):
114
 
115
- if name.startswith("_"):
116
- continue
117
 
118
- obj = getattr(
119
- g4f.models,
120
- name
121
  )
122
 
123
- if isinstance(name, str):
124
- found.append(name)
 
125
 
126
- found = sorted(list(set(found)))
127
 
128
- if not found:
129
- found = [
130
  "gpt-4o-mini",
131
  "gpt-4",
132
  "gpt-3.5-turbo"
133
  ]
134
 
135
  return {
136
- "object": "list",
137
- "data": [
 
 
138
  {
139
- "id": model,
140
- "object": "model",
141
- "owned_by": "g4f"
142
  }
143
- for model in found
144
  ]
145
  }
146
 
147
  except Exception as e:
148
 
149
- logger.error(e)
150
 
151
  return {
152
- "object": "list",
153
- "data": [
 
 
154
  {
155
- "id": "gpt-4o-mini",
156
- "object": "model"
157
  }
158
  ]
159
  }
@@ -163,34 +160,37 @@ async def models(
163
 
164
  @app.post("/v1/chat/completions")
165
  async def chat(
166
- body: ChatRequest,
167
- authorization: str = Header(None)
168
  ):
169
 
170
  verify_key(authorization)
171
 
172
  try:
173
 
174
- provider = None
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  if body.provider:
177
 
178
- provider = getattr(
179
  g4f.Provider,
180
  body.provider,
181
  None
182
  )
183
 
184
- messages = [
185
- {
186
- "role": m.role,
187
- "content": m.content
188
- }
189
- for m in body.messages
190
- ]
191
-
192
 
193
- # ================= STREAM =================
194
 
195
  if body.stream:
196
 
@@ -198,7 +198,7 @@ async def chat(
198
 
199
  try:
200
 
201
- response = g4f.ChatCompletion.create(
202
  model=body.model,
203
  messages=messages,
204
  provider=provider,
@@ -207,15 +207,9 @@ async def chat(
207
 
208
  for chunk in response:
209
 
210
- if not chunk:
211
- continue
212
 
213
- payload = {
214
-
215
- "id":
216
- "chatcmpl-"+str(
217
- int(time.time())
218
- ),
219
 
220
  "object":
221
  "chat.completion.chunk",
@@ -238,39 +232,32 @@ async def chat(
238
  }
239
 
240
  yield (
241
- "data: "
242
- + json.dumps(payload)
243
- + "\n\n"
244
  )
245
 
246
  yield "data: [DONE]\n\n"
247
 
248
  except Exception as e:
249
 
250
- logger.error(e)
251
-
252
- error_payload={
253
-
254
- "error":
255
- str(e)
256
-
257
- }
258
 
259
  yield (
260
- "data: "
261
- + json.dumps(error_payload)
262
- + "\n\n"
263
  )
264
 
 
265
  return StreamingResponse(
266
  generate(),
267
  media_type="text/event-stream"
268
  )
269
 
270
 
271
- # ================= NORMAL =================
 
 
 
 
272
 
273
- response = await g4f.ChatCompletion.create_async(
274
  model=body.model,
275
  messages=messages,
276
  provider=provider
@@ -278,35 +265,45 @@ async def chat(
278
 
279
  return {
280
 
281
- "id":
282
- "chatcmpl-g4f",
283
 
284
- "object":
285
- "chat.completion",
286
 
287
- "created":
288
- int(time.time()),
 
289
 
290
  "model":
291
  body.model,
292
 
293
  "choices":[
 
294
  {
 
295
  "index":0,
 
296
  "message":{
 
297
  "role":"assistant",
298
- "content":str(response)
 
 
 
299
  },
300
- "finish_reason":"stop"
 
 
301
  }
 
302
  ]
303
  }
304
 
 
305
  except Exception as e:
306
 
307
  logger.exception(e)
308
 
309
  raise HTTPException(
310
- status_code=503,
311
  detail=str(e)
312
  )
 
2
  import json
3
  import time
4
  import logging
5
+ import asyncio
6
  import g4f
7
 
8
  from fastapi import FastAPI, HTTPException, Header
 
15
  # ================= Logging =================
16
 
17
  logging.basicConfig(level=logging.INFO)
18
+ logger=logging.getLogger(__name__)
19
 
20
 
21
  # ================= App =================
22
 
23
+ app=FastAPI(
24
+ title="G4F OpenAI Compatible",
25
  version="1.0"
26
  )
27
 
 
33
  allow_headers=["*"]
34
  )
35
 
36
+ API_KEY=os.getenv(
 
37
  "API_KEY",
38
  "your_fallback_secret"
39
  )
 
42
  # ================= Models =================
43
 
44
  class ChatMessage(BaseModel):
45
+ role:str
46
+ content:str
47
 
48
 
49
  class ChatRequest(BaseModel):
50
+ model:str="gpt-4o-mini"
51
+ messages:List[ChatMessage]
52
+ stream:bool=False
53
+ provider:Optional[str]=None
 
 
54
 
55
 
56
  # ================= Auth =================
57
 
58
+ def verify_key(auth:str):
59
 
60
  if not auth:
61
  raise HTTPException(
 
63
  detail="Missing Authorization"
64
  )
65
 
66
+ parts=auth.split()
67
 
68
+ if len(parts)!=2:
69
  raise HTTPException(
70
  status_code=401,
71
  detail="Malformed Authorization"
72
  )
73
 
74
+ if parts[0]!="Bearer":
75
  raise HTTPException(
76
  status_code=401,
77
+ detail="Invalid Authorization"
78
  )
79
 
80
+ if parts[1]!=API_KEY:
81
  raise HTTPException(
82
  status_code=401,
83
  detail="Invalid API Key"
 
87
  # ================= Health =================
88
 
89
  @app.get("/")
90
+ async def health():
91
 
92
  return {
93
+ "status":"online"
94
  }
95
 
96
 
97
+ # ================= Models =================
98
 
99
  @app.get("/v1/models")
100
  async def models(
101
+ authorization:str=Header(None)
102
  ):
103
 
104
  verify_key(authorization)
105
 
106
  try:
107
 
108
+ models=[]
 
 
 
109
 
110
+ if hasattr(g4f.models,"ModelUtils"):
 
111
 
112
+ models=list(
113
+ g4f.models.ModelUtils.convert.keys()
 
114
  )
115
 
116
+ models=sorted(
117
+ list(set(models))
118
+ )
119
 
120
+ if not models:
121
 
122
+ models=[
 
123
  "gpt-4o-mini",
124
  "gpt-4",
125
  "gpt-3.5-turbo"
126
  ]
127
 
128
  return {
129
+
130
+ "object":"list",
131
+
132
+ "data":[
133
  {
134
+ "id":m,
135
+ "object":"model",
136
+ "owned_by":"g4f"
137
  }
138
+ for m in models
139
  ]
140
  }
141
 
142
  except Exception as e:
143
 
144
+ logger.exception(e)
145
 
146
  return {
147
+
148
+ "object":"list",
149
+
150
+ "data":[
151
  {
152
+ "id":"gpt-4o-mini",
153
+ "object":"model"
154
  }
155
  ]
156
  }
 
160
 
161
  @app.post("/v1/chat/completions")
162
  async def chat(
163
+ body:ChatRequest,
164
+ authorization:str=Header(None)
165
  ):
166
 
167
  verify_key(authorization)
168
 
169
  try:
170
 
171
+ messages=[
172
+
173
+ {
174
+ "role":m.role,
175
+ "content":m.content
176
+ }
177
+
178
+ for m in body.messages
179
+ ]
180
+
181
+
182
+ provider=None
183
 
184
  if body.provider:
185
 
186
+ provider=getattr(
187
  g4f.Provider,
188
  body.provider,
189
  None
190
  )
191
 
 
 
 
 
 
 
 
 
192
 
193
+ # ===== STREAM =====
194
 
195
  if body.stream:
196
 
 
198
 
199
  try:
200
 
201
+ response=g4f.ChatCompletion.create(
202
  model=body.model,
203
  messages=messages,
204
  provider=provider,
 
207
 
208
  for chunk in response:
209
 
210
+ payload={
 
211
 
212
+ "id":"chatcmpl",
 
 
 
 
 
213
 
214
  "object":
215
  "chat.completion.chunk",
 
232
  }
233
 
234
  yield (
235
+ f"data: {json.dumps(payload)}\n\n"
 
 
236
  )
237
 
238
  yield "data: [DONE]\n\n"
239
 
240
  except Exception as e:
241
 
242
+ logger.exception(e)
 
 
 
 
 
 
 
243
 
244
  yield (
245
+ f"data: {json.dumps({'error':str(e)})}\n\n"
 
 
246
  )
247
 
248
+
249
  return StreamingResponse(
250
  generate(),
251
  media_type="text/event-stream"
252
  )
253
 
254
 
255
+ # ===== NORMAL =====
256
+
257
+ response=await asyncio.to_thread(
258
+
259
+ g4f.ChatCompletion.create,
260
 
 
261
  model=body.model,
262
  messages=messages,
263
  provider=provider
 
265
 
266
  return {
267
 
268
+ "id":"chatcmpl",
 
269
 
270
+ "object":"chat.completion",
 
271
 
272
+ "created":int(
273
+ time.time()
274
+ ),
275
 
276
  "model":
277
  body.model,
278
 
279
  "choices":[
280
+
281
  {
282
+
283
  "index":0,
284
+
285
  "message":{
286
+
287
  "role":"assistant",
288
+
289
+ "content":
290
+ str(response)
291
+
292
  },
293
+
294
+ "finish_reason":
295
+ "stop"
296
  }
297
+
298
  ]
299
  }
300
 
301
+
302
  except Exception as e:
303
 
304
  logger.exception(e)
305
 
306
  raise HTTPException(
307
+ status_code=500,
308
  detail=str(e)
309
  )