From 7f8526a1227e016fc79328ddcbe7afd09c0d63d1 Mon Sep 17 00:00:00 2001 From: abc <98614666+xtekky@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:46:02 +0100 Subject: ~ New system for str to Provider --- g4f/gui/server/backend.py | 19 ++++++++++++------- g4f/gui/server/provider.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 g4f/gui/server/provider.py diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 9a1dbbf8..8f4b529f 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -1,9 +1,11 @@ import g4f from flask import request -from threading import Thread from .internet import search from .config import special_instructions +from .provider import get_provider + +g4f.logging = True class Backend_Api: def __init__(self, app) -> None: @@ -31,22 +33,25 @@ class Backend_Api: conversation = request.json['meta']['content']['conversation'] prompt = request.json['meta']['content']['parts'][0] model = request.json['model'] + provider = get_provider(request.json.get('provider')) messages = special_instructions[jailbreak] + conversation + search(internet_access, prompt) + [prompt] def stream(): - answer = g4f.ChatCompletion.create(model = model, - messages = messages, stream=True) + if provider: + answer = g4f.ChatCompletion.create(model=model, + provider=provider, messages=messages, stream=True) + else: + answer = g4f.ChatCompletion.create(model=model, + messages=messages, stream=True) for token in answer: yield token - + return self.app.response_class(stream(), mimetype='text/event-stream') - except Exception as e: - print(e) + except Exception as e: return { - '_token': 'anerroroccuredmf', '_action': '_ask', 'success': False, "error": f"an error occured {str(e)}"}, 400 \ No newline at end of file diff --git a/g4f/gui/server/provider.py b/g4f/gui/server/provider.py new file mode 100644 index 00000000..286f881b --- /dev/null +++ b/g4f/gui/server/provider.py @@ -0,0 +1,17 @@ +import g4f + +def get_provider(provider: str) -> g4f.Provider.BaseProvider: + + if isinstance(provider, str): + print(provider) + if provider == 'g4f.Provider.Auto': + return None + + if provider in g4f.Provider.ProviderUtils.convert: + return g4f.Provider.ProviderUtils.convert[provider] + + else: + return None + + else: + return None -- cgit v1.2.3