From 6067034d62affa8298943de03cf84fa5b9cd9940 Mon Sep 17 00:00:00 2001 From: Garfield Dai Date: Tue, 24 Oct 2023 17:35:21 +0800 Subject: [PATCH] add form schemas. --- {.vscode => api/.vscode}/launch.json | 4 +- api/controllers/console/__init__.py | 2 +- api/controllers/console/form_schemas.py | 23 +++++++++ api/core/hooks/__init__.py | 1 + api/core/hooks/base_hook.py | 30 +++++++++++ api/core/hooks/moderation/__init__.py | 0 api/core/hooks/moderation/base_moderation.py | 4 ++ .../moderation/cloud_service/__init__.py | 0 .../moderation/cloud_service/cloud_service.py | 4 ++ .../moderation/cloud_service/schema.json | 51 +++++++++++++++++++ api/services/form_schemas_service.py | 7 +++ 11 files changed, 123 insertions(+), 3 deletions(-) rename {.vscode => api/.vscode}/launch.json (90%) create mode 100644 api/controllers/console/form_schemas.py create mode 100644 api/core/hooks/__init__.py create mode 100644 api/core/hooks/base_hook.py create mode 100644 api/core/hooks/moderation/__init__.py create mode 100644 api/core/hooks/moderation/base_moderation.py create mode 100644 api/core/hooks/moderation/cloud_service/__init__.py create mode 100644 api/core/hooks/moderation/cloud_service/cloud_service.py create mode 100644 api/core/hooks/moderation/cloud_service/schema.json create mode 100644 api/services/form_schemas_service.py diff --git a/.vscode/launch.json b/api/.vscode/launch.json similarity index 90% rename from .vscode/launch.json rename to api/.vscode/launch.json index 515deb4c0cf85b..9f89168677d87c 100644 --- a/.vscode/launch.json +++ b/api/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "module": "flask", "env": { - "FLASK_APP": "api/app.py", + "FLASK_APP": "app.py", "FLASK_DEBUG": "1", "GEVENT_SUPPORT": "True" }, @@ -21,7 +21,7 @@ "--debug" ], "jinja": true, - "justMyCode": true + "justMyCode": false } ] } \ No newline at end of file diff --git a/api/controllers/console/__init__.py b/api/controllers/console/__init__.py index 2476d918870c6f..a5cf4c7415e3e4 100644 --- a/api/controllers/console/__init__.py +++ b/api/controllers/console/__init__.py @@ -6,7 +6,7 @@ api = ExternalApi(bp) # Import other controllers -from . import setup, version, apikey, admin +from . import setup, version, apikey, admin, form_schemas # Import app controllers from .app import advanced_prompt_template, app, site, completion, model_config, statistic, conversation, message, generator, audio diff --git a/api/controllers/console/form_schemas.py b/api/controllers/console/form_schemas.py new file mode 100644 index 00000000000000..b7547020c59fd0 --- /dev/null +++ b/api/controllers/console/form_schemas.py @@ -0,0 +1,23 @@ +from flask_restful import Resource, reqparse + +from controllers.console import api +from controllers.console.setup import setup_required +from controllers.console.wraps import account_initialization_required +from libs.login import login_required +from services.form_schemas_service import FormSchemasService + + +class FormSchemas(Resource): + + @setup_required + @login_required + @account_initialization_required + def get(self): + parser = reqparse.RequestParser() + parser.add_argument('module', type=str, required=True, location='args') + args = parser.parse_args() + + return FormSchemasService.get_form_schemas(args['module']) + + +api.add_resource(FormSchemas, '/form-schemas') \ No newline at end of file diff --git a/api/core/hooks/__init__.py b/api/core/hooks/__init__.py new file mode 100644 index 00000000000000..bdbe3af4821239 --- /dev/null +++ b/api/core/hooks/__init__.py @@ -0,0 +1 @@ +from core.hooks.moderation.cloud_service.cloud_service import CloudService \ No newline at end of file diff --git a/api/core/hooks/base_hook.py b/api/core/hooks/base_hook.py new file mode 100644 index 00000000000000..15af7dff40dd63 --- /dev/null +++ b/api/core/hooks/base_hook.py @@ -0,0 +1,30 @@ +import json +import os + +registered_hooks = {} + +class BaseHook: + + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) + cls.register() + + @classmethod + def register(cls): + subclass_path = os.path.abspath(cls.__module__.replace(".", os.path.sep) + '.py') + subclass_dir_path = os.path.dirname(subclass_path) + parent_folder_name = os.path.basename(os.path.dirname(subclass_dir_path)) + + if parent_folder_name == 'hooks': + return + + json_path = os.path.join(subclass_dir_path, 'schema.json') + json_data = {} + if os.path.exists(json_path): + with open(json_path, 'r') as f: + json_data = json.load(f) + + if parent_folder_name not in registered_hooks: + registered_hooks[parent_folder_name] = [] + + registered_hooks[parent_folder_name].append(json_data) \ No newline at end of file diff --git a/api/core/hooks/moderation/__init__.py b/api/core/hooks/moderation/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/api/core/hooks/moderation/base_moderation.py b/api/core/hooks/moderation/base_moderation.py new file mode 100644 index 00000000000000..a1ef35b1e2be76 --- /dev/null +++ b/api/core/hooks/moderation/base_moderation.py @@ -0,0 +1,4 @@ +from ..base_hook import BaseHook + +class BaseModeration(BaseHook): + pass \ No newline at end of file diff --git a/api/core/hooks/moderation/cloud_service/__init__.py b/api/core/hooks/moderation/cloud_service/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/api/core/hooks/moderation/cloud_service/cloud_service.py b/api/core/hooks/moderation/cloud_service/cloud_service.py new file mode 100644 index 00000000000000..b49365babc6a1f --- /dev/null +++ b/api/core/hooks/moderation/cloud_service/cloud_service.py @@ -0,0 +1,4 @@ +from ...moderation.base_moderation import BaseModeration + +class CloudService(BaseModeration): + pass \ No newline at end of file diff --git a/api/core/hooks/moderation/cloud_service/schema.json b/api/core/hooks/moderation/cloud_service/schema.json new file mode 100644 index 00000000000000..144f6989004e8e --- /dev/null +++ b/api/core/hooks/moderation/cloud_service/schema.json @@ -0,0 +1,51 @@ +{ + "name": "cloud_service", + "label": { + "en-US": "Cloud Service", + "zh-Hans": "云服务" + }, + "form_schema": [ + { + "select": { + "label": { + "en-US": "Cloud Provider", + "zh-Hans": "云计算厂商" + }, + "variable": "cloud_provider", + "required": true, + "options": [ + "腾讯云", + "阿里云", + "AWS" + ], + "default": "", + "placeholder": "" + } + }, + { + "text-input": { + "label": { + "en-US": "API Endpoint", + "zh-Hans": "API Endpoint" + }, + "variable": "api_endpoint", + "required": true, + "max-length": 100, + "default": "", + "placeholder": "" + } + }, + { + "paragraph": { + "label": { + "en-US": "API Key", + "zh-Hans": "API Key" + }, + "variable": "api_keys", + "required": true, + "default": "", + "placeholder": "" + } + } + ] +} \ No newline at end of file diff --git a/api/services/form_schemas_service.py b/api/services/form_schemas_service.py new file mode 100644 index 00000000000000..8f09f828090974 --- /dev/null +++ b/api/services/form_schemas_service.py @@ -0,0 +1,7 @@ +from core.hooks.base_hook import registered_hooks + +class FormSchemasService: + + @classmethod + def get_form_schemas(cls, module: str) -> list[dict]: + return registered_hooks.get(module, []) \ No newline at end of file