From 592d2455a1a42ce36ea11b000872fe6dace01f43 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Thu, 7 Nov 2024 15:01:23 +0800 Subject: [PATCH] create llm ops server process --- .gitignore | 6 ++++-- llmops/app_factory.py | 27 +++++++++++++++++++++++++++ llmops/config/config.py | 35 +++++++++++++++++++++++++++++++++++ llmops/llm-ops.yaml.template | 4 ++++ llmops/main.py | 4 ++++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 llmops/app_factory.py create mode 100644 llmops/config/config.py create mode 100644 llmops/llm-ops.yaml.template create mode 100644 llmops/main.py diff --git a/.gitignore b/.gitignore index 287785ec..efe75104 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ .vscode/ !build -config.yaml .DS_Store topology/pkg/ -topology/build \ No newline at end of file +topology/build +*.yaml +# python build cache +__pycache__ \ No newline at end of file diff --git a/llmops/app_factory.py b/llmops/app_factory.py new file mode 100644 index 00000000..9d8eeff5 --- /dev/null +++ b/llmops/app_factory.py @@ -0,0 +1,27 @@ +from flask import Flask +from config.config import init_config + + +def create_app() -> Flask: + app = Flask(__name__) + + # 初始化http服务 + config = init_config() + app.config["SERVER"] = config.app_conf.server + app.config["PORT"] = config.app_conf.port + app.config["DEBUG"] = config.app_conf.debug + + return app + + +def run_app(): + app = create_app() + try: + app.run( + host=app.config["SERVER"], + port=int(app.config["PORT"]), + debug=app.config["DEBUG"], + ) + except ValueError as e: + app.logger.error(f"Invalid app config: {e}") + exit(1) diff --git a/llmops/config/config.py b/llmops/config/config.py new file mode 100644 index 00000000..7b8e00c5 --- /dev/null +++ b/llmops/config/config.py @@ -0,0 +1,35 @@ +import os +import yaml +from dataclasses import dataclass + + +@dataclass +class AppConf: + server: str + port: str + debug: bool + + +class Config: + filename = "../llm-ops.yaml" + + def __init__(self): + self.app_conf: AppConf = None + + def load_config(self): + current_dir = os.path.dirname(os.path.abspath(__file__)) + config_path = os.path.join(current_dir, self.filename) + + try: + with open(config_path, "r") as f: + conf = yaml.safe_load(f) + except IOError as e: + raise ValueError("Load llm-ops config file failed") from e + + self.app_conf = AppConf(**conf["app"]) + + +def init_config() -> Config: + conf = Config() + conf.load_config() + return conf diff --git a/llmops/llm-ops.yaml.template b/llmops/llm-ops.yaml.template new file mode 100644 index 00000000..4dc7944d --- /dev/null +++ b/llmops/llm-ops.yaml.template @@ -0,0 +1,4 @@ +app: + server: localhost + port: 5000 + debug: true \ No newline at end of file diff --git a/llmops/main.py b/llmops/main.py new file mode 100644 index 00000000..f2a0ca04 --- /dev/null +++ b/llmops/main.py @@ -0,0 +1,4 @@ +from app_factory import run_app + +if __name__ == "__main__": + run_app() -- Gitee