2023-10-06 18:52:17 +00:00
|
|
|
import g4f
|
|
|
|
|
|
|
|
from flask import request
|
|
|
|
from .internet import search
|
|
|
|
from .config import special_instructions
|
2023-10-09 22:46:02 +00:00
|
|
|
|
2023-12-06 08:35:36 +00:00
|
|
|
g4f.debug.logging = True
|
2023-10-06 18:52:17 +00:00
|
|
|
|
|
|
|
class Backend_Api:
|
|
|
|
def __init__(self, app) -> None:
|
|
|
|
self.app = app
|
|
|
|
self.routes = {
|
2023-10-19 13:27:29 +00:00
|
|
|
'/backend-api/v2/models': {
|
|
|
|
'function': self.models,
|
|
|
|
'methods' : ['GET']
|
|
|
|
},
|
2023-12-06 08:35:36 +00:00
|
|
|
'/backend-api/v2/providers': {
|
|
|
|
'function': self.providers,
|
|
|
|
'methods' : ['GET']
|
|
|
|
},
|
2023-10-06 18:52:17 +00:00
|
|
|
'/backend-api/v2/conversation': {
|
|
|
|
'function': self._conversation,
|
|
|
|
'methods': ['POST']
|
|
|
|
},
|
|
|
|
'/backend-api/v2/gen.set.summarize:title': {
|
|
|
|
'function': self._gen_title,
|
|
|
|
'methods': ['POST']
|
|
|
|
},
|
2023-10-19 19:25:13 +00:00
|
|
|
'/backend-api/v2/error': {
|
|
|
|
'function': self.error,
|
|
|
|
'methods': ['POST']
|
|
|
|
}
|
2023-10-06 18:52:17 +00:00
|
|
|
}
|
2023-10-19 14:14:48 +00:00
|
|
|
|
2023-10-19 19:25:13 +00:00
|
|
|
def error(self):
|
|
|
|
print(request.json)
|
|
|
|
|
|
|
|
return 'ok', 200
|
|
|
|
|
2023-10-19 13:27:29 +00:00
|
|
|
def models(self):
|
2023-10-23 07:46:25 +00:00
|
|
|
return g4f._all_models
|
2023-10-06 18:52:17 +00:00
|
|
|
|
2023-12-06 08:35:36 +00:00
|
|
|
def providers(self):
|
2023-12-06 10:54:50 +00:00
|
|
|
return [
|
|
|
|
provider.__name__ for provider in g4f.Provider.__providers__
|
|
|
|
if provider.working and provider is not g4f.Provider.RetryProvider
|
|
|
|
]
|
2023-12-06 08:35:36 +00:00
|
|
|
|
2023-10-06 18:52:17 +00:00
|
|
|
def _gen_title(self):
|
|
|
|
return {
|
|
|
|
'title': ''
|
|
|
|
}
|
|
|
|
|
|
|
|
def _conversation(self):
|
2023-10-26 14:18:38 +00:00
|
|
|
try:
|
2023-10-27 20:35:10 +00:00
|
|
|
#jailbreak = request.json['jailbreak']
|
|
|
|
#internet_access = request.json['meta']['content']['internet_access']
|
|
|
|
#conversation = request.json['meta']['content']['conversation']
|
2023-12-06 08:35:36 +00:00
|
|
|
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)
|
2023-10-23 07:46:25 +00:00
|
|
|
|
2023-12-06 08:35:36 +00:00
|
|
|
response = g4f.ChatCompletion.create(
|
|
|
|
model=model,
|
|
|
|
provider=provider,
|
|
|
|
messages=messages,
|
|
|
|
stream=True,
|
|
|
|
ignore_stream_and_auth=True
|
|
|
|
)
|
2023-10-09 22:46:02 +00:00
|
|
|
|
2023-12-06 08:35:36 +00:00
|
|
|
return self.app.response_class(response, mimetype='text/event-stream')
|
2023-10-06 18:52:17 +00:00
|
|
|
|
2023-12-06 08:35:36 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2023-10-06 18:52:17 +00:00
|
|
|
return {
|
2023-10-10 14:48:56 +00:00
|
|
|
'code' : 'G4F_ERROR',
|
2023-10-06 18:52:17 +00:00
|
|
|
'_action': '_ask',
|
|
|
|
'success': False,
|
2023-10-11 03:35:49 +00:00
|
|
|
'error' : f'an error occurred {str(e)}'}, 400
|