返回提交历史
Modified
g4f/api/__init__.py
+138
-143
XFEstudio/gpt4free
Update __init__.py
0af4fc09
代码差异
1 个文件
+138
-143
@@ -1,167 +1,162 @@
1
from fastapi import FastAPI, Response, Request
2
from fastapi.middleware.cors import CORSMiddleware
3
from typing import List, Union, Any, Dict, AnyStr
4
from ._tokenizer import tokenize
5
import g4f
1
from fastapi import FastAPI, Response, Request
2
from typing import List, Union, Any, Dict, AnyStr
3
from ._tokenizer import tokenize
4
from .. import BaseProvider
5
6
6
import time
7
7
import json
8
8
import random
9
9
import string
10
10
import uvicorn
11
11
import nest_asyncio
12
import g4f
12
13
13
app = FastAPI()
14
nest_asyncio.apply()
15
16
origins = [
17
"http://localhost",
18
"http://localhost:1337",
19
]
20
21
app.add_middleware(
22
CORSMiddleware,
23
allow_origins=origins,
24
allow_credentials=True,
25
allow_methods=["*"],
26
allow_headers=["*"],
27
)
28
29
JSONObject = Dict[AnyStr, Any]
30
JSONArray = List[Any]
31
JSONStructure = Union[JSONArray, JSONObject]
32
33
@app.get("/")
34
async def read_root():
35
return Response(content=json.dumps({"info": "G4F API"}, indent=4), media_type="application/json")
36
37
@app.get("/v1")
38
async def read_root_v1():
39
return Response(content=json.dumps({"info": "Go to /v1/chat/completions or /v1/models."}, indent=4), media_type="application/json")
40
41
@app.get("/v1/models")
42
async def models():
43
model_list = [{
44
'id': model,
45
'object': 'model',
46
'created': 0,
47
'owned_by': 'g4f'} for model in g4f.Model.__all__()]
48
49
return Response(content=json.dumps({
50
'object': 'list',
51
'data': model_list}, indent=4), media_type="application/json")
52
53
@app.get("/v1/models/{model_name}")
54
async def model_info(model_name: str):
55
try:
56
model_info = (g4f.ModelUtils.convert[model_name])
57
58
return Response(content=json.dumps({
59
'id': model_name,
60
'object': 'model',
61
'created': 0,
62
'owned_by': model_info.base_provider
63
}, indent=4), media_type="application/json")
64
except:
65
return Response(content=json.dumps({"error": "The model does not exist."}, indent=4), media_type="application/json")
66
67
@app.post("/v1/chat/completions")
68
async def chat_completions(request: Request, item: JSONStructure = None):
69
70
item_data = {
71
'model': 'gpt-3.5-turbo',
72
'stream': False,
73
}
74
75
item_data.update(item or {})
76
model = item_data.get('model')
77
stream = item_data.get('stream')
78
messages = item_data.get('messages')
79
80
try:
81
response = g4f.ChatCompletion.create(model=model, stream=stream, messages=messages)
82
except:
83
return Response(content=json.dumps({"error": "An error occurred while generating the response."}, indent=4), media_type="application/json")
84
85
completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28))
86
completion_timestamp = int(time.time())
87
88
if not stream:
89
prompt_tokens, _ = tokenize(''.join([message['content'] for message in messages]))
90
completion_tokens, _ = tokenize(response)
91
92
json_data = {
93
'id': f'chatcmpl-{completion_id}',
94
'object': 'chat.completion',
95
'created': completion_timestamp,
96
'model': model,
97
'choices': [
98
{
99
'index': 0,
100
'message': {
101
'role': 'assistant',
102
'content': response,
103
},
104
'finish_reason': 'stop',
105
}
106
],
107
'usage': {
108
'prompt_tokens': prompt_tokens,
109
'completion_tokens': completion_tokens,
110
'total_tokens': prompt_tokens + completion_tokens,
111
},
112
}
113
114
return Response(content=json.dumps(json_data, indent=4), media_type="application/json")
115
116
def streaming():
117
try:
118
for chunk in response:
119
completion_data = {
14
class Api:
15
def __init__(self, engine: g4f, debug: bool = True, sentry: bool = False,
16
list_ignored_providers: List[Union[str, BaseProvider]] = None) -> None:
17
self.engine = engine
18
self.debug = debug
19
self.sentry = sentry
20
self.list_ignored_providers = list_ignored_providers
21
22
self.app = FastAPI()
23
nest_asyncio.apply()
24
25
JSONObject = Dict[AnyStr, Any]
26
JSONArray = List[Any]
27
JSONStructure = Union[JSONArray, JSONObject]
28
29
@self.app.get("/")
30
async def read_root():
31
return Response(content=json.dumps({"info": "g4f API"}, indent=4), media_type="application/json")
32
33
@self.app.get("/v1")
34
async def read_root_v1():
35
return Response(content=json.dumps({"info": "Go to /v1/chat/completions or /v1/models."}, indent=4), media_type="application/json")
36
37
@self.app.get("/v1/models")
38
async def models():
39
model_list = [{
40
'id': model,
41
'object': 'model',
42
'created': 0,
43
'owned_by': 'g4f'} for model in g4f.Model.__all__()]
44
45
return Response(content=json.dumps({
46
'object': 'list',
47
'data': model_list}, indent=4), media_type="application/json")
48
49
@self.app.get("/v1/models/{model_name}")
50
async def model_info(model_name: str):
51
try:
52
model_info = (g4f.ModelUtils.convert[model_name])
53
54
return Response(content=json.dumps({
55
'id': model_name,
56
'object': 'model',
57
'created': 0,
58
'owned_by': model_info.base_provider
59
}, indent=4), media_type="application/json")
60
except:
61
return Response(content=json.dumps({"error": "The model does not exist."}, indent=4), media_type="application/json")
62
63
@self.app.post("/v1/chat/completions")
64
async def chat_completions(request: Request, item: JSONStructure = None):
65
item_data = {
66
'model': 'gpt-3.5-turbo',
67
'stream': False,
68
}
69
70
item_data.update(item or {})
71
model = item_data.get('model')
72
stream = item_data.get('stream')
73
messages = item_data.get('messages')
74
75
try:
76
response = g4f.ChatCompletion.create(model=model, stream=stream, messages=messages)
77
except:
78
return Response(content=json.dumps({"error": "An error occurred while generating the response."}, indent=4), media_type="application/json")
79
80
completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28))
81
completion_timestamp = int(time.time())
82
83
if not stream:
84
prompt_tokens, _ = tokenize(''.join([message['content'] for message in messages]))
85
completion_tokens, _ = tokenize(response)
86
87
json_data = {
120
88
'id': f'chatcmpl-{completion_id}',
121
'object': 'chat.completion.chunk',
89
'object': 'chat.completion',
122
90
'created': completion_timestamp,
123
91
'model': model,
124
92
'choices': [
125
93
{
126
94
'index': 0,
127
'delta': {
128
'content': chunk,
95
'message': {
96
'role': 'assistant',
97
'content': response,
129
98
},
130
'finish_reason': None,
99
'finish_reason': 'stop',
131
100
}
132
101
],
102
'usage': {
103
'prompt_tokens': prompt_tokens,
104
'completion_tokens': completion_tokens,
105
'total_tokens': prompt_tokens + completion_tokens,
106
},
133
107
}
134
108
135
content = json.dumps(completion_data, separators=(',', ':'))
136
yield f'data: {content}\n\n'
137
time.sleep(0.03)
138
139
end_completion_data = {
140
'id': f'chatcmpl-{completion_id}',
141
'object': 'chat.completion.chunk',
142
'created': completion_timestamp,
143
'model': model,
144
'choices': [
145
{
146
'index': 0,
147
'delta': {},
148
'finish_reason': 'stop',
109
return Response(content=json.dumps(json_data, indent=4), media_type="application/json")
110
111
def streaming():
112
try:
113
for chunk in response:
114
completion_data = {
115
'id': f'chatcmpl-{completion_id}',
116
'object': 'chat.completion.chunk',
117
'created': completion_timestamp,
118
'model': model,
119
'choices': [
120
{
121
'index': 0,
122
'delta': {
123
'content': chunk,
124
},
125
'finish_reason': None,
126
}
127
],
128
}
129
130
content = json.dumps(completion_data, separators=(',', ':'))
131
yield f'data: {content}\n\n'
132
time.sleep(0.03)
133
134
end_completion_data = {
135
'id': f'chatcmpl-{completion_id}',
136
'object': 'chat.completion.chunk',
137
'created': completion_timestamp,
138
'model': model,
139
'choices': [
140
{
141
'index': 0,
142
'delta': {},
143
'finish_reason': 'stop',
144
}
145
],
149
146
}
150
],
151
}
152
147
153
content = json.dumps(end_completion_data, separators=(',', ':'))
154
yield f'data: {content}\n\n'
148
content = json.dumps(end_completion_data, separators=(',', ':'))
149
yield f'data: {content}\n\n'
155
150
156
except GeneratorExit:
157
pass
151
except GeneratorExit:
152
pass
158
153
159
return Response(content=json.dumps(streaming(), indent=4), media_type="application/json")
154
return Response(content=json.dumps(streaming(), indent=4), media_type="application/json")
160
155
161
@app.post("/v1/completions")
162
async def completions():
163
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
156
@self.app.post("/v1/completions")
157
async def completions():
158
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
164
159
165
def run(ip, thread_quantity):
166
split_ip = ip.split(":")
167
uvicorn.run(app, host=split_ip[0], port=int(split_ip[1]), use_colors=False, workers=thread_quantity)
160
def run(self, ip, thread_quantity):
161
split_ip = ip.split(":")
162
uvicorn.run(self.app, host=split_ip[0], port=int(split_ip[1]), use_colors=False, workers=thread_quantity)