2023-12-10 23:46:11 +03:00
|
|
|
import json
|
2024-01-10 12:34:56 +03:00
|
|
|
from flask import request, Flask
|
2024-05-19 02:33:05 +03:00
|
|
|
from g4f.image import is_allowed_extension, to_image
|
2024-03-16 12:48:37 +03:00
|
|
|
from .api import Api
|
2023-10-06 21:52:17 +03:00
|
|
|
|
2024-03-16 12:48:37 +03:00
|
|
|
class Backend_Api(Api):
|
2024-01-14 17:04:37 +03:00
|
|
|
"""
|
|
|
|
Handles various endpoints in a Flask application for backend operations.
|
|
|
|
|
|
|
|
This class provides methods to interact with models, providers, and to handle
|
|
|
|
various functionalities like conversations, error handling, and version management.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
app (Flask): A Flask application instance.
|
|
|
|
routes (dict): A dictionary mapping API endpoints to their respective handlers.
|
|
|
|
"""
|
2023-12-10 23:46:11 +03:00
|
|
|
def __init__(self, app: Flask) -> None:
|
2024-01-14 17:04:37 +03:00
|
|
|
"""
|
|
|
|
Initialize the backend API with the given Flask application.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
app (Flask): Flask application instance to attach routes to.
|
|
|
|
"""
|
2023-12-10 23:46:11 +03:00
|
|
|
self.app: Flask = app
|
2023-10-06 21:52:17 +03:00
|
|
|
self.routes = {
|
2023-10-19 16:27:29 +03:00
|
|
|
'/backend-api/v2/models': {
|
2024-01-14 17:04:37 +03:00
|
|
|
'function': self.get_models,
|
|
|
|
'methods': ['GET']
|
2023-10-19 16:27:29 +03:00
|
|
|
},
|
2024-03-13 19:52:48 +03:00
|
|
|
'/backend-api/v2/models/<provider>': {
|
|
|
|
'function': self.get_provider_models,
|
|
|
|
'methods': ['GET']
|
|
|
|
},
|
2024-04-21 16:15:55 +03:00
|
|
|
'/backend-api/v2/image_models': {
|
|
|
|
'function': self.get_image_models,
|
|
|
|
'methods': ['GET']
|
|
|
|
},
|
2023-12-06 11:35:36 +03:00
|
|
|
'/backend-api/v2/providers': {
|
2024-01-14 17:04:37 +03:00
|
|
|
'function': self.get_providers,
|
|
|
|
'methods': ['GET']
|
2023-12-06 11:35:36 +03:00
|
|
|
},
|
2023-12-07 09:18:05 +03:00
|
|
|
'/backend-api/v2/version': {
|
2024-01-14 17:04:37 +03:00
|
|
|
'function': self.get_version,
|
|
|
|
'methods': ['GET']
|
2023-12-07 09:18:05 +03:00
|
|
|
},
|
2023-10-06 21:52:17 +03:00
|
|
|
'/backend-api/v2/conversation': {
|
2024-01-14 17:04:37 +03:00
|
|
|
'function': self.handle_conversation,
|
2023-10-06 21:52:17 +03:00
|
|
|
'methods': ['POST']
|
|
|
|
},
|
2023-10-19 22:25:13 +03:00
|
|
|
'/backend-api/v2/error': {
|
2024-01-14 17:04:37 +03:00
|
|
|
'function': self.handle_error,
|
2023-10-19 22:25:13 +03:00
|
|
|
'methods': ['POST']
|
2024-05-20 02:28:08 +03:00
|
|
|
},
|
|
|
|
'/images/<path:name>': {
|
|
|
|
'function': self.serve_images,
|
|
|
|
'methods': ['GET']
|
2023-10-19 22:25:13 +03:00
|
|
|
}
|
2023-10-06 21:52:17 +03:00
|
|
|
}
|
2024-03-16 12:48:37 +03:00
|
|
|
|
2024-01-14 17:04:37 +03:00
|
|
|
def handle_error(self):
|
|
|
|
"""
|
|
|
|
Initialize the backend API with the given Flask application.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
app (Flask): Flask application instance to attach routes to.
|
|
|
|
"""
|
2023-10-19 22:25:13 +03:00
|
|
|
print(request.json)
|
|
|
|
return 'ok', 200
|
2024-01-14 17:04:37 +03:00
|
|
|
|
|
|
|
def handle_conversation(self):
|
|
|
|
"""
|
|
|
|
Handles conversation requests and streams responses back.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Response: A Flask response object for streaming.
|
|
|
|
"""
|
2024-03-16 12:48:37 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
kwargs = {}
|
2024-03-16 12:48:37 +03:00
|
|
|
if "file" in request.files:
|
|
|
|
file = request.files['file']
|
2024-01-13 17:37:36 +03:00
|
|
|
if file.filename != '' and is_allowed_extension(file.filename):
|
2024-05-19 02:33:05 +03:00
|
|
|
kwargs['image'] = to_image(file.stream, file.filename.endswith('.svg'))
|
2024-02-11 08:59:53 +03:00
|
|
|
kwargs['image_name'] = file.filename
|
2024-02-11 03:59:57 +03:00
|
|
|
if "json" in request.form:
|
2024-01-13 17:37:36 +03:00
|
|
|
json_data = json.loads(request.form['json'])
|
|
|
|
else:
|
|
|
|
json_data = request.json
|
2024-01-14 17:04:37 +03:00
|
|
|
|
2024-03-16 12:48:37 +03:00
|
|
|
kwargs = self._prepare_conversation_kwargs(json_data, kwargs)
|
2024-01-14 17:04:37 +03:00
|
|
|
|
2024-03-16 12:48:37 +03:00
|
|
|
return self.app.response_class(
|
2024-03-28 13:36:25 +03:00
|
|
|
self._create_response_stream(kwargs, json_data.get("conversation_id"), json_data.get("provider")),
|
2024-03-16 12:48:37 +03:00
|
|
|
mimetype='text/event-stream'
|
|
|
|
)
|
2024-01-14 17:04:37 +03:00
|
|
|
|
2024-03-16 12:48:37 +03:00
|
|
|
def get_provider_models(self, provider: str):
|
|
|
|
models = super().get_provider_models(provider)
|
|
|
|
if models is None:
|
|
|
|
return 404, "Provider not found"
|
|
|
|
return models
|
2024-01-14 17:04:37 +03:00
|
|
|
|
|
|
|
def _format_json(self, response_type: str, content) -> str:
|
|
|
|
"""
|
|
|
|
Formats and returns a JSON response.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
response_type (str): The type of the response.
|
|
|
|
content: The content to be included in the response.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: A JSON formatted string.
|
|
|
|
"""
|
2024-03-16 12:48:37 +03:00
|
|
|
return json.dumps(super()._format_json(response_type, content)) + "\n"
|