From bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 10 Dec 2023 21:46:11 +0100 Subject: Add G4F_PROXY environment Add regenerate button in gui --- g4f/gui/server/backend.py | 52 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'g4f/gui/server/backend.py') diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index b0c55c8a..d17e21b7 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -1,14 +1,15 @@ import g4f from g4f.Provider import __providers__ -from flask import request +import json +from flask import request, Flask from .internet import get_search_message g4f.debug.logging = True class Backend_Api: - def __init__(self, app) -> None: - self.app = app + def __init__(self, app: Flask) -> None: + self.app: Flask = app self.routes = { '/backend-api/v2/models': { 'function': self.models, @@ -61,19 +62,18 @@ class Backend_Api: } def _conversation(self): - try: - #jailbreak = request.json['jailbreak'] - web_search = request.json['meta']['content']['internet_access'] - messages = request.json['meta']['content']['parts'] - if web_search: - messages[-1]["content"] = get_search_message(messages[-1]["content"]) - model = request.json.get('model') - model = model if model else g4f.models.default - provider = request.json.get('provider').replace('g4f.Provider.', '') - provider = provider if provider and provider != "Auto" else None - if provider != None: - provider = g4f.Provider.ProviderUtils.convert.get(provider) - + #jailbreak = request.json['jailbreak'] + messages = request.json['meta']['content']['parts'] + if request.json['internet_access']: + messages[-1]["content"] = get_search_message(messages[-1]["content"]) + model = request.json.get('model') + model = model if model else g4f.models.default + provider = request.json.get('provider').replace('g4f.Provider.', '') + provider = provider if provider and provider != "Auto" else None + if provider != None: + provider = g4f.Provider.ProviderUtils.convert.get(provider) + + def try_response(): response = g4f.ChatCompletion.create( model=model, provider=provider, @@ -81,13 +81,15 @@ class Backend_Api: stream=True, ignore_stream_and_auth=True ) + try: + yield from response + except Exception as e: + print(e) + yield json.dumps({ + 'code' : 'G4F_ERROR', + '_action': '_ask', + 'success': False, + 'error' : f'an error occurred {str(e)}' + }) - return self.app.response_class(response, mimetype='text/event-stream') - - except Exception as e: - print(e) - return { - 'code' : 'G4F_ERROR', - '_action': '_ask', - 'success': False, - 'error' : f'an error occurred {str(e)}'}, 400 + return self.app.response_class(try_response(), mimetype='text/event-stream') \ No newline at end of file -- cgit v1.2.3