refactor(g4f/api/__init__.py): refactor API structure and improve async handling

This commit is contained in:
kqlio67 2024-10-25 19:39:12 +03:00
parent 04386f7b4b
commit 0a3565f215

View File

@ -14,17 +14,18 @@ from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY, HTTP_401_UNAUTHORIZE
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel from pydantic import BaseModel
from typing import Union, Optional from typing import Union, Optional, Iterator
import g4f import g4f
import g4f.debug import g4f.debug
from g4f.client import Client from g4f.client import Client, ChatCompletion, ChatCompletionChunk, ImagesResponse
from g4f.typing import Messages from g4f.typing import Messages
from g4f.cookies import read_cookie_files from g4f.cookies import read_cookie_files
def create_app(): def create_app(g4f_api_key: str = None):
app = FastAPI() app = FastAPI()
api = Api(app)
# Add CORS middleware
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origin_regex=".*", allow_origin_regex=".*",
@ -32,18 +33,19 @@ def create_app():
allow_methods=["*"], allow_methods=["*"],
allow_headers=["*"], allow_headers=["*"],
) )
api = Api(app, g4f_api_key=g4f_api_key)
api.register_routes() api.register_routes()
api.register_authorization() api.register_authorization()
api.register_validation_exception_handler() api.register_validation_exception_handler()
# Read cookie files if not ignored
if not AppConfig.ignore_cookie_files: if not AppConfig.ignore_cookie_files:
read_cookie_files() read_cookie_files()
return app return app
def create_app_debug(): class ChatCompletionsConfig(BaseModel):
g4f.debug.logging = True
return create_app()
class ChatCompletionsForm(BaseModel):
messages: Messages messages: Messages
model: str model: str
provider: Optional[str] = None provider: Optional[str] = None
@ -55,15 +57,12 @@ class ChatCompletionsForm(BaseModel):
web_search: Optional[bool] = None web_search: Optional[bool] = None
proxy: Optional[str] = None proxy: Optional[str] = None
class ImagesGenerateForm(BaseModel): class ImageGenerationConfig(BaseModel):
model: Optional[str] = None
provider: Optional[str] = None
prompt: str prompt: str
response_format: Optional[str] = None model: Optional[str] = None
api_key: Optional[str] = None response_format: str = "url"
proxy: Optional[str] = None
class AppConfig(): class AppConfig:
ignored_providers: Optional[list[str]] = None ignored_providers: Optional[list[str]] = None
g4f_api_key: Optional[str] = None g4f_api_key: Optional[str] = None
ignore_cookie_files: bool = False ignore_cookie_files: bool = False
@ -74,16 +73,23 @@ class AppConfig():
for key, value in data.items(): for key, value in data.items():
setattr(cls, key, value) 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: class Api:
def __init__(self, app: FastAPI) -> None: def __init__(self, app: FastAPI, g4f_api_key=None) -> None:
self.app = app self.app = app
self.client = Client() self.client = Client()
self.g4f_api_key = g4f_api_key
self.get_g4f_api_key = APIKeyHeader(name="g4f-api-key") self.get_g4f_api_key = APIKeyHeader(name="g4f-api-key")
def register_authorization(self): def register_authorization(self):
@self.app.middleware("http") @self.app.middleware("http")
async def authorization(request: Request, call_next): async def authorization(request: Request, call_next):
if AppConfig.g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]: if self.g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions", "/v1/images/generate"]:
try: try:
user_g4f_api_key = await self.get_g4f_api_key(request) user_g4f_api_key = await self.get_g4f_api_key(request)
except HTTPException as e: except HTTPException as e:
@ -92,22 +98,26 @@ class Api:
status_code=HTTP_401_UNAUTHORIZED, status_code=HTTP_401_UNAUTHORIZED,
content=jsonable_encoder({"detail": "G4F API key required"}), content=jsonable_encoder({"detail": "G4F API key required"}),
) )
if not secrets.compare_digest(AppConfig.g4f_api_key, user_g4f_api_key): if not secrets.compare_digest(self.g4f_api_key, user_g4f_api_key):
return JSONResponse( return JSONResponse(
status_code=HTTP_403_FORBIDDEN, status_code=HTTP_403_FORBIDDEN,
content=jsonable_encoder({"detail": "Invalid G4F API key"}), content=jsonable_encoder({"detail": "Invalid G4F API key"}),
) )
return await call_next(request)
response = await call_next(request)
return response
def register_validation_exception_handler(self): def register_validation_exception_handler(self):
@self.app.exception_handler(RequestValidationError) @self.app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError): async def validation_exception_handler(request: Request, exc: RequestValidationError):
details = exc.errors() details = exc.errors()
modified_details = [{ modified_details = []
"loc": error["loc"], for error in details:
"message": error["msg"], modified_details.append({
"type": error["type"], "loc": error["loc"],
} for error in details] "message": error["msg"],
"type": error["type"],
})
return JSONResponse( return JSONResponse(
status_code=HTTP_422_UNPROCESSABLE_ENTITY, status_code=HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": modified_details}), content=jsonable_encoder({"detail": modified_details}),
@ -121,25 +131,23 @@ class Api:
@self.app.get("/v1") @self.app.get("/v1")
async def read_root_v1(): async def read_root_v1():
return HTMLResponse('g4f API: Go to ' return HTMLResponse('g4f API: Go to '
'<a href="/v1/chat/completions">chat/completions</a> ' '<a href="/v1/chat/completions">chat/completions</a>, '
'or <a href="/v1/models">models</a>.') '<a href="/v1/models">models</a>, or '
'<a href="/v1/images/generate">images/generate</a>.')
@self.app.get("/v1/models") @self.app.get("/v1/models")
async def models(): async def models():
model_list = { model_list = dict(
model: g4f.models.ModelUtils.convert[model] (model, g4f.models.ModelUtils.convert[model])
for model in g4f.Model.__all__() for model in g4f.Model.__all__()
} )
model_list = [{ model_list = [{
'id': model_id, 'id': model_id,
'object': 'model', 'object': 'model',
'created': 0, 'created': 0,
'owned_by': model.base_provider 'owned_by': model.base_provider
} for model_id, model in model_list.items()] } for model_id, model in model_list.items()]
return JSONResponse({ return JSONResponse(model_list)
"object": "list",
"data": model_list,
})
@self.app.get("/v1/models/{model_name}") @self.app.get("/v1/models/{model_name}")
async def model_info(model_name: str): async def model_info(model_name: str):
@ -155,7 +163,7 @@ class Api:
return JSONResponse({"error": "The model does not exist."}) return JSONResponse({"error": "The model does not exist."})
@self.app.post("/v1/chat/completions") @self.app.post("/v1/chat/completions")
async def chat_completions(config: ChatCompletionsForm, request: Request = None, provider: str = None): async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None):
try: try:
config.provider = provider if config.provider is None else config.provider config.provider = provider if config.provider is None else config.provider
if config.api_key is None and request is not None: if config.api_key is None and request is not None:
@ -164,17 +172,27 @@ class Api:
auth_header = auth_header.split(None, 1)[-1] auth_header = auth_header.split(None, 1)[-1]
if auth_header and auth_header != "Bearer": if auth_header and auth_header != "Bearer":
config.api_key = auth_header config.api_key = auth_header
# Use the asynchronous create method and await it
response = await self.client.chat.completions.async_create( # Create the completion response
response = self.client.chat.completions.create(
**{ **{
**AppConfig.defaults, **AppConfig.defaults,
**config.dict(exclude_none=True), **config.dict(exclude_none=True),
}, },
ignored=AppConfig.ignored_providers ignored=AppConfig.ignored_providers
) )
if not config.stream:
# Check if the response is synchronous or asynchronous
if isinstance(response, ChatCompletion):
# Synchronous response
return JSONResponse(response.to_json()) return JSONResponse(response.to_json())
if not config.stream:
# If the response is an iterator but not streaming, collect the result
response_list = list(response) if isinstance(response, Iterator) else [response]
return JSONResponse(response_list[0].to_json())
# Streaming response
async def streaming(): async def streaming():
try: try:
async for chunk in response: async for chunk in response:
@ -185,41 +203,38 @@ class Api:
logging.exception(e) logging.exception(e)
yield f'data: {format_exception(e, config)}\n\n' yield f'data: {format_exception(e, config)}\n\n'
yield "data: [DONE]\n\n" yield "data: [DONE]\n\n"
return StreamingResponse(streaming(), media_type="text/event-stream") return StreamingResponse(streaming(), media_type="text/event-stream")
except Exception as e: except Exception as e:
logging.exception(e) logging.exception(e)
return Response(content=format_exception(e, config), status_code=500, media_type="application/json") return Response(content=format_exception(e, config), status_code=500, media_type="application/json")
@self.app.post("/v1/images/generate")
async def generate_image(config: ImageGenerationConfig):
try:
response: ImagesResponse = await self.client.images.async_generate(
prompt=config.prompt,
model=config.model,
response_format=config.response_format
)
# Convert Image objects to dictionaries
response_data = [image.to_dict() for image in response.data]
return JSONResponse({"data": response_data})
except Exception as e:
logging.exception(e)
return Response(content=format_exception(e, config), status_code=500, media_type="application/json")
@self.app.post("/v1/completions") @self.app.post("/v1/completions")
async def completions(): async def completions():
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json") return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
@self.app.post("/v1/images/generations") def format_exception(e: Exception, config: Union[ChatCompletionsConfig, ImageGenerationConfig]) -> str:
async def images_generate(config: ImagesGenerateForm, request: Request = None, provider: str = None):
try:
config.provider = provider if config.provider is None else config.provider
if config.api_key is None and request is not None:
auth_header = request.headers.get("Authorization")
if auth_header is not None:
auth_header = auth_header.split(None, 1)[-1]
if auth_header and auth_header != "Bearer":
config.api_key = auth_header
# Use the asynchronous generate method and await it
response = await self.client.images.async_generate(
**config.dict(exclude_none=True),
)
return JSONResponse(response.to_json())
except Exception as e:
logging.exception(e)
return Response(content=format_exception(e, config), status_code=500, media_type="application/json")
def format_exception(e: Exception, config: ChatCompletionsForm) -> str:
last_provider = g4f.get_last_provider(True) last_provider = g4f.get_last_provider(True)
return json.dumps({ return json.dumps({
"error": {"message": f"{e.__class__.__name__}: {e}"}, "error": {"message": f"{e.__class__.__name__}: {e}"},
"model": last_provider.get("model") if last_provider else config.model, "model": last_provider.get("model") if last_provider else getattr(config, 'model', None),
"provider": last_provider.get("name") if last_provider else config.provider "provider": last_provider.get("name") if last_provider else getattr(config, 'provider', None)
}) })
def run_api( def run_api(
@ -228,18 +243,22 @@ def run_api(
bind: str = None, bind: str = None,
debug: bool = False, debug: bool = False,
workers: int = None, workers: int = None,
use_colors: bool = None use_colors: bool = None,
g4f_api_key: str = None
) -> None: ) -> None:
print(f'Starting server... [g4f v-{g4f.version.utils.current_version}]' + (" (debug)" if debug else "")) print(f'Starting server... [g4f v-{g4f.version.utils.current_version}]' + (" (debug)" if debug else ""))
if use_colors is None: if use_colors is None:
use_colors = debug use_colors = debug
if bind is not None: if bind is not None:
host, port = bind.split(":") host, port = bind.split(":")
if debug:
g4f.debug.logging = True
uvicorn.run( uvicorn.run(
f"g4f.api:create_app{'_debug' if debug else ''}", "g4f.api:create_app",
host=host, port=int(port), host=host,
workers=workers, port=int(port),
use_colors=use_colors, workers=workers,
factory=True, use_colors=use_colors,
factory=True,
reload=debug reload=debug
) )