-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41e452d
commit 48fa847
Showing
19 changed files
with
249 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.extension_service import ExtensionService | ||
|
||
|
||
class CodeBasedExtension(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 ExtensionService.get_code_based_extensions(args['module']) | ||
|
||
|
||
api.add_resource(CodeBasedExtension, '/code-based-extensions') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import core.moderation.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Desc: Metaclass for auto-registering subclasses of a class. | ||
|
||
class AutoRegisterMeta(type): | ||
def __init__(cls, name, bases, attrs): | ||
super(AutoRegisterMeta, cls).__init__(name, bases, attrs) | ||
if not hasattr(cls, 'subclasses'): | ||
cls.subclasses = {} | ||
else: | ||
register_name = getattr(cls, 'register_name', name) | ||
cls.subclasses[register_name] = cls | ||
|
||
class AutoRegisterBase(metaclass=AutoRegisterMeta): | ||
@classmethod | ||
def create_instance(cls, subclass_name, *args, **kwargs): | ||
if subclass_name not in cls.subclasses: | ||
raise ValueError(f"No register_name with name '{subclass_name}' found") | ||
return cls.subclasses[subclass_name](*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import json | ||
import os | ||
import copy | ||
|
||
class Extensible: | ||
__extensions = {} | ||
|
||
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)) | ||
|
||
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 cls.__extensions: | ||
cls.__extensions[parent_folder_name] = { | ||
"module": parent_folder_name, | ||
"data": [] | ||
} | ||
|
||
cls.__extensions[parent_folder_name]["data"].append(json_data) | ||
|
||
@classmethod | ||
def get_extensions(cls) -> dict: | ||
return copy.deepcopy(cls.__extensions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from core.moderation.openai.openai import OpenAIModeration | ||
from core.moderation.keywords.keywords import KeywordsModeration | ||
from core.moderation.api_based.api_based import ApiBasedModeration | ||
from core.moderation.cloud_service.cloud_service import CloudServiceModeration |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from core.moderation.base import BaseModeration | ||
|
||
|
||
class ApiBasedModeration(BaseModeration): | ||
register_name = "api_based" | ||
|
||
@classmethod | ||
def validate_config(self, config: dict) -> None: | ||
api_based_extension_id = config.get("api_based_extension_id") | ||
if not api_based_extension_id: | ||
raise ValueError("api_based_extension_id is required") | ||
|
||
self._validate_inputs_and_outputs_config(config, False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from abc import abstractclassmethod | ||
from core.helper.auto_register import AutoRegisterBase | ||
|
||
|
||
class BaseModeration(AutoRegisterBase): | ||
|
||
@abstractclassmethod | ||
def validate_config(self, config: dict) -> None: | ||
pass | ||
|
||
@abstractclassmethod | ||
def moderation_for_inputs(self, config: dict): | ||
pass | ||
|
||
@abstractclassmethod | ||
def moderation_for_outputs(self, config: dict): | ||
pass | ||
|
||
@classmethod | ||
def _validate_inputs_and_outputs_config(self, config: dict, is_preset_response_required: bool) -> None: | ||
# inputs_configs | ||
inputs_configs = config.get("inputs_configs") | ||
if not isinstance(inputs_configs, dict): | ||
raise ValueError("inputs_configs must be a dict") | ||
|
||
# outputs_configs | ||
outputs_configs = config.get("outputs_configs") | ||
if not isinstance(outputs_configs, dict): | ||
raise ValueError("outputs_configs must be a dict") | ||
|
||
inputs_configs_enabled = inputs_configs.get("enabled") | ||
outputs_configs_enabled = outputs_configs.get("enabled") | ||
if not inputs_configs_enabled and not outputs_configs_enabled: | ||
raise ValueError("At least one of inputs_configs or outputs_configs must be enabled") | ||
|
||
# preset_response | ||
if not is_preset_response_required: | ||
return | ||
|
||
if inputs_configs_enabled and not inputs_configs.get("preset_response"): | ||
raise ValueError("inputs_configs.preset_response is required") | ||
|
||
if outputs_configs_enabled and not outputs_configs.get("preset_response"): | ||
raise ValueError("outputs_configs.preset_response is required") | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from core.moderation.base import BaseModeration | ||
from core.helper.extensible import Extensible | ||
|
||
class CloudServiceModeration(BaseModeration, Extensible): | ||
register_name = "cloud_service" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "" | ||
} | ||
} | ||
] | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from core.moderation.base import BaseModeration | ||
|
||
class KeywordsModeration(BaseModeration): | ||
register_name = "keywords" | ||
|
||
@classmethod | ||
def validate_config(self, config): | ||
keywords = config.get("keywords") | ||
if not keywords: | ||
raise ValueError("keywords is required") | ||
|
||
self._validate_inputs_and_outputs_config(config, True) | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from core.moderation.base import BaseModeration | ||
|
||
class OpenAIModeration(BaseModeration): | ||
register_name = "openai" | ||
|
||
@classmethod | ||
def validate_config(self, config: dict): | ||
self._validate_inputs_and_outputs_config(config, True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from core.helper.extensible import Extensible | ||
|
||
class ExtensionService: | ||
|
||
@classmethod | ||
def get_code_based_extensions(cls, module: str) -> list[dict]: | ||
return Extensible.get_extensions().get(module, []) |