diff options
author | Commenter123321 <36051603+Commenter123321@users.noreply.github.com> | 2023-10-09 18:02:06 +0200 |
---|---|---|
committer | Commenter123321 <36051603+Commenter123321@users.noreply.github.com> | 2023-10-09 18:02:06 +0200 |
commit | 119817c96349807efaf87ee432ce46446542b66a (patch) | |
tree | 1dbdf4d4dbf4f6c8a8247274ef500a2f1de765d1 /g4f/gui/server/backend.py | |
parent | aivvm's no life creator keeps patching it, but I'm just better 😉 (diff) | |
parent | Merge branch 'main' of https://github.com/xtekky/gpt4free (diff) | |
download | gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.gz gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.bz2 gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.lz gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.xz gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.zst gpt4free-119817c96349807efaf87ee432ce46446542b66a.zip |
Diffstat (limited to 'g4f/gui/server/backend.py')
-rw-r--r-- | g4f/gui/server/backend.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py new file mode 100644 index 00000000..9a1dbbf8 --- /dev/null +++ b/g4f/gui/server/backend.py @@ -0,0 +1,52 @@ +import g4f + +from flask import request +from threading import Thread +from .internet import search +from .config import special_instructions + +class Backend_Api: + def __init__(self, app) -> None: + self.app = app + self.routes = { + '/backend-api/v2/conversation': { + 'function': self._conversation, + 'methods': ['POST'] + }, + '/backend-api/v2/gen.set.summarize:title': { + 'function': self._gen_title, + 'methods': ['POST'] + }, + } + + def _gen_title(self): + return { + 'title': '' + } + + def _conversation(self): + try: + jailbreak = request.json['jailbreak'] + internet_access = request.json['meta']['content']['internet_access'] + conversation = request.json['meta']['content']['conversation'] + prompt = request.json['meta']['content']['parts'][0] + model = request.json['model'] + + messages = special_instructions[jailbreak] + conversation + search(internet_access, prompt) + [prompt] + + def stream(): + 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) + return { + '_token': 'anerroroccuredmf', + '_action': '_ask', + 'success': False, + "error": f"an error occured {str(e)}"}, 400
\ No newline at end of file |