-
Notifications
You must be signed in to change notification settings - Fork 0
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
eddie32
committed
Sep 8, 2024
0 parents
commit 9fed469
Showing
56 changed files
with
1,006 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/static/ |
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 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/9/8 | ||
# @Author : liuboyuan | ||
# @Description : |
Binary file not shown.
Binary file not shown.
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,3 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/3/27 | ||
# @Author : liuboyuan |
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,24 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/3/27 | ||
# @Author : liuboyuan | ||
|
||
from enum import Enum | ||
|
||
|
||
class EnumBase(Enum): | ||
"""枚举基础类""" | ||
@property | ||
def code(self): | ||
return self.value | ||
|
||
@code.getter | ||
def code(self): | ||
return self.value[0] | ||
|
||
@property | ||
def desc(self): | ||
return self.value | ||
|
||
@desc.getter | ||
def desc(self): | ||
return self.value[1] |
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,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/3/27 | ||
# @Author : liuboyuan | ||
|
||
|
||
from enum import unique | ||
from api.constants.base_enum import EnumBase | ||
|
||
|
||
@unique | ||
class Codes(EnumBase): | ||
"""API状态枚举值""" | ||
# 20000~30000 预留系统状态 | ||
SUCCESS = (20000, '操作成功') | ||
FAILURE = (20001, '操作失败') | ||
LOGOUT = (20002, '退出登陆成功') | ||
TOKEN_INVALID = (20003, 'token失效') | ||
INVALID_PARAMS = (20010, '无效参数') | ||
PARAMS_CHECK_FAILED = (20011, '参数检查失败') | ||
|
||
# 30001~40000 预留业务状态 | ||
NO_DATA = (30001, '未查询到数据') |
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,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/9/8 | ||
# @Author : liuboyuan | ||
# @Description : | ||
from flask_restx import Api | ||
|
||
from api.view.trial_sentinel import chat_api_namespace | ||
from logger import s_log | ||
|
||
route_map = { | ||
'agent_chat': chat_api_namespace | ||
} | ||
|
||
|
||
def init_app_router(api: Api, prefix: str): | ||
""" | ||
初始化应用程序路由。 | ||
:param api: Flask-RESTPlus或其他API对象实例 | ||
:param prefix: API前缀字符串 | ||
""" | ||
# 遍历全局route_map,注册每个命名空间 | ||
for path_suffix, namespace in route_map.items(): | ||
full_path = f'{prefix}/{path_suffix}' | ||
api.add_namespace(namespace, full_path) | ||
s_log.info('注册所有路由 registered routers') |
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 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/9/8 | ||
# @Author : liuboyuan | ||
# @Description : |
Binary file not shown.
Binary file not shown.
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,3 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/3/27 | ||
# @Author : liuboyuan |
Binary file not shown.
Binary file not shown.
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,68 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/3/27 | ||
# @Author : liuboyuan | ||
# @Description : 基本输出格式 | ||
import sys | ||
|
||
from flask import request, jsonify, make_response | ||
# Resource 继承了 MethodView | ||
# from flask.views import MethodView | ||
from logger import s_log | ||
from logger import register_logger | ||
from api.constants.status_code import Codes | ||
from flask_restx import Resource | ||
|
||
|
||
class BaseView(Resource): | ||
request = None | ||
redis_queue_producer = None | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.__setattr__('request', request) | ||
super(BaseView, self).__init__(*args, **kwargs) | ||
|
||
def response_raw(self, code, msg, data): | ||
return jsonify( | ||
{ | ||
"code": code, | ||
"message": msg, | ||
"data": data | ||
} | ||
) | ||
|
||
# 统一错误处理中间件 | ||
def handle_error(self, error, error_type): | ||
error_msg = str(error) | ||
error_code = 401 | ||
if hasattr(error, 'code'): | ||
error_code = error.code | ||
if error_type == 'ValueError': | ||
error_code = Codes.PARAMS_CHECK_FAILED.code | ||
error_msg = f"输入的数值错误 {error_msg}" | ||
if error_type == 'KeyError': | ||
error_code = Codes.PARAMS_CHECK_FAILED.code | ||
error_msg = f"该请求格式不正确, 参数值不支持或缺少参数 {error_msg}" | ||
if error_type == 'OperationalError': | ||
error_code = Codes.FAILURE.code | ||
error_msg = f"数据库连接错误, 请联系管理员解决 {error_msg}" | ||
if error_type == 'IntegrityError': | ||
error_code = Codes.FAILURE.code | ||
error_msg = f"创建请求失败, 已有数据。 {error_msg}" | ||
# return jsonify(response), getattr(error, 'code', 500) | ||
return self.response_raw( | ||
code=error_code, | ||
msg=error_msg, | ||
data='' | ||
) | ||
|
||
def dispatch_request(self, *args, **kwargs): | ||
# 每次请求时API重新注册logger, 这样logger的地址会保持一致。 | ||
register_logger() | ||
try: | ||
return super().dispatch_request(*args, **kwargs) | ||
except Exception as e: | ||
# 出错时也需要重置logger地址 | ||
register_logger() | ||
# 如果在执行视图方法时发生任何异常,调用handle_error方法 | ||
s_log.opt(exception=e).error(f'Error when dispatching - TYPE: [{type(e).__name__}], MESSAGE: {e}') | ||
return self.handle_error(e, error_type=type(e).__name__) |
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 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/9/8 | ||
# @Author : liuboyuan | ||
# @Description : | ||
from api.view.base.view import BaseView | ||
from api.constants.status_code import Codes | ||
from api_entry import rest_api_description as api | ||
from flask_restx import Namespace, fields | ||
|
||
chat_api_namespace = Namespace("队列测试", description='队列测试, 接口定义') | ||
|
||
@chat_api_namespace.route("") | ||
class LLMChatView(BaseView): | ||
|
||
@chat_api_namespace.doc() | ||
def get(self): | ||
""" | ||
调用Agent | ||
:param: current, limit | ||
:return: data, pager | ||
""" | ||
return self.response_raw( | ||
code=Codes.SUCCESS.code, | ||
msg=Codes.SUCCESS.desc, | ||
data=None | ||
) | ||
|
||
chat_payload = api.model('测试任务请求2', { | ||
'username': fields.Arbitrary(description='开发中的测试接口'), | ||
'password': fields.Arbitrary(description='开发中的测试接口'), | ||
}) | ||
|
||
@chat_api_namespace.doc(body=chat_payload) | ||
def post(self): | ||
""" | ||
创建测试任务 | ||
:body: task_payload | ||
:return: task_id | ||
""" | ||
un = self.request.form.get('username') | ||
pwd = self.request.form.get('password') | ||
return self.response_raw( | ||
code=Codes.SUCCESS.code, | ||
msg=Codes.SUCCESS.desc, | ||
data={ | ||
"username": un, | ||
"password": pwd | ||
} | ||
) |
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,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/5/8 | ||
# @Author : liuboyuan | ||
|
||
from flask import Flask | ||
from flask_restx import Api | ||
|
||
|
||
def create_rest_api(application, version, title, description, doc): | ||
return Api(application, version=version, title=title, description=description, doc=doc) | ||
|
||
|
||
app = Flask("FischlApi") | ||
rest_api_description = create_rest_api(app, | ||
version="1.0", | ||
title="Fischl Agent API", | ||
description="" | ||
"Fischl Agent API Documentation", | ||
doc='/' | ||
) | ||
app.config.SWAGGER_UI_DOC_EXPANSION = 'list' | ||
app.config.SWAGGER_UI_OPERATION_ID = True | ||
app.config.SWAGGER_UI_REQUEST_DURATION = True | ||
# disable Try it Out for all methods | ||
app.config.SWAGGER_SUPPORTED_SUBMIT_METHODS = [] | ||
# enable Try it Out for specific methods | ||
app.config.SWAGGER_SUPPORTED_SUBMIT_METHODS = ["get", "post"] |
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 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/9/8 | ||
# @Author : liuboyuan | ||
# @Description : |
Binary file not shown.
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 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/7/8 | ||
# @Author : liuboyuan | ||
from .logger import Logger | ||
from .log_and_raise import log_error_and_raise |
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,14 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/7/8 | ||
# @Author : liuboyuan | ||
from typing import Type | ||
import loguru | ||
|
||
|
||
def log_error_and_raise(error_message: str, log_handler: loguru.logger, | ||
exception_class: str | Type[BaseException] = 'general'): | ||
if exception_class == 'general': | ||
log_handler.error(f"General Error Message: {error_message}") | ||
raise Exception(error_message) | ||
log_handler.error(f"Error Type: {exception_class} Error Message: {error_message}") | ||
raise exception_class(error_message) |
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,41 @@ | ||
from loguru import logger | ||
import os | ||
import sys | ||
|
||
LOG_FILE_NAME = "app.log" | ||
ROTATION_TIME = "02:00" | ||
|
||
|
||
# TODO: 适配多进程 | ||
# TODO: exception处理 | ||
class Logger: | ||
def __init__(self, name="inori", log_dir="static", log_file=LOG_FILE_NAME, debug=False): | ||
current_folder = os.path.join(os.getcwd(), log_dir, str(name)) | ||
if not os.path.exists(log_dir): | ||
os.makedirs(current_folder) | ||
log_file_path = os.path.join(current_folder, log_file) | ||
# print(f'log_file: {log_file_path}') | ||
# Remove default loguru response_handler | ||
logger.remove() | ||
|
||
# Add console response_handler with a specific log level | ||
level = "DEBUG" if debug else "INFO" | ||
logger.add(sys.stdout, level=level) | ||
# logger.configure(extra={ | ||
# "thread": threading.current_thread().name | ||
# }) | ||
# logger.add(sys.stdout, format="on <light-blue><u>{extra[thread]}</u></light-blue>", level=level) | ||
# Add file response_handler with a specific log level and timed rotation | ||
logger.add(log_file_path, rotation=ROTATION_TIME, level="DEBUG") | ||
# logger.add('client.log', rotation="00:00", level="DEBUG") | ||
self.logger = logger | ||
# self.logger = logger.bind(thread=threading.current_thread().name) | ||
|
||
|
||
if __name__ == "__main__": | ||
log = Logger(debug=True).logger | ||
|
||
log.debug("This is a debug message.") | ||
log.info("This is an info message.") | ||
log.warning("This is a warning message.") | ||
log.error("This is an error message.") |
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 @@ | ||
# Plugin Center Base Class | ||
|
||
|
||
|
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,3 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/7/5 | ||
# @Author : liuboyuan |
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,14 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/4/6 | ||
# @Author : liuboyuan | ||
# @Description : | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class BasePlugin(ABC): | ||
name: str | ||
|
||
@abstractmethod | ||
def run(self, *args, **kwargs): | ||
pass |
Oops, something went wrong.