1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
from __future__ import annotations
import logging
import json
import uvicorn
import secrets
import os
import shutil
import os.path
from fastapi import FastAPI, Response, Request, UploadFile
from fastapi.responses import StreamingResponse, RedirectResponse, HTMLResponse, JSONResponse
from fastapi.exceptions import RequestValidationError
from fastapi.security import APIKeyHeader
from starlette.exceptions import HTTPException
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY, HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN
from fastapi.encoders import jsonable_encoder
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import FileResponse
from pydantic import BaseModel
from typing import Union, Optional, List
import g4f
import g4f.debug
from g4f.client import AsyncClient, ChatCompletion, convert_to_provider
from g4f.providers.response import BaseConversation
from g4f.client.helper import filter_none
from g4f.image import is_accepted_format, images_dir
from g4f.typing import Messages
from g4f.errors import ProviderNotFoundError
from g4f.cookies import read_cookie_files, get_cookies_dir
from g4f.Provider import ProviderType, ProviderUtils, __providers__
logger = logging.getLogger(__name__)
def create_app(g4f_api_key: str = None):
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origin_regex=".*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
api = Api(app, g4f_api_key=g4f_api_key)
api.register_routes()
api.register_authorization()
api.register_validation_exception_handler()
# Read cookie files if not ignored
if not AppConfig.ignore_cookie_files:
read_cookie_files()
return app
def create_app_debug(g4f_api_key: str = None):
g4f.debug.logging = True
return create_app(g4f_api_key)
class ChatCompletionsConfig(BaseModel):
messages: Messages
model: str
provider: Optional[str] = None
stream: bool = False
temperature: Optional[float] = None
max_tokens: Optional[int] = None
stop: Union[list[str], str, None] = None
api_key: Optional[str] = None
web_search: Optional[bool] = None
proxy: Optional[str] = None
conversation_id: str = None
class ImageGenerationConfig(BaseModel):
prompt: str
model: Optional[str] = None
provider: Optional[str] = None
response_format: str = "url"
api_key: Optional[str] = None
proxy: Optional[str] = None
class ProviderResponseModel(BaseModel):
id: str
object: str = "provider"
created: int
owned_by: Optional[str]
class ModelResponseModel(BaseModel):
id: str
object: str = "model"
created: int
owned_by: Optional[str]
class AppConfig:
ignored_providers: Optional[list[str]] = None
g4f_api_key: Optional[str] = None
ignore_cookie_files: bool = False
model: str = None,
provider: str = None
image_provider: str = None
proxy: str = None
@classmethod
def set_config(cls, **data):
for key, value in data.items():
setattr(cls, key, value)
list_ignored_providers: list[str] = None
def set_list_ignored_providers(ignored: list[str]):
global list_ignored_providers
list_ignored_providers = ignored
class Api:
def __init__(self, app: FastAPI, g4f_api_key=None) -> None:
self.app = app
self.client = AsyncClient()
self.g4f_api_key = g4f_api_key
self.get_g4f_api_key = APIKeyHeader(name="g4f-api-key")
self.conversations: dict[str, dict[str, BaseConversation]] = {}
def register_authorization(self):
@self.app.middleware("http")
async def authorization(request: Request, call_next):
if self.g4f_api_key and request.url.path not in ("/", "/v1"):
try:
user_g4f_api_key = await self.get_g4f_api_key(request)
except HTTPException as e:
if e.status_code == 403:
return JSONResponse(
status_code=HTTP_401_UNAUTHORIZED,
content=jsonable_encoder({"detail": "G4F API key required"}),
)
if not secrets.compare_digest(self.g4f_api_key, user_g4f_api_key):
return JSONResponse(
status_code=HTTP_403_FORBIDDEN,
content=jsonable_encoder({"detail": "Invalid G4F API key"}),
)
return await call_next(request)
def register_validation_exception_handler(self):
@self.app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
details = exc.errors()
modified_details = []
for error in details:
modified_details.append({
"loc": error["loc"],
"message": error["msg"],
"type": error["type"],
})
return JSONResponse(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": modified_details}),
)
def register_routes(self):
@self.app.get("/")
async def read_root():
return RedirectResponse("/v1", 302)
@self.app.get("/v1")
async def read_root_v1():
return HTMLResponse('g4f API: Go to '
'<a href="/v1/models">models</a>, '
'<a href="/v1/chat/completions">chat/completions</a>, or '
'<a href="/v1/images/generate">images/generate</a> <br><br>'
'Open Swagger UI at: '
'<a href="/docs">/docs</a>')
@self.app.get("/v1/models")
async def models() -> list[ModelResponseModel]:
model_list = dict(
(model, g4f.models.ModelUtils.convert[model])
for model in g4f.Model.__all__()
)
return [{
'id': model_id,
'object': 'model',
'created': 0,
'owned_by': model.base_provider
} for model_id, model in model_list.items()]
@self.app.get("/v1/models/{model_name}")
async def model_info(model_name: str):
if model_name in g4f.models.ModelUtils.convert:
model_info = g4f.models.ModelUtils.convert[model_name]
return JSONResponse({
'id': model_name,
'object': 'model',
'created': 0,
'owned_by': model_info.base_provider
})
return JSONResponse({"error": "The model does not exist."}, 404)
@self.app.post("/v1/chat/completions")
async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None):
try:
config.provider = provider if config.provider is None else config.provider
if config.provider is None:
config.provider = AppConfig.provider
if config.api_key is None and request is not None:
auth_header = request.headers.get("Authorization")
if auth_header is not None:
api_key = auth_header.split(None, 1)[-1]
if api_key and api_key != "Bearer":
config.api_key = api_key
conversation = return_conversation = None
if config.conversation_id is not None and config.provider is not None:
return_conversation = True
if config.conversation_id in self.conversations:
if config.provider in self.conversations[config.conversation_id]:
conversation = self.conversations[config.conversation_id][config.provider]
# Create the completion response
response = self.client.chat.completions.create(
**filter_none(
**{
"model": AppConfig.model,
"provider": AppConfig.provider,
"proxy": AppConfig.proxy,
**config.dict(exclude_none=True),
**{
"conversation_id": None,
"return_conversation": return_conversation,
"conversation": conversation
}
},
ignored=AppConfig.ignored_providers
),
)
if not config.stream:
response: ChatCompletion = await response
return JSONResponse(response.to_json())
async def streaming():
try:
async for chunk in response:
if isinstance(chunk, BaseConversation):
if config.conversation_id is not None and config.provider is not None:
if config.conversation_id not in self.conversations:
self.conversations[config.conversation_id] = {}
self.conversations[config.conversation_id][config.provider] = chunk
else:
yield f"data: {json.dumps(chunk.to_json())}\n\n"
except GeneratorExit:
pass
except Exception as e:
logger.exception(e)
yield f'data: {format_exception(e, config)}\n\n'
yield "data: [DONE]\n\n"
return StreamingResponse(streaming(), media_type="text/event-stream")
except Exception as e:
logger.exception(e)
return Response(content=format_exception(e, config), status_code=500, media_type="application/json")
@self.app.post("/v1/images/generate")
@self.app.post("/v1/images/generations")
async def generate_image(config: ImageGenerationConfig, request: Request):
if config.api_key is None:
auth_header = request.headers.get("Authorization")
if auth_header is not None:
api_key = auth_header.split(None, 1)[-1]
if api_key and api_key != "Bearer":
config.api_key = api_key
try:
response = await self.client.images.generate(
prompt=config.prompt,
model=config.model,
provider=AppConfig.image_provider if config.provider is None else config.provider,
**filter_none(
response_format = config.response_format,
api_key = config.api_key,
proxy = config.proxy
)
)
for image in response.data:
if hasattr(image, "url") and image.url.startswith("/"):
image.url = f"{request.base_url}{image.url.lstrip('/')}"
return JSONResponse(response.to_json())
except Exception as e:
logger.exception(e)
return Response(content=format_exception(e, config, True), status_code=500, media_type="application/json")
@self.app.get("/v1/providers")
async def providers() -> list[ProviderResponseModel]:
return [{
'id': provider.__name__,
'object': 'provider',
'created': 0,
'url': provider.url,
'label': getattr(provider, "label", None),
} for provider in __providers__ if provider.working]
@self.app.get("/v1/providers/{provider}")
async def providers_info(provider: str) -> ProviderResponseModel:
if provider not in ProviderUtils.convert:
return JSONResponse({"error": "The provider does not exist."}, 404)
provider: ProviderType = ProviderUtils.convert[provider]
def safe_get_models(provider: ProviderType) -> list[str]:
try:
return provider.get_models() if hasattr(provider, "get_models") else []
except:
return []
return {
'id': provider.__name__,
'object': 'provider',
'created': 0,
'url': provider.url,
'label': getattr(provider, "label", None),
'models': safe_get_models(provider),
'image_models': getattr(provider, "image_models", []) or [],
'vision_models': [model for model in [getattr(provider, "default_vision_model", None)] if model],
'params': [*provider.get_parameters()] if hasattr(provider, "get_parameters") else []
}
@self.app.post("/v1/upload_cookies")
def upload_cookies(files: List[UploadFile]):
response_data = []
for file in files:
try:
if file and file.filename.endswith(".json") or file.filename.endswith(".har"):
filename = os.path.basename(file.filename)
with open(os.path.join(get_cookies_dir(), filename), 'wb') as f:
shutil.copyfileobj(file.file, f)
response_data.append({"filename": filename})
finally:
file.file.close()
return response_data
@self.app.get("/v1/synthesize/{provider}")
async def synthesize(request: Request, provider: str):
try:
provider_handler = convert_to_provider(provider)
except ProviderNotFoundError:
return Response("Provider not found", 404)
if not hasattr(provider_handler, "synthesize"):
return Response("Provider doesn't support synthesize", 500)
if len(request.query_params) == 0:
return Response("Missing query params", 500)
response_data = provider_handler.synthesize({**request.query_params})
content_type = getattr(provider_handler, "synthesize_content_type", "application/octet-stream")
return StreamingResponse(response_data, media_type=content_type)
@self.app.get("/images/{filename}")
async def get_image(filename) -> FileResponse:
target = os.path.join(images_dir, filename)
if not os.path.isfile(target):
return Response(status_code=404)
with open(target, "rb") as f:
content_type = is_accepted_format(f.read(12))
return FileResponse(target, media_type=content_type)
@self.app.get("/providers")
async def providers():
model_list = [{
'id': provider.__name__,
'object': 'provider',
'created': 0,
'url': provider.url,
'label': getattr(provider, "label", None),
} for provider in __providers__ if provider.working]
return JSONResponse(model_list)
@self.app.get("/providers/{provider}")
async def providers_info(provider: str):
if provider not in ProviderUtils.convert:
return JSONResponse({"error": "The model does not exist."}, 404)
provider: ProviderType = ProviderUtils.convert[provider]
def safe_get_models(provider: ProviderType) -> list[str]:
try:
return provider.get_models() if hasattr(provider, "get_models") else []
except:
return []
provider_info = {
'id': provider.__name__,
'object': 'provider',
'created': 0,
'url': provider.url,
'label': getattr(provider, "label", None),
'models': safe_get_models(provider),
'image_models': getattr(provider, "image_models", []) or [],
'vision_models': [model for model in [getattr(provider, "default_vision_model", None)] if model],
'params': [*provider.get_parameters()] if hasattr(provider, "get_parameters") else []
}
return JSONResponse(provider_info)
def format_exception(e: Exception, config: Union[ChatCompletionsConfig, ImageGenerationConfig], image: bool = False) -> str:
last_provider = {} if not image else g4f.get_last_provider(True)
provider = (AppConfig.image_provider if image else AppConfig.provider) if config.provider is None else config.provider
model = AppConfig.model if config.model is None else config.model
return json.dumps({
"error": {"message": f"{e.__class__.__name__}: {e}"},
"model": last_provider.get("model") if model is None else model,
**filter_none(
provider=last_provider.get("name") if provider is None else provider
)
})
def run_api(
host: str = '0.0.0.0',
port: int = 1337,
bind: str = None,
debug: bool = False,
workers: int = None,
use_colors: bool = None,
reload: bool = False
) -> None:
print(f'Starting server... [g4f v-{g4f.version.utils.current_version}]' + (" (debug)" if debug else ""))
if use_colors is None:
use_colors = debug
if bind is not None:
host, port = bind.split(":")
uvicorn.run(
f"g4f.api:create_app{'_debug' if debug else ''}",
host=host,
port=int(port),
workers=workers,
use_colors=use_colors,
factory=True,
reload=reload
)
|