summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2023-12-06 12:02:41 +0100
committerGitHub <noreply@github.com>2023-12-06 12:02:41 +0100
commitf962993b8dac225d85f9fbb59be188e263af1c0b (patch)
treeae153b027ad5a2679b4f1fde881d61cc0e1276b4 /g4f/gui/server
parent~ | g4f `v-0.1.9.1` (diff)
parentImprove docker image (diff)
downloadgpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.gz
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.bz2
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.lz
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.xz
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.zst
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.zip
Diffstat (limited to 'g4f/gui/server')
-rw-r--r--g4f/gui/server/backend.py47
-rw-r--r--g4f/gui/server/provider.py14
2 files changed, 28 insertions, 33 deletions
diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py
index 3d7bfedc..e1abb764 100644
--- a/g4f/gui/server/backend.py
+++ b/g4f/gui/server/backend.py
@@ -3,9 +3,8 @@ import g4f
from flask import request
from .internet import search
from .config import special_instructions
-from .provider import get_provider
-g4f.logging = True
+g4f.debug.logging = True
class Backend_Api:
def __init__(self, app) -> None:
@@ -15,6 +14,10 @@ class Backend_Api:
'function': self.models,
'methods' : ['GET']
},
+ '/backend-api/v2/providers': {
+ 'function': self.providers,
+ 'methods' : ['GET']
+ },
'/backend-api/v2/conversation': {
'function': self._conversation,
'methods': ['POST']
@@ -37,6 +40,12 @@ class Backend_Api:
def models(self):
return g4f._all_models
+ def providers(self):
+ return [
+ provider.__name__ for provider in g4f.Provider.__providers__
+ if provider.working and provider is not g4f.Provider.RetryProvider
+ ]
+
def _gen_title(self):
return {
'title': ''
@@ -47,26 +56,26 @@ class Backend_Api:
#jailbreak = request.json['jailbreak']
#internet_access = request.json['meta']['content']['internet_access']
#conversation = request.json['meta']['content']['conversation']
- prompt = request.json['meta']['content']['parts']
- model = request.json['model']
- provider = request.json.get('provider').split('g4f.Provider.')[1]
-
- messages = prompt
- print(messages)
+ messages = request.json['meta']['content']['parts']
+ model = request.json.get('model')
+ model = model if model else g4f.models.default
+ provider = request.json.get('provider', 'Auto').replace('g4f.Provider.', '')
+ provider = provider if provider != "Auto" else None
+ if provider != None:
+ provider = g4f.Provider.ProviderUtils.convert.get(provider)
- def stream():
- yield from g4f.ChatCompletion.create(
- model=model,
- provider=get_provider(provider),
- messages=messages,
- stream=True,
- ) if provider else g4f.ChatCompletion.create(
- model=model, messages=messages, stream=True
- )
+ response = g4f.ChatCompletion.create(
+ model=model,
+ provider=provider,
+ messages=messages,
+ stream=True,
+ ignore_stream_and_auth=True
+ )
- return self.app.response_class(stream(), mimetype='text/event-stream')
+ return self.app.response_class(response, mimetype='text/event-stream')
- except Exception as e:
+ except Exception as e:
+ print(e)
return {
'code' : 'G4F_ERROR',
'_action': '_ask',
diff --git a/g4f/gui/server/provider.py b/g4f/gui/server/provider.py
deleted file mode 100644
index 8c7ac755..00000000
--- a/g4f/gui/server/provider.py
+++ /dev/null
@@ -1,14 +0,0 @@
-from __future__ import annotations
-
-import g4f
-from g4f import BaseProvider
-
-
-def get_provider(provider: str) -> BaseProvider | None:
- if not isinstance(provider, str):
- return None
- print(provider)
- if provider == 'g4f.Provider.Auto':
- return None
-
- return g4f.Provider.ProviderUtils.convert.get(provider)