2024-04-21 23:39:00 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-13 09:17:13 +03:00
|
|
|
import logging
|
2023-10-12 04:35:11 +03:00
|
|
|
import json
|
2023-11-02 04:27:35 +03:00
|
|
|
import uvicorn
|
2024-04-28 12:02:44 +03:00
|
|
|
import secrets
|
2024-01-01 19:48:57 +03:00
|
|
|
|
2024-04-07 11:36:13 +03:00
|
|
|
from fastapi import FastAPI, Response, Request
|
2024-02-23 04:35:13 +03:00
|
|
|
from fastapi.responses import StreamingResponse, RedirectResponse, HTMLResponse, JSONResponse
|
2024-04-07 11:36:13 +03:00
|
|
|
from fastapi.exceptions import RequestValidationError
|
2024-04-28 12:02:44 +03:00
|
|
|
from fastapi.security import APIKeyHeader
|
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY, HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN
|
2024-04-07 11:36:13 +03:00
|
|
|
from fastapi.encoders import jsonable_encoder
|
2024-10-17 12:44:01 +03:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2024-04-07 11:36:13 +03:00
|
|
|
from pydantic import BaseModel
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
from typing import Union, Optional
|
2024-01-01 19:48:57 +03:00
|
|
|
|
2023-11-05 00:16:09 +03:00
|
|
|
import g4f
|
2024-02-23 04:35:13 +03:00
|
|
|
import g4f.debug
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
from g4f.client import AsyncClient, ChatCompletion
|
|
|
|
from g4f.client.helper import filter_none
|
2024-02-23 04:35:13 +03:00
|
|
|
from g4f.typing import Messages
|
2024-04-29 21:21:47 +03:00
|
|
|
from g4f.cookies import read_cookie_files
|
2024-02-23 04:35:13 +03:00
|
|
|
|
2024-11-15 09:38:51 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
def create_app(g4f_api_key: str = None):
|
2024-04-21 23:39:00 +03:00
|
|
|
app = FastAPI()
|
2024-10-25 19:39:12 +03:00
|
|
|
|
|
|
|
# Add CORS middleware
|
2024-10-17 12:44:01 +03:00
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origin_regex=".*",
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
2024-10-25 19:39:12 +03:00
|
|
|
|
|
|
|
api = Api(app, g4f_api_key=g4f_api_key)
|
2024-04-21 23:39:00 +03:00
|
|
|
api.register_routes()
|
2024-04-28 12:02:44 +03:00
|
|
|
api.register_authorization()
|
2024-04-21 23:39:00 +03:00
|
|
|
api.register_validation_exception_handler()
|
2024-10-25 19:39:12 +03:00
|
|
|
|
|
|
|
# Read cookie files if not ignored
|
2024-04-29 21:21:47 +03:00
|
|
|
if not AppConfig.ignore_cookie_files:
|
|
|
|
read_cookie_files()
|
2024-04-20 16:41:49 +03:00
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
return app
|
2024-04-29 17:56:56 +03:00
|
|
|
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
def create_app_debug(g4f_api_key: str = None):
|
|
|
|
g4f.debug.logging = True
|
|
|
|
return create_app(g4f_api_key)
|
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
class ChatCompletionsConfig(BaseModel):
|
2024-02-23 04:35:13 +03:00
|
|
|
messages: Messages
|
|
|
|
model: str
|
2024-04-18 21:18:51 +03:00
|
|
|
provider: Optional[str] = None
|
2024-02-23 04:35:13 +03:00
|
|
|
stream: bool = False
|
2024-04-18 21:18:51 +03:00
|
|
|
temperature: Optional[float] = None
|
|
|
|
max_tokens: Optional[int] = None
|
2024-02-23 21:30:53 +03:00
|
|
|
stop: Union[list[str], str, None] = None
|
2024-04-18 21:18:51 +03:00
|
|
|
api_key: Optional[str] = None
|
|
|
|
web_search: Optional[bool] = None
|
2024-04-20 16:41:49 +03:00
|
|
|
proxy: Optional[str] = None
|
2023-11-02 04:27:35 +03:00
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
class ImageGenerationConfig(BaseModel):
|
2024-05-18 08:37:37 +03:00
|
|
|
prompt: str
|
2024-10-25 19:39:12 +03:00
|
|
|
model: Optional[str] = None
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
provider: Optional[str] = None
|
2024-10-25 19:39:12 +03:00
|
|
|
response_format: str = "url"
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
api_key: Optional[str] = None
|
|
|
|
proxy: Optional[str] = None
|
2024-05-18 08:37:37 +03:00
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
class AppConfig:
|
2024-09-26 20:27:57 +03:00
|
|
|
ignored_providers: Optional[list[str]] = None
|
2024-04-29 21:21:47 +03:00
|
|
|
g4f_api_key: Optional[str] = None
|
|
|
|
ignore_cookie_files: bool = False
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
model: str = None,
|
|
|
|
provider: str = None
|
|
|
|
image_provider: str = None
|
|
|
|
proxy: str = None
|
2024-04-21 23:39:00 +03:00
|
|
|
|
2024-04-29 21:21:47 +03:00
|
|
|
@classmethod
|
2024-05-06 09:16:49 +03:00
|
|
|
def set_config(cls, **data):
|
|
|
|
for key, value in data.items():
|
|
|
|
setattr(cls, key, value)
|
2024-04-29 17:56:56 +03:00
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
list_ignored_providers: list[str] = None
|
|
|
|
|
|
|
|
def set_list_ignored_providers(ignored: list[str]):
|
|
|
|
global list_ignored_providers
|
|
|
|
list_ignored_providers = ignored
|
|
|
|
|
2023-11-05 00:16:09 +03:00
|
|
|
class Api:
|
2024-10-25 19:39:12 +03:00
|
|
|
def __init__(self, app: FastAPI, g4f_api_key=None) -> None:
|
2024-04-21 23:39:00 +03:00
|
|
|
self.app = app
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
self.client = AsyncClient()
|
2024-10-25 19:39:12 +03:00
|
|
|
self.g4f_api_key = g4f_api_key
|
2024-04-28 12:02:44 +03:00
|
|
|
self.get_g4f_api_key = APIKeyHeader(name="g4f-api-key")
|
|
|
|
|
|
|
|
def register_authorization(self):
|
|
|
|
@self.app.middleware("http")
|
|
|
|
async def authorization(request: Request, call_next):
|
2024-10-25 19:39:12 +03:00
|
|
|
if self.g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions", "/v1/images/generate"]:
|
2024-04-28 12:02:44 +03:00
|
|
|
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"}),
|
|
|
|
)
|
2024-10-25 19:39:12 +03:00
|
|
|
if not secrets.compare_digest(self.g4f_api_key, user_g4f_api_key):
|
2024-04-28 12:02:44 +03:00
|
|
|
return JSONResponse(
|
2024-04-29 17:56:56 +03:00
|
|
|
status_code=HTTP_403_FORBIDDEN,
|
|
|
|
content=jsonable_encoder({"detail": "Invalid G4F API key"}),
|
|
|
|
)
|
2024-10-25 19:39:12 +03:00
|
|
|
|
|
|
|
response = await call_next(request)
|
|
|
|
return response
|
2024-04-07 11:36:13 +03:00
|
|
|
|
|
|
|
def register_validation_exception_handler(self):
|
2024-04-21 23:39:00 +03:00
|
|
|
@self.app.exception_handler(RequestValidationError)
|
2024-04-07 11:36:13 +03:00
|
|
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
|
|
|
details = exc.errors()
|
2024-10-25 19:39:12 +03:00
|
|
|
modified_details = []
|
|
|
|
for error in details:
|
|
|
|
modified_details.append({
|
|
|
|
"loc": error["loc"],
|
|
|
|
"message": error["msg"],
|
|
|
|
"type": error["type"],
|
|
|
|
})
|
2024-04-07 11:36:13 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
content=jsonable_encoder({"detail": modified_details}),
|
|
|
|
)
|
2023-11-05 00:16:09 +03:00
|
|
|
|
2024-04-20 16:41:49 +03:00
|
|
|
def register_routes(self):
|
2024-04-21 23:39:00 +03:00
|
|
|
@self.app.get("/")
|
2023-11-05 00:16:09 +03:00
|
|
|
async def read_root():
|
2024-02-23 04:35:13 +03:00
|
|
|
return RedirectResponse("/v1", 302)
|
2023-11-05 00:16:09 +03:00
|
|
|
|
2024-04-21 23:39:00 +03:00
|
|
|
@self.app.get("/v1")
|
2023-11-05 00:16:09 +03:00
|
|
|
async def read_root_v1():
|
2024-02-23 04:35:13 +03:00
|
|
|
return HTMLResponse('g4f API: Go to '
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
'<a href="/v1/models">models</a>, '
|
|
|
|
'<a href="/v1/chat/completions">chat/completions</a>, or '
|
2024-10-25 19:39:12 +03:00
|
|
|
'<a href="/v1/images/generate">images/generate</a>.')
|
2023-11-05 00:16:09 +03:00
|
|
|
|
2024-04-21 23:39:00 +03:00
|
|
|
@self.app.get("/v1/models")
|
2023-11-05 00:16:09 +03:00
|
|
|
async def models():
|
2024-10-25 19:39:12 +03:00
|
|
|
model_list = dict(
|
|
|
|
(model, g4f.models.ModelUtils.convert[model])
|
2024-02-23 04:35:13 +03:00
|
|
|
for model in g4f.Model.__all__()
|
2024-10-25 19:39:12 +03:00
|
|
|
)
|
2024-02-23 04:35:13 +03:00
|
|
|
model_list = [{
|
|
|
|
'id': model_id,
|
2023-11-05 00:16:09 +03:00
|
|
|
'object': 'model',
|
|
|
|
'created': 0,
|
2024-02-23 04:35:13 +03:00
|
|
|
'owned_by': model.base_provider
|
|
|
|
} for model_id, model in model_list.items()]
|
2024-10-25 19:39:12 +03:00
|
|
|
return JSONResponse(model_list)
|
2023-11-05 00:16:09 +03:00
|
|
|
|
2024-04-21 23:39:00 +03:00
|
|
|
@self.app.get("/v1/models/{model_name}")
|
2023-11-05 00:16:09 +03:00
|
|
|
async def model_info(model_name: str):
|
|
|
|
try:
|
2024-04-18 21:18:51 +03:00
|
|
|
model_info = g4f.models.ModelUtils.convert[model_name]
|
2024-02-23 04:35:13 +03:00
|
|
|
return JSONResponse({
|
2023-11-05 00:16:09 +03:00
|
|
|
'id': model_name,
|
|
|
|
'object': 'model',
|
|
|
|
'created': 0,
|
|
|
|
'owned_by': model_info.base_provider
|
2024-02-23 04:35:13 +03:00
|
|
|
})
|
2023-11-05 00:16:09 +03:00
|
|
|
except:
|
2024-02-23 04:35:13 +03:00
|
|
|
return JSONResponse({"error": "The model does not exist."})
|
2023-11-05 00:16:09 +03:00
|
|
|
|
2024-04-21 23:39:00 +03:00
|
|
|
@self.app.post("/v1/chat/completions")
|
2024-10-25 19:39:12 +03:00
|
|
|
async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None):
|
2023-11-05 00:16:09 +03:00
|
|
|
try:
|
2024-02-23 04:35:13 +03:00
|
|
|
config.provider = provider if config.provider is None else config.provider
|
2024-02-23 13:33:38 +03:00
|
|
|
if config.api_key is None and request is not None:
|
2024-02-23 04:35:13 +03:00
|
|
|
auth_header = request.headers.get("Authorization")
|
|
|
|
if auth_header is not None:
|
2024-02-29 16:44:51 +03:00
|
|
|
auth_header = auth_header.split(None, 1)[-1]
|
|
|
|
if auth_header and auth_header != "Bearer":
|
|
|
|
config.api_key = auth_header
|
2024-10-25 19:39:12 +03:00
|
|
|
|
|
|
|
# Create the completion response
|
|
|
|
response = self.client.chat.completions.create(
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
**filter_none(
|
|
|
|
**{
|
|
|
|
"model": AppConfig.model,
|
|
|
|
"provider": AppConfig.provider,
|
|
|
|
"proxy": AppConfig.proxy,
|
|
|
|
**config.dict(exclude_none=True),
|
|
|
|
},
|
|
|
|
ignored=AppConfig.ignored_providers
|
|
|
|
),
|
2023-12-23 22:50:56 +03:00
|
|
|
)
|
2024-10-25 19:39:12 +03:00
|
|
|
|
|
|
|
if not config.stream:
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
response: ChatCompletion = await response
|
|
|
|
return JSONResponse(response.to_json())
|
2024-10-31 01:34:49 +03:00
|
|
|
|
2024-05-18 08:37:37 +03:00
|
|
|
async def streaming():
|
|
|
|
try:
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
async for chunk in response:
|
2024-05-18 08:37:37 +03:00
|
|
|
yield f"data: {json.dumps(chunk.to_json())}\n\n"
|
|
|
|
except GeneratorExit:
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
2024-11-15 09:38:51 +03:00
|
|
|
logger.exception(e)
|
2024-05-18 08:37:37 +03:00
|
|
|
yield f'data: {format_exception(e, config)}\n\n'
|
|
|
|
yield "data: [DONE]\n\n"
|
2024-10-25 19:39:12 +03:00
|
|
|
|
2024-05-18 08:37:37 +03:00
|
|
|
return StreamingResponse(streaming(), media_type="text/event-stream")
|
|
|
|
|
2023-11-13 09:17:13 +03:00
|
|
|
except Exception as e:
|
2024-11-15 09:38:51 +03:00
|
|
|
logger.exception(e)
|
2024-02-23 04:35:13 +03:00
|
|
|
return Response(content=format_exception(e, config), status_code=500, media_type="application/json")
|
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
@self.app.post("/v1/images/generate")
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
@self.app.post("/v1/images/generations")
|
2024-10-25 19:39:12 +03:00
|
|
|
async def generate_image(config: ImageGenerationConfig):
|
2024-05-18 08:37:37 +03:00
|
|
|
try:
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
response = await self.client.images.generate(
|
2024-10-25 19:39:12 +03:00
|
|
|
prompt=config.prompt,
|
|
|
|
model=config.model,
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
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
|
|
|
|
)
|
2024-05-18 08:37:37 +03:00
|
|
|
)
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
return JSONResponse(response.to_json())
|
2024-05-18 08:37:37 +03:00
|
|
|
except Exception as e:
|
2024-11-15 09:38:51 +03:00
|
|
|
logger.exception(e)
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
return Response(content=format_exception(e, config, True), status_code=500, media_type="application/json")
|
2023-11-02 04:27:35 +03:00
|
|
|
|
2024-10-25 19:39:12 +03:00
|
|
|
@self.app.post("/v1/completions")
|
|
|
|
async def completions():
|
|
|
|
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
|
|
|
|
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
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
|
2024-02-23 04:35:13 +03:00
|
|
|
return json.dumps({
|
2024-02-24 16:52:23 +03:00
|
|
|
"error": {"message": f"{e.__class__.__name__}: {e}"},
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
"model": last_provider.get("model") if model is None else model,
|
|
|
|
**filter_none(
|
|
|
|
provider=last_provider.get("name") if provider is None else provider
|
|
|
|
)
|
2024-02-23 13:33:38 +03:00
|
|
|
})
|
|
|
|
|
2024-04-20 16:41:49 +03:00
|
|
|
def run_api(
|
|
|
|
host: str = '0.0.0.0',
|
|
|
|
port: int = 1337,
|
|
|
|
bind: str = None,
|
|
|
|
debug: bool = False,
|
|
|
|
workers: int = None,
|
2024-10-25 19:39:12 +03:00
|
|
|
use_colors: bool = None,
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
reload: bool = False
|
2024-04-20 16:41:49 +03:00
|
|
|
) -> 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(":")
|
2024-04-29 17:56:56 +03:00
|
|
|
uvicorn.run(
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
f"g4f.api:create_app{'_debug' if debug else ''}",
|
2024-10-25 19:39:12 +03:00
|
|
|
host=host,
|
|
|
|
port=int(port),
|
|
|
|
workers=workers,
|
|
|
|
use_colors=use_colors,
|
|
|
|
factory=True,
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
reload=reload
|
|
|
|
)
|