返回提交历史
Modified
g4f/Provider/Perplexity.py
+2
-0
Modified
g4f/gui/server/api.py
+3
-1
Modified
g4f/gui/server/backend_api.py
+5
-4
XFEstudio/gpt4free
Enhance Perplexity provider to handle chunked responses; update API to yield model and provider details in JSON formatting for SSE compatibility
ce770082
代码差异
3 个文件
+10
-5
@@ -272,6 +272,8 @@ class Perplexity(AsyncGeneratorProvider, ProviderModelMixin):
272
272
if patch.get("path") == "/progress":
273
273
continue
274
274
value = patch.get("value", "")
275
if isinstance(value, dict) and "chunks" in value:
276
value = "".join(value.get("chunks", []))
275
277
if patch.get("path").startswith("/goals"):
276
278
if isinstance(value, str):
277
279
if value.startswith(full_reasoning):
@@ -206,6 +206,8 @@ class Api:
206
206
result = iter_run_tools(provider_handler, **{**kwargs, "model": model, "download_media": download_media})
207
207
for chunk in result:
208
208
if isinstance(chunk, ProviderInfo):
209
model = getattr(chunk, "model", model)
210
provider = getattr(chunk, "provider", provider)
209
211
yield self.handle_provider(chunk, model)
210
212
elif isinstance(chunk, JsonConversation):
211
213
if provider is not None:
@@ -262,7 +264,7 @@ class Api:
262
264
elif isinstance(chunk, FinishReason):
263
265
yield self._format_json("finish", chunk.get_dict())
264
266
elif isinstance(chunk, Usage):
265
yield self._format_json("usage", chunk.get_dict())
267
yield self._format_json("usage", chunk.get_dict(), model=model, provider=provider)
266
268
elif isinstance(chunk, Reasoning):
267
269
yield self._format_json("reasoning", **chunk.get_dict())
268
270
elif isinstance(chunk, YouTubeResponse):
@@ -606,13 +606,14 @@ class Backend_Api(Api):
606
606
607
607
def _format_json(self, response_type: str, content = None, **kwargs) -> str:
608
608
"""
609
Formats and returns a JSON response.
609
Formats and returns a SSE (Server-Sent Events) formatted JSON response.
610
610
611
611
Args:
612
response_type (str): The type of the response.
612
response_type (str): The type of the response, used as the SSE event name.
613
613
content: The content to be included in the response.
614
614
615
615
Returns:
616
str: A JSON formatted string.
616
str: A SSE formatted string with event type and JSON data.
617
617
"""
618
return json.dumps(super()._format_json(response_type, content, **kwargs)) + "\n"
618
data = json.dumps(super()._format_json(response_type, content, **kwargs))
619
return f"event: {response_type}\ndata: {data}\n\n"