summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/backend.py
blob: 9a1dbbf8d7e20635b9e879f7a93b964b1b441d04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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