返回提交历史
Modified
g4f/api/__init__.py
+5
-15
XFEstudio/gpt4free
feat(api): support async streaming in chat completions
0d05825a
代码差异
1 个文件
+5
-15
@@ -165,19 +165,6 @@ class Api:
165
165
@self.app.post("/v1/chat/completions")
166
166
async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None):
167
167
try:
168
# Find the last delimiter with ':' or '-'
169
if ':' in config.model:
170
model_parts = config.model.rsplit(":", 1)
171
elif '-' in config.model:
172
model_parts = config.model.rsplit("-", 1)
173
else:
174
model_parts = [config.model] # There is no prefix.
175
176
base_model = model_parts[0] # We use the base model name
177
model_prefix = model_parts[1] if len(model_parts) > 1 else None
178
179
config.model = base_model # Update the configuration to the basic model
180
181
168
config.provider = provider if config.provider is None else config.provider
182
169
if config.api_key is None and request is not None:
183
170
auth_header = request.headers.get("Authorization")
@@ -206,9 +193,13 @@ class Api:
206
193
return JSONResponse(response_list[0].to_json())
207
194
208
195
# Streaming response
196
async def async_generator(sync_gen):
197
for item in sync_gen:
198
yield item
199
209
200
async def streaming():
210
201
try:
211
async for chunk in response:
202
async for chunk in async_generator(response):
212
203
yield f"data: {json.dumps(chunk.to_json())}\n\n"
213
204
except GeneratorExit:
214
205
pass
@@ -242,7 +233,6 @@ class Api:
242
233
async def completions():
243
234
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
244
235
245
246
236
def format_exception(e: Exception, config: Union[ChatCompletionsConfig, ImageGenerationConfig]) -> str:
247
237
last_provider = g4f.get_last_provider(True)
248
238
return json.dumps({