bahi-bh commited on
Commit
931b5d1
·
verified ·
1 Parent(s): 09be010

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -88
app.py CHANGED
@@ -19,18 +19,13 @@ API_KEY = "sk-your-secret-key"
19
  executor = ThreadPoolExecutor(max_workers=5)
20
 
21
  # =====================================
22
- # FASTAPI
23
  # =====================================
24
 
25
  app = FastAPI(
26
- title="AI Gateway",
27
- version="2.0.0"
28
  )
29
 
30
- # =====================================
31
- # CORS
32
- # =====================================
33
-
34
  app.add_middleware(
35
  CORSMiddleware,
36
  allow_origins=["*"],
@@ -52,7 +47,7 @@ class ChatRequest(BaseModel):
52
  stream: bool = False
53
 
54
  # =====================================
55
- # API KEY
56
  # =====================================
57
 
58
  def verify_api_key(auth: str):
@@ -66,7 +61,7 @@ def verify_api_key(auth: str):
66
  if not auth.startswith("Bearer "):
67
  raise HTTPException(
68
  status_code=401,
69
- detail="Invalid Authorization Format"
70
  )
71
 
72
  token = auth.replace("Bearer ", "")
@@ -78,68 +73,52 @@ def verify_api_key(auth: str):
78
  )
79
 
80
  # =====================================
81
- # NORMAL RESPONSE
82
  # =====================================
83
 
84
- def generate_response(model, messages):
85
-
86
- try:
87
-
88
- response = g4f.ChatCompletion.create(
89
- model=model,
90
- messages=messages
91
- )
92
-
93
- return response
94
 
95
- except Exception as e:
 
 
 
96
 
97
- raise Exception(str(e))
98
 
99
  # =====================================
100
- # STREAMING RESPONSE
101
  # =====================================
102
 
103
- async def stream_response(model, messages):
104
 
105
  loop = asyncio.get_event_loop()
106
 
107
- try:
108
-
109
- response = await loop.run_in_executor(
110
- executor,
111
- lambda: g4f.ChatCompletion.create(
112
- model=model,
113
- messages=messages,
114
- stream=True
115
- )
116
  )
 
117
 
118
- for chunk in response:
119
 
120
- if chunk:
121
 
122
- payload = {
123
- "choices": [
124
- {
125
- "delta": {
126
- "content": chunk
127
- }
128
  }
129
- ]
130
- }
131
-
132
- yield f"data: {json.dumps(payload)}\\n\\n"
133
-
134
- yield "data: [DONE]\\n\\n"
135
-
136
- except Exception as e:
137
 
138
- error_payload = {
139
- "error": str(e)
140
- }
141
 
142
- yield f"data: {json.dumps(error_payload)}\\n\\n"
143
 
144
  # =====================================
145
  # HOME
@@ -149,12 +128,11 @@ async def stream_response(model, messages):
149
  async def home():
150
 
151
  return {
152
- "status": "online",
153
- "service": "AI Gateway"
154
  }
155
 
156
  # =====================================
157
- # CHAT COMPLETIONS
158
  # =====================================
159
 
160
  @app.post("/v1/chat/completions")
@@ -163,7 +141,6 @@ async def chat(
163
  authorization: str = Header(None)
164
  ):
165
 
166
- # تحقق API Key
167
  verify_api_key(authorization)
168
 
169
  messages = [
@@ -171,50 +148,41 @@ async def chat(
171
  for m in req.messages
172
  ]
173
 
174
- # =================================
175
- # STREAMING
176
- # =================================
177
 
178
  if req.stream:
179
 
180
  return StreamingResponse(
181
- stream_response(
182
  req.model,
183
  messages
184
  ),
185
  media_type="text/event-stream"
186
  )
187
 
188
- # =================================
189
- # NORMAL RESPONSE
190
- # =================================
191
 
192
  loop = asyncio.get_event_loop()
193
 
194
- try:
195
-
196
- response = await loop.run_in_executor(
197
- executor,
198
- lambda: generate_response(
199
- req.model,
200
- messages
201
- )
202
  )
 
203
 
204
- return {
205
- "choices": [
206
- {
207
- "message": {
208
- "role": "assistant",
209
- "content": response
210
- }
211
  }
212
- ]
213
- }
214
-
215
- except Exception as e:
216
-
217
- raise HTTPException(
218
- status_code=500,
219
- detail=str(e)
220
- )
 
19
  executor = ThreadPoolExecutor(max_workers=5)
20
 
21
  # =====================================
22
+ # APP
23
  # =====================================
24
 
25
  app = FastAPI(
26
+ title="AI Gateway"
 
27
  )
28
 
 
 
 
 
29
  app.add_middleware(
30
  CORSMiddleware,
31
  allow_origins=["*"],
 
47
  stream: bool = False
48
 
49
  # =====================================
50
+ # AUTH
51
  # =====================================
52
 
53
  def verify_api_key(auth: str):
 
61
  if not auth.startswith("Bearer "):
62
  raise HTTPException(
63
  status_code=401,
64
+ detail="Invalid Authorization"
65
  )
66
 
67
  token = auth.replace("Bearer ", "")
 
73
  )
74
 
75
  # =====================================
76
+ # GENERATE
77
  # =====================================
78
 
79
+ def generate(model, messages):
 
 
 
 
 
 
 
 
 
80
 
81
+ response = g4f.ChatCompletion.create(
82
+ model=model,
83
+ messages=messages
84
+ )
85
 
86
+ return response
87
 
88
  # =====================================
89
+ # STREAM
90
  # =====================================
91
 
92
+ async def stream_generate(model, messages):
93
 
94
  loop = asyncio.get_event_loop()
95
 
96
+ response = await loop.run_in_executor(
97
+ executor,
98
+ lambda: g4f.ChatCompletion.create(
99
+ model=model,
100
+ messages=messages,
101
+ stream=True
 
 
 
102
  )
103
+ )
104
 
105
+ for chunk in response:
106
 
107
+ if chunk:
108
 
109
+ payload = {
110
+ "choices": [
111
+ {
112
+ "delta": {
113
+ "content": chunk
 
114
  }
115
+ }
116
+ ]
117
+ }
 
 
 
 
 
118
 
119
+ yield f"data: {json.dumps(payload)}\\n\\n"
 
 
120
 
121
+ yield "data: [DONE]\\n\\n"
122
 
123
  # =====================================
124
  # HOME
 
128
  async def home():
129
 
130
  return {
131
+ "status": "online"
 
132
  }
133
 
134
  # =====================================
135
+ # CHAT
136
  # =====================================
137
 
138
  @app.post("/v1/chat/completions")
 
141
  authorization: str = Header(None)
142
  ):
143
 
 
144
  verify_api_key(authorization)
145
 
146
  messages = [
 
148
  for m in req.messages
149
  ]
150
 
151
+ # =========================
152
+ # STREAM
153
+ # =========================
154
 
155
  if req.stream:
156
 
157
  return StreamingResponse(
158
+ stream_generate(
159
  req.model,
160
  messages
161
  ),
162
  media_type="text/event-stream"
163
  )
164
 
165
+ # =========================
166
+ # NORMAL
167
+ # =========================
168
 
169
  loop = asyncio.get_event_loop()
170
 
171
+ response = await loop.run_in_executor(
172
+ executor,
173
+ lambda: generate(
174
+ req.model,
175
+ messages
 
 
 
176
  )
177
+ )
178
 
179
+ return {
180
+ "choices": [
181
+ {
182
+ "message": {
183
+ "role": "assistant",
184
+ "content": response
 
185
  }
186
+ }
187
+ ]
188
+ }