mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-06 02:30:41 -08:00
Fix workers argument in api
This commit is contained in:
parent
bfce3af7a5
commit
26d5fcd216
2 changed files with 39 additions and 28 deletions
|
|
@ -20,14 +20,18 @@ import g4f.debug
|
||||||
from g4f.client import AsyncClient
|
from g4f.client import AsyncClient
|
||||||
from g4f.typing import Messages
|
from g4f.typing import Messages
|
||||||
|
|
||||||
def create_app(g4f_api_key:str = None):
|
def create_app():
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
api = Api(app, g4f_api_key=g4f_api_key)
|
api = Api(app)
|
||||||
api.register_routes()
|
api.register_routes()
|
||||||
api.register_authorization()
|
api.register_authorization()
|
||||||
api.register_validation_exception_handler()
|
api.register_validation_exception_handler()
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
def create_debug_app():
|
||||||
|
g4f.debug.logging = True
|
||||||
|
return create_app()
|
||||||
|
|
||||||
class ChatCompletionsConfig(BaseModel):
|
class ChatCompletionsConfig(BaseModel):
|
||||||
messages: Messages
|
messages: Messages
|
||||||
model: str
|
model: str
|
||||||
|
|
@ -46,17 +50,22 @@ def set_list_ignored_providers(ignored: list[str]):
|
||||||
global list_ignored_providers
|
global list_ignored_providers
|
||||||
list_ignored_providers = ignored
|
list_ignored_providers = ignored
|
||||||
|
|
||||||
|
g4f_api_key: str = None
|
||||||
|
|
||||||
|
def set_g4f_api_key(key: str = None):
|
||||||
|
global g4f_api_key
|
||||||
|
g4f_api_key = key
|
||||||
|
|
||||||
class Api:
|
class Api:
|
||||||
def __init__(self, app: FastAPI, g4f_api_key=None) -> None:
|
def __init__(self, app: FastAPI) -> None:
|
||||||
self.app = app
|
self.app = app
|
||||||
self.client = AsyncClient()
|
self.client = AsyncClient()
|
||||||
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 self.g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]:
|
if g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]:
|
||||||
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:
|
||||||
|
|
@ -65,26 +74,22 @@ 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(self.g4f_api_key, user_g4f_api_key):
|
if not secrets.compare_digest(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 = [{
|
||||||
for error in details:
|
"loc": error["loc"],
|
||||||
modified_details.append({
|
"message": error["msg"],
|
||||||
"loc": error["loc"],
|
"type": error["type"],
|
||||||
"message": error["msg"],
|
} for error in details]
|
||||||
"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}),
|
||||||
|
|
@ -180,14 +185,18 @@ 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:
|
uvicorn.run(
|
||||||
g4f.debug.logging = True
|
f"g4f.api:{'create_debug_app' if debug else 'create_app'}",
|
||||||
uvicorn.run(create_app(g4f_api_key), host=host, port=int(port), workers=workers, use_colors=use_colors)
|
host=host, port=int(port),
|
||||||
|
workers=workers,
|
||||||
|
use_colors=use_colors,
|
||||||
|
factory=True,
|
||||||
|
reload=debug
|
||||||
|
)
|
||||||
10
g4f/cli.py
10
g4f/cli.py
|
|
@ -16,9 +16,9 @@ def main():
|
||||||
api_parser.add_argument("--workers", type=int, default=None, help="Number of workers.")
|
api_parser.add_argument("--workers", type=int, default=None, help="Number of workers.")
|
||||||
api_parser.add_argument("--disable-colors", action="store_true", help="Don't use colors.")
|
api_parser.add_argument("--disable-colors", action="store_true", help="Don't use colors.")
|
||||||
api_parser.add_argument("--ignore-cookie-files", action="store_true", help="Don't read .har and cookie files.")
|
api_parser.add_argument("--ignore-cookie-files", action="store_true", help="Don't read .har and cookie files.")
|
||||||
api_parser.add_argument("--g4f-api-key", type=str, default=None, help="Sets an authentication key for your API.")
|
api_parser.add_argument("--g4f-api-key", type=str, default=None, help="Sets an authentication key for your API. (incompatible with --debug and --workers)")
|
||||||
api_parser.add_argument("--ignored-providers", nargs="+", choices=[provider for provider in Provider.__map__],
|
api_parser.add_argument("--ignored-providers", nargs="+", choices=[provider.__name__ for provider in Provider.__providers__ if provider.working],
|
||||||
default=[], help="List of providers to ignore when processing request.")
|
default=[], help="List of providers to ignore when processing request. (incompatible with --debug and --workers)")
|
||||||
subparsers.add_parser("gui", parents=[gui_parser()], add_help=False)
|
subparsers.add_parser("gui", parents=[gui_parser()], add_help=False)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
@ -39,11 +39,13 @@ def run_api_args(args):
|
||||||
g4f.api.set_list_ignored_providers(
|
g4f.api.set_list_ignored_providers(
|
||||||
args.ignored_providers
|
args.ignored_providers
|
||||||
)
|
)
|
||||||
|
g4f.api.set_g4f_api_key(
|
||||||
|
args.g4f_api_key
|
||||||
|
)
|
||||||
g4f.api.run_api(
|
g4f.api.run_api(
|
||||||
bind=args.bind,
|
bind=args.bind,
|
||||||
debug=args.debug,
|
debug=args.debug,
|
||||||
workers=args.workers,
|
workers=args.workers,
|
||||||
g4f_api_key=args.g4f_api_key,
|
|
||||||
use_colors=not args.disable_colors
|
use_colors=not args.disable_colors
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue