summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/backend.py
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/gui/server/backend.py')
-rw-r--r--g4f/gui/server/backend.py52
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