jeff86 commited on
Commit
f6e820b
1 Parent(s): 0e0a169

Update run_app.py

Browse files
Files changed (1) hide show
  1. run_app.py +210 -2
run_app.py CHANGED
@@ -1,7 +1,215 @@
1
- import g4f.api
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  list_ignored_providers = [
4
  ]
5
 
6
  if __name__ == "__main__":
7
- g4f.api.run_api(debug=True)
 
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ import json
5
+ import uvicorn
6
+ import secrets
7
+
8
+ from fastapi import FastAPI, Response, Request
9
+ from fastapi.responses import StreamingResponse, RedirectResponse, HTMLResponse, JSONResponse
10
+ from fastapi.exceptions import RequestValidationError
11
+ from fastapi.security import APIKeyHeader
12
+ from starlette.exceptions import HTTPException
13
+ from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY, HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN
14
+ from fastapi.encoders import jsonable_encoder
15
+ from pydantic import BaseModel
16
+ from typing import Union, Optional
17
+
18
+ import g4f
19
+ import g4f.debug
20
+ from g4f.client import AsyncClient
21
+ from g4f.typing import Messages
22
+ from g4f.cookies import read_cookie_files
23
+
24
+ def create_app():
25
+ app = FastAPI()
26
+ api = Api(app)
27
+ api.register_routes()
28
+ api.register_authorization()
29
+ api.register_validation_exception_handler()
30
+ if not AppConfig.ignore_cookie_files:
31
+ read_cookie_files()
32
+ return app
33
+
34
+ def create_app_debug():
35
+ g4f.debug.logging = True
36
+ return create_app()
37
+
38
+ class ChatCompletionsForm(BaseModel):
39
+ messages: Messages
40
+ model: str
41
+ provider: Optional[str] = None
42
+ stream: bool = False
43
+ temperature: Optional[float] = None
44
+ max_tokens: Optional[int] = None
45
+ stop: Union[list[str], str, None] = None
46
+ api_key: Optional[str] = None
47
+ web_search: Optional[bool] = None
48
+ proxy: Optional[str] = None
49
+
50
+ class AppConfig():
51
+ list_ignored_providers: Optional[list[str]] = None
52
+ g4f_api_key: Optional[str] = None
53
+ ignore_cookie_files: bool = False
54
+ defaults: dict = {}
55
+
56
+ @classmethod
57
+ def set_config(cls, **data):
58
+ for key, value in data.items():
59
+ setattr(cls, key, value)
60
+
61
+ class Api:
62
+ def __init__(self, app: FastAPI) -> None:
63
+ self.app = app
64
+ self.client = AsyncClient()
65
+ self.get_g4f_api_key = APIKeyHeader(name="g4f-api-key")
66
+
67
+ def register_authorization(self):
68
+ @self.app.middleware("http")
69
+ async def authorization(request: Request, call_next):
70
+ if AppConfig.g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]:
71
+ try:
72
+ user_g4f_api_key = await self.get_g4f_api_key(request)
73
+ except HTTPException as e:
74
+ if e.status_code == 403:
75
+ return JSONResponse(
76
+ status_code=HTTP_401_UNAUTHORIZED,
77
+ content=jsonable_encoder({"detail": "G4F API key required"}),
78
+ )
79
+ if not secrets.compare_digest(AppConfig.g4f_api_key, user_g4f_api_key):
80
+ return JSONResponse(
81
+ status_code=HTTP_403_FORBIDDEN,
82
+ content=jsonable_encoder({"detail": "Invalid G4F API key"}),
83
+ )
84
+ return await call_next(request)
85
+
86
+ def register_validation_exception_handler(self):
87
+ @self.app.exception_handler(RequestValidationError)
88
+ async def validation_exception_handler(request: Request, exc: RequestValidationError):
89
+ details = exc.errors()
90
+ modified_details = [{
91
+ "loc": error["loc"],
92
+ "message": error["msg"],
93
+ "type": error["type"],
94
+ } for error in details]
95
+ return JSONResponse(
96
+ status_code=HTTP_422_UNPROCESSABLE_ENTITY,
97
+ content=jsonable_encoder({"detail": modified_details}),
98
+ )
99
+
100
+ def register_routes(self):
101
+ @self.app.get("/")
102
+ async def read_root():
103
+ return RedirectResponse("/v1", 302)
104
+
105
+ @self.app.get("/v1")
106
+ async def read_root_v1():
107
+ return HTMLResponse('g4f API: Go to '
108
+ '<a href="/v1/chat/completions">chat/completions</a> '
109
+ 'or <a href="/v1/models">models</a>.')
110
+
111
+ @self.app.get("/api/v1/models")
112
+ async def models():
113
+ model_list = {
114
+ model: g4f.models.ModelUtils.convert[model]
115
+ for model in g4f.Model.__all__()
116
+ }
117
+ model_list = [{
118
+ 'id': model_id,
119
+ 'object': 'model',
120
+ 'created': 0,
121
+ 'owned_by': model.base_provider
122
+ } for model_id, model in model_list.items()]
123
+ return JSONResponse(model_list)
124
+
125
+ @self.app.get("/api/v1/models/{model_name}")
126
+ async def model_info(model_name: str):
127
+ try:
128
+ model_info = g4f.models.ModelUtils.convert[model_name]
129
+ return JSONResponse({
130
+ 'id': model_name,
131
+ 'object': 'model',
132
+ 'created': 0,
133
+ 'owned_by': model_info.base_provider
134
+ })
135
+ except:
136
+ return JSONResponse({"error": "The model does not exist."})
137
+
138
+ @self.app.post("/api/v1/chat/completions")
139
+ async def chat_completions(config: ChatCompletionsForm, request: Request = None, provider: str = None):
140
+ try:
141
+ config.provider = provider if config.provider is None else config.provider
142
+ if config.api_key is None and request is not None:
143
+ auth_header = request.headers.get("Authorization")
144
+ if auth_header is not None:
145
+ auth_header = auth_header.split(None, 1)[-1]
146
+ if auth_header and auth_header != "Bearer":
147
+ config.api_key = auth_header
148
+ response = self.client.chat.completions.create(
149
+ **{
150
+ **AppConfig.defaults,
151
+ **config.dict(exclude_none=True),
152
+ },
153
+
154
+ ignored=AppConfig.list_ignored_providers
155
+ )
156
+ except Exception as e:
157
+ logging.exception(e)
158
+ return Response(content=format_exception(e, config), status_code=500, media_type="application/json")
159
+
160
+ if not config.stream:
161
+ return JSONResponse((await response).to_json())
162
+
163
+ async def streaming():
164
+ try:
165
+ async for chunk in response:
166
+ yield f"data: {json.dumps(chunk.to_json())}\n\n"
167
+ except GeneratorExit:
168
+ pass
169
+ except Exception as e:
170
+ logging.exception(e)
171
+ yield f'data: {format_exception(e, config)}\n\n'
172
+ yield "data: [DONE]\n\n"
173
+
174
+ return StreamingResponse(streaming(), media_type="text/event-stream")
175
+
176
+ @self.app.post("/v1/completions")
177
+ async def completions():
178
+ return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
179
+
180
+ def format_exception(e: Exception, config: ChatCompletionsForm) -> str:
181
+ last_provider = g4f.get_last_provider(True)
182
+ return json.dumps({
183
+ "error": {"message": f"{e.__class__.__name__}: {e}"},
184
+ "model": last_provider.get("model") if last_provider else config.model,
185
+ "provider": last_provider.get("name") if last_provider else config.provider
186
+ })
187
+
188
+ def run_api(
189
+ host: str = '0.0.0.0',
190
+ port: int = 1337,
191
+ bind: str = None,
192
+ debug: bool = False,
193
+ workers: int = None,
194
+ use_colors: bool = None
195
+ ) -> None:
196
+ print(f'Starting server... [g4f v-{g4f.version.utils.current_version}]' + (" (debug)" if debug else ""))
197
+ if use_colors is None:
198
+ use_colors = debug
199
+ if bind is not None:
200
+ host, port = bind.split(":")
201
+ uvicorn.run(
202
+ f"g4f.api:create_app{'_debug' if debug else ''}",
203
+ host=host, port=int(port),
204
+ workers=workers,
205
+ use_colors=use_colors,
206
+ factory=True,
207
+ reload=debug
208
+ )
209
+
210
 
211
  list_ignored_providers = [
212
  ]
213
 
214
  if __name__ == "__main__":
215
+ run_api(debug=True)