From f72d54e9a6d89d31be57c70ef252b98ee62a911d Mon Sep 17 00:00:00 2001 From: Eero Date: Thu, 8 Feb 2024 21:43:33 +0800 Subject: [PATCH] update --- .github/workflows/publish.yml | 6 +-- src/delibird/__init__.py | 4 +- src/delibird/cli.py | 81 +++++++++++++++++++++++++++++++++ src/delibird/log.py | 2 +- src/delibird/main.py | 71 ----------------------------- src/delibird/mock/__init__.py | 2 +- src/delibird/pm.py | 8 ++-- src/delibird/router/__init__.py | 4 +- src/delibird/router/qwen.py | 6 +-- src/delibird/router/spark.py | 6 +-- src/delibird/server.py | 6 +-- src/delibird/stream/wsstream.py | 4 +- tests/test_config.py | 2 +- 13 files changed, 106 insertions(+), 96 deletions(-) create mode 100644 src/delibird/cli.py delete mode 100644 src/delibird/main.py diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index f11aa12..410bed5 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,7 +1,7 @@ name: Publish to PyPI.org on: release: - types: [ published ] + types: [published] jobs: pypi: runs-on: ubuntu-latest @@ -13,10 +13,10 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: 3.11 + python-version: 3.12 - name: Build package run: python3 -m pip install --upgrade build && python3 -m build - name: Publish package to PyPI uses: pypa/gh-action-pypi-publish@release/v1 with: - password: ${{ secrets.PYPI_API_TOKEN }} \ No newline at end of file + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/src/delibird/__init__.py b/src/delibird/__init__.py index ecd3379..4d52a61 100644 --- a/src/delibird/__init__.py +++ b/src/delibird/__init__.py @@ -1,3 +1,3 @@ - from . import _version -__version__ = _version.get_versions()['version'] + +__version__ = _version.get_versions()["version"] diff --git a/src/delibird/cli.py b/src/delibird/cli.py new file mode 100644 index 0000000..7f54cee --- /dev/null +++ b/src/delibird/cli.py @@ -0,0 +1,81 @@ +import click +import psutil +import subprocess + +from delibird.server import app +from delibird.log import Log, LogLevel +from delibird.pm import kill_process, http_process +from delibird.config import read_config + + +@click.group() +def cli(): + pass + + +@cli.command() +@click.option( + "--config", "-c", type=click.Path(exists=True), help="指定配置文件", required=True +) +def start(config): + # init log + logger = Log("delibird") + + # print and log + logger.echo("Starting...") + click.echo("Starting...") + + # check if the program is running + if check_process("delibird"): + logger.echo("The program is already running", "error") + click.echo("The program is already running") + return + + if not config: + click.echo("没有配置文件") + return + + # 从配置文件中读取 server 配置, host 和 port + config_info = read_config(config) + + host = config_info.get("server", {}).get("host") + port = config_info.get("server", {}).get("port") + + # check if host or port is null + if not host or not port: + logger.echo("host or port is null", "error") + click.echo("host or port is null") + return + + # Start the http server + http_process(config, host, port) + + # Print and log + logger.echo("Started") + click.echo("Started") + + +@cli.command() +def stop(): + # get logger + logger = Log("delibird") + + # print and log + logger.echo("Stopping...") + click.echo("Stopping...") + + # kill the process + kill_process() + + # log and close + logger.echo("Stopped") + click.echo("Stopped") + + +def check_process(process_name): + result = subprocess.run( + ["pgrep", "-f", process_name], capture_output=True, text=True + ) + + # 如果pgrep命令的输出不是空的,则有匹配的进程正在运行 + return bool(result.stdout.strip()) diff --git a/src/delibird/log.py b/src/delibird/log.py index af823c4..15eca17 100644 --- a/src/delibird/log.py +++ b/src/delibird/log.py @@ -53,7 +53,7 @@ def __init__(self, name, level=LogLevel.DEBUG): console_handler.setFormatter(formatter) # 创建文件处理器并设置日志文件路径 - log_file_path = get_log_path("llmproxy") + log_file_path = get_log_path("delibird") file_handler = logging.FileHandler(log_file_path) file_handler.setFormatter(formatter) diff --git a/src/delibird/main.py b/src/delibird/main.py deleted file mode 100644 index 9d85e52..0000000 --- a/src/delibird/main.py +++ /dev/null @@ -1,71 +0,0 @@ -import click -import psutil - -from llmproxy.server import app -from log import Log, LogLevel -from llmproxy.pm import kill_process, http_process -from llmproxy.config import read_config - - -@click.group() -def cli(): - pass - - -@cli.command() -@click.option("--config", "-c", type=click.Path(exists=True), help="指定配置文件路径") -def start(config): - # init log - logger = Log("llmproxy") - - # print and log - logger.echo("Starting...") - click.echo("Starting...") - - # check if the program is running - if check_process("llmproxy"): - logger.echo("The program is already running", "error") - click.echo("The program is already running") - return - - # 从配置文件中读取 server 配置, host 和 port - config_info = read_config(config) - - host = config_info.get("server", {}).get("host", "localhost") - port = config_info.get("server", {}).get("port", 8000) - - # Start the http server - http_process(config, host, port) - - # Print and log - logger.echo("Started") - click.echo("Started") - - -@cli.command() -def stop(): - # get logger - logger = Log("llmproxy") - - # print and log - logger.echo("Stopping...") - click.echo("Stopping...") - - # kill the process - kill_process() - - # log and close - logger.echo("Stopped") - click.echo("Stopped") - - -def check_process(process_name): - for proc in psutil.process_iter(): - print(f"proc name: {proc.name()}") - if process_name in proc.name(): - return True - return False - - -if __name__ == "__main__": - cli() diff --git a/src/delibird/mock/__init__.py b/src/delibird/mock/__init__.py index 04f1476..82f4e2a 100644 --- a/src/delibird/mock/__init__.py +++ b/src/delibird/mock/__init__.py @@ -1,4 +1,4 @@ -from llmproxy.mock.send import send_mock +from delibird.mock.send import send_mock __all__ = ["send_mock"] diff --git a/src/delibird/pm.py b/src/delibird/pm.py index fce405e..aeb2593 100644 --- a/src/delibird/pm.py +++ b/src/delibird/pm.py @@ -3,12 +3,12 @@ import os import subprocess from multiprocessing import Process, cpu_count -from llmproxy.config import read_config +from delibird.config import read_config def kill_process(): - # Kill process named 'llmproxy' if it exists - os.system("pkill -f llmproxy") + # Kill process named 'delibird' if it exists + os.system("pkill -f delibird") def http_process(config_file, host, port): @@ -26,7 +26,7 @@ def http_process(config_file, host, port): "uvicorn.workers.UvicornWorker", "-b", f"{host}:{port}", - "llmproxy.server:app", + "delibird.server:app", "--timeout", "60", ] diff --git a/src/delibird/router/__init__.py b/src/delibird/router/__init__.py index 5a1ba3a..c57a3db 100644 --- a/src/delibird/router/__init__.py +++ b/src/delibird/router/__init__.py @@ -1,4 +1,4 @@ -from llmproxy.router.spark import send as spark_send -from llmproxy.router.qwen import send as qwen_send +from delibird.router.spark import send as spark_send +from delibird.router.qwen import send as qwen_send __all__ = ["spark_send", "qwen_send"] diff --git a/src/delibird/router/qwen.py b/src/delibird/router/qwen.py index 6faf353..3bfc2ff 100644 --- a/src/delibird/router/qwen.py +++ b/src/delibird/router/qwen.py @@ -4,7 +4,7 @@ from fastapi.responses import StreamingResponse from http import HTTPStatus import dashscope -from llmproxy.log import Log +from delibird.log import Log class Qwen: @@ -33,7 +33,7 @@ def read_config(self, config, request): request: 请求参数.格式为 {"chat": messages, "model": "v15"} """ - logger = Log("llmproxy") + logger = Log("delibird") # 检查配置文件和模型是否存在 if not config: logger.echo("配置文件不存在", "error") @@ -101,7 +101,7 @@ async def send(self, chunk_size=24): def send(config, request): """发送处理.""" - logger = Log("llmproxy") + logger = Log("delibird") # 创建 Qwen 实例 qwen = Qwen() diff --git a/src/delibird/router/spark.py b/src/delibird/router/spark.py index cfd4e94..2a2630e 100644 --- a/src/delibird/router/spark.py +++ b/src/delibird/router/spark.py @@ -15,10 +15,10 @@ import websocket from wsgiref.handlers import format_date_time from fastapi.responses import StreamingResponse -from llmproxy.log import Log +from delibird.log import Log from time import sleep import asyncio -from llmproxy.stream import WsStream +from delibird.stream import WsStream class Spark: @@ -39,7 +39,7 @@ def read_config(self): config: 配置文件 request: 请求参数.格式为 {"chat": messages, "model": "v15"} """ - logger = Log("llmproxy") + logger = Log("delibird") if not self.config: logger.echo("配置文件不存在", "error") return False diff --git a/src/delibird/server.py b/src/delibird/server.py index 3623362..bb66f0d 100644 --- a/src/delibird/server.py +++ b/src/delibird/server.py @@ -6,9 +6,9 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from llmproxy.router import spark_send, qwen_send -from llmproxy.config import read_config -from llmproxy.log import Log +from delibird.router import spark_send, qwen_send +from delibird.config import read_config +from delibird.log import Log app = FastAPI() diff --git a/src/delibird/stream/wsstream.py b/src/delibird/stream/wsstream.py index a307220..c614996 100644 --- a/src/delibird/stream/wsstream.py +++ b/src/delibird/stream/wsstream.py @@ -1,5 +1,5 @@ from .base import StreamBase -from llmproxy.log import Log +from delibird.log import Log import websocket import json @@ -29,7 +29,7 @@ def close(self): async def _spark_send(self, messages, model, app_id): """星火大模型发送处理.""" - logger = Log("llmproxy") + logger = Log("delibird") websocket.enableTrace(False) self.websocket = websocket.create_connection(self.url) diff --git a/tests/test_config.py b/tests/test_config.py index 118855f..4f894aa 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,4 @@ -from llmproxy.config import read_config +from delibird.config import read_config def test_config():