summaryrefslogtreecommitdiffstats
path: root/g4f/api
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-29 20:21:47 +0200
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-29 20:21:47 +0200
commitff8c1fc140d78b2b5cfd4338d91e88d3323e185e (patch)
tree36b9fd4c06ce84a1df6981ce5ec20cf1f368a0e3 /g4f/api
parentFix workers argument in api (diff)
downloadgpt4free-ff8c1fc140d78b2b5cfd4338d91e88d3323e185e.tar
gpt4free-ff8c1fc140d78b2b5cfd4338d91e88d3323e185e.tar.gz
gpt4free-ff8c1fc140d78b2b5cfd4338d91e88d3323e185e.tar.bz2
gpt4free-ff8c1fc140d78b2b5cfd4338d91e88d3323e185e.tar.lz
gpt4free-ff8c1fc140d78b2b5cfd4338d91e88d3323e185e.tar.xz
gpt4free-ff8c1fc140d78b2b5cfd4338d91e88d3323e185e.tar.zst
gpt4free-ff8c1fc140d78b2b5cfd4338d91e88d3323e185e.zip
Diffstat (limited to 'g4f/api')
-rw-r--r--g4f/api/__init__.py46
1 files changed, 27 insertions, 19 deletions
diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py
index ec18ece5..f252ab71 100644
--- a/g4f/api/__init__.py
+++ b/g4f/api/__init__.py
@@ -19,6 +19,7 @@ import g4f
import g4f.debug
from g4f.client import AsyncClient
from g4f.typing import Messages
+from g4f.cookies import read_cookie_files
def create_app():
app = FastAPI()
@@ -26,13 +27,15 @@ def create_app():
api.register_routes()
api.register_authorization()
api.register_validation_exception_handler()
+ if not AppConfig.ignore_cookie_files:
+ read_cookie_files()
return app
-def create_debug_app():
+def create_app_debug():
g4f.debug.logging = True
return create_app()
-class ChatCompletionsConfig(BaseModel):
+class ChatCompletionsForm(BaseModel):
messages: Messages
model: str
provider: Optional[str] = None
@@ -44,17 +47,22 @@ class ChatCompletionsConfig(BaseModel):
web_search: Optional[bool] = None
proxy: Optional[str] = None
-list_ignored_providers: list[str] = None
+class AppConfig():
+ list_ignored_providers: Optional[list[str]] = None
+ g4f_api_key: Optional[str] = None
+ ignore_cookie_files: bool = False
-def set_list_ignored_providers(ignored: list[str]):
- global list_ignored_providers
- list_ignored_providers = ignored
+ @classmethod
+ def set_list_ignored_providers(cls, ignored: list[str]):
+ cls.list_ignored_providers = ignored
-g4f_api_key: str = None
+ @classmethod
+ def set_g4f_api_key(cls, key: str = None):
+ cls.g4f_api_key = key
-def set_g4f_api_key(key: str = None):
- global g4f_api_key
- g4f_api_key = key
+ @classmethod
+ def set_ignore_cookie_files(cls, value: bool):
+ cls.ignore_cookie_files = value
class Api:
def __init__(self, app: FastAPI) -> None:
@@ -65,7 +73,7 @@ class Api:
def register_authorization(self):
@self.app.middleware("http")
async def authorization(request: Request, call_next):
- if g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]:
+ if AppConfig.g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]:
try:
user_g4f_api_key = await self.get_g4f_api_key(request)
except HTTPException as e:
@@ -74,7 +82,7 @@ class Api:
status_code=HTTP_401_UNAUTHORIZED,
content=jsonable_encoder({"detail": "G4F API key required"}),
)
- if not secrets.compare_digest(g4f_api_key, user_g4f_api_key):
+ if not secrets.compare_digest(AppConfig.g4f_api_key, user_g4f_api_key):
return JSONResponse(
status_code=HTTP_403_FORBIDDEN,
content=jsonable_encoder({"detail": "Invalid G4F API key"}),
@@ -108,10 +116,10 @@ class Api:
@self.app.get("/v1/models")
async def models():
- model_list = dict(
- (model, g4f.models.ModelUtils.convert[model])
+ model_list = {
+ model: g4f.models.ModelUtils.convert[model]
for model in g4f.Model.__all__()
- )
+ }
model_list = [{
'id': model_id,
'object': 'model',
@@ -134,7 +142,7 @@ class Api:
return JSONResponse({"error": "The model does not exist."})
@self.app.post("/v1/chat/completions")
- async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None):
+ async def chat_completions(config: ChatCompletionsForm, 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:
@@ -145,7 +153,7 @@ class Api:
config.api_key = auth_header
response = self.client.chat.completions.create(
**config.dict(exclude_none=True),
- ignored=list_ignored_providers
+ ignored=AppConfig.list_ignored_providers
)
except Exception as e:
logging.exception(e)
@@ -171,7 +179,7 @@ class Api:
async def completions():
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
-def format_exception(e: Exception, config: ChatCompletionsConfig) -> str:
+def format_exception(e: Exception, config: ChatCompletionsForm) -> str:
last_provider = g4f.get_last_provider(True)
return json.dumps({
"error": {"message": f"{e.__class__.__name__}: {e}"},
@@ -193,7 +201,7 @@ def run_api(
if bind is not None:
host, port = bind.split(":")
uvicorn.run(
- f"g4f.api:{'create_debug_app' if debug else 'create_app'}",
+ f"g4f.api:{'create_app_debug' if debug else 'create_app'}",
host=host, port=int(port),
workers=workers,
use_colors=use_colors,