summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/backend.py
blob: d17e21b7ebc88b37052901128e5e271b89322e09 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import g4f
from g4f.Provider import __providers__

import json
from flask      import request, Flask
from .internet  import get_search_message

g4f.debug.logging = True

class Backend_Api:
    def __init__(self, app: Flask) -> None:
        self.app: Flask = app
        self.routes = {
            '/backend-api/v2/models': {
                'function': self.models,
                'methods' : ['GET']
            },
            '/backend-api/v2/providers': {
                'function': self.providers,
                'methods' : ['GET']
            },
            '/backend-api/v2/version': {
                'function': self.version,
                'methods' : ['GET']
            },
            '/backend-api/v2/conversation': {
                'function': self._conversation,
                'methods': ['POST']
            },
            '/backend-api/v2/gen.set.summarize:title': {
                'function': self._gen_title,
                'methods': ['POST']
            },
            '/backend-api/v2/error': {
                'function': self.error,
                'methods': ['POST']
            }
        }
    
    def error(self):
        print(request.json)
        
        return 'ok', 200
    
    def models(self):
        return g4f._all_models
    
    def providers(self):
        return [
            provider.__name__ for provider in __providers__ if provider.working
        ]
        
    def version(self):
        return {
            "version": g4f.get_version(),
            "lastet_version": g4f.get_lastet_version(),
        }
    
    def _gen_title(self):
        return {
            'title': ''
        }
    
    def _conversation(self):
        #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,
                messages=messages,
                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(try_response(), mimetype='text/event-stream')