返回提交历史
Modified
README.md
+11
-9
Modified
g4f/api/__init__.py
+27
-19
Modified
g4f/cli.py
+8
-10
XFEstudio/gpt4free
Add AppConfig class, update readme
ff8c1fc1
代码差异
3 个文件
+46
-38
@@ -862,8 +862,8 @@ A list of all contributors is available [here](https://github.com/xtekky/gpt4fre
862
862
<a href="https://github.com/Commenter123321" target="_blank"><img src="https://avatars.githubusercontent.com/u/36051603?v=4&s=45" width="45" title="Commenter123321"></a>
863
863
<a href="https://github.com/DanielShemesh" target="_blank"><img src="https://avatars.githubusercontent.com/u/20585236?v=4&s=45" width="45" title="DanielShemesh"></a>
864
864
<a href="https://github.com/Luneye" target="_blank"><img src="https://avatars.githubusercontent.com/u/73485421?v=4&s=45" width="45" title="Luneye"></a>
865
<a href="https://github.com/enganese" target="_blank"><img src="https://avatars.githubusercontent.com/u/69082498?v=4&s=45" width="45" title="enganese"></a>
866
865
<a href="https://github.com/ezerinz" target="_blank"><img src="https://avatars.githubusercontent.com/u/100193740?v=4&s=45" width="45" title="ezerinz"></a>
866
<a href="https://github.com/enganese" target="_blank"><img src="https://avatars.githubusercontent.com/u/69082498?v=4&s=45" width="45" title="enganese"></a>
867
867
<a href="https://github.com/Lin-jun-xiang" target="_blank"><img src="https://avatars.githubusercontent.com/u/63782903?v=4&s=45" width="45" title="Lin-jun-xiang"></a>
868
868
<a href="https://github.com/nullstreak" target="_blank"><img src="https://avatars.githubusercontent.com/u/139914347?v=4&s=45" width="45" title="nullstreak"></a>
869
869
<a href="https://github.com/valerii-chirkov" target="_blank"><img src="https://avatars.githubusercontent.com/u/81074936?v=4&s=45" width="45" title="valerii-chirkov"></a>
@@ -871,16 +871,16 @@ A list of all contributors is available [here](https://github.com/xtekky/gpt4fre
871
871
<a href="https://github.com/repollo" target="_blank"><img src="https://avatars.githubusercontent.com/u/2671466?v=4&s=45" width="45" title="repollo"></a>
872
872
<a href="https://github.com/hpsj" target="_blank"><img src="https://avatars.githubusercontent.com/u/54535414?v=4&s=45" width="45" title="hpsj"></a>
873
873
<a href="https://github.com/taiyi747" target="_blank"><img src="https://avatars.githubusercontent.com/u/63543716?v=4&s=45" width="45" title="taiyi747"></a>
874
<a href="https://github.com/ostix360" target="_blank"><img src="https://avatars.githubusercontent.com/u/55257054?v=4&s=45" width="45" title="ostix360"></a>
875
<a href="https://github.com/WdR-Tech" target="_blank"><img src="https://avatars.githubusercontent.com/u/143020293?v=4&s=45" width="45" title="WdR-Tech"></a>
876
<a href="https://github.com/HexyeDEV" target="_blank"><img src="https://avatars.githubusercontent.com/u/65314629?v=4&s=45" width="45" title="HexyeDEV"></a>
877
874
<a href="https://github.com/9fo" target="_blank"><img src="https://avatars.githubusercontent.com/u/71867245?v=4&s=45" width="45" title="9fo"></a>
878
<a href="https://github.com/eltociear" target="_blank"><img src="https://avatars.githubusercontent.com/u/22633385?v=4&s=45" width="45" title="eltociear"></a>
879
<a href="https://github.com/ramonvc" target="_blank"><img src="https://avatars.githubusercontent.com/u/13617054?v=4&s=45" width="45" title="ramonvc"></a>
880
<a href="https://github.com/naa7" target="_blank"><img src="https://avatars.githubusercontent.com/u/44613678?v=4&s=45" width="45" title="naa7"></a>
881
<a href="https://github.com/zeng-rr" target="_blank"><img src="https://avatars.githubusercontent.com/u/47846202?v=4&s=45" width="45" title="zeng-rr"></a>
882
<a href="https://github.com/editor-syntax" target="_blank"><img src="https://avatars.githubusercontent.com/u/109844019?v=4&s=45" width="45" title="editor-syntax"></a>
875
<a href="https://github.com/HexyeDEV" target="_blank"><img src="https://avatars.githubusercontent.com/u/65314629?v=4&s=45" width="45" title="HexyeDEV"></a>
876
<a href="https://github.com/WdR-Tech" target="_blank"><img src="https://avatars.githubusercontent.com/u/143020293?v=4&s=45" width="45" title="WdR-Tech"></a>
877
<a href="https://github.com/ostix360" target="_blank"><img src="https://avatars.githubusercontent.com/u/55257054?v=4&s=45" width="45" title="ostix360"></a>
883
878
<a href="https://github.com/devAdityaa" target="_blank"><img src="https://avatars.githubusercontent.com/u/77636021?v=4&s=45" width="45" title="devAdityaa"></a>
879
<a href="https://github.com/editor-syntax" target="_blank"><img src="https://avatars.githubusercontent.com/u/109844019?v=4&s=45" width="45" title="editor-syntax"></a>
880
<a href="https://github.com/zeng-rr" target="_blank"><img src="https://avatars.githubusercontent.com/u/47846202?v=4&s=45" width="45" title="zeng-rr"></a>
881
<a href="https://github.com/naa7" target="_blank"><img src="https://avatars.githubusercontent.com/u/44613678?v=4&s=45" width="45" title="naa7"></a>
882
<a href="https://github.com/ramonvc" target="_blank"><img src="https://avatars.githubusercontent.com/u/13617054?v=4&s=45" width="45" title="ramonvc"></a>
883
<a href="https://github.com/eltociear" target="_blank"><img src="https://avatars.githubusercontent.com/u/22633385?v=4&s=45" width="45" title="eltociear"></a>
884
884
<a href="https://github.com/kggn" target="_blank"><img src="https://avatars.githubusercontent.com/u/95663228?v=4&s=45" width="45" title="kggn"></a>
885
885
<a href="https://github.com/xiangsx" target="_blank"><img src="https://avatars.githubusercontent.com/u/29322721?v=4&s=45" width="45" title="xiangsx"></a>
886
886
<a href="https://github.com/ggindinson" target="_blank"><img src="https://avatars.githubusercontent.com/u/97807772?v=4&s=45" width="45" title="ggindinson"></a>
@@ -889,12 +889,14 @@ A list of all contributors is available [here](https://github.com/xtekky/gpt4fre
889
889
<img src="https://avatars.githubusercontent.com/u/12299238?s=45&v=4" width="45" title="xqdoo00o">
890
890
<img src="https://avatars.githubusercontent.com/u/97126670?s=45&v=4" width="45" title="nathanrchn">
891
891
<img src="https://avatars.githubusercontent.com/u/81407603?v=4&s=45" width="45" title="dsdanielpark">
892
<img src="https://avatars.githubusercontent.com/u/55200481?v=4&s=45" width="45" title="missuo">
892
893
893
894
- The [`Vercel.py`](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/Vercel.py) file contains code from [vercel-llm-api](https://github.com/ading2210/vercel-llm-api) by [@ading2210](https://github.com/ading2210)
894
895
- The [`har_file.py`](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/openai/har_file.py) has input from [xqdoo00o/ChatGPT-to-API](https://github.com/xqdoo00o/ChatGPT-to-API)
895
896
- The [`PerplexityLabs.py`](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/openai/har_file.py) has input from [nathanrchn/perplexityai](https://github.com/nathanrchn/perplexityai)
896
897
- The [`Gemini.py`](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/needs_auth/Gemini.py) has input from [dsdanielpark/Gemini-API](https://github.com/dsdanielpark/Gemini-API)
897
898
- The [`MetaAI.py`](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/MetaAI.py) file contains code from [meta-ai-api](https://github.com/Strvm/meta-ai-api) by [@Strvm](https://github.com/Strvm)
899
- The [`proofofwork.py`](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/openai/proofofwork.py) has input from [missuo/FreeGPT35](https://github.com/missuo/FreeGPT35)
898
900
899
901
*Having input implies that the AI's code generation utilized it as one of many sources.*
900
902
@@ -19,6 +19,7 @@ import g4f
19
19
import g4f.debug
20
20
from g4f.client import AsyncClient
21
21
from g4f.typing import Messages
22
from g4f.cookies import read_cookie_files
22
23
23
24
def create_app():
24
25
app = FastAPI()
@@ -26,13 +27,15 @@ def create_app():
26
27
api.register_routes()
27
28
api.register_authorization()
28
29
api.register_validation_exception_handler()
30
if not AppConfig.ignore_cookie_files:
31
read_cookie_files()
29
32
return app
30
33
31
def create_debug_app():
34
def create_app_debug():
32
35
g4f.debug.logging = True
33
36
return create_app()
34
37
35
class ChatCompletionsConfig(BaseModel):
38
class ChatCompletionsForm(BaseModel):
36
39
messages: Messages
37
40
model: str
38
41
provider: Optional[str] = None
@@ -44,17 +47,22 @@ class ChatCompletionsConfig(BaseModel):
44
47
web_search: Optional[bool] = None
45
48
proxy: Optional[str] = None
46
49
47
list_ignored_providers: list[str] = None
50
class AppConfig():
51
list_ignored_providers: Optional[list[str]] = None
52
g4f_api_key: Optional[str] = None
53
ignore_cookie_files: bool = False
48
54
49
def set_list_ignored_providers(ignored: list[str]):
50
global list_ignored_providers
51
list_ignored_providers = ignored
55
@classmethod
56
def set_list_ignored_providers(cls, ignored: list[str]):
57
cls.list_ignored_providers = ignored
52
58
53
g4f_api_key: str = None
59
@classmethod
60
def set_g4f_api_key(cls, key: str = None):
61
cls.g4f_api_key = key
54
62
55
def set_g4f_api_key(key: str = None):
56
global g4f_api_key
57
g4f_api_key = key
63
@classmethod
64
def set_ignore_cookie_files(cls, value: bool):
65
cls.ignore_cookie_files = value
58
66
59
67
class Api:
60
68
def __init__(self, app: FastAPI) -> None:
@@ -65,7 +73,7 @@ class Api:
65
73
def register_authorization(self):
66
74
@self.app.middleware("http")
67
75
async def authorization(request: Request, call_next):
68
if g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]:
76
if AppConfig.g4f_api_key and request.url.path in ["/v1/chat/completions", "/v1/completions"]:
69
77
try:
70
78
user_g4f_api_key = await self.get_g4f_api_key(request)
71
79
except HTTPException as e:
@@ -74,7 +82,7 @@ class Api:
74
82
status_code=HTTP_401_UNAUTHORIZED,
75
83
content=jsonable_encoder({"detail": "G4F API key required"}),
76
84
)
77
if not secrets.compare_digest(g4f_api_key, user_g4f_api_key):
85
if not secrets.compare_digest(AppConfig.g4f_api_key, user_g4f_api_key):
78
86
return JSONResponse(
79
87
status_code=HTTP_403_FORBIDDEN,
80
88
content=jsonable_encoder({"detail": "Invalid G4F API key"}),
@@ -108,10 +116,10 @@ class Api:
108
116
109
117
@self.app.get("/v1/models")
110
118
async def models():
111
model_list = dict(
112
(model, g4f.models.ModelUtils.convert[model])
119
model_list = {
120
model: g4f.models.ModelUtils.convert[model]
113
121
for model in g4f.Model.__all__()
114
)
122
}
115
123
model_list = [{
116
124
'id': model_id,
117
125
'object': 'model',
@@ -134,7 +142,7 @@ class Api:
134
142
return JSONResponse({"error": "The model does not exist."})
135
143
136
144
@self.app.post("/v1/chat/completions")
137
async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None):
145
async def chat_completions(config: ChatCompletionsForm, request: Request = None, provider: str = None):
138
146
try:
139
147
config.provider = provider if config.provider is None else config.provider
140
148
if config.api_key is None and request is not None:
@@ -145,7 +153,7 @@ class Api:
145
153
config.api_key = auth_header
146
154
response = self.client.chat.completions.create(
147
155
**config.dict(exclude_none=True),
148
ignored=list_ignored_providers
156
ignored=AppConfig.list_ignored_providers
149
157
)
150
158
except Exception as e:
151
159
logging.exception(e)
@@ -171,7 +179,7 @@ class Api:
171
179
async def completions():
172
180
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
173
181
174
def format_exception(e: Exception, config: ChatCompletionsConfig) -> str:
182
def format_exception(e: Exception, config: ChatCompletionsForm) -> str:
175
183
last_provider = g4f.get_last_provider(True)
176
184
return json.dumps({
177
185
"error": {"message": f"{e.__class__.__name__}: {e}"},
@@ -193,7 +201,7 @@ def run_api(
193
201
if bind is not None:
194
202
host, port = bind.split(":")
195
203
uvicorn.run(
196
f"g4f.api:{'create_debug_app' if debug else 'create_app'}",
204
f"g4f.api:{'create_app_debug' if debug else 'create_app'}",
197
205
host=host, port=int(port),
198
206
workers=workers,
199
207
use_colors=use_colors,
@@ -4,8 +4,6 @@ import argparse
4
4
5
5
from g4f import Provider
6
6
from g4f.gui.run import gui_parser, run_gui_args
7
from g4f.cookies import read_cookie_files
8
from g4f import debug
9
7
10
8
def main():
11
9
parser = argparse.ArgumentParser(description="Run gpt4free")
@@ -31,18 +29,18 @@ def main():
31
29
exit(1)
32
30
33
31
def run_api_args(args):
34
if args.debug:
35
debug.logging = True
36
if not args.ignore_cookie_files:
37
read_cookie_files()
38
import g4f.api
39
g4f.api.set_list_ignored_providers(
32
from g4f.api import AppConfig, run_api
33
34
AppConfig.set_ignore_cookie_files(
35
args.ignore_cookie_files
36
)
37
AppConfig.set_list_ignored_providers(
40
38
args.ignored_providers
41
39
)
42
g4f.api.set_g4f_api_key(
40
AppConfig.set_g4f_api_key(
43
41
args.g4f_api_key
44
42
)
45
g4f.api.run_api(
43
run_api(
46
44
bind=args.bind,
47
45
debug=args.debug,
48
46
workers=args.workers,