bahi-bh commited on
Commit
869a377
·
verified ·
1 Parent(s): 1265910

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -48
app.py CHANGED
@@ -3,30 +3,31 @@ from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi.responses import StreamingResponse, JSONResponse
4
  from pydantic import BaseModel
5
  from typing import List
 
 
6
  import json
7
  import uuid
8
  import time
9
  import asyncio
10
- import g4f
11
 
12
- # =========================================
13
  # CONFIG
14
- # =========================================
15
 
16
  API_KEY = "sk-your-secret-key"
17
 
18
- # =========================================
19
  # FASTAPI
20
- # =========================================
21
 
22
  app = FastAPI(
23
- title="Universal OpenAI API",
24
- version="1.0.0"
25
  )
26
 
27
- # =========================================
28
  # CORS
29
- # =========================================
30
 
31
  app.add_middleware(
32
  CORSMiddleware,
@@ -36,21 +37,9 @@ app.add_middleware(
36
  allow_headers=["*"],
37
  )
38
 
39
- # =========================================
40
  # MODELS
41
- # =========================================
42
-
43
- AVAILABLE_MODELS = [
44
- "gpt-4o-mini",
45
- "gpt-4",
46
- "claude-3-haiku",
47
- "llama-3.1-70b",
48
- "mixtral-8x7b"
49
- ]
50
-
51
- # =========================================
52
- # Pydantic
53
- # =========================================
54
 
55
  class Message(BaseModel):
56
  role: str
@@ -63,9 +52,9 @@ class ChatRequest(BaseModel):
63
  temperature: float = 0.7
64
  max_tokens: int = 4096
65
 
66
- # =========================================
67
  # AUTH
68
- # =========================================
69
 
70
  def verify_api_key(req: Request):
71
 
@@ -91,40 +80,67 @@ def verify_api_key(req: Request):
91
  detail="Invalid API Key"
92
  )
93
 
94
- # =========================================
95
  # ROOT
96
- # =========================================
97
 
98
  @app.get("/")
99
  async def root():
100
 
101
  return {
102
- "status": "online"
 
103
  }
104
 
105
- # =========================================
106
  # MODELS ENDPOINT
107
- # =========================================
108
 
109
  @app.get("/v1/models")
110
  async def models():
111
 
112
- return {
113
- "object": "list",
114
- "data": [
115
- {
116
- "id": model,
 
 
 
 
 
117
  "object": "model",
118
  "created": int(time.time()),
119
- "owned_by": "9router"
120
- }
121
- for model in AVAILABLE_MODELS
 
 
 
 
 
 
 
 
122
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  }
124
 
125
- # =========================================
126
  # CHAT COMPLETIONS
127
- # =========================================
128
 
129
  @app.post("/v1/chat/completions")
130
  async def chat_completions(
@@ -142,11 +158,9 @@ async def chat_completions(
142
  for m in body.messages
143
  ]
144
 
145
- provider = g4f.Provider.Blackbox
146
-
147
- # =====================================
148
  # STREAMING
149
- # =====================================
150
 
151
  if body.stream:
152
 
@@ -156,7 +170,6 @@ async def chat_completions(
156
 
157
  response = g4f.ChatCompletion.create(
158
  model=body.model,
159
- provider=provider,
160
  messages=messages,
161
  stream=True
162
  )
@@ -219,15 +232,14 @@ async def chat_completions(
219
  media_type="text/event-stream"
220
  )
221
 
222
- # =====================================
223
  # NORMAL RESPONSE
224
- # =====================================
225
 
226
  try:
227
 
228
  response = g4f.ChatCompletion.create(
229
  model=body.model,
230
- provider=provider,
231
  messages=messages
232
  )
233
 
 
3
  from fastapi.responses import StreamingResponse, JSONResponse
4
  from pydantic import BaseModel
5
  from typing import List
6
+ import g4f
7
+ import g4f.models
8
  import json
9
  import uuid
10
  import time
11
  import asyncio
 
12
 
13
+ # =====================================================
14
  # CONFIG
15
+ # =====================================================
16
 
17
  API_KEY = "sk-your-secret-key"
18
 
19
+ # =====================================================
20
  # FASTAPI
21
+ # =====================================================
22
 
23
  app = FastAPI(
24
+ title="Universal AI Gateway",
25
+ version="2.0.0"
26
  )
27
 
28
+ # =====================================================
29
  # CORS
30
+ # =====================================================
31
 
32
  app.add_middleware(
33
  CORSMiddleware,
 
37
  allow_headers=["*"],
38
  )
39
 
40
+ # =====================================================
41
  # MODELS
42
+ # =====================================================
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  class Message(BaseModel):
45
  role: str
 
52
  temperature: float = 0.7
53
  max_tokens: int = 4096
54
 
55
+ # =====================================================
56
  # AUTH
57
+ # =====================================================
58
 
59
  def verify_api_key(req: Request):
60
 
 
80
  detail="Invalid API Key"
81
  )
82
 
83
+ # =====================================================
84
  # ROOT
85
+ # =====================================================
86
 
87
  @app.get("/")
88
  async def root():
89
 
90
  return {
91
+ "status": "online",
92
+ "service": "Universal AI Gateway"
93
  }
94
 
95
+ # =====================================================
96
  # MODELS ENDPOINT
97
+ # =====================================================
98
 
99
  @app.get("/v1/models")
100
  async def models():
101
 
102
+ data = []
103
+
104
+ try:
105
+
106
+ all_models = list(g4f.models._all_models)
107
+
108
+ for model_name in all_models:
109
+
110
+ data.append({
111
+ "id": str(model_name),
112
  "object": "model",
113
  "created": int(time.time()),
114
+ "owned_by": "g4f"
115
+ })
116
+
117
+ except Exception:
118
+
119
+ fallback = [
120
+ "gpt-4o-mini",
121
+ "gpt-4",
122
+ "claude-3-haiku",
123
+ "llama-3.1-70b",
124
+ "mixtral-8x7b"
125
  ]
126
+
127
+ for model_name in fallback:
128
+
129
+ data.append({
130
+ "id": model_name,
131
+ "object": "model",
132
+ "created": int(time.time()),
133
+ "owned_by": "g4f"
134
+ })
135
+
136
+ return {
137
+ "object": "list",
138
+ "data": data
139
  }
140
 
141
+ # =====================================================
142
  # CHAT COMPLETIONS
143
+ # =====================================================
144
 
145
  @app.post("/v1/chat/completions")
146
  async def chat_completions(
 
158
  for m in body.messages
159
  ]
160
 
161
+ # =================================================
 
 
162
  # STREAMING
163
+ # =================================================
164
 
165
  if body.stream:
166
 
 
170
 
171
  response = g4f.ChatCompletion.create(
172
  model=body.model,
 
173
  messages=messages,
174
  stream=True
175
  )
 
232
  media_type="text/event-stream"
233
  )
234
 
235
+ # =================================================
236
  # NORMAL RESPONSE
237
+ # =================================================
238
 
239
  try:
240
 
241
  response = g4f.ChatCompletion.create(
242
  model=body.model,
 
243
  messages=messages
244
  )
245