From ae552cfef60fdfce6cef107af9e3d9f702fdeb21 Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Wed, 24 Sep 2025 21:14:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=89=93=E5=8C=85=20github=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 10 ++++++++++ .github/workflows/docker.yaml | 31 +++++++++++++++++++++++++++++++ .run/main start.run.xml | 26 ++++++++++++++++++++++++++ Dockerfile | 34 ++++++++++++++++++++++++++++++++++ Makefile | 9 ++++++++- README.md | 7 +++++++ app/web/handler.py | 3 +++ config.yaml | 2 +- 8 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker.yaml create mode 100644 .run/main start.run.xml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..bda4b36 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.env +__pycache__/ +*.pyc +.git +.gitignore +.github +.script +.venv +tests +.pre-commit-config.yaml \ No newline at end of file diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..d5491b1 --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,31 @@ +name: Docker Build + +on: + push: + branches: + - main # 你可以改成需要触发的分支,比如 master/dev + - dev + - actions + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout source + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: "flycash" + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Build Docker image with Make + run: make docker_build + + - name: Push Docker image + run: docker push flycash/kbase:latest diff --git a/.run/main start.run.xml b/.run/main start.run.xml new file mode 100644 index 0000000..23f042a --- /dev/null +++ b/.run/main start.run.xml @@ -0,0 +1,26 @@ + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a4836b2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +# 使用官方 Python 镜像 +FROM python:3.12-slim + +# 设置环境变量 +ENV PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 + +# 安装 uv(高性能依赖管理器) +RUN pip install --no-cache-dir uv + +# 设置工作目录 +WORKDIR /app + +# 先复制依赖文件(利用缓存) +COPY pyproject.toml uv.lock ./ + +# 安装依赖到虚拟环境 +RUN uv sync --frozen --no-cache --no-install-project + +# 再复制源码 +COPY . . + +# 安装项目本身(可选,如果 pyproject.toml 里定义了 [project]) +RUN uv sync --frozen --no-cache + +# 暴露端口(如果是 web 服务,比如 FastAPI/Flask) +EXPOSE 8000 + +# 启动命令(根据实际情况修改) +# 如果是 Web 服务 +CMD ["uv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"] + +# 如果是 CLI 程序 +# CMD ["uv", "run", "python", "kbase/main.py"] diff --git a/Makefile b/Makefile index abb2919..0cc4a95 100644 --- a/Makefile +++ b/Makefile @@ -111,4 +111,11 @@ _check_python: exit 1; \ fi; \ echo "✅ Python ${PYTHON_VERSION_TARGET} 安装成功。"; \ - fi \ No newline at end of file + fi + +.PHONY: docker_build +docker_build: + docker build -t kbase:latest . + +.PHONY: docker_run + docker run \ No newline at end of file diff --git a/README.md b/README.md index 0d2d797..1df8465 100644 --- a/README.md +++ b/README.md @@ -283,3 +283,10 @@ make check # 自动格式化和修复大部分问题 --- 💡 **提示**: 使用 `make help` 查看所有可用命令 + +## Docker 镜像和部署 +Docker 打包的时候,忽略掉了很多文件,具体可以参考项目下的 .dockerignore 文件。 + +因此在使用 docker 来部署的时候,必须挂载: +- .env 文件 +- config.yaml 文件 \ No newline at end of file diff --git a/app/web/handler.py b/app/web/handler.py index 2b7b729..50cc951 100644 --- a/app/web/handler.py +++ b/app/web/handler.py @@ -81,6 +81,9 @@ def __init__( self._task_status: dict[str, str] = {} def register_routes(self) -> None: + self._router.get("/hello", summary="健康检查接口")( + lambda: {"message": "Hello, KBase RAG!"} + ) """将本处理器中的所有API端点注册到构造时传入的路由器上。""" self._router.post( "/documents/upload-file", diff --git a/config.yaml b/config.yaml index 92bc11f..3d89829 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ elasticsearch: - url: "http://localhost:9200" + url: "http://local.ubuntu:9200" metadata_index: "knowledge_base_metadatas" chunk_index: "knowledge_base_chunks" request_timeout: 60 From 96596bdf681ecef79ae50dff45c1c246e5117efc Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Wed, 24 Sep 2025 21:33:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20flycash/kbase=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++-- README.md | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 0cc4a95..0c4b72c 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ _check_python: .PHONY: docker_build docker_build: - docker build -t kbase:latest . + docker build -t flycash/kbase:latest . .PHONY: docker_run - docker run \ No newline at end of file + docker rm kbase && docker run --name kbase -p 8082:8082 --env-file .env flycash/kbase:latest \ No newline at end of file diff --git a/README.md b/README.md index 1df8465..8baf658 100644 --- a/README.md +++ b/README.md @@ -80,17 +80,19 @@ make run ## 📋 开发命令 -| 命令 | 描述 | -|------|------| -| `make setup` | 🚀 一键设置完整开发环境 | -| `make check` | ✅ 运行所有代码质量检查 | -| `make test` | 🧪 运行测试并生成覆盖率报告 | -| `make run` | ▶️ 启动开发服务器 | -| `make fmt` | 🎨 格式化代码 | -| `make lint` | ✨ 检查代码并自动修复 | -| `make type` | 🔍 类型检查 | -| `make audit` | 🛡️ 扫描安全漏洞 | -| `make clean` | 🧹 清理临时文件 | +| 命令 | 描述 | +|---------------------|-----------------| +| `make setup` | 🚀 一键设置完整开发环境 | +| `make check` | ✅ 运行所有代码质量检查 | +| `make test` | 🧪 运行测试并生成覆盖率报告 | +| `make run` | ▶️ 启动开发服务器 | +| `make fmt` | 🎨 格式化代码 | +| `make lint` | ✨ 检查代码并自动修复 | +| `make type` | 🔍 类型检查 | +| `make audit` | 🛡️ 扫描安全漏洞 | +| `make clean` | 🧹 清理临时文件 | +| `make docker_build` | docker打包 | +| `make docker_run` | 运行 docker 镜像 | ## 🔧 API 接口