diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2023-12-10 21:46:11 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2023-12-10 21:46:11 +0100 |
commit | bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3 (patch) | |
tree | cc9dcbdc5bc3b81050ff74a47d00e13c9c63570c /g4f/gui/server/backend.py | |
parent | Fix llama2 provider, Update TOC in readme (#1325) (diff) | |
download | gpt4free-bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3.tar gpt4free-bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3.tar.gz gpt4free-bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3.tar.bz2 gpt4free-bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3.tar.lz gpt4free-bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3.tar.xz gpt4free-bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3.tar.zst gpt4free-bf41cfc5d1d7ef6e2e8dde6237e54757c22b66b3.zip |
Diffstat (limited to 'g4f/gui/server/backend.py')
-rw-r--r-- | g4f/gui/server/backend.py | 52 |
1 files changed, 27 insertions, 25 deletions
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 |