bahi-bh commited on
Commit
517572d
·
verified ·
1 Parent(s): 5b1d2f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -52
app.py CHANGED
@@ -4,7 +4,6 @@ from fastapi.responses import StreamingResponse, JSONResponse
4
  from pydantic import BaseModel
5
  from typing import List, Optional
6
 
7
- import asyncio
8
  import json
9
  import time
10
  import uuid
@@ -35,7 +34,7 @@ API_KEY = "sk-your-secret-key"
35
 
36
  app = FastAPI(
37
  title="Universal AI Gateway",
38
- version="5.2.0"
39
  )
40
 
41
 
@@ -77,6 +76,7 @@ def verify_api_key(req: Request):
77
 
78
  auth = req.headers.get("Authorization")
79
 
 
80
  if not auth:
81
  return True
82
 
@@ -107,7 +107,7 @@ async def root():
107
  return {
108
  "status": "online",
109
  "service": "Universal AI Gateway",
110
- "version": "5.2.0"
111
  }
112
 
113
 
@@ -145,40 +145,6 @@ async def get_models():
145
  }
146
 
147
 
148
- # =====================================================
149
- # NORMAL COMPLETION
150
- # =====================================================
151
-
152
- def normal_completion(model, messages):
153
-
154
- client = Client()
155
-
156
- response = client.chat.completions.create(
157
- model=model,
158
- messages=messages,
159
- stream=False
160
- )
161
-
162
- return response
163
-
164
-
165
- # =====================================================
166
- # STREAM COMPLETION
167
- # =====================================================
168
-
169
- def stream_completion(model, messages):
170
-
171
- client = Client()
172
-
173
- response = client.chat.completions.create(
174
- model=model,
175
- messages=messages,
176
- stream=True
177
- )
178
-
179
- return response
180
-
181
-
182
  # =====================================================
183
  # CHAT COMPLETIONS
184
  # =====================================================
@@ -209,16 +175,18 @@ async def chat_completions(
209
 
210
  if body.stream:
211
 
212
- async def generate_stream():
213
 
214
  chunk_id = f"chatcmpl-{uuid.uuid4().hex}"
215
 
216
  try:
217
 
218
- response = await asyncio.to_thread(
219
- stream_completion,
220
- body.model,
221
- messages
 
 
222
  )
223
 
224
  has_content = False
@@ -232,15 +200,12 @@ async def chat_completions(
232
  if (
233
  hasattr(chunk, "choices")
234
  and chunk.choices
 
 
235
  and chunk.choices[0].delta
236
  and chunk.choices[0].delta.content
237
  ):
238
- content = (
239
- chunk
240
- .choices[0]
241
- .delta
242
- .content
243
- )
244
 
245
  if not content:
246
  continue
@@ -352,10 +317,12 @@ async def chat_completions(
352
 
353
  try:
354
 
355
- response = await asyncio.to_thread(
356
- normal_completion,
357
- body.model,
358
- messages
 
 
359
  )
360
 
361
  assistant_message = ""
 
4
  from pydantic import BaseModel
5
  from typing import List, Optional
6
 
 
7
  import json
8
  import time
9
  import uuid
 
34
 
35
  app = FastAPI(
36
  title="Universal AI Gateway",
37
+ version="5.3.0"
38
  )
39
 
40
 
 
76
 
77
  auth = req.headers.get("Authorization")
78
 
79
+ # السماح بدون مفتاح للاختبار
80
  if not auth:
81
  return True
82
 
 
107
  return {
108
  "status": "online",
109
  "service": "Universal AI Gateway",
110
+ "version": "5.3.0"
111
  }
112
 
113
 
 
145
  }
146
 
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  # =====================================================
149
  # CHAT COMPLETIONS
150
  # =====================================================
 
175
 
176
  if body.stream:
177
 
178
+ def generate_stream():
179
 
180
  chunk_id = f"chatcmpl-{uuid.uuid4().hex}"
181
 
182
  try:
183
 
184
+ client = Client()
185
+
186
+ response = client.chat.completions.create(
187
+ model=body.model,
188
+ messages=messages,
189
+ stream=True
190
  )
191
 
192
  has_content = False
 
200
  if (
201
  hasattr(chunk, "choices")
202
  and chunk.choices
203
+ and len(chunk.choices) > 0
204
+ and hasattr(chunk.choices[0], "delta")
205
  and chunk.choices[0].delta
206
  and chunk.choices[0].delta.content
207
  ):
208
+ content = chunk.choices[0].delta.content
 
 
 
 
 
209
 
210
  if not content:
211
  continue
 
317
 
318
  try:
319
 
320
+ client = Client()
321
+
322
+ response = client.chat.completions.create(
323
+ model=body.model,
324
+ messages=messages,
325
+ stream=False
326
  )
327
 
328
  assistant_message = ""