summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/api.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-11-19 15:26:03 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-11-19 15:26:03 +0100
commit8f3fbee0d8a4ca69af72dae989ae8954181d0108 (patch)
tree0481d391ff397c7d4be93a7224c3717e03ae2b3f /g4f/gui/server/api.py
parentFix generate messages on error in gui (diff)
downloadgpt4free-8f3fbee0d8a4ca69af72dae989ae8954181d0108.tar
gpt4free-8f3fbee0d8a4ca69af72dae989ae8954181d0108.tar.gz
gpt4free-8f3fbee0d8a4ca69af72dae989ae8954181d0108.tar.bz2
gpt4free-8f3fbee0d8a4ca69af72dae989ae8954181d0108.tar.lz
gpt4free-8f3fbee0d8a4ca69af72dae989ae8954181d0108.tar.xz
gpt4free-8f3fbee0d8a4ca69af72dae989ae8954181d0108.tar.zst
gpt4free-8f3fbee0d8a4ca69af72dae989ae8954181d0108.zip
Diffstat (limited to 'g4f/gui/server/api.py')
-rw-r--r--g4f/gui/server/api.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/g4f/gui/server/api.py b/g4f/gui/server/api.py
index ed8454c3..a6c4bef4 100644
--- a/g4f/gui/server/api.py
+++ b/g4f/gui/server/api.py
@@ -18,6 +18,7 @@ from g4f.requests.aiohttp import get_connector
from g4f.Provider import ProviderType, __providers__, __map__
from g4f.providers.base_provider import ProviderModelMixin, FinishReason
from g4f.providers.conversation import BaseConversation
+from g4f import debug
logger = logging.getLogger(__name__)
@@ -140,6 +141,13 @@ class Api:
}
def _create_response_stream(self, kwargs: dict, conversation_id: str, provider: str) -> Iterator:
+ if debug.logging:
+ logs = []
+ print_callback = debug.log_handler
+ def log_handler(text: str):
+ logs.append(text)
+ print_callback(text)
+ debug.log_handler = log_handler
try:
result = ChatCompletion.create(**kwargs)
first = True
@@ -168,6 +176,10 @@ class Api:
yield self._format_json("content", str(ImageResponse(images, chunk.alt)))
elif not isinstance(chunk, FinishReason):
yield self._format_json("content", str(chunk))
+ if logs:
+ for log in logs:
+ yield self._format_json("log", str(log))
+ logs = []
except Exception as e:
logger.exception(e)
yield self._format_json('error', get_error_message(e))